From dd7a44677b68da84764ac19cf10c66f57687605b Mon Sep 17 00:00:00 2001 From: DongHyeonka Date: Sat, 26 Sep 2026 01:06:48 +0900 Subject: [PATCH] fix(web-push): open the registration scope for a refused click (06-09) The fallback opened `${origin}/`, outside the app under a router base path. The scope host now passes registration.scope and the click adapter opens it, refusing any app root outside the origin. Co-Authored-By: Claude Opus 5.5 --- ...ime-events-web-push-and-bounded-polling.md | 5 +- .../inbound/notification-click-adapter.ts | 20 +++++-- .../web-push/service-worker-runtime.ts | 5 ++ .../web-push/service-worker-scope-host.ts | 2 + tests/unit/web-push-worker-runtime.test.ts | 53 +++++++++++++++++++ 5 files changed, 79 insertions(+), 6 deletions(-) diff --git a/docs/architecture/realtime-events-web-push-and-bounded-polling.md b/docs/architecture/realtime-events-web-push-and-bounded-polling.md index dbe2efda..f78e5a3d 100644 --- a/docs/architecture/realtime-events-web-push-and-bounded-polling.md +++ b/docs/architecture/realtime-events-web-push-and-bounded-polling.md @@ -1486,7 +1486,10 @@ worker가 종료·재시작된 뒤에도 click을 처리할 수 있도록 이 bo envelope만 `NotificationOptions.data`에 넣는다. click handler는 codec/size/expiry, current local `ACTIVE` fence와 release epoch를 다시 검증한다. unknown/mismatch (만료, fence·release 불일치, fence 읽기 실패, 모르는 route)는 notification을 닫고 -요청한 route 대신 **앱 루트(`/`)를 handoff 데이터 없이 연다**(D-14). +요청한 route 대신 **앱 루트를 handoff 데이터 없이 연다**(D-14). 앱 루트는 worker의 +`registration.scope`, 즉 router base path다(예: `/app/`). scope를 모르면 +`/`을 쓴다(`src/adapters/web-push/service-worker-scope-host.ts`, +`inbound/notification-click-adapter.ts`의 `appRootUrl`, 06-09). 사용자의 탭이 아무 반응 없이 사라지지 않게 하되, 거절된 click의 route와 notification 정보는 페이지로 넘기지 않는다. 결과는 여전히 실패로 보고하고 (`web_push_click_dispatched` `FAILED` + reason, `nativeEffect`는 루트 창의 diff --git a/src/adapters/web-push/inbound/notification-click-adapter.ts b/src/adapters/web-push/inbound/notification-click-adapter.ts index b49fe4d1..dba1d551 100644 --- a/src/adapters/web-push/inbound/notification-click-adapter.ts +++ b/src/adapters/web-push/inbound/notification-click-adapter.ts @@ -84,6 +84,11 @@ export function createNotificationClickAdapter(dependencies: Readonly<{ fenceStore: PushAssociationFenceStore; clients: WorkerClientsFacade; origin: string; + /** + * 06-09. Where a refused click opens: the registration scope, which is the + * router base path. Defaults to the origin root; must be same-origin. + */ + appRootUrl?: string; now?: () => number; handlerDeadlineMs?: number; scheduler?: TimeoutScheduler; @@ -104,6 +109,11 @@ export function createNotificationClickAdapter(dependencies: Readonly<{ throw new TypeError("Web Push click adapter configuration is invalid."); } const origin: string = parsedOrigin; + const parsedAppRootUrl = safeTarget(origin, dependencies.appRootUrl ?? "/"); + if (!parsedAppRootUrl) { + throw new TypeError("Web Push click adapter configuration is invalid."); + } + const appRootUrl: string = parsedAppRootUrl; return Object.freeze({ handle(event) { @@ -246,9 +256,10 @@ export function createNotificationClickAdapter(dependencies: Readonly<{ /** * Q-6.6-11 / D-14. A click whose data or authority was refused (expired, * fence or release mismatch, unreadable fence, unknown route) still opens - * the app, at the same-origin root and with no handoff data, so the user's - * tap is never silently dropped. The refusal stays the result. Native - * failures (the browser refused a window) and aborts are not retried. + * the app at its root (06-09: the router base path) with no handoff data, + * so the user's tap is never silently dropped. The refusal stays the + * result. Native failures (the browser refused a window) and aborts are not + * retried. */ async function openAppRootIfRejected( result: WebPushResult, @@ -265,8 +276,7 @@ export function createNotificationClickAdapter(dependencies: Readonly<{ ) { return result; } - // `origin` is already a parsed origin, so this is its root URL. - await openWindowBounded(`${origin}/`, signal, effect).catch(() => { + await openWindowBounded(appRootUrl, signal, effect).catch(() => { // The native call threw: whether a window opened is unknown. }); return result; diff --git a/src/adapters/web-push/service-worker-runtime.ts b/src/adapters/web-push/service-worker-runtime.ts index a339a0ac..ffafb6e1 100644 --- a/src/adapters/web-push/service-worker-runtime.ts +++ b/src/adapters/web-push/service-worker-runtime.ts @@ -60,6 +60,8 @@ type ServiceWorkerClientsFacade = Readonly<{ */ export type ServiceWorkerEventHost = Readonly<{ origin: string; + /** 06-09. `registration.scope`, the app root a refused click opens. */ + scope?: string; registration: ServiceWorkerRegistrationFacade; clients: ServiceWorkerClientsFacade; addEventListener(type: string, listener: (event: unknown) => void): void; @@ -118,6 +120,9 @@ export function createWebPushServiceWorkerRuntime( }, }, origin: dependencies.host.origin, + ...(dependencies.host.scope === undefined + ? {} + : { appRootUrl: dependencies.host.scope }), now: dependencies.now, scheduler: dependencies.scheduler, observer: dependencies.observer, diff --git a/src/adapters/web-push/service-worker-scope-host.ts b/src/adapters/web-push/service-worker-scope-host.ts index dda28aac..4b12343c 100644 --- a/src/adapters/web-push/service-worker-scope-host.ts +++ b/src/adapters/web-push/service-worker-scope-host.ts @@ -19,6 +19,7 @@ type MatchAllOptions = Parameters< export type NativeWorkerScope = Readonly<{ location: Readonly<{ origin: string }>; registration: Readonly<{ + scope: string; showNotification(title: string, options: unknown): Promise; }>; clients: Readonly<{ @@ -36,6 +37,7 @@ export function createServiceWorkerScopeHost( ): ServiceWorkerEventHost { return Object.freeze({ origin: scope.location.origin, + scope: scope.registration.scope, registration: Object.freeze({ showNotification: (title: string, options: NotificationOptions) => scope.registration.showNotification(title, { diff --git a/tests/unit/web-push-worker-runtime.test.ts b/tests/unit/web-push-worker-runtime.test.ts index 44672418..0e3bc6d2 100644 --- a/tests/unit/web-push-worker-runtime.test.ts +++ b/tests/unit/web-push-worker-runtime.test.ts @@ -445,6 +445,59 @@ describe("Web Push worker runtime", () => { expect(click.postMessage).not.toHaveBeenCalled(); }); + // 06-09. Under a router base path the app root is the registration + // scope, not the origin root. + it("opens the registration scope under a base path", async () => { + const store = await activeFence(); + const listeners = new Map void>(); + const openWindow = vi.fn(async () => null); + const runtime = createWebPushServiceWorkerRuntime({ + fenceStore: store, + registry, + now: () => Date.parse("2026-07-28T02:00:00.000Z"), + host: { + origin: "https://app.example.test", + scope: "https://app.example.test/app/", + registration: { async showNotification() {} }, + clients: { matchAll: async () => [], openWindow }, + addEventListener(type, listener) { + listeners.set(type, listener); + }, + removeEventListener() {}, + }, + }); + let waited: Promise | null = null; + listeners.get("notificationclick")?.({ + notification: new WebIdlNotification(clickData()), + waitUntil(task: Promise) { + waited = task; + }, + }); + await waited; + + expect(openWindow.mock.calls).toEqual([ + ["https://app.example.test/app/"], + ]); + runtime.dispose(); + }); + + it("refuses an app root outside the origin", async () => { + expect(() => + createNotificationClickAdapter({ + fenceStore: createPushAssociationFenceStore( + createFakePushControlStoreDependencies(), + ), + registry, + origin: "https://app.example.test", + appRootUrl: "https://other.example.test/app/", + clients: { + matchControlledWindowClients: async () => [], + openWindow: async () => null, + }, + }), + ).toThrow(TypeError); + }); + it("does not retry the root after the browser refused the target window", async () => { const store = await activeFence(); const openWindow = vi.fn(async () => null);