astro-web-vitals-pocketbase
Version:
55 lines (49 loc) • 1.8 kB
text/typescript
import type { AstroIntegration } from "astro";
import type { webVitalsIntegrationOptions } from "./types/web-vitals-integration-options.type";
import { checkBatchApi } from "./utils/check-batch-api";
import { checkCollection } from "./utils/check-collection";
import { getSuperuserToken } from "./utils/get-superuser-token";
export function webVitalsIntegration(
options: webVitalsIntegrationOptions
): AstroIntegration {
return {
name: "web-vitals-pocketbase",
hooks: {
"astro:config:setup": async ({
command,
injectScript,
logger
}): Promise<void> => {
let superuserToken: string | undefined;
if (options.superuserCredentials) {
superuserToken = await getSuperuserToken(
options.url,
options.superuserCredentials,
logger
);
}
// Check if the collection setup is done
await checkCollection(options, superuserToken);
let batchEnabled = false;
if (superuserToken) {
batchEnabled = await checkBatchApi(options, superuserToken);
} else {
logger.warn(
"Could not check batch API settings, no valid superuser credentials provided."
);
}
// Temporarily disable the command check for testing purposes
// eslint-disable-next-line no-constant-condition
if (command === "build" || true) {
// Pass the PocketBase URL to the client-side script
injectScript(
"head-inline",
`window.__web_vitals__ = ${JSON.stringify({ url: options.url, batchEnabled })}`
);
// Inject core script into the page
injectScript("page", "import 'astro-web-vitals-pocketbase/core';");
}
}
}
};
}