question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

next/script is not loading when navigating between pages

See original GitHub issue

What version of Next.js are you using?

11.0.0

What version of Node.js are you using?

14.16.0

What browser are you using?

Chrome

What operating system are you using?

Windows

How are you deploying your application?

next start

Describe the Bug

next/script is not loading when navigating between pages with next/link, only if you load or reload the page outside of the SPA

Expected Behavior

the next/script load normally

To Reproduce

  1. npx create-next-app
  2. create any page, put a script url to load
  3. create a navigation (next/link) to the created page
  4. navigate to the page

index.js:

import Link from 'next/link'

export default function Home() {
  return (
    <div>
      <Link href="/script" passHref><a>navigate to script</a></Link>
    </div>
  )
}

script.js:

import React from "react";
import Script from "next/script";

export default function script() {
  return (
    <>
      <Script key="script_from_url" id="script_from_url" src="http://localhost:3000/script.js" strategy="beforeInteractive" onLoad={() => console.log("script loaded")} />
      <div>script</div>
    </>
  );
}

Notes:

  1. onLoad is not being activated at any time with strategy beforeInteractive
  2. I am getting this error on the console when the script is first loaded (even though I put key and id in the Script props):

Warning: Each child in a list should have a unique “key” prop. See https://reactjs.org/link/warning-keys for more information.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
willnix86commented, Jun 22, 2021

+1 for this For me, none of the Script tags load when i redirect or navigate to the page containing them, it’s only when i hit “refresh” that they then load.

0reactions
balazsorban44commented, Jan 28, 2022

This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

Read more comments on GitHub >

github_iconTop Results From Across the Web

next js script is not working when navigating between the pages
I am using next/link for navigating between the pages for fast refreshing. The problem is it does not load script when I navigate...
Read more >
Normal scripts "<script>" not working after page navigation
Normal scripts are working when the page is accessed for the first time or reloaded. It's not working when I navigate to any...
Read more >
Script Loading, performance and Next Script
I have run tests on those pages through both webpage tests and PageSpeed Insights. Unfortunately, the results are not conclusive. The set up...
Read more >
Optimizing third-party script loading in Next.js
It's important to consider the resource loading sequence of your page so that critical resources are not delayed and non-critical resources ...
Read more >
Advanced Features: Custom `App` - Next.js
Custom App · Persist layouts between page changes · Keeping state when navigating pages · Inject additional data into pages · Add global...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found