diff --git a/packages/web/src/generic.ts b/packages/web/src/generic.ts index 0b7ec40..9595027 100644 --- a/packages/web/src/generic.ts +++ b/packages/web/src/generic.ts @@ -3,6 +3,18 @@ import { initQueue } from './queue'; import type { SpeedInsightsProps, BeforeSendMiddleware } from './types'; import { computeRoute, getScriptSrc, isBrowser, isDevelopment } from './utils'; +/** + * Triggers a fetch call to the specified URL to warm up the endpoint. + * Any errors are silently ignored. + */ +async function warmUpScript(src: string): Promise { + try { + await fetch(src); + } catch (err) { + void 0; // no-op to satisfy non-empty block rules + } +} + /** * 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). * @param [props] - Speed Insights options. @@ -65,7 +77,9 @@ function injectSpeedInsights( ); }; - document.head.appendChild(script); + void warmUpScript(src).finally(() => { + document.head.appendChild(script); + }); return { setRoute: (route: string | null): void => {