I am going to use a theme of themeforest on Next.js
I tried to load jquery core and some plugins, but Nextjs can't load it.
import { useEffect } from "react"
import { SessionProvider } from "next-auth/react"
import Script from 'next/script'
import '../public/plugin/datatables/responsive.dataTables.min.css'
import '../public/plugin/datatables/dataTables.bootstrap5.min.css'
import '../public/css/cryptoon.style.min.css'
// import "./styles.css"
export default function App({ Component, pageProps }) {
return (
<div>
<SessionProvider session={pageProps.session}>
<Component {...pageProps} />
</SessionProvider>
<Script type="text/javascript" src="/bundles/libscripts.bundle.js" strategy="lazyOnload" />
<Script type="text/javascript" src="/bundles/apexcharts.bundle.js" strategy="lazyOnload" />
<Script type="text/javascript" src="/js/template.js" strategy="lazyOnload" />
<Script type="text/javascript" src="/js/page/index.js" strategy="lazyOnload" />
</div>
)
}
But a bunch of errors have occurred:
Unhandled Runtime Error
TypeError: Cannot read properties of null (reading 'addEventListener')
Call Stack
HTMLDocument.<anonymous>
/js/template.js (185:18)
e
/bundles/libscripts.bundle.js (1:29754)
t
/bundles/libscripts.bundle.js (1:30078)
....
How to load template(HTML/CSS/JAVASCRIPT) to Nextjs component?
I would recommend you to use next/head I normally use this to put my scripts but it depends on your case, remember that using different strategies (like lazyOnload) and the order in some cases is important, but if you require to use next/script as the docs says then, check your strategies try with other ones like beforeInteractive and double check the order in which your scripts should be loaded and if you continue getting those errors probably the error is related with the content of your scripts, open your network tab and inspect what happened.