Skip to content

Commit

Permalink
strip query
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi committed Jun 8, 2023
1 parent ea2a20d commit bc239bc
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 18 deletions.
2 changes: 2 additions & 0 deletions packages/next/src/client/components/app-router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import { RedirectBoundary } from './redirect-boundary'
import { NotFoundBoundary } from './not-found-boundary'
import { findHeadInCache } from './router-reducer/reducers/find-head-in-cache'
import { createInfinitePromise } from './infinite-promise'
import { NEXT_RSC_UNION_QUERY } from './app-router-headers'

const isServer = typeof window === 'undefined'

Expand All @@ -65,6 +66,7 @@ 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 @@ -64,7 +64,7 @@ export async function fetchServerResponse(
)

try {
let fetchUrl = url
let fetchUrl = new URL(url)
if (process.env.NODE_ENV === 'production') {
if (process.env.__NEXT_CONFIG_OUTPUT === 'export') {
fetchUrl = new URL(url) // clone
Expand Down
2 changes: 2 additions & 0 deletions packages/next/src/server/base-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ import {
RSC,
RSC_VARY_HEADER,
FLIGHT_PARAMETERS,
NEXT_RSC_UNION_QUERY,
} from '../client/components/app-router-headers'
import {
MatchOptions,
Expand Down Expand Up @@ -2207,6 +2208,7 @@ export default abstract class Server<ServerOptions extends Options = Options> {
const { res, query, pathname } = ctx
let page = pathname
const bubbleNoFallback = !!query._nextBubbleNoFallback
delete query[NEXT_RSC_UNION_QUERY]
delete query._nextBubbleNoFallback

const options: MatchOptions = {
Expand Down
22 changes: 12 additions & 10 deletions packages/next/src/server/internal-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,29 @@ const INTERNAL_QUERY_NAMES = [
NEXT_RSC_UNION_QUERY,
] as const

const EXTENDED_INTERNAL_QUERY_NAMES = ['__nextDataReq'] as const
const EDGE_EXTENDED_INTERNAL_QUERY_NAMES = ['__nextDataReq'] as const

export function stripInternalQueries(query: NextParsedUrlQuery) {
for (const name of INTERNAL_QUERY_NAMES) {
delete query[name]
}
}

export function stripInternalSearchParams(
searchParams: URLSearchParams,
extended?: boolean
) {
export function stripInternalSearchParams<T extends string | URL>(
url: T,
isEdge: boolean
): T {
const isStringUrl = typeof url === 'string'
const instance = isStringUrl ? new URL(url) : (url as URL)
for (const name of INTERNAL_QUERY_NAMES) {
searchParams.delete(name)
instance.searchParams.delete(name)
}

if (extended) {
for (const name of EXTENDED_INTERNAL_QUERY_NAMES) {
searchParams.delete(name)
if (isEdge) {
for (const name of EDGE_EXTENDED_INTERNAL_QUERY_NAMES) {
instance.searchParams.delete(name)
}
}

return searchParams
return (isStringUrl ? instance.toString() : instance) as T
}
2 changes: 2 additions & 0 deletions packages/next/src/server/next-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ import { invokeRequest } from './lib/server-ipc/invoke-request'
import { filterReqHeaders } from './lib/server-ipc/utils'
import { createRequestResponseMocks } from './lib/mock-request'
import chalk from 'next/dist/compiled/chalk'
import { NEXT_RSC_UNION_QUERY } from '../client/components/app-router-headers'

export * from './base-server'

Expand Down Expand Up @@ -1560,6 +1561,7 @@ export default class NextNodeServer extends BaseServer {
return { finished: true }
}
delete query._nextBubbleNoFallback
delete query[NEXT_RSC_UNION_QUERY]

const handledAsEdgeFunction = await this.runEdgeFunction({
req,
Expand Down
10 changes: 5 additions & 5 deletions packages/next/src/server/web/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,14 @@ export async function adapter(
}
}

// Strip internal query parameters off the request.
stripInternalSearchParams(requestUrl.searchParams, true)
const normalizeUrl = process.env.__NEXT_NO_MIDDLEWARE_URL_NORMALIZE
? new URL(params.request.url)
: requestUrl

const request = new NextRequestHint({
page: params.page,
input: process.env.__NEXT_NO_MIDDLEWARE_URL_NORMALIZE
? params.request.url
: String(requestUrl),
// Strip internal query parameters off the request.
input: stripInternalSearchParams(normalizeUrl, true).toString(),
init: {
body: params.request.body,
geo: params.request.geo,
Expand Down
5 changes: 5 additions & 0 deletions packages/next/src/server/web/sandbox/sandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { getModuleContext } from './context'
import { EdgeFunctionDefinition } from '../../../build/webpack/plugins/middleware-plugin'
import { requestToBodyStream } from '../../body-streams'
import type { EdgeRuntime } from 'next/dist/compiled/edge-runtime'
import { NEXT_RSC_UNION_QUERY } from '../../../client/components/app-router-headers'

export const ErrorSource = Symbol('SandboxError')

Expand Down Expand Up @@ -96,6 +97,10 @@ export const run = withTaggedErrors(async function runWithTaggedErrors(params) {
: undefined

const KUint8Array = runtime.evaluate('Uint8Array')
const urlInstance = new URL(params.request.url)
urlInstance.searchParams.delete(NEXT_RSC_UNION_QUERY)

params.request.url = urlInstance.toString()

try {
const result = await edgeFunction({
Expand Down
4 changes: 2 additions & 2 deletions test/e2e/app-dir/app-prefetch/prefetching.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ createNextDescribe(

await check(() => {
return requests.some(
(req) => req.includes('static-page') && req.includes('__nextRSC=')
(req) => req.includes('static-page') && !req.includes('__nextRSC')
)
? 'success'
: JSON.stringify(requests)
Expand Down Expand Up @@ -118,7 +118,7 @@ createNextDescribe(
)
await check(() => {
return requests.some(
(req) => req.includes('static-page') && req.includes('__nextRSC=')
(req) => req.includes('static-page') && !req.includes('__nextRSC')
)
? 'success'
: JSON.stringify(requests)
Expand Down

0 comments on commit bc239bc

Please sign in to comment.