From ce33a5c7b2fe8fafca54c11668c9bd23590a81b1 Mon Sep 17 00:00:00 2001 From: Owen Buckley Date: Sat, 3 Aug 2024 19:25:09 -0400 Subject: [PATCH] better refine when standard CSS plugin should intercept --- packages/cli/src/config/rollup.config.js | 4 ++-- packages/cli/src/plugins/resource/plugin-standard-css.js | 6 ++++-- .../src/components/hero/hero.js | 5 ----- 3 files changed, 6 insertions(+), 9 deletions(-) diff --git a/packages/cli/src/config/rollup.config.js b/packages/cli/src/config/rollup.config.js index 1354e6663..e50e2aa61 100644 --- a/packages/cli/src/config/rollup.config.js +++ b/packages/cli/src/config/rollup.config.js @@ -530,8 +530,8 @@ function greenwoodSyncImportAttributes(compilation) { // since we can't do async work inside a sync AST operation if (!asset.preBundled) { const assetUrl = unbundledAssetsRefMapper[asset].sourceURL; - const request = new Request(assetUrl, { headers: { 'Content-Type': 'text/css' } }); - let response = new Response(unbundledAssetsRefMapper[asset].source); + const request = new Request(assetUrl, { headers: { 'Accept': 'text/css' } }); + let response = new Response(unbundledAssetsRefMapper[asset].source, { headers: { 'Content-Type': 'text/css' } }); for (const plugin of resourcePlugins) { if (plugin.shouldPreIntercept && await plugin.shouldPreIntercept(assetUrl, request, response.clone())) { diff --git a/packages/cli/src/plugins/resource/plugin-standard-css.js b/packages/cli/src/plugins/resource/plugin-standard-css.js index d8409ea98..27bfbcee0 100644 --- a/packages/cli/src/plugins/resource/plugin-standard-css.js +++ b/packages/cli/src/plugins/resource/plugin-standard-css.js @@ -306,11 +306,13 @@ class StandardCssResource extends ResourceInterface { }); } - async shouldIntercept(url) { + async shouldIntercept(url, request, response) { const { pathname } = url; const ext = pathname.split('.').pop(); - return url.protocol === 'file:' && ext === this.extensions[0]; + return url.protocol === 'file:' + && ext === this.extensions[0] + && (response.headers.get('Content-Type')?.indexOf('text/css') >= 0 || request.headers.get('Accept')?.indexOf('text/javascript') >= 0); } async intercept(url, request, response) { diff --git a/packages/cli/test/cases/loaders-build.prerender-import-attributes/src/components/hero/hero.js b/packages/cli/test/cases/loaders-build.prerender-import-attributes/src/components/hero/hero.js index 9ef6625cb..93e21ad56 100644 --- a/packages/cli/test/cases/loaders-build.prerender-import-attributes/src/components/hero/hero.js +++ b/packages/cli/test/cases/loaders-build.prerender-import-attributes/src/components/hero/hero.js @@ -29,11 +29,6 @@ export default class HeroBanner extends HTMLElement { } this.shadowRoot.adoptedStyleSheets = [sheet]; - // TODO upstream to WCC? - // this.shadowRoot.querySelectorAll('button') - // .forEach(button => { - // button.addEventListener('click', () => this.clickButton(button)) - // }); } }