From d86bfbecfde567fbbf1b8586db7f832fe4cc3719 Mon Sep 17 00:00:00 2001 From: devfive Date: Tue, 1 Sep 2026 18:43:02 +0900 Subject: [PATCH] fix: route Pages navigation to static RSC files --- packages/app/src/lib/mock-api.ts | 42 +++++++++++++++++++++++++++++--- 1 file changed, 39 insertions(+), 3 deletions(-) diff --git a/packages/app/src/lib/mock-api.ts b/packages/app/src/lib/mock-api.ts index 6e2b085..8add8a4 100644 --- a/packages/app/src/lib/mock-api.ts +++ b/packages/app/src/lib/mock-api.ts @@ -658,10 +658,41 @@ function mockResponse(url: URL, method: string, init?: RequestInit): Response { ) } -function normalizeStaticRscResponse(url: URL, response: Response): Response { +function staticRscRequestUrl( + input: RequestInfo | URL, + init: RequestInit | undefined, + url: URL, +): URL | null { + if ( + url.origin !== window.location.origin || + (DEMO_BASE_PATH !== '' && + url.pathname !== DEMO_BASE_PATH && + !url.pathname.startsWith(`${DEMO_BASE_PATH}/`)) + ) { + return null + } + + const headers = new Headers(input instanceof Request ? input.headers : {}) + new Headers(init?.headers).forEach((value, name) => headers.set(name, value)) + if ( + headers.get('RSC') !== '1' && + !headers.get('Accept')?.includes('text/x-component') + ) { + return null + } + + const staticUrl = new URL(url) + if (!staticUrl.pathname.endsWith('.rsc')) { + staticUrl.pathname = staticUrl.pathname.endsWith('/') + ? `${staticUrl.pathname}index.rsc` + : `${staticUrl.pathname}.rsc` + } + return staticUrl +} + +function normalizeStaticRscResponse(response: Response): Response { if ( !response.ok || - !url.pathname.endsWith('.rsc') || response.headers.get('Content-Type')?.startsWith('text/x-component') ) { return response @@ -698,7 +729,12 @@ export function installMockApi(): void { ) { return mockResponse(url, method, init) } - return normalizeStaticRscResponse(url, await nativeFetch(input, init)) + const staticRscUrl = staticRscRequestUrl(input, init, url) + if (staticRscUrl === null) return nativeFetch(input, init) + + const staticInput = + input instanceof Request ? new Request(staticRscUrl, input) : staticRscUrl + return normalizeStaticRscResponse(await nativeFetch(staticInput, init)) } }