Skip to content

importAnalysisBuild.ts injects same CSS styles multiple times #13169

Closed
@daniele-orlando

Description

@daniele-orlando

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    p3-minor-bugAn edge case that only affects very specific usage (priority)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions