Skip to content

Commit 5f67232

Browse files
meleuclaude
andcommitted
Serve the offline suite from a subdirectory, as Pages does
The first deploy shipped a broken service worker: `import.meta.env.BASE_URL` is handed to the app exactly as written, so a base without a trailing slash — which is the form the Pages API reports — appended to `/drumnotessw.js`. Vite adds the slash when it rewrites the URLs it controls, which is what made the build look right. The slash is now forced where the base is read, and the offline suite previews the build under a subdirectory rather than at a root, deliberately named after nothing and deliberately without a trailing slash. Reverting the one-line fix fails all three of its tests, which is the point: a root-only suite stayed green through a deploy that did not work. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
1 parent 4228c77 commit 5f67232

3 files changed

Lines changed: 42 additions & 11 deletions

File tree

‎playwright.config.ts‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,21 @@ import { defineConfig, devices } from '@playwright/test';
33
const DEV_PORT = 5173;
44
const BUILD_PORT = 5174;
55

6+
/**
7+
* The build is previewed under a subdirectory rather than at a root, because
8+
* that is the shape GitHub Pages serves and the one worth guarding: a root-only
9+
* suite stays green while a subdirectory deploy is broken. The name is
10+
* deliberately not the repository's — nothing here may depend on which
11+
* subdirectory it is.
12+
*
13+
* It has no trailing slash, which is not an oversight: that is the form the
14+
* Pages API reports and hands to the deploy, and the form that has to survive
15+
* being appended to.
16+
*/
17+
const BASE = '/served-from-here';
18+
619
const dev = `http://localhost:${DEV_PORT}`;
7-
const build = `http://localhost:${BUILD_PORT}`;
20+
const build = `http://localhost:${BUILD_PORT}${BASE}/`;
821

922
// Most of the browser suite runs against the dev server rather than a production
1023
// build, so a failure points at source rather than at bundled output.
@@ -42,7 +55,9 @@ export default defineConfig({
4255
reuseExistingServer: !process.env.CI,
4356
},
4457
{
45-
command: `pnpm run build && pnpm exec vite preview --port ${BUILD_PORT} --strictPort`,
58+
// The base is given to the preview as well as to the build: the server
59+
// serves from it, so it has to be told the same thing the bundle was.
60+
command: `BASE_PATH=${BASE} pnpm run build && BASE_PATH=${BASE} pnpm exec vite preview --port ${BUILD_PORT} --strictPort`,
4661
url: build,
4762
reuseExistingServer: !process.env.CI,
4863
},

‎tests/e2e/offline.spec.ts‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ async function ready(page: Page): Promise<void> {
3737

3838
/** One online visit, waited out until the worker has everything it needs. */
3939
async function warm(page: Page): Promise<void> {
40-
await page.goto('/');
40+
// Relative, so it lands on whatever directory the build is served from
41+
// rather than on the origin's root.
42+
await page.goto('./');
4143
await ready(page);
4244
await page.waitForFunction(() => navigator.serviceWorker.controller !== null);
4345

@@ -99,7 +101,12 @@ test('picks up a redeployed entry document rather than pinning the first one', a
99101
const poison = async () =>
100102
await page.evaluate(async (html) => {
101103
const cache = await caches.open((await caches.keys())[0]!);
102-
await cache.put('/', new Response(html, { headers: { 'content-type': 'text/html' } }));
104+
// The entry is cached under the directory the app is served from, which
105+
// is the one the page is sitting on.
106+
await cache.put(
107+
location.pathname,
108+
new Response(html, { headers: { 'content-type': 'text/html' } }),
109+
);
103110
}, '<!doctype html><title>stale</title>');
104111

105112
await poison();
@@ -109,7 +116,7 @@ test('picks up a redeployed entry document rather than pinning the first one', a
109116
// And the fresh copy replaced the poisoned one, so the next load offline gets
110117
// the new deploy rather than the one before it.
111118
await page.waitForFunction(async () => {
112-
const cached = await caches.match('/', { ignoreVary: true });
119+
const cached = await caches.match(location.pathname, { ignoreVary: true });
113120
return (await cached?.text())?.includes('<title>drumnotes</title>') === true;
114121
});
115122
});

‎vite.config.ts‎

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,22 @@
22
import { svelte } from '@sveltejs/vite-plugin-svelte';
33
import { defineConfig } from 'vite';
44

5+
/**
6+
* Where the app will be served from. Root by default, which is what the dev
7+
* server, `vite preview` and the browser suite all assume; CI overrides it for
8+
* the GitHub Pages build, where the site lives under the repository name.
9+
* Everything that names a URL derives it from here, so neither case is a
10+
* special case at runtime.
11+
*
12+
* The trailing slash is forced rather than assumed. Vite adds one when it
13+
* rewrites the URLs it controls, but `import.meta.env.BASE_URL` is handed to
14+
* the app exactly as written — and code that appends to it, as `sw-register`
15+
* does, would otherwise silently build `/drumnotessw.js`.
16+
*/
17+
const base = (process.env.BASE_PATH ?? '/').replace(/\/?$/, '/');
18+
519
export default defineConfig({
6-
// Where the app will be served from. Root by default, which is what the dev
7-
// server, `vite preview` and the browser suite all assume; CI overrides it
8-
// for the GitHub Pages build, where the site lives under the repository name.
9-
// Everything that names a URL derives it from here, so neither case is a
10-
// special case at runtime.
11-
base: process.env.BASE_PATH ?? '/',
20+
base,
1221
plugins: [svelte()],
1322
test: {
1423
// Unit tests live beside the pure modules they cover. The Playwright suite

0 commit comments

Comments
 (0)