Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure RSC paths are normalized in minimal mode #41348

Merged
merged 2 commits into from Oct 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -29,6 +29,7 @@ import cookie from 'next/dist/compiled/cookie'
import { TEMPORARY_REDIRECT_STATUS } from '../../../../shared/lib/constants'
import { addRequestMeta } from '../../../../server/request-meta'
import { removeTrailingSlash } from '../../../../shared/lib/router/utils/remove-trailing-slash'
import { normalizeRscPath } from '../../../../shared/lib/router/utils/app-paths'

export const vercelHeader = 'x-vercel-id'

Expand Down Expand Up @@ -366,6 +367,18 @@ export function getUtils({
params = Object.keys(defaultRouteRegex.groups).reduce((prev, key) => {
let value: string | string[] | undefined = params[key]

if (typeof value === 'string') {
value = normalizeRscPath(value, true)
}
if (Array.isArray(value)) {
value = value.map((val) => {
if (typeof val === 'string') {
val = normalizeRscPath(val, true)
}
return val
})
}

// if the value matches the default value we can't rely
// on the parsed params, this is used to signal if we need
// to parse x-now-route-matches or not
Expand Down
18 changes: 13 additions & 5 deletions packages/next/server/base-server.ts
Expand Up @@ -64,7 +64,10 @@ import { addRequestMeta, getRequestMeta } from './request-meta'

import { ImageConfigComplete } from '../shared/lib/image-config'
import { removePathPrefix } from '../shared/lib/router/utils/remove-path-prefix'
import { normalizeAppPath } from '../shared/lib/router/utils/app-paths'
import {
normalizeAppPath,
normalizeRscPath,
} from '../shared/lib/router/utils/app-paths'
import { getRouteMatcher } from '../shared/lib/router/utils/route-matcher'
import { getRouteRegex } from '../shared/lib/router/utils/route-regex'
import { getHostname } from '../shared/lib/get-hostname'
Expand Down Expand Up @@ -495,6 +498,11 @@ export default abstract class Server<ServerOptions extends Options = Options> {
if (typeof parsedUrl.query === 'string') {
parsedUrl.query = parseQs(parsedUrl.query)
}
req.url = normalizeRscPath(req.url, this.hasAppDir)
parsedUrl.pathname = normalizeRscPath(
parsedUrl.pathname || '',
this.hasAppDir
)

this.attachRequestMeta(req, parsedUrl)

Expand Down Expand Up @@ -525,10 +533,10 @@ export default abstract class Server<ServerOptions extends Options = Options> {
try {
// x-matched-path is the source of truth, it tells what page
// should be rendered because we don't process rewrites in minimalMode
let matchedPath = new URL(
req.headers['x-matched-path'],
'http://localhost'
).pathname
let matchedPath = normalizeRscPath(
new URL(req.headers['x-matched-path'], 'http://localhost').pathname,
this.hasAppDir
)

let urlPathname = new URL(req.url, 'http://localhost').pathname

Expand Down
3 changes: 3 additions & 0 deletions packages/next/server/web/adapter.ts
Expand Up @@ -9,6 +9,7 @@ import { relativizeURL } from '../../shared/lib/router/utils/relativize-url'
import { waitUntilSymbol } from './spec-extension/fetch-event'
import { NextURL } from './next-url'
import { stripInternalSearchParams } from '../internal-utils'
import { normalizeRscPath } from '../../shared/lib/router/utils/app-paths'

class NextRequestHint extends NextRequest {
sourcePage: string
Expand Down Expand Up @@ -49,6 +50,8 @@ export async function adapter(params: {
// TODO-APP: use explicit marker for this
const isEdgeRendering = typeof self.__BUILD_MANIFEST !== 'undefined'

params.request.url = normalizeRscPath(params.request.url, true)

const requestUrl = new NextURL(params.request.url, {
headers: params.request.headers,
nextConfig: params.request.nextConfig,
Expand Down
4 changes: 4 additions & 0 deletions packages/next/shared/lib/router/utils/app-paths.ts
Expand Up @@ -21,3 +21,7 @@ export function normalizeAppPath(pathname: string) {
return acc + `/${segment}`
}, '')
}

export function normalizeRscPath(pathname: string, enabled?: boolean) {
return enabled ? pathname.replace(/\.rsc($|\?)/, '') : pathname
}