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) <noreply@anthropic.com>
This commit is contained in:
Georges-Antoine Assi
2026-09-17 07:39:31 -04:00
parent 8bcf72df7d
commit 3eadc4c33c
6 changed files with 69 additions and 41 deletions

View File

@@ -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);
},
};
}

View File

@@ -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", () => {

View File

@@ -14,7 +14,11 @@ export function usePlayerExit(
): {
/** True once an exit is replacing the document, so an unload prompt can stand down. */
departing: Ref<boolean>;
/** 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<RouteLocationNormalized, "fullPath">) => 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<RouteLocationNormalized, "fullPath">): boolean {

View File

@@ -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<string, unknown> | 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();
});

View File

@@ -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<string> | 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<string> {
@@ -27,10 +29,13 @@ async function inject(): Promise<string> {
? 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;

View File

@@ -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<string, string> {
function headersFor(
url: string | undefined,
hook?: ServerHook,
): Record<string, string> {
const headers: Record<string, string> = {};
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