Skip to content

Commit

Permalink
refactor and add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi committed Jun 9, 2023
1 parent 8ea3055 commit 2baa005
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 9 deletions.
2 changes: 1 addition & 1 deletion packages/next/src/client/components/app-router-headers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ export const FLIGHT_PARAMETERS = [
[NEXT_ROUTER_PREFETCH],
] as const

export const NEXT_RSC_UNION_QUERY = '__nextRSC' as const
export const NEXT_RSC_UNION_QUERY = '_rsc' as const
1 change: 0 additions & 1 deletion packages/next/src/client/components/app-router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ export function getServerActionDispatcher() {
export function urlToUrlWithoutFlightMarker(url: string): URL {
const urlWithoutFlightParameters = new URL(url, location.origin)
urlWithoutFlightParameters.searchParams.delete(NEXT_RSC_UNION_QUERY)
// TODO-APP: handle .rsc for static export case
return urlWithoutFlightParameters
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ export async function fetchServerResponse(
let fetchUrl = new URL(url)
if (process.env.NODE_ENV === 'production') {
if (process.env.__NEXT_CONFIG_OUTPUT === 'export') {
fetchUrl = new URL(url) // clone
if (fetchUrl.pathname.endsWith('/')) {
fetchUrl.pathname += 'index.txt'
} else {
Expand Down
2 changes: 1 addition & 1 deletion packages/next/src/server/base-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1352,7 +1352,7 @@ export default abstract class Server<ServerOptions extends Options = Options> {
return null
}

delete query.__nextRSC
delete query[NEXT_RSC_UNION_QUERY]
delete query.__nextDataReq

// normalize req.url for SSG paths as it is not exposed
Expand Down
3 changes: 2 additions & 1 deletion packages/next/src/server/request-meta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { UrlWithParsedQuery } from 'url'
import type { BaseNextRequest } from './base-http'
import type { CloneableBody } from './body-streams'
import { RouteMatch } from './future/route-matches/route-match'
import { NEXT_RSC_UNION_QUERY } from '../client/components/app-router-headers'

// FIXME: (wyattjoh) this is a temporary solution to allow us to pass data between bundled modules
export const NEXT_REQUEST_META = Symbol.for('NextInternalRequestMeta')
Expand Down Expand Up @@ -97,7 +98,7 @@ type NextQueryMetadata = {
_nextBubbleNoFallback?: '1'
__nextDataReq?: '1'
__nextCustomErrorRender?: '1'
__nextRSC?: string
[NEXT_RSC_UNION_QUERY]?: string
}

export type NextParsedUrlQuery = ParsedUrlQuery &
Expand Down
18 changes: 14 additions & 4 deletions test/e2e/app-dir/app-prefetch/prefetching.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { createNextDescribe } from 'e2e-utils'
import { check, waitFor } from 'next-test-utils'

// @ts-ignore
import { NEXT_RSC_UNION_QUERY } from 'next/dist/src/client/components/app-router-headers'

const browserConfigWithFixedTime = {
beforePageLoad: (page) => {
page.addInitScript(() => {
Expand Down Expand Up @@ -38,6 +41,10 @@ createNextDescribe(
return
}

it('NEXT_RSC_UNION_QUERY query name is _rsc', async () => {
expect(NEXT_RSC_UNION_QUERY).toBe('_rsc')
})

it('should show layout eagerly when prefetched with loading one level down', async () => {
const browser = await next.browser('/', browserConfigWithFixedTime)
// Ensure the page is prefetched
Expand Down Expand Up @@ -88,7 +95,8 @@ createNextDescribe(

await check(() => {
return requests.some(
(req) => req.includes('static-page') && !req.includes('__nextRSC')
(req) =>
req.includes('static-page') && !req.includes(NEXT_RSC_UNION_QUERY)
)
? 'success'
: JSON.stringify(requests)
Expand Down Expand Up @@ -118,7 +126,8 @@ createNextDescribe(
)
await check(() => {
return requests.some(
(req) => req.includes('static-page') && !req.includes('__nextRSC')
(req) =>
req.includes('static-page') && !req.includes(NEXT_RSC_UNION_QUERY)
)
? 'success'
: JSON.stringify(requests)
Expand All @@ -143,7 +152,7 @@ createNextDescribe(
expect(
requests.filter(
(request) =>
request === '/static-page' || request.includes('__nextRSC')
request === '/static-page' || request.includes(NEXT_RSC_UNION_QUERY)
).length
).toBe(1)
})
Expand All @@ -169,7 +178,8 @@ createNextDescribe(
expect(
requests.filter(
(request) =>
request === '/static-page' || request.includes('__nextRSC')
request === '/static-page' ||
request.includes(NEXT_RSC_UNION_QUERY)
).length
).toBe(0)
}
Expand Down
9 changes: 9 additions & 0 deletions test/e2e/app-dir/navigation/app/assertion/page/page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { strict as assert } from 'node:assert'
// @ts-ignore
import { NEXT_RSC_UNION_QUERY } from 'next/dist/client/components/app-router-headers'

export default function Page({ searchParams }) {
assert(searchParams[NEXT_RSC_UNION_QUERY] === undefined)

return <p>no rsc query page</p>
}
8 changes: 8 additions & 0 deletions test/e2e/app-dir/navigation/app/assertion/route/route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { strict as assert } from 'node:assert'
// @ts-ignore
import { NEXT_RSC_UNION_QUERY } from 'next/dist/client/components/app-router-headers'

export function GET(request) {
assert(request.nextUrl.searchParams.get(NEXT_RSC_UNION_QUERY) === null)
return new Response('no rsc query route')
}
13 changes: 13 additions & 0 deletions test/e2e/app-dir/navigation/middleware.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
// @ts-check
import { NextResponse } from 'next/server'
// @ts-ignore
import { NEXT_RSC_UNION_QUERY } from 'next/dist/client/components/app-router-headers'

if (NEXT_RSC_UNION_QUERY !== '_rsc') {
throw new Error(`NEXT_RSC_UNION_QUERY should be _rsc`)
}

/**
* @param {import('next/server').NextRequest} request
* @returns {NextResponse | undefined}
*/
export function middleware(request) {
const rscQuery = request.nextUrl.searchParams.get(NEXT_RSC_UNION_QUERY)

// Test that the RSC query is not present in the middleware
if (rscQuery) {
throw new Error('RSC query should not be present in the middleware')
}

if (request.nextUrl.pathname === '/redirect-middleware-to-dashboard') {
return NextResponse.redirect(new URL('/redirect-dest', request.url))
}
Expand Down

0 comments on commit 2baa005

Please sign in to comment.