Sitemap
FrontEnd Web

FrontendWeb is a platform for writers and readers to read and write a new post about frontend developers, full-stack developers, Web developers, and Computer science. It is an open-source community that joins and gathers frontend developers to build and share knowledge with other

Nextjs

The Script component in Next.js?

4 min readJan 4, 2022

--

Script component in Next.js.

Demo

Play with code online. Plz, check the _app.js file.

How to use the script component in nextjs?

import Script from 'next/script'
<Script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" />
import Script from "next/script";
import Head from "next/head";
export default function MyApp({ Component, pageProps }) {
return (
<>
<Head>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css"
rel="stylesheet"
integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3"
crossOrigin="anonymous" />
</Head>
<Script
id="bootstrap-cdn"

src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" />
<Component {...pageProps} />
</>
);
}

Additional property

id

<Script  id="bootstrap-cdn"     src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"  strategy='lazyOnload' />

strategy

<Script  id="bootstrap-cdn"     src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"  strategy='lazyOnload' />

dangerouslySetInnerHTML

<Script
strategy="afterInteractive"
dangerouslySetInnerHTML={{
__html: `
(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer', 'GTM-XXXXXX');
`,
}}

/>

Inline scripts

<Script id="show-banner" strategy="lazyOnload">
{`document.getElementById('banner').classList.remove('hidden')`}
</Script>

onLoad

import Script from 'next/script'

export default function Home() {
return (
<>
<Script
id="onload-id"
src="https://cdn.example.com/script.js"
onLoad={() => {
// ... code here
}}

/>
</>
)
}

onError

import Script from 'next/script'

export default function Home() {
return (
<>
<Script
id="onerror-id"
src="https://cdn.example.com/script.js"
onError={(e) => {
console.log("error: ",e)
}}

/>
</>
)
}

Additional attributes

Full Example

import Script from 'next/script'

export default function Home() {
return (
<>
<Script
id="script-id"
src="https://cdn.example.com/script.js"
dangerouslySetInnerHTML={{
__html: code here
}}
onLoad={ ()=> { console.log('code here')}}
onError={(e) => {
console.log("error: ",e)
}}

/>
</>
)
}

Previous Articles

Conclusion

--

--

FrontEnd Web
FrontEnd Web

Published in FrontEnd Web

FrontendWeb is a platform for writers and readers to read and write a new post about frontend developers, full-stack developers, Web developers, and Computer science. It is an open-source community that joins and gathers frontend developers to build and share knowledge with other

Rajdeep Singh
Rajdeep Singh

Written by Rajdeep Singh

Follow me if you learn more about JavaScript | TypeScript | React.js | Next.js | Linux | NixOS | Frontend Developer | https://linktr.ee/officialrajdeepsingh

Responses (1)