Skip to content

Commit

Permalink
Ensure RSC paths are normalized in minimal mode (vercel#41348)
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
ijjk authored and Kikobeats committed Oct 24, 2022
1 parent 33fbe7e commit 7220690
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 5 deletions.
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
}

0 comments on commit 7220690

Please sign in to comment.