Using Next.js Script’s strategy option to optimize asset loading
The Next.js Script
component gives us more control over script loading behavior, allowing us to improve page load performance.
The strategy
prop allows us to control the loading strategy; it defaults to afterInteractive
, which will begin loading after some of the Next.js code has run. It can be set to beforeInteractive
, in which case the script is loaded and executed before all Next.js code. lazyOnLoad
can be used for lower-priority scripts to delay loading until there’s browser idle time.
The final option is experimental; it’s the worker
strategy, which will load and run the script in a web worker.
Per the Next.js docs for the Script#strategy
option, the following list contains the loading strategies of the script (see the docs: https://nextjs.org/docs/pages/api-reference/components/script#strategy).
There are four different strategies that can be used:
beforeInteractive
: Load...