next.js icon indicating copy to clipboard operation
next.js copied to clipboard

Normal scripts "<script>" not working after page navigation

Open parthlogicwind opened this issue 8 years ago • 34 comments

Hi, I am facing a problem with scripts. I have included normal scripts in NextJS with tag. Normal scripts are working when the page is accessed for the first time or reloaded. It's not working when I navigate to any other page. I have even tried with simple alert, but it is still not working. It's only working when the page is opened for the first time or on reload. I have even try to include scripts in _document.js file but still same issue.

Is there any other way to access or recall scripts when I navigate the page? Please help

parthlogicwind avatar May 26 '18 10:05 parthlogicwind

I'm not sure how to help without an example. Also following the issue template this should be posted on https://spectrum.chat/next-js

timneutkens avatar May 26 '18 10:05 timneutkens

Hi, @timneutkens I have created a repo for the example, Please check https://github.com/parthlogicwind/NextJs-Demo

I am using Bootstrap template to implement a site in NextJs.

The issue is scripts are working when the page is accessed for the first time or reloaded. It's not working when I navigate to any other page.

parthlogicwind avatar May 28 '18 07:05 parthlogicwind

@parthlogicwind did you ever find a fix for this? I'm experiencing the same issue. Thanks!

kilowattkris avatar Jul 12 '18 15:07 kilowattkris

@parthlogicwind I found a fix. Not sure which script you're using, but my issue was that the bootstrap init occurred on $(document).ready, which only fires on the page reloads. I instead moved the init process into a function I could call whenever a new page is loaded. Not sure if this is the best solution, but it fixes the issue! Hope you were able to resolve your issues too!

kilowattkris avatar Jul 12 '18 17:07 kilowattkris

Also having this issue

johndevor avatar Feb 10 '19 23:02 johndevor

Same problem! Please help...

pooladkhay avatar Jul 10 '19 20:07 pooladkhay

did anyone found the fix? please help

aesthytik avatar Jul 30 '19 07:07 aesthytik

me too

oyatek avatar Aug 27 '19 16:08 oyatek

Same problem here, you have to click back, go to the same page again, then the ssr loaded instance will available on other pages, it's just weird.

eldyvoon avatar Oct 15 '19 02:10 eldyvoon

One solution is to install “loadjs” and load js files manually in “componentDidMount”. I ended up writing this code in every class I used:

componentDidMount() { loadJs(“/path/to/js/file.js”); }

pooladkhay avatar Oct 15 '19 10:10 pooladkhay

I've decided to inject the script in component level rather than pages level, I used this package in componentDidMount

https://www.npmjs.com/package/postscribe

eldyvoon avatar Oct 16 '19 01:10 eldyvoon

This worked for me

import Safe from "react-safe" // in render <Safe.script src="https://use.typekit.net/foobar.js"></Safe.script> <Safe.script>{ try{Typekit.load({ async: true });}catch(e){} } </Safe.script>

https://stackoverflow.com/questions/34424845/adding-script-tag-to-react-jsx

nfmshow avatar Dec 29 '19 08:12 nfmshow

Just came across this and was able to solve it using the StackOverflow answer @nfmshow linked with the updated answer. I needed to insert the script tag within the dom, not just on the body. (For inserting a convertkit form)

Created this hook:

import { useEffect, RefObject } from "react";

export const useScript = (
  url: string,
  ref: RefObject<HTMLDivElement>
) => {
  useEffect(() => {
    const script = document.createElement("script");

    script.src = url;
    script.async = true;

    if (ref.current) {
      ref.current.appendChild(script);
    }

    return () => {
      ref.current.removeChild(script);
    };
  }, [url, ref]);
};

And then used it in a component:

import { useRef } from "react";
import { useScript } from "../hooks/useScript";

export function ScriptInsertion() {
  const ref = useRef<HTMLDivElement>(null);
  useScript(
    "script-url",
    ref
  );

  return <div ref={ref}></div>;
}

rywils21 avatar Jul 08 '20 18:07 rywils21

I have been working on this issue for a long time now and I have found that external script tags only load once and then there is no interactivity. So the only fix is to use react js instead of vanilla javascript or jquery.

Arpitgoyalgg avatar Oct 10 '20 07:10 Arpitgoyalgg

Did anyone find a solution to get the scripts working? I have 4 external js files which somehow don't execute when I load the page first time. I checked the html and it is being added at the bottom but, the code isn't executed untill I paste and run the whole code from the file into the console.

jagrajbains avatar Nov 07 '20 14:11 jagrajbains

The way I fixed this was to create a function doThing() and copy paste the code from the js files. (Although I'm not sure how jquery fares well with this solution)

export const doThing = () => {
// the js file in here
}

then in your components, use componentDidMount or useEffect hook to call the doThing() function and it will run all the document.querySelector and those jazz...

  • An ** important note** is probably to not include things like document.addEventListener("DOMContentLoaded", () => {...}) or the equivalent in jquery Although, I haven't tested refactoring everything to the _app.js or _document.js... I guess y'all can try it out :)

vinlegend1 avatar Dec 22 '20 14:12 vinlegend1

I have put in _document.js in body part as a script, if ı used <a tag the jquery and javascript files works, but if ı used next/link it doesnt work, anyone has any idea

Ramekici avatar Apr 16 '21 11:04 Ramekici

I am also getting same issue

yashrabari avatar Jun 23 '21 06:06 yashrabari

how did you guys fix this?

ekoydakoykoy avatar Jul 23 '21 05:07 ekoydakoykoy

I have tried with the different strategy used in next/script in v11 but still, it doesn't work.

If anyone managed to solve this, could you please comment how?

surjithctly avatar Aug 11 '21 14:08 surjithctly