Use specific fetch destination for JSON/CSS modules
- Category
- JavaScript
- Type
- Chromium catches up
- Status
- In development (Chrome In development)
- Intent stage
- None
WPT details: - https://wpt.fyi/results/fetch/api/request/destination/fetch-destination.https.html - https://wpt.fyi/results/content-security-policy/connect-src/connect-src-json-import-allowed.sub.html - https://wpt.fyi/results/content-security-policy/connect-src/connect-src-json-import-blocked.sub.html - https://wpt.fyi/results/content-security-policy/style-src/import-style-allowed.sub.html - https://wpt.fyi/results/content-security-policy/style-src/import-style-blocked.sub.html
Summary
CSS and JSON modules will be fetched using a specific fetch destination (either "css" or "json") rather than a generic "script", that is normally used for JavaScript modules. This has the following effects: - the `Accept` HTTP header in the request will describe the expected mime type (`text/css,*/*;q=0.1` or `application/json,*/*;q=0.5`) - those modules will respect the style-src or connect-src Content Security Policies, rather than using JavaScript's script-src - When inspecting the request's destination (either through a service worker or through the `Sec-Fetch-Destination` HTTP header) it will be reported as `"style"` or `"json"`, rather than empty.
Motivation
Currently, browsers throw an error if a server provides a non-JSON/CSS file in response to a JSON/CSS module import. However, there is no way for the browser to distinguish between requests for JavaScript modules and JSON/CSS modules even though in most other parts of the web platform this is done through the Accept HTTP header. In addition to that, JSON/CSS modules were fetched without respecting the relevant CSPs for JSON and CSS resources, but instead using the CSP defined for JavaScript resources.
Standards & signals
- Specification: https://html.spec.whatwg.org/#fetch-a-single-module-script
- Firefox: No signal
- Safari: Support — Webkit was supportive of the import attributes proposal conditional on these changes to how JSON/CSS modules are fetched
- Web developers: No signals
- Tracking bug: https://bugs.chromium.org/p/chromium/issues/detail?id=1491336
Ecosystem
- Web Platform Tests: https://wpt.fyi/results/?q=fetch%2Fapi%2Frequest%2Fdestination%2Ffetch-destination
- Chrome Platform Showcase: https://chrome-platform-showcase.paulkinlan-ea.deno.net/features
- GenDN reference: https://gendn.paulkinlan-ea.deno.net/features
- chrome.dev demos search: https://www.google.com/search?q=site%3Achrome.dev+Use%20specific%20fetch%20destination%20for%20JSON%2FCSS%20modules
- MDN Web Docs: https://developer.mozilla.org/en-US/search?q=Use%20specific%20fetch%20destination%20for%20JSON%2FCSS%20modules
- Baseline status: https://webstatus.dev/?q=Use%20specific%20fetch%20destination%20for%20JSON%2FCSS%20modules