Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Make arg required and rename prefix
  • Loading branch information
ijjk committed Apr 5, 2023
commit 5d98a3c39c04587af628c4b39d9bb63c3791ae2a
10 changes: 6 additions & 4 deletions packages/next/src/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2393,7 +2393,7 @@ export default async function build(
// dynamicParams for non-static paths?
finalDynamicRoutes[page] = {
routeRegex: normalizeRouteRegex(
getNamedRouteRegex(page).re.source
getNamedRouteRegex(page, false).re.source
),
dataRoute,
// if dynamicParams are enabled treat as fallback:
Expand All @@ -2405,7 +2405,8 @@ export default async function build(
? null
: normalizeRouteRegex(
getNamedRouteRegex(
dataRoute.replace(/\.rsc$/, '')
dataRoute.replace(/\.rsc$/, ''),
false
).re.source.replace(/\(\?:\\\/\)\?\$$/, '\\.rsc$')
),
}
Expand Down Expand Up @@ -2770,7 +2771,7 @@ export default async function build(

finalDynamicRoutes[tbdRoute] = {
routeRegex: normalizeRouteRegex(
getNamedRouteRegex(tbdRoute).re.source
getNamedRouteRegex(tbdRoute, false).re.source
),
dataRoute,
fallback: ssgBlockingFallbackPages.has(tbdRoute)
Expand All @@ -2780,7 +2781,8 @@ export default async function build(
: false,
dataRouteRegex: normalizeRouteRegex(
getNamedRouteRegex(
dataRoute.replace(/\.json$/, '')
dataRoute.replace(/\.json$/, ''),
false
).re.source.replace(/\(\?:\\\/\)\?\$$/, '\\.json$')
),
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ async function nextMetadataImageLoader(this: any, content: Buffer) {
const exportedImageData = { ...exported }
export default (props) => {
const pathname = ${JSON.stringify(route)}
const routeRegex = getNamedRouteRegex(pathname)
const routeRegex = getNamedRouteRegex(pathname, false)
const route = interpolateDynamicPath(pathname, props.params, routeRegex)

const imageData = {
Expand Down Expand Up @@ -111,7 +111,7 @@ async function nextMetadataImageLoader(this: any, content: Buffer) {

export default (props) => {
const pathname = ${JSON.stringify(route)}
const routeRegex = getNamedRouteRegex(pathname)
const routeRegex = getNamedRouteRegex(pathname, false)
const route = interpolateDynamicPath(pathname, props.params, routeRegex)

const imageData = ${JSON.stringify(imageData)};
Expand Down
2 changes: 1 addition & 1 deletion packages/next/src/lib/constants.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { ServerRuntime } from '../../types'

export const NEXT_QUERY_PARAM_PREFIX = 'nextPriv'
export const NEXT_QUERY_PARAM_PREFIX = 'nextParam'

// in seconds
export const CACHE_ONE_YEAR = 31536000
Expand Down
5 changes: 4 additions & 1 deletion packages/next/src/server/base-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -789,7 +789,10 @@ export default abstract class Server<ServerOptions extends Options = Options> {
for (const key of Object.keys(parsedUrl.query)) {
const value = parsedUrl.query[key]

if (key.startsWith(NEXT_QUERY_PARAM_PREFIX)) {
if (
key !== NEXT_QUERY_PARAM_PREFIX &&
key.startsWith(NEXT_QUERY_PARAM_PREFIX)
) {
const normalizedKey = key.substring(
NEXT_QUERY_PARAM_PREFIX.length
)
Expand Down
5 changes: 3 additions & 2 deletions packages/next/src/server/server-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ export function normalizeVercelUrl(

for (const key of Object.keys(_parsedUrl.query)) {
if (
key.startsWith(NEXT_QUERY_PARAM_PREFIX) ||
(key !== NEXT_QUERY_PARAM_PREFIX &&
key.startsWith(NEXT_QUERY_PARAM_PREFIX)) ||
(paramKeys || Object.keys(defaultRouteRegex.groups)).includes(key)
) {
delete _parsedUrl.query[key]
Expand Down Expand Up @@ -113,7 +114,7 @@ export function getUtils({
let defaultRouteMatches: ParsedUrlQuery | undefined

if (pageIsDynamic) {
defaultRouteRegex = getNamedRouteRegex(page)
defaultRouteRegex = getNamedRouteRegex(page, false)
dynamicRouteMatcher = getRouteMatcher(defaultRouteRegex)
defaultRouteMatches = dynamicRouteMatcher(page) as ParsedUrlQuery
}
Expand Down
2 changes: 1 addition & 1 deletion packages/next/src/server/web-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ export default class NextWebServer extends BaseServer<WebServerOptions> {
pathname = this.serverOptions.webServerConfig.page

if (isDynamicRoute(pathname)) {
const routeRegex = getNamedRouteRegex(pathname)
const routeRegex = getNamedRouteRegex(pathname, false)
pathname = interpolateDynamicPath(pathname, query, routeRegex)
normalizeVercelUrl(
req,
Expand Down
16 changes: 11 additions & 5 deletions packages/next/src/shared/lib/router/utils/route-regex.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { escapeStringRegexp } from '../../escape-regexp'
import { removeTrailingSlash } from './remove-trailing-slash'

const NEXT_QUERY_PARAM_PREFIX = 'nextPriv'
const NEXT_QUERY_PARAM_PREFIX = 'nextParam'

export interface Group {
pos: number
Expand Down Expand Up @@ -90,7 +90,7 @@ function buildGetSafeRouteKey() {
}
}

function getNamedParametrizedRoute(route: string, prefixRouteKeys?: boolean) {
function getNamedParametrizedRoute(route: string, prefixRouteKeys: boolean) {
const segments = removeTrailingSlash(route).slice(1).split('/')
const getSafeRouteKey = buildGetSafeRouteKey()
const routeKeys: { [named: string]: string } = {}
Expand Down Expand Up @@ -141,11 +141,14 @@ function getNamedParametrizedRoute(route: string, prefixRouteKeys?: boolean) {
/**
* This function extends `getRouteRegex` generating also a named regexp where
* each group is named along with a routeKeys object that indexes the assigned
* named group with its corresponding key.
* named group with its corresponding key. When the routeKeys need to be
* prefixed to uniquely identify internally the "prefixRouteKey" arg should
* be "true" currently this is only the case when creating the routes-manifest
* during the build
*/
export function getNamedRouteRegex(
normalizedRoute: string,
prefixRouteKey?: boolean
prefixRouteKey: boolean
) {
const result = getNamedParametrizedRoute(normalizedRoute, prefixRouteKey)
return {
Expand Down Expand Up @@ -174,7 +177,10 @@ export function getNamedMiddlewareRegex(
}
}

const { namedParameterizedRoute } = getNamedParametrizedRoute(normalizedRoute)
const { namedParameterizedRoute } = getNamedParametrizedRoute(
normalizedRoute,
false
)
let catchAllGroupedRegex = catchAll ? '(?:(/.*)?)' : ''
return {
namedRegex: `^${namedParameterizedRoute}${catchAllGroupedRegex}$`,
Expand Down
4 changes: 2 additions & 2 deletions test/e2e/edge-render-getserversideprops/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,10 @@ describe('edge-render-getserversideprops', () => {
),
namedDataRouteRegex: `^/_next/data/${escapeStringRegexp(
next.buildId
)}/(?<nextPrivid>[^/]+?)\\.json$`,
)}/(?<nextParamid>[^/]+?)\\.json$`,
page: '/[id]',
routeKeys: {
nextPrivid: 'nextPrivid',
nextParamid: 'nextParamid',
},
},
])
Expand Down
22 changes: 11 additions & 11 deletions test/e2e/getserversideprops/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,40 +43,40 @@ const expectedManifestRoutes = () => [
{
namedDataRouteRegex: `^/_next/data/${escapeRegex(
buildId
)}/blog/(?<nextPrivpost>[^/]+?)\\.json$`,
)}/blog/(?<nextParampost>[^/]+?)\\.json$`,
dataRouteRegex: normalizeRegEx(
`^\\/_next\\/data\\/${escapeRegex(buildId)}\\/blog\\/([^\\/]+?)\\.json$`
),
page: '/blog/[post]',
routeKeys: {
nextPrivpost: 'nextPrivpost',
nextParampost: 'nextParampost',
},
},
{
namedDataRouteRegex: `^/_next/data/${escapeRegex(
buildId
)}/blog/(?<nextPrivpost>[^/]+?)/(?<nextPrivcomment>[^/]+?)\\.json$`,
)}/blog/(?<nextParampost>[^/]+?)/(?<nextParamcomment>[^/]+?)\\.json$`,
dataRouteRegex: normalizeRegEx(
`^\\/_next\\/data\\/${escapeRegex(
buildId
)}\\/blog\\/([^\\/]+?)\\/([^\\/]+?)\\.json$`
),
page: '/blog/[post]/[comment]',
routeKeys: {
nextPrivpost: 'nextPrivpost',
nextPrivcomment: 'nextPrivcomment',
nextParampost: 'nextParampost',
nextParamcomment: 'nextParamcomment',
},
},
{
namedDataRouteRegex: `^/_next/data/${escapeRegex(
buildId
)}/catchall/(?<nextPrivpath>.+?)\\.json$`,
)}/catchall/(?<nextParampath>.+?)\\.json$`,
dataRouteRegex: normalizeRegEx(
`^\\/_next\\/data\\/${escapeRegex(buildId)}\\/catchall\\/(.+?)\\.json$`
),
page: '/catchall/[...path]',
routeKeys: {
nextPrivpath: 'nextPrivpath',
nextParampath: 'nextParampath',
},
},
{
Expand Down Expand Up @@ -127,10 +127,10 @@ const expectedManifestRoutes = () => [
)}\\/not\\-found\\/([^\\/]+?)\\.json$`,
namedDataRouteRegex: `^/_next/data/${escapeRegex(
buildId
)}/not\\-found/(?<nextPrivslug>[^/]+?)\\.json$`,
)}/not\\-found/(?<nextParamslug>[^/]+?)\\.json$`,
page: '/not-found/[slug]',
routeKeys: {
nextPrivslug: 'nextPrivslug',
nextParamslug: 'nextParamslug',
},
},
{
Expand Down Expand Up @@ -182,15 +182,15 @@ const expectedManifestRoutes = () => [
{
namedDataRouteRegex: `^/_next/data/${escapeRegex(
buildId
)}/user/(?<nextPrivuser>[^/]+?)/profile\\.json$`,
)}/user/(?<nextParamuser>[^/]+?)/profile\\.json$`,
dataRouteRegex: normalizeRegEx(
`^\\/_next\\/data\\/${escapeRegex(
buildId
)}\\/user\\/([^\\/]+?)\\/profile\\.json$`
),
page: '/user/[user]/profile',
routeKeys: {
nextPrivuser: 'nextPrivuser',
nextParamuser: 'nextParamuser',
},
},
]
Expand Down
Loading