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

Duplicate script tags inserted #1290

Closed
benmccann opened this issue May 1, 2021 · 5 comments
Closed

Duplicate script tags inserted #1290

benmccann opened this issue May 1, 2021 · 5 comments
Labels
bug Something isn't working vite

Comments

@benmccann
Copy link
Member

benmccann commented May 1, 2021

Describe the bug

I'm noticing duplicate script tags in my SvelteKit app.

		<link rel="modulepreload" href="/./_app/pages/$layout.svelte-a6ce5e1a.js">
...
	<link rel="modulepreload" as="script" crossorigin="" href="/_app/pages/$layout.svelte-a6ce5e1a.js">

To Reproduce

  • npm init svelte@next and choose all the defaults
  • npm run preview

Inspect head element with Chrome inspector

Diagnostics
  • The output of npx envinfo --system --npmPackages svelte,@sveltejs/kit,vite --binaries --browsers
  System:
    OS: Linux 5.8 Ubuntu 20.04.2 LTS (Focal Fossa)
    CPU: (8) x64 Intel(R) Core(TM) i7-10510U CPU @ 1.80GHz
    Memory: 11.30 GB / 31.14 GB
    Container: Yes
    Shell: 5.0.17 - /bin/bash
  Binaries:
    Node: 14.16.0 - ~/.nvm/versions/node/v14.16.0/bin/node
    Yarn: 1.22.10 - ~/.nvm/versions/node/v14.16.0/bin/yarn
    npm: 7.11.1 - ~/.nvm/versions/node/v14.16.0/bin/npm
  Browsers:
    Chrome: 90.0.4430.93
    Firefox: 88.0
  npmPackages:
    @sveltejs/kit: next => 1.0.0-next.94 
    svelte: 3.37.0 => 3.37.0 
    vite: ^2.1.0 => 2.2.3
  • Your browser: Chrome

  • Your adapter (e.g. Node, static, Vercel, Begin, etc...): Node

Severity
Well I didn't notice it for awhile, so I guess it's not the worst, but it seems like there'd be some performance impact at the very least

@benmccann benmccann added the bug Something isn't working label May 1, 2021
@benmccann benmccann added this to the 1.0 milestone May 1, 2021
@benmccann benmccann changed the title Duplicate script tags Duplicate script tags inserted May 19, 2021
@Yahatix
Copy link

Yahatix commented Jun 28, 2021

I've done some digging and it seems to originate in vite see here
I'm not sure if this is something that can be changed by sk but i would assume that it would need to be changed in vite

@benmccann
Copy link
Member Author

I would guess this is related to hydration. I don't see it if I view source, but only when inspecting the document with Chrome inspector. @hbirler maybe this would be an interesting test case for you if you're still interested in working on hydration?

@hbirler
Copy link

hbirler commented Jul 29, 2021

I am not sure this is directly about hydration, since I do not see a hydration function being called with the HEAD node. And in my browser (firefox) the duplicates look like this:

<link rel="modulepreload" href="/./_app/pages/__layout.svelte-fd0963f2.js">
...
<link rel="preload" as="script" crossorigin="" href="/_app/pages/__layout.svelte-fd0963f2.js">

So module preload also becomes preload. I don't know why that would happen.

@benmccann
Copy link
Member Author

I see the following in build/app/start-4aff77ac.js, so I guess @Yahatix was right:

const __vitePreload = function preload(baseModule, deps) {
  if (!deps || deps.length === 0) {
    return baseModule();
  }
  if (scriptRel === void 0) {
    const relList = document.createElement("link").relList;
    scriptRel = relList && relList.supports && relList.supports("modulepreload") ? "modulepreload" : "preload";
  }
  return Promise.all(deps.map((dep) => {
    dep = `${base}${dep}`;
    if (dep in seen)
      return;
    seen[dep] = true;
    const isCss = dep.endsWith(".css");
    const cssSelector = isCss ? '[rel="stylesheet"]' : "";
    if (document.querySelector(`link[href="${dep}"]${cssSelector}`)) {
      return;
    }
    const link = document.createElement("link");
    link.rel = isCss ? "stylesheet" : scriptRel;
    if (!isCss) {
      link.as = "script";
      link.crossOrigin = "";
    }
    link.href = dep;
    document.head.appendChild(link);
    if (isCss) {
      return new Promise((res, rej) => {
        link.addEventListener("load", res);
        link.addEventListener("error", rej);
      });
    }
  })).then(() => baseModule());
};

@benmccann benmccann added the vite label Jul 29, 2021
@benmccann
Copy link
Member Author

I filed an issue in the Vite tracker for this here: vitejs/vite#4444

@benmccann benmccann removed this from the 1.0 milestone Jul 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working vite
Projects
None yet
Development

No branches or pull requests

3 participants