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

fix: augment hash for CSS files to prevent chromium erroring by loading previous files #18367

Merged
merged 1 commit into from
Oct 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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, '')
}
}
},
}
}
Expand Down Expand Up @@ -1577,6 +1587,9 @@ function combineSourcemapsIfExists(
: map1
}

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

async function finalizeCss(
css: string,
minify: boolean,
Expand All @@ -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
}

Expand Down