Skip to content

Commit

Permalink
fix: backport #18367,augment hash for CSS files to prevent chromium e…
Browse files Browse the repository at this point in the history
…rroring by loading previous files (#18412)
  • Loading branch information
sapphi-red authored Oct 21, 2024
1 parent be6d562 commit 7d1a3bc
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions packages/vite/src/node/plugins/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -954,6 +954,16 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
delete bundle[`${fileName}.map`]
})
}

const cssAssets = Object.values(bundle).filter(
(asset): asset is OutputAsset =>
asset.type === 'asset' && asset.fileName.endsWith('.css'),
)
for (const cssAsset of cssAssets) {
if (typeof cssAsset.source === 'string') {
cssAsset.source = cssAsset.source.replace(viteHashUpdateMarkerRE, '')
}
}
},
}
}
Expand Down Expand Up @@ -1562,6 +1572,9 @@ function combineSourcemapsIfExists(
: map1
}

const viteHashUpdateMarker = '/*$vite$:1*/'
const viteHashUpdateMarkerRE = /\/\*\$vite\$:\d+\*\//

async function finalizeCss(
css: string,
minify: boolean,
Expand All @@ -1574,6 +1587,16 @@ async function finalizeCss(
if (minify && config.build.cssMinify) {
css = await minifyCSS(css, config, false)
}
// inject an additional string to generate a different hash for https://github.com/vitejs/vite/issues/18038
//
// pre-5.4.3, we generated CSS link tags without crossorigin attribute and generated an hash without
// this string
// in 5.4.3, we added crossorigin attribute to the generated CSS link tags but that made chromium browsers
// to block the CSSs from loading due to chromium's weird behavior
// (https://www.hacksoft.io/blog/handle-images-cors-error-in-chrome, https://issues.chromium.org/issues/40381978)
// to avoid that happening, we inject an additional string so that a different hash is generated
// for the same CSS content
css += viteHashUpdateMarker
return css
}

Expand Down

0 comments on commit 7d1a3bc

Please sign in to comment.