Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

next/script is run once #62303

Open
michaltarasiuk opened this issue Feb 20, 2024 · 0 comments
Open

next/script is run once #62303

michaltarasiuk opened this issue Feb 20, 2024 · 0 comments
Labels
bug Issue was opened via the bug report template. Script (next/script) Related to Next.js Script Optimization.

Comments

@michaltarasiuk
Copy link
Contributor

michaltarasiuk commented Feb 20, 2024

Link to the code that reproduces this issue

https://github.com/michaltarasiuk/next-script-fragment-hash

To Reproduce

  1. Start the application in development mode.
  2. Go to page 1 (script indicator is green - script is executed)
  3. Go back to home page
  4. Revisit page 1 (script indicator is red - script is not executed)

Current vs. Expected behavior

Following the steps from the previous section, I expected that script will be executed each time I visit the page. Instead it is executed only once.

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 23.0.0: Fri Sep 15 14:41:43 PDT 2023; root:xnu-10002.1.13~1/RELEASE_ARM64_T6000
  Available memory (MB): 16384
  Available CPU cores: 10
Binaries:
  Node: 21.4.0
  npm: 10.2.4
  Yarn: 1.22.19
  pnpm: 8.12.1
Relevant Packages:
  next: 14.1.1-canary.63 // Latest available version is detected (14.1.1-canary.63).
  eslint-config-next: N/A
  react: 18.2.0
  react-dom: 18.2.0
  typescript: 5.1.3
Next.js Config:
  output: N/A

Which area(s) are affected? (Select all that apply)

Script optimization (next/script)

Which stage(s) are affected? (Select all that apply)

next dev (local), next start (local)

Additional context

Related with:


  1. Script is fetched and loaded only once (current behavior)

When I first visited the page, the script executed successfully, and DOM elements were modified. The issue occured when moving from another page to the main one; the script failed to execute, leading to the page not functioning correctly.

I found that fragment of code.

const cacheKey = id || src;

// Script has already loaded
if (cacheKey && LoadCache.has(cacheKey)) {
  return;
}

// Contents of this script are already loading/loaded
if (ScriptCache.has(src)) {
  LoadCache.add(cacheKey);
  // It is possible that multiple `next/script` components all have same "src", but has different “onLoad”
  // This is to make sure the same remote script will only load once, but "onLoad" are executed in order
  ScriptCache.get(src).then(onLoad, onError);
  return;
}

Such caching is appropriate when the script assigns values/methods to the window object, allowing for later use by onReady.

  1. Script is fetched once and executed every time (expected behavior)

URL fragment hash does not cause browser to refetch resource. Next.js thinks of that as the different URL so the script gets executed again, and everything works as expected.

import cryptoRandomString from "crypto-random-string";

function getScriptSrc() {
  const urlObject = new URL("script.js", "http://localhost:3000");
  urlObject.hash = cryptoRandomString({ length: 10, type: "url-safe" });
  return urlObject.toString();
}

This works, but it seems to not be the best solution.

@michaltarasiuk michaltarasiuk added the bug Issue was opened via the bug report template. label Feb 20, 2024
@github-actions github-actions bot added the Script (next/script) Related to Next.js Script Optimization. label Feb 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issue was opened via the bug report template. Script (next/script) Related to Next.js Script Optimization.
Projects
None yet
Development

No branches or pull requests

1 participant