Unverified Commit 1a319ea4 authored by Luke Sandberg's avatar Luke Sandberg Committed by GitHub

[backport] Fix CSS HMR on Safari (#92174)

## Summary
- Backport of #92123 to `next-16-2` release branch
- Cherry-picked the CSS HMR Safari fix and regenerated turbopack-tests
snapshot goldens for the release branch

## Test plan
- [x] `UPDATE=1 cargo nextest r -p turbopack-tests` — all 297 tests pass
- [x] `pnpm build-all` — clean build, no issues with
`generated-native.d.ts`
parent c0edad27
......@@ -1047,19 +1047,24 @@ jobs:
# these all run without concurrency because they're heavier
export TEST_CONCURRENCY=1
export IS_WEBPACK_TEST=1
BROWSER_NAME=firefox node run-tests.js \
export IS_TURBOPACK_TEST=1
TURBOPACK_BUILD=1 NEXT_TEST_MODE=start BROWSER_NAME=firefox node run-tests.js \
test/production/pages-dir/production/test/index.test.ts \
test/production/chunk-load-failure/chunk-load-failure.test.ts
NEXT_TEST_MODE=start BROWSER_NAME=safari node run-tests.js \
TURBOPACK_DEV=1 NEXT_TEST_MODE=dev BROWSER_NAME=firefox node run-tests.js \
test/e2e/app-dir/scss/hmr-module/hmr-module.test.ts
TURBOPACK_DEV=1 NEXT_TEST_MODE=dev BROWSER_NAME=safari node run-tests.js \
test/e2e/app-dir/scss/hmr-module/hmr-module.test.ts
TURBOPACK_BUILD=1 NEXT_TEST_MODE=start BROWSER_NAME=safari node run-tests.js \
test/production/pages-dir/production/test/index.test.ts \
test/production/chunk-load-failure/chunk-load-failure.test.ts \
test/e2e/basepath/basepath.test.ts \
test/e2e/basepath/error-pages.test.ts
BROWSER_NAME=safari DEVICE_NAME='iPhone XR' node run-tests.js \
TURBOPACK_BUILD=1 NEXT_TEST_MODE=start BROWSER_NAME=safari DEVICE_NAME='iPhone XR' node run-tests.js \
test/production/prerender-prefetch/index.test.ts
stepName: 'test-firefox-safari'
secrets: inherit
......
......@@ -237,6 +237,11 @@ function mergeChunkUpdates(
return undefined
}
if (updateB.type === 'total') {
// A total update replaces the entire chunk, so it supersedes any prior update.
return updateB
}
if (updateA.type === 'partial') {
invariant(updateA.instruction, 'Partial updates are unsupported')
}
......
......@@ -18,12 +18,15 @@ let DEV_BACKEND: DevRuntimeBackend
unloadChunk(chunkUrl) {
deleteResolver(chunkUrl)
// Strip query string so we match links regardless of cache-busting
// params (e.g. ?ts=) that may differ between HMR updates.
const baseChunkUrl = chunkUrl.split('?')[0]
// TODO(PACK-2140): remove this once all filenames are guaranteed to be escaped.
const decodedChunkUrl = decodeURI(chunkUrl)
const decodedBaseChunkUrl = decodeURI(baseChunkUrl)
if (isCss(chunkUrl)) {
const links = document.querySelectorAll(
`link[href="${chunkUrl}"],link[href^="${chunkUrl}?"],link[href="${decodedChunkUrl}"],link[href^="${decodedChunkUrl}?"]`
`link[href="${baseChunkUrl}"],link[href^="${baseChunkUrl}?"],link[href="${decodedBaseChunkUrl}"],link[href^="${decodedBaseChunkUrl}?"]`
)
for (const link of Array.from(links)) {
link.remove()
......@@ -34,7 +37,7 @@ let DEV_BACKEND: DevRuntimeBackend
// However, we still want to remove the script tag from the DOM to keep
// the HTML somewhat consistent from the user's perspective.
const scripts = document.querySelectorAll(
`script[src="${chunkUrl}"],script[src^="${chunkUrl}?"],script[src="${decodedChunkUrl}"],script[src^="${decodedChunkUrl}?"]`
`script[src="${baseChunkUrl}"],script[src^="${baseChunkUrl}?"],script[src="${decodedBaseChunkUrl}"],script[src^="${decodedBaseChunkUrl}?"]`
)
for (const script of Array.from(scripts)) {
script.remove()
......@@ -51,9 +54,12 @@ let DEV_BACKEND: DevRuntimeBackend
return
}
const decodedChunkUrl = decodeURI(chunkUrl)
// Strip query string so we match links regardless of cache-busting
// params (e.g. ?ts=) that may differ between HMR updates.
const baseChunkUrl = chunkUrl.split('?')[0]
const decodedBaseChunkUrl = decodeURI(baseChunkUrl)
const previousLinks = document.querySelectorAll(
`link[rel=stylesheet][href="${chunkUrl}"],link[rel=stylesheet][href^="${chunkUrl}?"],link[rel=stylesheet][href="${decodedChunkUrl}"],link[rel=stylesheet][href^="${decodedChunkUrl}?"]`
`link[rel=stylesheet][href="${baseChunkUrl}"],link[rel=stylesheet][href^="${baseChunkUrl}?"],link[rel=stylesheet][href="${decodedBaseChunkUrl}"],link[rel=stylesheet][href^="${decodedBaseChunkUrl}?"]`
)
if (previousLinks.length === 0) {
......@@ -64,15 +70,28 @@ let DEV_BACKEND: DevRuntimeBackend
const link = document.createElement('link')
link.rel = 'stylesheet'
if (navigator.userAgent.includes('Firefox')) {
// Firefox won't reload CSS files that were previously loaded on the current page,
// we need to add a query param to make sure CSS is actually reloaded from the server.
if (
navigator.userAgent.includes('Firefox') ||
(navigator.userAgent.includes('Safari') &&
!navigator.userAgent.includes('Chrome') &&
!navigator.userAgent.includes('Chromium'))
) {
// Firefox won't reload CSS files that were previously loaded on the
// current page: https://bugzilla.mozilla.org/show_bug.cgi?id=1037506
//
// I believe this is this issue: https://bugzilla.mozilla.org/show_bug.cgi?id=1037506
// Safari serves cached CSS when a <link rel=preload> exists for the
// same URL: https://bugs.webkit.org/show_bug.cgi?id=187726
//
// Safari has a similar issue, but only if you have a `<link rel=preload ... />` tag
// pointing to the same URL as the stylesheet: https://bugs.webkit.org/show_bug.cgi?id=187726
link.href = `${chunkUrl}?ts=${Date.now()}`
// Replace or add a fresh `ts` cache-busting param without
// discarding other query parameters that may already be present.
const url = new URL(chunkUrl, location.origin)
url.searchParams.set('ts', String(Date.now()))
// Reduced timer precision in some browers could lead to an update getting dropped
// in firefox if it happens fast enough (in firefox precision is sometimes 100ms!).
// So trust that the server is only updating us when it is important and use a
// random number to bust the cache.
url.searchParams.set('_next_rand', String(Math.random()))
link.href = url.pathname + url.search
} else {
link.href = chunkUrl
}
......
......@@ -11,13 +11,10 @@
function getAssetSuffixFromScriptSrc() {
// TURBOPACK_ASSET_SUFFIX is set in web workers
return (
(self.TURBOPACK_ASSET_SUFFIX ??
document?.currentScript
?.getAttribute?.('src')
?.replace(/^(.*(?=\?)|^.*$)/, '')) ||
''
)
if (self.TURBOPACK_ASSET_SUFFIX != null) return self.TURBOPACK_ASSET_SUFFIX
const src = document?.currentScript?.getAttribute?.('src') ?? ''
const qi = src.indexOf('?')
return qi >= 0 ? src.slice(qi) : ''
}
type ChunkResolver = {
......
;!function(){try { var e="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof global?global:"undefined"!=typeof window?window:"undefined"!=typeof self?self:{},n=(new e.Error).stack;n&&((e._debugIds|| (e._debugIds={}))[n]="c7d0d6f1-2fed-24f2-514f-5f447a850612")}catch(e){}}();
;!function(){try { var e="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof global?global:"undefined"!=typeof window?window:"undefined"!=typeof self?self:{},n=(new e.Error).stack;n&&((e._debugIds|| (e._debugIds={}))[n]="154fe60a-f286-b7e0-80b6-f5300e0e86a1")}catch(e){}}();
(globalThis["TURBOPACK"] || (globalThis["TURBOPACK"] = [])).push([
"output/0v.~_crates_turbopack-tests_tests_snapshot_debug-ids_browser_input_index_0jat3.b.js",
{"otherChunks":["output/0teu_crates_turbopack-tests_tests_snapshot_debug-ids_browser_input_index_01.8xqj.js"],"runtimeModuleIds":["[project]/turbopack/crates/turbopack-tests/tests/snapshot/debug-ids/browser/input/index.js [test] (ecmascript)"]}
......@@ -1957,7 +1957,10 @@ globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS ??= [];
/// <reference path="../../../shared/runtime/runtime-types.d.ts" />
function getAssetSuffixFromScriptSrc() {
// TURBOPACK_ASSET_SUFFIX is set in web workers
return (self.TURBOPACK_ASSET_SUFFIX ?? document?.currentScript?.getAttribute?.('src')?.replace(/^(.*(?=\?)|^.*$)/, '')) || '';
if (self.TURBOPACK_ASSET_SUFFIX != null) return self.TURBOPACK_ASSET_SUFFIX;
const src = document?.currentScript?.getAttribute?.('src') ?? '';
const qi = src.indexOf('?');
return qi >= 0 ? src.slice(qi) : '';
}
let BACKEND;
/**
......@@ -2129,10 +2132,13 @@ let DEV_BACKEND;
DEV_BACKEND = {
unloadChunk (chunkUrl) {
deleteResolver(chunkUrl);
// Strip query string so we match links regardless of cache-busting
// params (e.g. ?ts=) that may differ between HMR updates.
const baseChunkUrl = chunkUrl.split('?')[0];
// TODO(PACK-2140): remove this once all filenames are guaranteed to be escaped.
const decodedChunkUrl = decodeURI(chunkUrl);
const decodedBaseChunkUrl = decodeURI(baseChunkUrl);
if (isCss(chunkUrl)) {
const links = document.querySelectorAll(`link[href="${chunkUrl}"],link[href^="${chunkUrl}?"],link[href="${decodedChunkUrl}"],link[href^="${decodedChunkUrl}?"]`);
const links = document.querySelectorAll(`link[href="${baseChunkUrl}"],link[href^="${baseChunkUrl}?"],link[href="${decodedBaseChunkUrl}"],link[href^="${decodedBaseChunkUrl}?"]`);
for (const link of Array.from(links)){
link.remove();
}
......@@ -2141,7 +2147,7 @@ let DEV_BACKEND;
// runtime once evaluated.
// However, we still want to remove the script tag from the DOM to keep
// the HTML somewhat consistent from the user's perspective.
const scripts = document.querySelectorAll(`script[src="${chunkUrl}"],script[src^="${chunkUrl}?"],script[src="${decodedChunkUrl}"],script[src^="${decodedChunkUrl}?"]`);
const scripts = document.querySelectorAll(`script[src="${baseChunkUrl}"],script[src^="${baseChunkUrl}?"],script[src="${decodedBaseChunkUrl}"],script[src^="${decodedBaseChunkUrl}?"]`);
for (const script of Array.from(scripts)){
script.remove();
}
......@@ -2155,23 +2161,34 @@ let DEV_BACKEND;
reject(new Error('The DOM backend can only reload CSS chunks'));
return;
}
const decodedChunkUrl = decodeURI(chunkUrl);
const previousLinks = document.querySelectorAll(`link[rel=stylesheet][href="${chunkUrl}"],link[rel=stylesheet][href^="${chunkUrl}?"],link[rel=stylesheet][href="${decodedChunkUrl}"],link[rel=stylesheet][href^="${decodedChunkUrl}?"]`);
// Strip query string so we match links regardless of cache-busting
// params (e.g. ?ts=) that may differ between HMR updates.
const baseChunkUrl = chunkUrl.split('?')[0];
const decodedBaseChunkUrl = decodeURI(baseChunkUrl);
const previousLinks = document.querySelectorAll(`link[rel=stylesheet][href="${baseChunkUrl}"],link[rel=stylesheet][href^="${baseChunkUrl}?"],link[rel=stylesheet][href="${decodedBaseChunkUrl}"],link[rel=stylesheet][href^="${decodedBaseChunkUrl}?"]`);
if (previousLinks.length === 0) {
reject(new Error(`No link element found for chunk ${chunkUrl}`));
return;
}
const link = document.createElement('link');
link.rel = 'stylesheet';
if (navigator.userAgent.includes('Firefox')) {
// Firefox won't reload CSS files that were previously loaded on the current page,
// we need to add a query param to make sure CSS is actually reloaded from the server.
if (navigator.userAgent.includes('Firefox') || navigator.userAgent.includes('Safari') && !navigator.userAgent.includes('Chrome') && !navigator.userAgent.includes('Chromium')) {
// Firefox won't reload CSS files that were previously loaded on the
// current page: https://bugzilla.mozilla.org/show_bug.cgi?id=1037506
//
// I believe this is this issue: https://bugzilla.mozilla.org/show_bug.cgi?id=1037506
// Safari serves cached CSS when a <link rel=preload> exists for the
// same URL: https://bugs.webkit.org/show_bug.cgi?id=187726
//
// Safari has a similar issue, but only if you have a `<link rel=preload ... />` tag
// pointing to the same URL as the stylesheet: https://bugs.webkit.org/show_bug.cgi?id=187726
link.href = `${chunkUrl}?ts=${Date.now()}`;
// Replace or add a fresh `ts` cache-busting param without
// discarding other query parameters that may already be present.
const url = new URL(chunkUrl, location.origin);
url.searchParams.set('ts', String(Date.now()));
// Reduced timer precision in some browers could lead to an update getting dropped
// in firefox if it happens fast enough (in firefox precision is sometimes 100ms!).
// So trust that the server is only updating us when it is important and use a
// random number to bust the cache.
url.searchParams.set('_next_rand', String(Math.random()));
link.href = url.pathname + url.search;
} else {
link.href = chunkUrl;
}
......@@ -2217,5 +2234,5 @@ chunkListsToRegister.forEach(registerChunkList);
})();
//# debugId=c7d0d6f1-2fed-24f2-514f-5f447a850612
//# debugId=154fe60a-f286-b7e0-80b6-f5300e0e86a1
//# sourceMappingURL=0teu_crates_turbopack-tests_tests_snapshot_debug-ids_browser_input_index_0jat3.b.js.map
\ No newline at end of file
......@@ -1956,7 +1956,10 @@ globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS ??= [];
/// <reference path="../../../shared/runtime/runtime-types.d.ts" />
function getAssetSuffixFromScriptSrc() {
// TURBOPACK_ASSET_SUFFIX is set in web workers
return (self.TURBOPACK_ASSET_SUFFIX ?? document?.currentScript?.getAttribute?.('src')?.replace(/^(.*(?=\?)|^.*$)/, '')) || '';
if (self.TURBOPACK_ASSET_SUFFIX != null) return self.TURBOPACK_ASSET_SUFFIX;
const src = document?.currentScript?.getAttribute?.('src') ?? '';
const qi = src.indexOf('?');
return qi >= 0 ? src.slice(qi) : '';
}
let BACKEND;
/**
......@@ -2128,10 +2131,13 @@ let DEV_BACKEND;
DEV_BACKEND = {
unloadChunk (chunkUrl) {
deleteResolver(chunkUrl);
// Strip query string so we match links regardless of cache-busting
// params (e.g. ?ts=) that may differ between HMR updates.
const baseChunkUrl = chunkUrl.split('?')[0];
// TODO(PACK-2140): remove this once all filenames are guaranteed to be escaped.
const decodedChunkUrl = decodeURI(chunkUrl);
const decodedBaseChunkUrl = decodeURI(baseChunkUrl);
if (isCss(chunkUrl)) {
const links = document.querySelectorAll(`link[href="${chunkUrl}"],link[href^="${chunkUrl}?"],link[href="${decodedChunkUrl}"],link[href^="${decodedChunkUrl}?"]`);
const links = document.querySelectorAll(`link[href="${baseChunkUrl}"],link[href^="${baseChunkUrl}?"],link[href="${decodedBaseChunkUrl}"],link[href^="${decodedBaseChunkUrl}?"]`);
for (const link of Array.from(links)){
link.remove();
}
......@@ -2140,7 +2146,7 @@ let DEV_BACKEND;
// runtime once evaluated.
// However, we still want to remove the script tag from the DOM to keep
// the HTML somewhat consistent from the user's perspective.
const scripts = document.querySelectorAll(`script[src="${chunkUrl}"],script[src^="${chunkUrl}?"],script[src="${decodedChunkUrl}"],script[src^="${decodedChunkUrl}?"]`);
const scripts = document.querySelectorAll(`script[src="${baseChunkUrl}"],script[src^="${baseChunkUrl}?"],script[src="${decodedBaseChunkUrl}"],script[src^="${decodedBaseChunkUrl}?"]`);
for (const script of Array.from(scripts)){
script.remove();
}
......@@ -2154,23 +2160,34 @@ let DEV_BACKEND;
reject(new Error('The DOM backend can only reload CSS chunks'));
return;
}
const decodedChunkUrl = decodeURI(chunkUrl);
const previousLinks = document.querySelectorAll(`link[rel=stylesheet][href="${chunkUrl}"],link[rel=stylesheet][href^="${chunkUrl}?"],link[rel=stylesheet][href="${decodedChunkUrl}"],link[rel=stylesheet][href^="${decodedChunkUrl}?"]`);
// Strip query string so we match links regardless of cache-busting
// params (e.g. ?ts=) that may differ between HMR updates.
const baseChunkUrl = chunkUrl.split('?')[0];
const decodedBaseChunkUrl = decodeURI(baseChunkUrl);
const previousLinks = document.querySelectorAll(`link[rel=stylesheet][href="${baseChunkUrl}"],link[rel=stylesheet][href^="${baseChunkUrl}?"],link[rel=stylesheet][href="${decodedBaseChunkUrl}"],link[rel=stylesheet][href^="${decodedBaseChunkUrl}?"]`);
if (previousLinks.length === 0) {
reject(new Error(`No link element found for chunk ${chunkUrl}`));
return;
}
const link = document.createElement('link');
link.rel = 'stylesheet';
if (navigator.userAgent.includes('Firefox')) {
// Firefox won't reload CSS files that were previously loaded on the current page,
// we need to add a query param to make sure CSS is actually reloaded from the server.
if (navigator.userAgent.includes('Firefox') || navigator.userAgent.includes('Safari') && !navigator.userAgent.includes('Chrome') && !navigator.userAgent.includes('Chromium')) {
// Firefox won't reload CSS files that were previously loaded on the
// current page: https://bugzilla.mozilla.org/show_bug.cgi?id=1037506
//
// I believe this is this issue: https://bugzilla.mozilla.org/show_bug.cgi?id=1037506
// Safari serves cached CSS when a <link rel=preload> exists for the
// same URL: https://bugs.webkit.org/show_bug.cgi?id=187726
//
// Safari has a similar issue, but only if you have a `<link rel=preload ... />` tag
// pointing to the same URL as the stylesheet: https://bugs.webkit.org/show_bug.cgi?id=187726
link.href = `${chunkUrl}?ts=${Date.now()}`;
// Replace or add a fresh `ts` cache-busting param without
// discarding other query parameters that may already be present.
const url = new URL(chunkUrl, location.origin);
url.searchParams.set('ts', String(Date.now()));
// Reduced timer precision in some browers could lead to an update getting dropped
// in firefox if it happens fast enough (in firefox precision is sometimes 100ms!).
// So trust that the server is only updating us when it is important and use a
// random number to bust the cache.
url.searchParams.set('_next_rand', String(Math.random()));
link.href = url.pathname + url.search;
} else {
link.href = chunkUrl;
}
......
......@@ -1613,10 +1613,13 @@ function _ts_generator(thisArg, body) {
*/ /* eslint-disable @typescript-eslint/no-unused-vars */ /// <reference path="../../../browser/runtime/base/runtime-base.ts" />
/// <reference path="../../../shared/runtime/runtime-types.d.ts" />
function getAssetSuffixFromScriptSrc() {
var _self_TURBOPACK_ASSET_SUFFIX;
var _document_currentScript_getAttribute, _document_currentScript_getAttribute1, _document_currentScript, _document;
var _ref;
var _document_currentScript_getAttribute, _document_currentScript, _document;
// TURBOPACK_ASSET_SUFFIX is set in web workers
return ((_self_TURBOPACK_ASSET_SUFFIX = self.TURBOPACK_ASSET_SUFFIX) !== null && _self_TURBOPACK_ASSET_SUFFIX !== void 0 ? _self_TURBOPACK_ASSET_SUFFIX : (_document = document) === null || _document === void 0 ? void 0 : (_document_currentScript = _document.currentScript) === null || _document_currentScript === void 0 ? void 0 : (_document_currentScript_getAttribute1 = _document_currentScript.getAttribute) === null || _document_currentScript_getAttribute1 === void 0 ? void 0 : (_document_currentScript_getAttribute = _document_currentScript_getAttribute1.call(_document_currentScript, 'src')) === null || _document_currentScript_getAttribute === void 0 ? void 0 : _document_currentScript_getAttribute.replace(/^(.*(?=\?)|^.*$)/, '')) || '';
if (self.TURBOPACK_ASSET_SUFFIX != null) return self.TURBOPACK_ASSET_SUFFIX;
var src = (_ref = (_document = document) === null || _document === void 0 ? void 0 : (_document_currentScript = _document.currentScript) === null || _document_currentScript === void 0 ? void 0 : (_document_currentScript_getAttribute = _document_currentScript.getAttribute) === null || _document_currentScript_getAttribute === void 0 ? void 0 : _document_currentScript_getAttribute.call(_document_currentScript, 'src')) !== null && _ref !== void 0 ? _ref : '';
var qi = src.indexOf('?');
return qi >= 0 ? src.slice(qi) : '';
}
var BACKEND;
/**
......
......@@ -1956,7 +1956,10 @@ globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS ??= [];
/// <reference path="../../../shared/runtime/runtime-types.d.ts" />
function getAssetSuffixFromScriptSrc() {
// TURBOPACK_ASSET_SUFFIX is set in web workers
return (self.TURBOPACK_ASSET_SUFFIX ?? document?.currentScript?.getAttribute?.('src')?.replace(/^(.*(?=\?)|^.*$)/, '')) || '';
if (self.TURBOPACK_ASSET_SUFFIX != null) return self.TURBOPACK_ASSET_SUFFIX;
const src = document?.currentScript?.getAttribute?.('src') ?? '';
const qi = src.indexOf('?');
return qi >= 0 ? src.slice(qi) : '';
}
let BACKEND;
/**
......@@ -2128,10 +2131,13 @@ let DEV_BACKEND;
DEV_BACKEND = {
unloadChunk (chunkUrl) {
deleteResolver(chunkUrl);
// Strip query string so we match links regardless of cache-busting
// params (e.g. ?ts=) that may differ between HMR updates.
const baseChunkUrl = chunkUrl.split('?')[0];
// TODO(PACK-2140): remove this once all filenames are guaranteed to be escaped.
const decodedChunkUrl = decodeURI(chunkUrl);
const decodedBaseChunkUrl = decodeURI(baseChunkUrl);
if (isCss(chunkUrl)) {
const links = document.querySelectorAll(`link[href="${chunkUrl}"],link[href^="${chunkUrl}?"],link[href="${decodedChunkUrl}"],link[href^="${decodedChunkUrl}?"]`);
const links = document.querySelectorAll(`link[href="${baseChunkUrl}"],link[href^="${baseChunkUrl}?"],link[href="${decodedBaseChunkUrl}"],link[href^="${decodedBaseChunkUrl}?"]`);
for (const link of Array.from(links)){
link.remove();
}
......@@ -2140,7 +2146,7 @@ let DEV_BACKEND;
// runtime once evaluated.
// However, we still want to remove the script tag from the DOM to keep
// the HTML somewhat consistent from the user's perspective.
const scripts = document.querySelectorAll(`script[src="${chunkUrl}"],script[src^="${chunkUrl}?"],script[src="${decodedChunkUrl}"],script[src^="${decodedChunkUrl}?"]`);
const scripts = document.querySelectorAll(`script[src="${baseChunkUrl}"],script[src^="${baseChunkUrl}?"],script[src="${decodedBaseChunkUrl}"],script[src^="${decodedBaseChunkUrl}?"]`);
for (const script of Array.from(scripts)){
script.remove();
}
......@@ -2154,23 +2160,34 @@ let DEV_BACKEND;
reject(new Error('The DOM backend can only reload CSS chunks'));
return;
}
const decodedChunkUrl = decodeURI(chunkUrl);
const previousLinks = document.querySelectorAll(`link[rel=stylesheet][href="${chunkUrl}"],link[rel=stylesheet][href^="${chunkUrl}?"],link[rel=stylesheet][href="${decodedChunkUrl}"],link[rel=stylesheet][href^="${decodedChunkUrl}?"]`);
// Strip query string so we match links regardless of cache-busting
// params (e.g. ?ts=) that may differ between HMR updates.
const baseChunkUrl = chunkUrl.split('?')[0];
const decodedBaseChunkUrl = decodeURI(baseChunkUrl);
const previousLinks = document.querySelectorAll(`link[rel=stylesheet][href="${baseChunkUrl}"],link[rel=stylesheet][href^="${baseChunkUrl}?"],link[rel=stylesheet][href="${decodedBaseChunkUrl}"],link[rel=stylesheet][href^="${decodedBaseChunkUrl}?"]`);
if (previousLinks.length === 0) {
reject(new Error(`No link element found for chunk ${chunkUrl}`));
return;
}
const link = document.createElement('link');
link.rel = 'stylesheet';
if (navigator.userAgent.includes('Firefox')) {
// Firefox won't reload CSS files that were previously loaded on the current page,
// we need to add a query param to make sure CSS is actually reloaded from the server.
if (navigator.userAgent.includes('Firefox') || navigator.userAgent.includes('Safari') && !navigator.userAgent.includes('Chrome') && !navigator.userAgent.includes('Chromium')) {
// Firefox won't reload CSS files that were previously loaded on the
// current page: https://bugzilla.mozilla.org/show_bug.cgi?id=1037506
//
// I believe this is this issue: https://bugzilla.mozilla.org/show_bug.cgi?id=1037506
// Safari serves cached CSS when a <link rel=preload> exists for the
// same URL: https://bugs.webkit.org/show_bug.cgi?id=187726
//
// Safari has a similar issue, but only if you have a `<link rel=preload ... />` tag
// pointing to the same URL as the stylesheet: https://bugs.webkit.org/show_bug.cgi?id=187726
link.href = `${chunkUrl}?ts=${Date.now()}`;
// Replace or add a fresh `ts` cache-busting param without
// discarding other query parameters that may already be present.
const url = new URL(chunkUrl, location.origin);
url.searchParams.set('ts', String(Date.now()));
// Reduced timer precision in some browers could lead to an update getting dropped
// in firefox if it happens fast enough (in firefox precision is sometimes 100ms!).
// So trust that the server is only updating us when it is important and use a
// random number to bust the cache.
url.searchParams.set('_next_rand', String(Math.random()));
link.href = url.pathname + url.search;
} else {
link.href = chunkUrl;
}
......
......@@ -1956,7 +1956,10 @@ globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS ??= [];
/// <reference path="../../../shared/runtime/runtime-types.d.ts" />
function getAssetSuffixFromScriptSrc() {
// TURBOPACK_ASSET_SUFFIX is set in web workers
return (self.TURBOPACK_ASSET_SUFFIX ?? document?.currentScript?.getAttribute?.('src')?.replace(/^(.*(?=\?)|^.*$)/, '')) || '';
if (self.TURBOPACK_ASSET_SUFFIX != null) return self.TURBOPACK_ASSET_SUFFIX;
const src = document?.currentScript?.getAttribute?.('src') ?? '';
const qi = src.indexOf('?');
return qi >= 0 ? src.slice(qi) : '';
}
let BACKEND;
/**
......@@ -2128,10 +2131,13 @@ let DEV_BACKEND;
DEV_BACKEND = {
unloadChunk (chunkUrl) {
deleteResolver(chunkUrl);
// Strip query string so we match links regardless of cache-busting
// params (e.g. ?ts=) that may differ between HMR updates.
const baseChunkUrl = chunkUrl.split('?')[0];
// TODO(PACK-2140): remove this once all filenames are guaranteed to be escaped.
const decodedChunkUrl = decodeURI(chunkUrl);
const decodedBaseChunkUrl = decodeURI(baseChunkUrl);
if (isCss(chunkUrl)) {
const links = document.querySelectorAll(`link[href="${chunkUrl}"],link[href^="${chunkUrl}?"],link[href="${decodedChunkUrl}"],link[href^="${decodedChunkUrl}?"]`);
const links = document.querySelectorAll(`link[href="${baseChunkUrl}"],link[href^="${baseChunkUrl}?"],link[href="${decodedBaseChunkUrl}"],link[href^="${decodedBaseChunkUrl}?"]`);
for (const link of Array.from(links)){
link.remove();
}
......@@ -2140,7 +2146,7 @@ let DEV_BACKEND;
// runtime once evaluated.
// However, we still want to remove the script tag from the DOM to keep
// the HTML somewhat consistent from the user's perspective.
const scripts = document.querySelectorAll(`script[src="${chunkUrl}"],script[src^="${chunkUrl}?"],script[src="${decodedChunkUrl}"],script[src^="${decodedChunkUrl}?"]`);
const scripts = document.querySelectorAll(`script[src="${baseChunkUrl}"],script[src^="${baseChunkUrl}?"],script[src="${decodedBaseChunkUrl}"],script[src^="${decodedBaseChunkUrl}?"]`);
for (const script of Array.from(scripts)){
script.remove();
}
......@@ -2154,23 +2160,34 @@ let DEV_BACKEND;
reject(new Error('The DOM backend can only reload CSS chunks'));
return;
}
const decodedChunkUrl = decodeURI(chunkUrl);
const previousLinks = document.querySelectorAll(`link[rel=stylesheet][href="${chunkUrl}"],link[rel=stylesheet][href^="${chunkUrl}?"],link[rel=stylesheet][href="${decodedChunkUrl}"],link[rel=stylesheet][href^="${decodedChunkUrl}?"]`);
// Strip query string so we match links regardless of cache-busting
// params (e.g. ?ts=) that may differ between HMR updates.
const baseChunkUrl = chunkUrl.split('?')[0];
const decodedBaseChunkUrl = decodeURI(baseChunkUrl);
const previousLinks = document.querySelectorAll(`link[rel=stylesheet][href="${baseChunkUrl}"],link[rel=stylesheet][href^="${baseChunkUrl}?"],link[rel=stylesheet][href="${decodedBaseChunkUrl}"],link[rel=stylesheet][href^="${decodedBaseChunkUrl}?"]`);
if (previousLinks.length === 0) {
reject(new Error(`No link element found for chunk ${chunkUrl}`));
return;
}
const link = document.createElement('link');
link.rel = 'stylesheet';
if (navigator.userAgent.includes('Firefox')) {
// Firefox won't reload CSS files that were previously loaded on the current page,
// we need to add a query param to make sure CSS is actually reloaded from the server.
if (navigator.userAgent.includes('Firefox') || navigator.userAgent.includes('Safari') && !navigator.userAgent.includes('Chrome') && !navigator.userAgent.includes('Chromium')) {
// Firefox won't reload CSS files that were previously loaded on the
// current page: https://bugzilla.mozilla.org/show_bug.cgi?id=1037506
//
// I believe this is this issue: https://bugzilla.mozilla.org/show_bug.cgi?id=1037506
// Safari serves cached CSS when a <link rel=preload> exists for the
// same URL: https://bugs.webkit.org/show_bug.cgi?id=187726
//
// Safari has a similar issue, but only if you have a `<link rel=preload ... />` tag
// pointing to the same URL as the stylesheet: https://bugs.webkit.org/show_bug.cgi?id=187726
link.href = `${chunkUrl}?ts=${Date.now()}`;
// Replace or add a fresh `ts` cache-busting param without
// discarding other query parameters that may already be present.
const url = new URL(chunkUrl, location.origin);
url.searchParams.set('ts', String(Date.now()));
// Reduced timer precision in some browers could lead to an update getting dropped
// in firefox if it happens fast enough (in firefox precision is sometimes 100ms!).
// So trust that the server is only updating us when it is important and use a
// random number to bust the cache.
url.searchParams.set('_next_rand', String(Math.random()));
link.href = url.pathname + url.search;
} else {
link.href = chunkUrl;
}
......
......@@ -1956,7 +1956,10 @@ globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS ??= [];
/// <reference path="../../../shared/runtime/runtime-types.d.ts" />
function getAssetSuffixFromScriptSrc() {
// TURBOPACK_ASSET_SUFFIX is set in web workers
return (self.TURBOPACK_ASSET_SUFFIX ?? document?.currentScript?.getAttribute?.('src')?.replace(/^(.*(?=\?)|^.*$)/, '')) || '';
if (self.TURBOPACK_ASSET_SUFFIX != null) return self.TURBOPACK_ASSET_SUFFIX;
const src = document?.currentScript?.getAttribute?.('src') ?? '';
const qi = src.indexOf('?');
return qi >= 0 ? src.slice(qi) : '';
}
let BACKEND;
/**
......@@ -2128,10 +2131,13 @@ let DEV_BACKEND;
DEV_BACKEND = {
unloadChunk (chunkUrl) {
deleteResolver(chunkUrl);
// Strip query string so we match links regardless of cache-busting
// params (e.g. ?ts=) that may differ between HMR updates.
const baseChunkUrl = chunkUrl.split('?')[0];
// TODO(PACK-2140): remove this once all filenames are guaranteed to be escaped.
const decodedChunkUrl = decodeURI(chunkUrl);
const decodedBaseChunkUrl = decodeURI(baseChunkUrl);
if (isCss(chunkUrl)) {
const links = document.querySelectorAll(`link[href="${chunkUrl}"],link[href^="${chunkUrl}?"],link[href="${decodedChunkUrl}"],link[href^="${decodedChunkUrl}?"]`);
const links = document.querySelectorAll(`link[href="${baseChunkUrl}"],link[href^="${baseChunkUrl}?"],link[href="${decodedBaseChunkUrl}"],link[href^="${decodedBaseChunkUrl}?"]`);
for (const link of Array.from(links)){
link.remove();
}
......@@ -2140,7 +2146,7 @@ let DEV_BACKEND;
// runtime once evaluated.
// However, we still want to remove the script tag from the DOM to keep
// the HTML somewhat consistent from the user's perspective.
const scripts = document.querySelectorAll(`script[src="${chunkUrl}"],script[src^="${chunkUrl}?"],script[src="${decodedChunkUrl}"],script[src^="${decodedChunkUrl}?"]`);
const scripts = document.querySelectorAll(`script[src="${baseChunkUrl}"],script[src^="${baseChunkUrl}?"],script[src="${decodedBaseChunkUrl}"],script[src^="${decodedBaseChunkUrl}?"]`);
for (const script of Array.from(scripts)){
script.remove();
}
......@@ -2154,23 +2160,34 @@ let DEV_BACKEND;
reject(new Error('The DOM backend can only reload CSS chunks'));
return;
}
const decodedChunkUrl = decodeURI(chunkUrl);
const previousLinks = document.querySelectorAll(`link[rel=stylesheet][href="${chunkUrl}"],link[rel=stylesheet][href^="${chunkUrl}?"],link[rel=stylesheet][href="${decodedChunkUrl}"],link[rel=stylesheet][href^="${decodedChunkUrl}?"]`);
// Strip query string so we match links regardless of cache-busting
// params (e.g. ?ts=) that may differ between HMR updates.
const baseChunkUrl = chunkUrl.split('?')[0];
const decodedBaseChunkUrl = decodeURI(baseChunkUrl);
const previousLinks = document.querySelectorAll(`link[rel=stylesheet][href="${baseChunkUrl}"],link[rel=stylesheet][href^="${baseChunkUrl}?"],link[rel=stylesheet][href="${decodedBaseChunkUrl}"],link[rel=stylesheet][href^="${decodedBaseChunkUrl}?"]`);
if (previousLinks.length === 0) {
reject(new Error(`No link element found for chunk ${chunkUrl}`));
return;
}
const link = document.createElement('link');
link.rel = 'stylesheet';
if (navigator.userAgent.includes('Firefox')) {
// Firefox won't reload CSS files that were previously loaded on the current page,
// we need to add a query param to make sure CSS is actually reloaded from the server.
if (navigator.userAgent.includes('Firefox') || navigator.userAgent.includes('Safari') && !navigator.userAgent.includes('Chrome') && !navigator.userAgent.includes('Chromium')) {
// Firefox won't reload CSS files that were previously loaded on the
// current page: https://bugzilla.mozilla.org/show_bug.cgi?id=1037506
//
// I believe this is this issue: https://bugzilla.mozilla.org/show_bug.cgi?id=1037506
// Safari serves cached CSS when a <link rel=preload> exists for the
// same URL: https://bugs.webkit.org/show_bug.cgi?id=187726
//
// Safari has a similar issue, but only if you have a `<link rel=preload ... />` tag
// pointing to the same URL as the stylesheet: https://bugs.webkit.org/show_bug.cgi?id=187726
link.href = `${chunkUrl}?ts=${Date.now()}`;
// Replace or add a fresh `ts` cache-busting param without
// discarding other query parameters that may already be present.
const url = new URL(chunkUrl, location.origin);
url.searchParams.set('ts', String(Date.now()));
// Reduced timer precision in some browers could lead to an update getting dropped
// in firefox if it happens fast enough (in firefox precision is sometimes 100ms!).
// So trust that the server is only updating us when it is important and use a
// random number to bust the cache.
url.searchParams.set('_next_rand', String(Math.random()));
link.href = url.pathname + url.search;
} else {
link.href = chunkUrl;
}
......
......@@ -1956,7 +1956,10 @@ globalThis.TURBOPACK_CHUNK_UPDATE_LISTENERS ??= [];
/// <reference path="../../../shared/runtime/runtime-types.d.ts" />
function getAssetSuffixFromScriptSrc() {
// TURBOPACK_ASSET_SUFFIX is set in web workers
return (self.TURBOPACK_ASSET_SUFFIX ?? document?.currentScript?.getAttribute?.('src')?.replace(/^(.*(?=\?)|^.*$)/, '')) || '';
if (self.TURBOPACK_ASSET_SUFFIX != null) return self.TURBOPACK_ASSET_SUFFIX;
const src = document?.currentScript?.getAttribute?.('src') ?? '';
const qi = src.indexOf('?');
return qi >= 0 ? src.slice(qi) : '';
}
let BACKEND;
/**
......@@ -2128,10 +2131,13 @@ let DEV_BACKEND;
DEV_BACKEND = {
unloadChunk (chunkUrl) {
deleteResolver(chunkUrl);
// Strip query string so we match links regardless of cache-busting
// params (e.g. ?ts=) that may differ between HMR updates.
const baseChunkUrl = chunkUrl.split('?')[0];
// TODO(PACK-2140): remove this once all filenames are guaranteed to be escaped.
const decodedChunkUrl = decodeURI(chunkUrl);
const decodedBaseChunkUrl = decodeURI(baseChunkUrl);
if (isCss(chunkUrl)) {
const links = document.querySelectorAll(`link[href="${chunkUrl}"],link[href^="${chunkUrl}?"],link[href="${decodedChunkUrl}"],link[href^="${decodedChunkUrl}?"]`);
const links = document.querySelectorAll(`link[href="${baseChunkUrl}"],link[href^="${baseChunkUrl}?"],link[href="${decodedBaseChunkUrl}"],link[href^="${decodedBaseChunkUrl}?"]`);
for (const link of Array.from(links)){
link.remove();
}
......@@ -2140,7 +2146,7 @@ let DEV_BACKEND;
// runtime once evaluated.
// However, we still want to remove the script tag from the DOM to keep
// the HTML somewhat consistent from the user's perspective.
const scripts = document.querySelectorAll(`script[src="${chunkUrl}"],script[src^="${chunkUrl}?"],script[src="${decodedChunkUrl}"],script[src^="${decodedChunkUrl}?"]`);
const scripts = document.querySelectorAll(`script[src="${baseChunkUrl}"],script[src^="${baseChunkUrl}?"],script[src="${decodedBaseChunkUrl}"],script[src^="${decodedBaseChunkUrl}?"]`);
for (const script of Array.from(scripts)){
script.remove();
}
......@@ -2154,23 +2160,34 @@ let DEV_BACKEND;
reject(new Error('The DOM backend can only reload CSS chunks'));
return;
}
const decodedChunkUrl = decodeURI(chunkUrl);
const previousLinks = document.querySelectorAll(`link[rel=stylesheet][href="${chunkUrl}"],link[rel=stylesheet][href^="${chunkUrl}?"],link[rel=stylesheet][href="${decodedChunkUrl}"],link[rel=stylesheet][href^="${decodedChunkUrl}?"]`);
// Strip query string so we match links regardless of cache-busting
// params (e.g. ?ts=) that may differ between HMR updates.
const baseChunkUrl = chunkUrl.split('?')[0];
const decodedBaseChunkUrl = decodeURI(baseChunkUrl);
const previousLinks = document.querySelectorAll(`link[rel=stylesheet][href="${baseChunkUrl}"],link[rel=stylesheet][href^="${baseChunkUrl}?"],link[rel=stylesheet][href="${decodedBaseChunkUrl}"],link[rel=stylesheet][href^="${decodedBaseChunkUrl}?"]`);
if (previousLinks.length === 0) {
reject(new Error(`No link element found for chunk ${chunkUrl}`));
return;
}
const link = document.createElement('link');
link.rel = 'stylesheet';
if (navigator.userAgent.includes('Firefox')) {
// Firefox won't reload CSS files that were previously loaded on the current page,
// we need to add a query param to make sure CSS is actually reloaded from the server.
if (navigator.userAgent.includes('Firefox') || navigator.userAgent.includes('Safari') && !navigator.userAgent.includes('Chrome') && !navigator.userAgent.includes('Chromium')) {
// Firefox won't reload CSS files that were previously loaded on the
// current page: https://bugzilla.mozilla.org/show_bug.cgi?id=1037506
//
// I believe this is this issue: https://bugzilla.mozilla.org/show_bug.cgi?id=1037506
// Safari serves cached CSS when a <link rel=preload> exists for the
// same URL: https://bugs.webkit.org/show_bug.cgi?id=187726
//
// Safari has a similar issue, but only if you have a `<link rel=preload ... />` tag
// pointing to the same URL as the stylesheet: https://bugs.webkit.org/show_bug.cgi?id=187726
link.href = `${chunkUrl}?ts=${Date.now()}`;
// Replace or add a fresh `ts` cache-busting param without
// discarding other query parameters that may already be present.
const url = new URL(chunkUrl, location.origin);
url.searchParams.set('ts', String(Date.now()));
// Reduced timer precision in some browers could lead to an update getting dropped
// in firefox if it happens fast enough (in firefox precision is sometimes 100ms!).
// So trust that the server is only updating us when it is important and use a
// random number to bust the cache.
url.searchParams.set('_next_rand', String(Math.random()));
link.href = url.pathname + url.search;
} else {
link.href = chunkUrl;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment