From 3eadc4c33c5d2ca52b2f97756dcd881f46a031a2 Mon Sep 17 00:00:00 2001 From: Georges-Antoine Assi Date: Thu, 17 Sep 2026 07:39:31 -0400 Subject: [PATCH] fix(v2): apply the code review fixes to the launch path Replace the player entry when leaving by SPA navigation, keep a failed js-dos runtime load from stacking a second stylesheet, and answer the player URLs from `vite preview` as well as the dev server. Co-Authored-By: Claude Opus 5 (1M context) --- frontend/scripts/playerIsolationHeaders.ts | 24 +++++++++----- .../composables/usePlayerExit/index.test.ts | 12 +++---- .../src/v2/composables/usePlayerExit/index.ts | 8 +++-- frontend/src/v2/views/Player/JsDos.test.ts | 22 ++++++------- frontend/src/v2/views/Player/jsDosRuntime.ts | 13 +++++--- frontend/test/playerIsolationHeaders.test.ts | 31 +++++++++++++------ 6 files changed, 69 insertions(+), 41 deletions(-) diff --git a/frontend/scripts/playerIsolationHeaders.ts b/frontend/scripts/playerIsolationHeaders.ts index f9a17ebdb..d0573c88b 100644 --- a/frontend/scripts/playerIsolationHeaders.ts +++ b/frontend/scripts/playerIsolationHeaders.ts @@ -5,7 +5,7 @@ * isolated document exposes, and only the player URLs may be isolated, since * the rest of the app embeds third-party images that COEP would block. */ -import type { Plugin } from "vite"; +import type { Connect, Plugin } from "vite"; // One entry per entry of the map in docker/nginx/templates/default.conf.template, // matched against the request URI as it is there, since try_files rewrites the @@ -20,17 +20,25 @@ export function isIsolatedPlayerUrl(url: string): boolean { return ISOLATED_PLAYER_URLS.some((pattern) => pattern.test(url)); } +const isolate: Connect.NextHandleFunction = (req, res, next) => { + if (isIsolatedPlayerUrl(req.url ?? "")) { + res.setHeader("Cross-Origin-Embedder-Policy", "require-corp"); + res.setHeader("Cross-Origin-Opener-Policy", "same-origin"); + } + next(); +}; + +// `vite preview` serves the built app with no nginx in front of it either, so +// it answers the player URLs the same way the dev server does. export function playerIsolationHeaders(): Plugin { return { name: "romm:player-isolation-headers", + apply: "serve", configureServer(server) { - server.middlewares.use((req, res, next) => { - if (isIsolatedPlayerUrl(req.url ?? "")) { - res.setHeader("Cross-Origin-Embedder-Policy", "require-corp"); - res.setHeader("Cross-Origin-Opener-Policy", "same-origin"); - } - next(); - }); + server.middlewares.use(isolate); + }, + configurePreviewServer(server) { + server.middlewares.use(isolate); }, }; } diff --git a/frontend/src/v2/composables/usePlayerExit/index.test.ts b/frontend/src/v2/composables/usePlayerExit/index.test.ts index 3b99c100d..1ade04136 100644 --- a/frontend/src/v2/composables/usePlayerExit/index.test.ts +++ b/frontend/src/v2/composables/usePlayerExit/index.test.ts @@ -9,12 +9,12 @@ import { } from "vitest"; import { usePlayerExit } from "./index"; -const push = vi.fn(); +const replace = vi.fn(); const locationReplace = vi.fn(); let originalLocation: Location; vi.mock("vue-router", () => ({ - useRouter: () => ({ push }), + useRouter: () => ({ replace }), })); function setIsolated(isolated: boolean) { @@ -40,7 +40,7 @@ afterAll(() => { }); afterEach(() => { - push.mockClear(); + replace.mockClear(); locationReplace.mockClear(); setIsolated(false); }); @@ -51,7 +51,7 @@ describe("usePlayerExit", () => { exit.leave("/rom/1"); - expect(push).toHaveBeenCalledWith("/rom/1"); + expect(replace).toHaveBeenCalledWith("/rom/1"); expect(locationReplace).not.toHaveBeenCalled(); expect(exit.departing.value).toBe(false); }); @@ -63,7 +63,7 @@ describe("usePlayerExit", () => { exit.leave("/rom/1"); expect(locationReplace).toHaveBeenCalledWith("/rom/1"); - expect(push).not.toHaveBeenCalled(); + expect(replace).not.toHaveBeenCalled(); }); it("replaces the document once a runtime is bound to it", () => { @@ -72,7 +72,7 @@ describe("usePlayerExit", () => { exit.leave("/rom/1"); expect(locationReplace).toHaveBeenCalledWith("/rom/1"); - expect(push).not.toHaveBeenCalled(); + expect(replace).not.toHaveBeenCalled(); }); it("lets a route departure through when nothing binds the document", () => { diff --git a/frontend/src/v2/composables/usePlayerExit/index.ts b/frontend/src/v2/composables/usePlayerExit/index.ts index a8a78cb83..f26c09ce1 100644 --- a/frontend/src/v2/composables/usePlayerExit/index.ts +++ b/frontend/src/v2/composables/usePlayerExit/index.ts @@ -14,7 +14,11 @@ export function usePlayerExit( ): { /** True once an exit is replacing the document, so an unload prompt can stand down. */ departing: Ref; - /** Go to `path`, by a full navigation when the document is bound. */ + /** + * Go to `path` in place of the player, by a full navigation when the document + * is bound. The player is replaced either way, so Back never re-enters the + * view it just left and relaunches the game. + */ leave: (path: string) => void; /** `onBeforeRouteLeave` guard: lets a departure through unless bound. */ guard: (to: Pick) => boolean; @@ -33,7 +37,7 @@ export function usePlayerExit( function leave(path: string): void { if (documentBound()) replaceDocument(path); - else void router.push(path); + else void router.replace(path); } function guard(to: Pick): boolean { diff --git a/frontend/src/v2/views/Player/JsDos.test.ts b/frontend/src/v2/views/Player/JsDos.test.ts index 1a300b507..6b2f0314b 100644 --- a/frontend/src/v2/views/Player/JsDos.test.ts +++ b/frontend/src/v2/views/Player/JsDos.test.ts @@ -18,7 +18,7 @@ const mocks = vi.hoisted(() => ({ loadRuntime: vi.fn(), locationReplace: vi.fn(), playSessionStart: vi.fn(), - push: vi.fn(() => Promise.resolve()), + routerReplace: vi.fn(() => Promise.resolve()), confirm: vi.fn(), galleryRom: null as Record | null, routeLeaveGuard: null as ((to: { fullPath: string }) => unknown) | null, @@ -37,7 +37,7 @@ vi.mock("vue-router", () => ({ mocks.routeLeaveGuard = guard; }, useRoute: () => ({ params: { rom: "1" } }), - useRouter: () => ({ push: mocks.push }), + useRouter: () => ({ replace: mocks.routerReplace }), })); vi.mock("@/plugins/router", () => ({ @@ -272,7 +272,7 @@ describe("JsDos player exit", () => { expect(handle.save).toHaveBeenCalledOnce(); expect(handle.stop).toHaveBeenCalledOnce(); - expect(mocks.push).toHaveBeenCalledWith("/rom/1"); + expect(mocks.routerReplace).toHaveBeenCalledWith("/rom/1"); expect(mocks.locationReplace).not.toHaveBeenCalled(); expect(mocks.flushPlaySession).toHaveBeenCalledOnce(); expect(mocks.setPlaying).toHaveBeenLastCalledWith(false); @@ -292,7 +292,7 @@ describe("JsDos player exit", () => { expect(handle.save).toHaveBeenCalledOnce(); expect(mocks.locationReplace).toHaveBeenCalledWith("/rom/1"); - expect(mocks.push).not.toHaveBeenCalled(); + expect(mocks.routerReplace).not.toHaveBeenCalled(); wrapper.unmount(); }); @@ -326,7 +326,7 @@ describe("JsDos player exit", () => { "play.stream-save-unconfirmed", ); expect(handle.stop).not.toHaveBeenCalled(); - expect(mocks.push).not.toHaveBeenCalled(); + expect(mocks.routerReplace).not.toHaveBeenCalled(); expect(mocks.locationReplace).not.toHaveBeenCalled(); expect(mocks.flushPlaySession).not.toHaveBeenCalled(); expect(mocks.setPlaying).not.toHaveBeenCalledWith(false); @@ -347,7 +347,7 @@ describe("JsDos player exit", () => { expect(handle.stop).toHaveBeenCalledOnce(); expect(mocks.flushPlaySession).toHaveBeenCalledOnce(); expect(mocks.setPlaying).toHaveBeenLastCalledWith(false); - expect(mocks.push).toHaveBeenCalledWith("/rom/1"); + expect(mocks.routerReplace).toHaveBeenCalledWith("/rom/1"); wrapper.unmount(); }); @@ -363,7 +363,7 @@ describe("JsDos player exit", () => { "play.stream-save-unconfirmed", ); expect(handle.stop).not.toHaveBeenCalled(); - expect(mocks.push).not.toHaveBeenCalled(); + expect(mocks.routerReplace).not.toHaveBeenCalled(); wrapper.unmount(); }); @@ -383,7 +383,7 @@ describe("JsDos player exit", () => { finishSave?.(true); await flushPromises(); - expect(mocks.push).toHaveBeenCalledOnce(); + expect(mocks.routerReplace).toHaveBeenCalledOnce(); wrapper.unmount(); }); @@ -403,8 +403,8 @@ describe("JsDos player exit", () => { finishSave?.(true); await flushPromises(); - expect(mocks.push).toHaveBeenCalledOnce(); - expect(mocks.push).toHaveBeenCalledWith("/rom/1"); + expect(mocks.routerReplace).toHaveBeenCalledOnce(); + expect(mocks.routerReplace).toHaveBeenCalledWith("/rom/1"); wrapper.unmount(); }); @@ -416,7 +416,7 @@ describe("JsDos player exit", () => { await flushPromises(); expect(handle.save).toHaveBeenCalledOnce(); - expect(mocks.push).toHaveBeenCalledWith("/platform/2"); + expect(mocks.routerReplace).toHaveBeenCalledWith("/platform/2"); wrapper.unmount(); }); diff --git a/frontend/src/v2/views/Player/jsDosRuntime.ts b/frontend/src/v2/views/Player/jsDosRuntime.ts index 42d65a046..adb29ae3e 100644 --- a/frontend/src/v2/views/Player/jsDosRuntime.ts +++ b/frontend/src/v2/views/Player/jsDosRuntime.ts @@ -11,6 +11,8 @@ const LOCAL_BASE = "/assets/jsdos"; const CDN_BASE = "https://cdn.jsdelivr.net/npm/js-dos@8.4.1/dist"; let pending: Promise | null = null; +// A retry after a failed script load would otherwise stack a second stylesheet. +let styledBase: string | null = null; /** Load the runtime, resolving with the base the emulator payloads follow. */ export function loadJsDosRuntime(): Promise { @@ -27,10 +29,13 @@ async function inject(): Promise { ? LOCAL_BASE : CDN_BASE; - const css = document.createElement("link"); - css.rel = "stylesheet"; - css.href = `${base}/js-dos.css`; - document.head.appendChild(css); + if (styledBase !== base) { + const css = document.createElement("link"); + css.rel = "stylesheet"; + css.href = `${base}/js-dos.css`; + document.head.appendChild(css); + styledBase = base; + } await loadScript(`${base}/js-dos.js`); return base; diff --git a/frontend/test/playerIsolationHeaders.test.ts b/frontend/test/playerIsolationHeaders.test.ts index 1e3efc5af..aa2801c73 100644 --- a/frontend/test/playerIsolationHeaders.test.ts +++ b/frontend/test/playerIsolationHeaders.test.ts @@ -13,15 +13,16 @@ type Middleware = ( next: () => void, ) => void; -/** Drive the plugin's server hook and hand back the middleware it registered. */ -function middleware(): Middleware { +type ServerHook = "configureServer" | "configurePreviewServer"; + +/** Drive one of the plugin's server hooks and hand back its middleware. */ +function middleware(hook: ServerHook = "configureServer"): Middleware { let registered: Middleware | undefined; - const { configureServer } = playerIsolationHeaders() as unknown as { - configureServer: (server: { - middlewares: { use: (fn: Middleware) => void }; - }) => void; - }; - configureServer({ + const plugin = playerIsolationHeaders() as unknown as Record< + ServerHook, + (server: { middlewares: { use: (fn: Middleware) => void } }) => void + >; + plugin[hook]({ middlewares: { use: (fn) => { registered = fn; @@ -32,10 +33,13 @@ function middleware(): Middleware { return registered; } -function headersFor(url: string | undefined): Record { +function headersFor( + url: string | undefined, + hook?: ServerHook, +): Record { const headers: Record = {}; const next = vi.fn(); - middleware()( + middleware(hook)( { url }, { setHeader: (name, value) => { @@ -90,6 +94,13 @@ describe("playerIsolationHeaders", () => { it("passes a request with no URL through", () => { expect(headersFor(undefined)).toEqual({}); }); + + // `vite preview` has no nginx in front of it either. + it("isolates a player document on the preview server too", () => { + expect(headersFor("/rom/1/ejs", "configurePreviewServer")).toEqual( + ISOLATED, + ); + }); }); // The dev server and nginx have to isolate the same documents, and nothing