Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is proper way to import script files in Next Js?

I was trying to use a template of bootstrap, CSS files are imported properly by I'm not able to import JS. I came to know that in Next Js you can import them in useEffect hook. But still, it gives an error that the script not found. Here is my code,

import Head from 'next/head';
import '../styles/globals.css';
import { useEffect } from 'react';

function MyApp({ Component, pageProps }) {
  useEffect(() => {
    import("../public/lib/js/jquery-3.3.1.min.js");
    import("../public/lib/js/bootstrap.min.js");
    import("../public/lib/js/jquery.nice-select.min.js");
    import("../public/lib/js/jquery-ui.min.js");
    import("../public/lib/js/jquery.slicknav.js");
    import("../public/lib/js/mixitup.min.js");
    import("../public/lib/js/owl.carousel.min.js");
    import("../public/lib/js/main.js");
  }, []);
  return (
      <>
      <Head>

        <meta charset="UTF-8"/>
        <meta name="keywords" content="Ogani, unica, creative, html"/>
        <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
        <meta http-equiv="X-UA-Compatible" content="ie=edge"/>

        {/* Font */}
        <link href="https://fonts.googleapis.com/css2?family=Cairo:wght@200;300;400;600;900&display=swap" rel="stylesheet"/>
        {/* CSS */}
        <link rel="stylesheet" href="lib/css/bootstrap.min.css" type="text/css"/>
        <link rel="stylesheet" href="lib/css/font-awesome.min.css" type="text/css"/>
        <link rel="stylesheet" href="lib/css/elegant-icons.css" type="text/css"/>
        <link rel="stylesheet" href="lib/css/nice-select.css" type="text/css"/>
        <link rel="stylesheet" href="lib/css/jquery-ui.min.css" type="text/css"/>
        <link rel="stylesheet" href="lib/css/owl.carousel.min.css" type="text/css"/>
        <link rel="stylesheet" href="lib/css/slicknav.min.css" type="text/css"/>
        <link rel="stylesheet" href="lib/css/style.css" type="text/css"/>


      </Head>
      <Component {...pageProps} />
      </>
   );
  }
  export default MyApp;

Here is my directory structure,

Directory Structure

After the suggestion is used _document.js inside the pages directory to include local scripts but still they aren't included when the page is rendered.

pages/_document.js code is below,

import { Html, Head, Main, NextScript } from 'next/document'
import Script from 'next/script'

export default function Document() {
  return (
    <Html>
      <Head>
            {/* Site Tag */}
            <Script src="lib/js/jquery-3.3.1.min.js" strategy="beforeInteractive"/>
            <Script src="lib/js/bootstrap.min.js" strategy="beforeInteractive"/>
            <Script src="lib/js/jquery.nice-select.min.js" strategy="beforeInteractive"/>
            <Script src="lib/js/jquery-ui.min.js" strategy="beforeInteractive"/>
            <Script src="lib/js/jquery.slicknav.js" strategy="beforeInteractive"/>
            <Script src="lib/js/mixitup.min.js" strategy="beforeInteractive"/>
            <Script src="lib/js/owl.carousel.min.js" strategy="beforeInteractive"/>
            <Script src="lib/js/main.js" strategy="beforeInteractive" />
      </Head>
      <body>
        <Main />
        <NextScript />
      </body>
    </Html>
  );
}
like image 977
Sami Ullah Avatar asked Oct 24 '25 00:10

Sami Ullah


1 Answers

Update:

Changed beforeInteractive to lazyOnload. You may also use afterInteractive.


Say you have a local script file: myscript.js that you are trying to include in your NextJS project, to include it:

  1. Place the script file in your /public folder and

  2. In your page component add:


Inside page component:

import Script from 'next/script'

export default function Document() {
  return (
    <div>
       {/* Other stuff */}
        <Script
          src="myscript.js"
          strategy="lazyOnload"
        />
    </div>
  )
} 

NextJS will know to look for scripts referenced without a URL prefix like https in the public folder.

Learn more about the <Script> component and other load strategys here:

https://nextjs.org/docs/basic-features/script#overview and https://nextjs.org/docs/api-reference/next/script

like image 199
Chizaram Igolo Avatar answered Oct 26 '25 22:10

Chizaram Igolo