From 17f05f9b341fb283946d0d7039072ab36fed8d9a Mon Sep 17 00:00:00 2001 From: sapphi-red <49056869+sapphi-red@users.noreply.github.com> Date: Wed, 16 Oct 2024 20:50:58 +0900 Subject: [PATCH] fix: augment hash for CSS files to prevent chromium erroring by loading previous files --- packages/vite/src/node/plugins/css.ts | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/packages/vite/src/node/plugins/css.ts b/packages/vite/src/node/plugins/css.ts index 3d4ba3b086e0c9..3114a1bdd9da90 100644 --- a/packages/vite/src/node/plugins/css.ts +++ b/packages/vite/src/node/plugins/css.ts @@ -957,6 +957,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, '') + } + } }, } } @@ -1577,6 +1587,9 @@ function combineSourcemapsIfExists( : map1 } +const viteHashUpdateMarker = '/*$vite$:1*/' +const viteHashUpdateMarkerRE = /\/\*\$vite\$:\d+\*\// + async function finalizeCss( css: string, minify: boolean, @@ -1589,6 +1602,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 }