Unverified Commit e0bb2580 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Ensure RSC paths are normalized in minimal mode (#41348)

This ensures we probably remove RSC from the path/URL when in minimal mode. 

## Bug

- [x] Related issues linked using `fixes #number`
- [x] Integration tests added
- [ ] Errors have a helpful link attached, see `contributing.md`

Fixes: [slack thread](https://vercel.slack.com/archives/C043ANYDB24/p1665517894397169?thread_ts=1664913868.516049&cid=C043ANYDB24)
parent de8ae731
...@@ -29,6 +29,7 @@ import cookie from 'next/dist/compiled/cookie' ...@@ -29,6 +29,7 @@ import cookie from 'next/dist/compiled/cookie'
import { TEMPORARY_REDIRECT_STATUS } from '../../../../shared/lib/constants' import { TEMPORARY_REDIRECT_STATUS } from '../../../../shared/lib/constants'
import { addRequestMeta } from '../../../../server/request-meta' import { addRequestMeta } from '../../../../server/request-meta'
import { removeTrailingSlash } from '../../../../shared/lib/router/utils/remove-trailing-slash' 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' export const vercelHeader = 'x-vercel-id'
...@@ -366,6 +367,18 @@ export function getUtils({ ...@@ -366,6 +367,18 @@ export function getUtils({
params = Object.keys(defaultRouteRegex.groups).reduce((prev, key) => { params = Object.keys(defaultRouteRegex.groups).reduce((prev, key) => {
let value: string | string[] | undefined = params[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 // if the value matches the default value we can't rely
// on the parsed params, this is used to signal if we need // on the parsed params, this is used to signal if we need
// to parse x-now-route-matches or not // to parse x-now-route-matches or not
......
...@@ -64,7 +64,10 @@ import { addRequestMeta, getRequestMeta } from './request-meta' ...@@ -64,7 +64,10 @@ import { addRequestMeta, getRequestMeta } from './request-meta'
import { ImageConfigComplete } from '../shared/lib/image-config' import { ImageConfigComplete } from '../shared/lib/image-config'
import { removePathPrefix } from '../shared/lib/router/utils/remove-path-prefix' 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 { getRouteMatcher } from '../shared/lib/router/utils/route-matcher'
import { getRouteRegex } from '../shared/lib/router/utils/route-regex' import { getRouteRegex } from '../shared/lib/router/utils/route-regex'
import { getHostname } from '../shared/lib/get-hostname' import { getHostname } from '../shared/lib/get-hostname'
...@@ -495,6 +498,11 @@ export default abstract class Server<ServerOptions extends Options = Options> { ...@@ -495,6 +498,11 @@ export default abstract class Server<ServerOptions extends Options = Options> {
if (typeof parsedUrl.query === 'string') { if (typeof parsedUrl.query === 'string') {
parsedUrl.query = parseQs(parsedUrl.query) parsedUrl.query = parseQs(parsedUrl.query)
} }
req.url = normalizeRscPath(req.url, this.hasAppDir)
parsedUrl.pathname = normalizeRscPath(
parsedUrl.pathname || '',
this.hasAppDir
)
this.attachRequestMeta(req, parsedUrl) this.attachRequestMeta(req, parsedUrl)
...@@ -525,10 +533,10 @@ export default abstract class Server<ServerOptions extends Options = Options> { ...@@ -525,10 +533,10 @@ export default abstract class Server<ServerOptions extends Options = Options> {
try { try {
// x-matched-path is the source of truth, it tells what page // x-matched-path is the source of truth, it tells what page
// should be rendered because we don't process rewrites in minimalMode // should be rendered because we don't process rewrites in minimalMode
let matchedPath = new URL( let matchedPath = normalizeRscPath(
req.headers['x-matched-path'], new URL(req.headers['x-matched-path'], 'http://localhost').pathname,
'http://localhost' this.hasAppDir
).pathname )
let urlPathname = new URL(req.url, 'http://localhost').pathname let urlPathname = new URL(req.url, 'http://localhost').pathname
......
...@@ -9,6 +9,7 @@ import { relativizeURL } from '../../shared/lib/router/utils/relativize-url' ...@@ -9,6 +9,7 @@ import { relativizeURL } from '../../shared/lib/router/utils/relativize-url'
import { waitUntilSymbol } from './spec-extension/fetch-event' import { waitUntilSymbol } from './spec-extension/fetch-event'
import { NextURL } from './next-url' import { NextURL } from './next-url'
import { stripInternalSearchParams } from '../internal-utils' import { stripInternalSearchParams } from '../internal-utils'
import { normalizeRscPath } from '../../shared/lib/router/utils/app-paths'
class NextRequestHint extends NextRequest { class NextRequestHint extends NextRequest {
sourcePage: string sourcePage: string
...@@ -49,6 +50,8 @@ export async function adapter(params: { ...@@ -49,6 +50,8 @@ export async function adapter(params: {
// TODO-APP: use explicit marker for this // TODO-APP: use explicit marker for this
const isEdgeRendering = typeof self.__BUILD_MANIFEST !== 'undefined' const isEdgeRendering = typeof self.__BUILD_MANIFEST !== 'undefined'
params.request.url = normalizeRscPath(params.request.url, true)
const requestUrl = new NextURL(params.request.url, { const requestUrl = new NextURL(params.request.url, {
headers: params.request.headers, headers: params.request.headers,
nextConfig: params.request.nextConfig, nextConfig: params.request.nextConfig,
......
...@@ -21,3 +21,7 @@ export function normalizeAppPath(pathname: string) { ...@@ -21,3 +21,7 @@ export function normalizeAppPath(pathname: string) {
return acc + `/${segment}` return acc + `/${segment}`
}, '') }, '')
} }
export function normalizeRscPath(pathname: string, enabled?: boolean) {
return enabled ? pathname.replace(/\.rsc($|\?)/, '') : pathname
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment