Move beforeSend
out of config
This commit is contained in:
parent
2e11ca5ea9
commit
97511dd714
7 changed files with 95 additions and 30 deletions
|
@ -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`
|
**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 Vercel’s tracking scripts into all of your pages.
|
You can enable [Vercel Web Analytics](https://vercel.com/docs/concepts/analytics) by setting `webAnalytics: { enabled: true }`. This will inject Vercel’s 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
|
```js
|
||||||
// astro.config.mjs
|
// astro.config.mjs
|
||||||
|
@ -104,20 +104,32 @@ export default defineConfig({
|
||||||
adapter: vercel({
|
adapter: vercel({
|
||||||
webAnalytics: {
|
webAnalytics: {
|
||||||
enabled: true,
|
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
|
### 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.
|
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
|
### imagesConfig
|
||||||
|
|
||||||
|
|
|
@ -55,14 +55,17 @@ export default function vercelEdge({
|
||||||
return {
|
return {
|
||||||
name: PACKAGE_NAME,
|
name: PACKAGE_NAME,
|
||||||
hooks: {
|
hooks: {
|
||||||
'astro:config:setup': ({ command, config, updateConfig, injectScript }) => {
|
'astro:config:setup': async ({ command, config, updateConfig, injectScript }) => {
|
||||||
if (webAnalytics?.enabled) {
|
if (webAnalytics?.enabled) {
|
||||||
injectScript(
|
injectScript(
|
||||||
'page',
|
'page',
|
||||||
getInjectableWebAnalyticsContent({
|
await getInjectableWebAnalyticsContent(
|
||||||
...webAnalytics.config,
|
{
|
||||||
mode: command === 'dev' ? 'development' : 'production',
|
...webAnalytics.config,
|
||||||
})
|
mode: command === 'dev' ? 'development' : 'production',
|
||||||
|
},
|
||||||
|
config.root
|
||||||
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
if (command === 'build' && speedInsights?.enabled) {
|
if (command === 'build' && speedInsights?.enabled) {
|
||||||
|
|
|
@ -1,15 +1,56 @@
|
||||||
import type { AnalyticsProps } from '@vercel/analytics';
|
import type { AnalyticsProps } from '@vercel/analytics';
|
||||||
|
import { fileURLToPath } from 'url';
|
||||||
|
|
||||||
export type VercelWebAnalyticsConfig = {
|
export type VercelWebAnalyticsConfig = {
|
||||||
enabled: boolean;
|
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';
|
return `import { inject } from '@vercel/analytics';
|
||||||
inject({
|
inject({
|
||||||
mode: ${config?.mode},
|
mode: ${config?.mode},
|
||||||
beforeSend: ${config?.beforeSend},
|
beforeSend: ${beforeSend},
|
||||||
debug: ${config?.debug}
|
debug: ${config?.debug}
|
||||||
});`;
|
});`;
|
||||||
}
|
}
|
||||||
|
|
|
@ -116,14 +116,17 @@ export default function vercelServerless({
|
||||||
return {
|
return {
|
||||||
name: PACKAGE_NAME,
|
name: PACKAGE_NAME,
|
||||||
hooks: {
|
hooks: {
|
||||||
'astro:config:setup': ({ command, config, updateConfig, injectScript }) => {
|
'astro:config:setup': async ({ command, config, updateConfig, injectScript }) => {
|
||||||
if (webAnalytics?.enabled) {
|
if (webAnalytics?.enabled) {
|
||||||
injectScript(
|
injectScript(
|
||||||
'page',
|
'page',
|
||||||
getInjectableWebAnalyticsContent({
|
await getInjectableWebAnalyticsContent(
|
||||||
...webAnalytics.config,
|
{
|
||||||
mode: command === 'dev' ? 'development' : 'production',
|
...webAnalytics.config,
|
||||||
})
|
mode: command === 'dev' ? 'development' : 'production',
|
||||||
|
},
|
||||||
|
config.root
|
||||||
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
if (command === 'build' && speedInsights?.enabled) {
|
if (command === 'build' && speedInsights?.enabled) {
|
||||||
|
|
|
@ -37,14 +37,17 @@ export default function vercelStatic({
|
||||||
return {
|
return {
|
||||||
name: '@astrojs/vercel',
|
name: '@astrojs/vercel',
|
||||||
hooks: {
|
hooks: {
|
||||||
'astro:config:setup': ({ command, config, injectScript, updateConfig }) => {
|
'astro:config:setup': async ({ command, config, injectScript, updateConfig }) => {
|
||||||
if (webAnalytics?.enabled) {
|
if (webAnalytics?.enabled) {
|
||||||
injectScript(
|
injectScript(
|
||||||
'page',
|
'page',
|
||||||
getInjectableWebAnalyticsContent({
|
await getInjectableWebAnalyticsContent(
|
||||||
...webAnalytics.config,
|
{
|
||||||
mode: command === 'dev' ? 'development' : 'production',
|
...webAnalytics.config,
|
||||||
})
|
mode: command === 'dev' ? 'development' : 'production',
|
||||||
|
},
|
||||||
|
config.root
|
||||||
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
if (command === 'build' && speedInsights?.enabled) {
|
if (command === 'build' && speedInsights?.enabled) {
|
||||||
|
|
3
packages/integrations/vercel/src/types.d.ts
vendored
Normal file
3
packages/integrations/vercel/src/types.d.ts
vendored
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
import type { AnalyticsProps } from '@vercel/analytics';
|
||||||
|
|
||||||
|
export type VercelWebAnalyticsBeforeSend = AnalyticsProps['beforeSend'];
|
Loading…
Reference in a new issue