If you want to incorporate third-party scripts into your Next.js project, consider using the next/script
component.
import Script from 'next/script'
export default function Home() {
return (
<>
<Script src="https://www.google-analytics.com/analytics.js" />
</>
)
}
With the next/script component, you have control over when the external script is loaded by utilizing the strategy
property.
<Script src="https://connect.facebook.net/en_US/sdk.js" strategy="lazyOnload" />
There are various loading strategies available:
beforeInteractive
: Loads before the page becomes interactive
afterInteractive
: (default) Loads immediately after the page becomes interactive
lazyOnload
: Loads during idle time
worker
: (experimental) Loads in a web worker
To learn more about using the next/script component, check out the official documentation.