I’m integrating an existing React app into Next.js for mainly SEO features.
i pasted the css files links inside the <Header>
tag in Next.js
and they seem to work just fine. when i tried to do the same with the javascript files using the <script>
tag, it doesn’t execute the code inside those scripts. but i can see them loaded with http 200
in the chrome dev tools.
I tried using a library called Loadjs
from npm
to load the scripts inside componentDidMount
but i got the exact same result as using <script>
tag.
is there a proper way to do such simple thing in Next.js
that i’m not aware of ?
Here’s the code included in the pages/index.js
file.
import React from "react"
import Head from 'next/head'
import MyAwesomeComponent from "../components/mycomponent.js"
export default () => (
<div>
<Head>
<link type="text/css" rel="stylesheet" href="static/css/chatwidget.css" />
<link type="text/css" rel="stylesheet" href="static/css/download.css" />
<script type="text/javascript" src="static/libs/jquery.min.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/jquery.mCustomScrollbar.concat.min.js"></script>
<script type="text/javascript" src="static/libs/bootstrap.min.js"></script>
<script type="text/javascript" src="static/libs/owl.carousel.min.js"></script>
<script type="text/javascript" src="static/scripts/chatHead.js"></script>
<script type="text/javascript" src="static/libs/jquery.magnific-popup.js"></script>
</Head>
<MyAwesomeComponent /> {/* a simple React component that returns : <p>Hello World </p>*/}
</div>
)
Sorry for the late answer.
it turned out that all the scripts
i linked missed one script that would actually run the functions
for each action.
8
Answers
This works to me:
Create a folder for your static files:
<root>/public/static/script.js
in your index.js at
<root>/pages/index.js
Note that
static
is the name I used in this example, it’s not a requirement, it would work with any other folder name.May this helps you Nextjs public folder
Move your static folder into public folder in your root directory
You can also run js code this
With the below approach you can easily put a script file’s raw script text into a generated Next.js HTML page’s
<head>
without screwing around with character escaping, formatting and general pretending that we aren’t actually just building an HTML page in the end anyways.There are many use cases you may want a script to run without going to network. Ex: 3rd party scripts, monitoring / analytics scripts that expect to be in the
<head>
without a separate network load. Many of these come minified, mangled, you-name-it and are just supposed to be copy, paste, move on with life.Next.js makes this very hard pretending that everything with web development is magically React and Webpack all the time now (I wish right?)
The best developer experience way I’ve found is to do this:
_document.js
next.config.js
https://github.com/vercel/next.js/blob/canary/packages/next/next-server/server/config.ts#L33
rawJsFromFile.js
Hope this saves someone from hours of frustration that I endured coming up with this… 😭
This is what I tried and it worked for me.
I used two files
entry-script.js
andmain-script.js
. I put these like this<root>/static/entry-script.js
and<root>/static/main-script.js
The content of
entry-script.js
is below.and the main logic is in the file
main-script.js
.In the file
_doucment.js
of NextJS I included my fileentry-script.js
in body like belowWith Next.js v11 and onward, you can use the Next component
Script
https://nextjs.org/blog/next-11#script-optimization
I wrote an article elaborating on this question, hopefully it comes in handy:
https://www.devtwins.com/blog/how-to-add-a-third-party-script-to-a-nextjs-website
Or if you want to try another way to import Js file like I did