fix(service-worker): limit the previous-release fallback to build assets (06-08)

Any same-origin GET with a hashed-looking name, API and download URLs
included, was routed through the worker. The fallback now applies only under
the build asset directory of the registration scope.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
DongHyeonka
2026-09-26 01:01:47 +09:00
co-authored by Claude Opus 5.5
parent f2e14af0a8
commit 8c17eab668
3 changed files with 60 additions and 8 deletions
@@ -265,7 +265,8 @@ export function createServiceWorkerRuntime(
/**
* R-6.6 / Q-6.6-6b. Synchronous, so the fetch listener decides before it
* calls `respondWith`. Only a manifest asset of this release or a
* same-origin content-hashed URL (an old build's chunk) is answered;
* content-hashed URL under the build asset directory (an old build's chunk,
* 06-08) is answered;
* navigations, API calls, cross-origin and unhashed URLs are left alone.
*/
function intercepts(request: FetchRequest): boolean {
@@ -20,9 +20,10 @@ export type FetchClassification =
| "NETWORK_ONLY"
| "VERIFIED_CACHE_FIRST"
/**
* Q-6.6-6b. A same-origin content-hashed URL this release does not list,
* typically a lazy chunk of the build a tab still runs. Only the previous
* release cache may answer it; otherwise it goes to the network.
* Q-6.6-6b. A content-hashed URL under the build asset directory that this
* release does not list, typically a lazy chunk of the build a tab still
* runs. Only the previous release cache may answer it; otherwise it goes to
* the network.
*/
| "PREVIOUS_RELEASE_FALLBACK";
@@ -57,11 +58,18 @@ export function classifyFetch(input: ClassificationInput): FetchClassification {
return "NETWORK_PASSTHROUGH";
}
/**
* 06-08. Vite's default `build.assetsDir`, under the app base that is also the
* registration scope. A hashed-looking API or download name elsewhere is not an
* old build chunk and is left to the browser.
*/
const BUILD_ASSET_DIRECTORY = "assets/";
function isSameOriginHashedAsset(requestUrl: string, scopeUrl: string): boolean {
try {
const request = new URL(requestUrl);
return (
request.origin === new URL(scopeUrl).origin &&
request.href.startsWith(new URL(BUILD_ASSET_DIRECTORY, scopeUrl).href) &&
HASHED_STATIC_ASSET_NAME.test(request.pathname)
);
} catch {
+46 -3
View File
@@ -3,7 +3,10 @@ import { describe, expect, it, vi } from "vitest";
import { createServiceWorkerPageController } from "../../src/adapters/service-worker/service-worker-page-controller.ts";
import { createServiceWorkerRuntime } from "../../src/adapters/service-worker/service-worker-lifecycle.ts";
import { createServiceWorkerMessage } from "../../src/adapters/service-worker/service-worker-protocol.ts";
import { installStaticAssets } from "../../src/adapters/service-worker/service-worker-static-assets.ts";
import {
classifyFetch,
installStaticAssets,
} from "../../src/adapters/service-worker/service-worker-static-assets.ts";
import { createServiceWorkerRuntimeHost } from "../../src/bootstrap/register-service-worker.ts";
import { resolveRuntimeCapabilities } from "../../src/contracts/runtime-capabilities.ts";
import {
@@ -293,8 +296,9 @@ describe("service worker static cache authority", () => {
it("serves an old build's hashed chunk that the current manifest does not list from the previous release cache (Q-6.6-6b)", async () => {
// The page still runs the previous build and lazy-loads a chunk the new
// manifest (and so the current cache) no longer has.
const oldChunk = "/assets/settings-page-Zz9Yy8Xx.js";
// manifest (and so the current cache) no longer has. The fixture scope is
// `/app/`, so the build's asset directory is `/app/assets/` (06-08).
const oldChunk = "/app/assets/settings-page-Zz9Yy8Xx.js";
const fixture = previousReleaseCaches(oldChunk);
const runtime = staticRuntime(fixture.caches, "/assets/app-Dq3x8Yk2.js");
@@ -1954,3 +1958,42 @@ describe("SW-01 the activation marker read is bounded in bytes", () => {
expect(fixture.response.body?.locked).toBe(false);
});
});
/**
* 06-08. The previous-release fallback is for old build chunks only. A hashed
* looking API or download name outside the build asset directory is left to
* the browser.
*/
describe("fetch classification of hashed-looking URLs", () => {
const classify = (path: string, scopeUrl = `${ORIGIN}/`) =>
classifyFetch({
method: "GET",
requestUrl: `${ORIGIN}${path}`,
isNavigation: false,
runtimeConfigUrl: `${ORIGIN}/config.json`,
releaseManifestUrl: `${ORIGIN}/release-manifest.json`,
manifestUrls: new Set(),
scopeUrl,
});
it.each([
"/api/v1/user-profiles.json",
"/api/reports/monthly-summary2026.csv",
"/media/onboarding-video-final.mp4",
"/downloads/release-notes-20260925.pdf",
])("passes %s through", (path) => {
expect(classify(path)).toBe("NETWORK_PASSTHROUGH");
});
it("falls back only for a hashed name under the build asset directory", () => {
expect(classify("/assets/route-chunk-AbCd1234.js")).toBe(
"PREVIOUS_RELEASE_FALLBACK",
);
expect(classify("/app/assets/route-chunk-AbCd1234.js", `${ORIGIN}/app/`)).toBe(
"PREVIOUS_RELEASE_FALLBACK",
);
expect(classify("/assets/route-chunk-AbCd1234.js", `${ORIGIN}/app/`)).toBe(
"NETWORK_PASSTHROUGH",
);
});
});