Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 39 additions & 3 deletions packages/app/src/lib/mock-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
}
}

Expand Down
Loading