-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
index.ts
41 lines (37 loc) · 1.43 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import { defineDbIntegration } from '@astrojs/db/utils';
import { AstroError } from 'astro/errors';
export default function webVitals() {
return defineDbIntegration({
name: '@astrojs/web-vitals',
hooks: {
'astro:db:setup'({ extendDb }) {
extendDb({ configEntrypoint: '@astrojs/web-vitals/db-config' });
},
'astro:config:setup'({ addMiddleware, config, injectRoute, injectScript }) {
if (!config.integrations.find(({ name }) => name === 'astro:db')) {
throw new AstroError(
'Astro DB integration not found.',
'Run `npx astro add db` to install `@astrojs/db` and add it to your Astro config.'
);
}
if (config.output !== 'hybrid' && config.output !== 'server') {
throw new AstroError(
'No SSR adapter found.',
'`@astrojs/web-vitals` requires your site to be built with `hybrid` or `server` output.\n' +
'Please add an SSR adapter: https://docs.astro.build/en/guides/server-side-rendering/'
);
}
// Middleware that adds a `<meta>` tag to each page.
addMiddleware({ entrypoint: '@astrojs/web-vitals/middleware', order: 'post' });
// Endpoint that collects metrics and inserts them in Astro DB.
injectRoute({
entrypoint: '@astrojs/web-vitals/endpoint',
pattern: '/_/astro-vitals',
prerender: false,
});
// Client-side performance measurement script.
injectScript('page', `import '@astrojs/web-vitals/client-script';`);
},
},
});
}