Skip to content

Commit 8a00b2c

Browse files
committed
feat: add warm-up script function to fetch resources before injection
1 parent 23ab42e commit 8a00b2c

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

packages/web/src/generic.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,18 @@ import { initQueue } from './queue';
33
import type { SpeedInsightsProps } from './types';
44
import { computeRoute, getScriptSrc, isBrowser, isDevelopment } from './utils';
55

6+
/**
7+
* Triggers a fetch call to the specified URL to warm up the endpoint.
8+
* Any errors are silently ignored.
9+
*/
10+
async function warmUpScript(src: string): Promise<void> {
11+
try {
12+
await fetch(src);
13+
} catch (err) {
14+
void 0; // no-op to satisfy non-empty block rules
15+
}
16+
}
17+
618
/**
719
* Injects the Vercel Speed Insights script into the page head and starts tracking page views. Read more in our [documentation](https://vercel.com/docs/speed-insights).
820
* @param [props] - Speed Insights options.
@@ -65,7 +77,9 @@ function injectSpeedInsights(
6577
);
6678
};
6779

68-
document.head.appendChild(script);
80+
void warmUpScript(src).finally(() => {
81+
document.head.appendChild(script);
82+
});
6983

7084
return {
7185
setRoute: (route: string | null): void => {

0 commit comments

Comments
 (0)