Move beforeSend out of config

This commit is contained in:
Chris 2023-08-29 12:12:00 +02:00
parent 2e11ca5ea9
commit 97511dd714
7 changed files with 95 additions and 30 deletions

View file

@ -92,7 +92,7 @@ To configure this adapter, pass an object to the `vercel()` function call in `as
**Added in:** `@astrojs/vercel@3.8.0`
You can enable [Vercel Web Analytics](https://vercel.com/docs/concepts/analytics) by setting `webAnalytics: { enabled: true }`. This will inject Vercels tracking scripts into all of your pages.
Alternatively, you can pass all [available configuration options](https://vercel.com/docs/concepts/analytics/package) via the `config` property inside `webAnalytics`.
Alternatively, you can also pass [configuration options](https://vercel.com/docs/concepts/analytics/package) (except functions like `beforeSend`) via the `config` property inside `webAnalytics`.
```js
// astro.config.mjs
@ -104,20 +104,32 @@ export default defineConfig({
adapter: vercel({
webAnalytics: {
enabled: true,
config: {
beforeSend: (event) => {
// Ignore all events that have a `/private` inside the URL
if (event.url.includes('/private')) {
return null;
}
return event;
}
}
}
},
}),
});
```
#### `beforeSend`
Since functions can't be passed down via `astro.config.mjs`, you need to export the `beforeSend` function in a separate file inside your root called `vercel-web-analytics.ts`.
If you're not using TypeScript, you can define the function inside `vercel-web-analytics.js`.
```js
// vercel-web-analytics.ts
import type { VercelWebAnalyticsBeforeSend } from '@astrojs/vercel';
export const beforeSend: VercelWebAnalyticsBeforeSend = (event) => {
// Ignore all events that have a `/private` inside the URL
if (event.url.includes('/private')) {
return null;
}
return event;
}
}
```
````js
### Speed Insights
You can enable [Vercel Speed Insights](https://vercel.com/docs/concepts/speed-insights) by setting `speedInsights: { enabled: true }`. This will collect and send Web Vital data to Vercel.
@ -138,7 +150,7 @@ export default defineConfig({
}
}),
});
```
````
### imagesConfig

View file

@ -55,14 +55,17 @@ export default function vercelEdge({
return {
name: PACKAGE_NAME,
hooks: {
'astro:config:setup': ({ command, config, updateConfig, injectScript }) => {
'astro:config:setup': async ({ command, config, updateConfig, injectScript }) => {
if (webAnalytics?.enabled) {
injectScript(
'page',
getInjectableWebAnalyticsContent({
...webAnalytics.config,
mode: command === 'dev' ? 'development' : 'production',
})
await getInjectableWebAnalyticsContent(
{
...webAnalytics.config,
mode: command === 'dev' ? 'development' : 'production',
},
config.root
)
);
}
if (command === 'build' && speedInsights?.enabled) {

View file

@ -1,15 +1,56 @@
import type { AnalyticsProps } from '@vercel/analytics';
import { fileURLToPath } from 'url';
export type VercelWebAnalyticsConfig = {
enabled: boolean;
config?: AnalyticsProps;
config?: Omit<AnalyticsProps, 'beforeSend'>;
};
export function getInjectableWebAnalyticsContent(config?: AnalyticsProps) {
async function getWebAnalyticsFunctions(root: URL) {
try {
const files = await Promise.all([
import(/* @vite-ignore */ fileURLToPath(new URL('./vercel-web-analytics.ts', root))).catch(
() => undefined
),
import(/* @vite-ignore */ fileURLToPath(new URL('./vercel-web-analytics.js', root))).catch(
() => undefined
),
]);
const functions = files[0] || files[1];
if (functions?.default) {
if (typeof functions.default.beforeSend !== 'function') {
throw new Error(
`@astrojs/vercel: ./vercel-web-analytics.js should export a \`beforeSend\` function.`
);
}
return {
beforeSend: functions.default.beforeSend,
};
}
return {
beforeSend: undefined,
};
} catch (e) {
return {
beforeSend: undefined,
};
}
}
export async function getInjectableWebAnalyticsContent(
config: Omit<AnalyticsProps, 'beforeSend'> | undefined,
root: URL
) {
const { beforeSend } = await getWebAnalyticsFunctions(root);
return `import { inject } from '@vercel/analytics';
inject({
mode: ${config?.mode},
beforeSend: ${config?.beforeSend},
beforeSend: ${beforeSend},
debug: ${config?.debug}
});`;
}

View file

@ -116,14 +116,17 @@ export default function vercelServerless({
return {
name: PACKAGE_NAME,
hooks: {
'astro:config:setup': ({ command, config, updateConfig, injectScript }) => {
'astro:config:setup': async ({ command, config, updateConfig, injectScript }) => {
if (webAnalytics?.enabled) {
injectScript(
'page',
getInjectableWebAnalyticsContent({
...webAnalytics.config,
mode: command === 'dev' ? 'development' : 'production',
})
await getInjectableWebAnalyticsContent(
{
...webAnalytics.config,
mode: command === 'dev' ? 'development' : 'production',
},
config.root
)
);
}
if (command === 'build' && speedInsights?.enabled) {

View file

@ -37,14 +37,17 @@ export default function vercelStatic({
return {
name: '@astrojs/vercel',
hooks: {
'astro:config:setup': ({ command, config, injectScript, updateConfig }) => {
'astro:config:setup': async ({ command, config, injectScript, updateConfig }) => {
if (webAnalytics?.enabled) {
injectScript(
'page',
getInjectableWebAnalyticsContent({
...webAnalytics.config,
mode: command === 'dev' ? 'development' : 'production',
})
await getInjectableWebAnalyticsContent(
{
...webAnalytics.config,
mode: command === 'dev' ? 'development' : 'production',
},
config.root
)
);
}
if (command === 'build' && speedInsights?.enabled) {

View file

@ -0,0 +1,3 @@
import type { AnalyticsProps } from '@vercel/analytics';
export type VercelWebAnalyticsBeforeSend = AnalyticsProps['beforeSend'];