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

importAnalysisBuild.ts injects same CSS styles multiple times #13169

Closed
7 tasks done
daniele-orlando opened this issue May 11, 2023 · 5 comments · Fixed by #16083
Closed
7 tasks done

importAnalysisBuild.ts injects same CSS styles multiple times #13169

daniele-orlando opened this issue May 11, 2023 · 5 comments · Fixed by #16083
Labels
p3-minor-bug An edge case that only affects very specific usage (priority)

Comments

@daniele-orlando
Copy link

daniele-orlando commented May 11, 2023

Expected

Hydrating a React app, server side rendered through Puppeteer, I expect that CSS styles already injected inside the index.html file, are not injected a second time at runtime.

Actual

The preload() function from the packages/vite/src/node/plugins/importAnalysisBuild.ts
file is failing detecting already injected stylesheets, and injects them a second time.

A picture is worth a thousand words.
build_mode

Analysis

I discovered that the check at line 110 does not handle all cases. From my test, it can be corrected changing this block.

- if (link.href === dep && (!isCss || link.rel === 'stylesheet')) {
+ if ((link.href === dep || link.getAttribute('href') === dep) && (!isCss || link.rel === 'stylesheet')) {

The point is that the routine should test even the href attribute, and not only the href property.

Reproduction

https://stackblitz.com/edit/vitejs-vite-qcgqrn?file=index.html

Steps to reproduce

I explain the reproduction steps inside the README of the reproduction repository here

https://stackblitz.com/edit/vitejs-vite-qcgqrn?file=README.md

System Info

System:
    OS: Linux 5.0 undefined
    CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 0 Bytes / 0 Bytes
    Shell: 1.0 - /bin/jsh
  Binaries:
    Node: 16.14.2 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 7.17.0 - /usr/local/bin/npm
  npmPackages:
    @vitejs/plugin-react: 4.0.0 => 4.0.0 
    vite: 4.3.5 => 4.3.5

Used Package Manager

npm

Logs

No response

Validations

@stackblitz
Copy link

stackblitz bot commented May 11, 2023

Fix this issue in StackBlitz Codeflow Start a new pull request in StackBlitz Codeflow.

@sapphi-red
Copy link
Member

sapphi-red commented May 12, 2023

I guess the problem here is this dep variable doesn't have an absolute URL.

dep = assetsURL(dep, importerUrl)

This is because this function is used

`function(dep, importerUrl) { return dep.startsWith('.') ? new URL(dep, importerUrl).href : dep }`

instead of
`function(dep, importerUrl) { return new URL(dep, importerUrl).href }`

(modulePreload.resolveDependencies is set).

@sapphi-red sapphi-red added the p3-minor-bug An edge case that only affects very specific usage (priority) label May 12, 2023
@daniele-orlando
Copy link
Author

Yes exactly. The dep and the href attribute have a relative path, while the href prop has an absolute path.

@daniele-orlando
Copy link
Author

Hello dear Vite maintainers. Is there any progress on this bug?

@dominiklippl
Copy link

@daniele-orlando I have the same Problem. My server already renders the link in the HTML, when the client hydrates the DOM the stylesheet link gets added a second time.
I managed to "fix" this with the experimental renderBuiltUrl function.

export default defineConfig({
  plugins: [vue()],
  experimental: {
    renderBuiltUrl(filename) {
      // make css assets relative
      if (filename.includes('.css')) {
        return { relative: true };
      }
    }
  }
});

This will make the file path relative and the if (link.href === dep && (!isCss || link.rel === 'stylesheet')) will return true.
I don't know what edge cases or problems might occur as a result, but it's working at the moment.

@github-actions github-actions bot locked and limited conversation to collaborators Aug 24, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
p3-minor-bug An edge case that only affects very specific usage (priority)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants