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

Update query param prefix to reduce length #48051

Merged
merged 1 commit into from
Apr 7, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 = 'nextParam'
export const NEXT_QUERY_PARAM_PREFIX = 'nxtP'

// in seconds
export const CACHE_ONE_YEAR = 31536000
Expand Down
7 changes: 5 additions & 2 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 = 'nextParam'
const NEXT_QUERY_PARAM_PREFIX = 'nxtP'

export interface Group {
pos: number
Expand Down Expand Up @@ -102,6 +102,10 @@ function getNamedParametrizedRoute(route: string, prefixRouteKeys: boolean) {
// replace any non-word characters since they can break
// the named regex
let cleanedKey = key.replace(/\W/g, '')

if (prefixRouteKeys) {
cleanedKey = `${NEXT_QUERY_PARAM_PREFIX}${cleanedKey}`
}
let invalidKey = false

// check if the key is still invalid and fallback to using a known
Expand All @@ -118,7 +122,6 @@ function getNamedParametrizedRoute(route: string, prefixRouteKeys: boolean) {
}

if (prefixRouteKeys) {
cleanedKey = `${NEXT_QUERY_PARAM_PREFIX}${cleanedKey}`
routeKeys[cleanedKey] = `${NEXT_QUERY_PARAM_PREFIX}${key}`
} else {
routeKeys[cleanedKey] = `${key}`
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
)}/(?<nextParamid>[^/]+?)\\.json$`,
)}/(?<nxtPid>[^/]+?)\\.json$`,
page: '/[id]',
routeKeys: {
nextParamid: 'nextParamid',
nxtPid: 'nxtPid',
},
},
])
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/(?<nextParampost>[^/]+?)\\.json$`,
)}/blog/(?<nxtPpost>[^/]+?)\\.json$`,
dataRouteRegex: normalizeRegEx(
`^\\/_next\\/data\\/${escapeRegex(buildId)}\\/blog\\/([^\\/]+?)\\.json$`
),
page: '/blog/[post]',
routeKeys: {
nextParampost: 'nextParampost',
nxtPpost: 'nxtPpost',
},
},
{
namedDataRouteRegex: `^/_next/data/${escapeRegex(
buildId
)}/blog/(?<nextParampost>[^/]+?)/(?<nextParamcomment>[^/]+?)\\.json$`,
)}/blog/(?<nxtPpost>[^/]+?)/(?<nxtPcomment>[^/]+?)\\.json$`,
dataRouteRegex: normalizeRegEx(
`^\\/_next\\/data\\/${escapeRegex(
buildId
)}\\/blog\\/([^\\/]+?)\\/([^\\/]+?)\\.json$`
),
page: '/blog/[post]/[comment]',
routeKeys: {
nextParampost: 'nextParampost',
nextParamcomment: 'nextParamcomment',
nxtPpost: 'nxtPpost',
nxtPcomment: 'nxtPcomment',
},
},
{
namedDataRouteRegex: `^/_next/data/${escapeRegex(
buildId
)}/catchall/(?<nextParampath>.+?)\\.json$`,
)}/catchall/(?<nxtPpath>.+?)\\.json$`,
dataRouteRegex: normalizeRegEx(
`^\\/_next\\/data\\/${escapeRegex(buildId)}\\/catchall\\/(.+?)\\.json$`
),
page: '/catchall/[...path]',
routeKeys: {
nextParampath: 'nextParampath',
nxtPpath: 'nxtPpath',
},
},
{
Expand Down Expand Up @@ -127,10 +127,10 @@ const expectedManifestRoutes = () => [
)}\\/not\\-found\\/([^\\/]+?)\\.json$`,
namedDataRouteRegex: `^/_next/data/${escapeRegex(
buildId
)}/not\\-found/(?<nextParamslug>[^/]+?)\\.json$`,
)}/not\\-found/(?<nxtPslug>[^/]+?)\\.json$`,
page: '/not-found/[slug]',
routeKeys: {
nextParamslug: 'nextParamslug',
nxtPslug: 'nxtPslug',
},
},
{
Expand Down Expand Up @@ -182,15 +182,15 @@ const expectedManifestRoutes = () => [
{
namedDataRouteRegex: `^/_next/data/${escapeRegex(
buildId
)}/user/(?<nextParamuser>[^/]+?)/profile\\.json$`,
)}/user/(?<nxtPuser>[^/]+?)/profile\\.json$`,
dataRouteRegex: normalizeRegEx(
`^\\/_next\\/data\\/${escapeRegex(
buildId
)}\\/user\\/([^\\/]+?)\\/profile\\.json$`
),
page: '/user/[user]/profile',
routeKeys: {
nextParamuser: 'nextParamuser',
nxtPuser: 'nxtPuser',
},
},
]
Expand Down
62 changes: 31 additions & 31 deletions test/e2e/prerender.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1316,10 +1316,10 @@ describe('Prerender', () => {
),
namedDataRouteRegex: `^/_next/data/${escapeRegex(
next.buildId
)}/api\\-docs/(?<nextParamslug>.+?)\\.json$`,
)}/api\\-docs/(?<nxtPslug>.+?)\\.json$`,
page: '/api-docs/[...slug]',
routeKeys: {
nextParamslug: 'nextParamslug',
nxtPslug: 'nxtPslug',
},
},
{
Expand All @@ -1346,9 +1346,9 @@ describe('Prerender', () => {
),
namedDataRouteRegex: `^/_next/data/${escapeRegex(
next.buildId
)}/blocking\\-fallback/(?<nextParamslug>[^/]+?)\\.json$`,
)}/blocking\\-fallback/(?<nxtPslug>[^/]+?)\\.json$`,
page: '/blocking-fallback/[slug]',
routeKeys: { nextParamslug: 'nextParamslug' },
routeKeys: { nxtPslug: 'nxtPslug' },
},
{
dataRouteRegex: normalizeRegEx(
Expand All @@ -1358,9 +1358,9 @@ describe('Prerender', () => {
),
namedDataRouteRegex: `^/_next/data/${escapeRegex(
next.buildId
)}/blocking\\-fallback\\-once/(?<nextParamslug>[^/]+?)\\.json$`,
)}/blocking\\-fallback\\-once/(?<nxtPslug>[^/]+?)\\.json$`,
page: '/blocking-fallback-once/[slug]',
routeKeys: { nextParamslug: 'nextParamslug' },
routeKeys: { nxtPslug: 'nxtPslug' },
},
{
dataRouteRegex: normalizeRegEx(
Expand All @@ -1370,9 +1370,9 @@ describe('Prerender', () => {
),
namedDataRouteRegex: `^/_next/data/${escapeRegex(
next.buildId
)}/blocking\\-fallback\\-some/(?<nextParamslug>[^/]+?)\\.json$`,
)}/blocking\\-fallback\\-some/(?<nxtPslug>[^/]+?)\\.json$`,
page: '/blocking-fallback-some/[slug]',
routeKeys: { nextParamslug: 'nextParamslug' },
routeKeys: { nxtPslug: 'nxtPslug' },
},
{
dataRouteRegex: normalizeRegEx(
Expand All @@ -1383,72 +1383,72 @@ describe('Prerender', () => {
{
namedDataRouteRegex: `^/_next/data/${escapeRegex(
next.buildId
)}/blog/(?<nextParampost>[^/]+?)\\.json$`,
)}/blog/(?<nxtPpost>[^/]+?)\\.json$`,
dataRouteRegex: normalizeRegEx(
`^\\/_next\\/data\\/${escapeRegex(
next.buildId
)}\\/blog\\/([^\\/]+?)\\.json$`
),
page: '/blog/[post]',
routeKeys: {
nextParampost: 'nextParampost',
nxtPpost: 'nxtPpost',
},
},
{
namedDataRouteRegex: `^/_next/data/${escapeRegex(
next.buildId
)}/blog/(?<nextParampost>[^/]+?)/(?<nextParamcomment>[^/]+?)\\.json$`,
)}/blog/(?<nxtPpost>[^/]+?)/(?<nxtPcomment>[^/]+?)\\.json$`,
dataRouteRegex: normalizeRegEx(
`^\\/_next\\/data\\/${escapeRegex(
next.buildId
)}\\/blog\\/([^\\/]+?)\\/([^\\/]+?)\\.json$`
),
page: '/blog/[post]/[comment]',
routeKeys: {
nextParampost: 'nextParampost',
nextParamcomment: 'nextParamcomment',
nxtPpost: 'nxtPpost',
nxtPcomment: 'nxtPcomment',
},
},
{
namedDataRouteRegex: `^/_next/data/${escapeRegex(
next.buildId
)}/catchall/(?<nextParamslug>.+?)\\.json$`,
)}/catchall/(?<nxtPslug>.+?)\\.json$`,
dataRouteRegex: normalizeRegEx(
`^\\/_next\\/data\\/${escapeRegex(
next.buildId
)}\\/catchall\\/(.+?)\\.json$`
),
page: '/catchall/[...slug]',
routeKeys: {
nextParamslug: 'nextParamslug',
nxtPslug: 'nxtPslug',
},
},
{
namedDataRouteRegex: `^/_next/data/${escapeRegex(
next.buildId
)}/catchall\\-explicit/(?<nextParamslug>.+?)\\.json$`,
)}/catchall\\-explicit/(?<nxtPslug>.+?)\\.json$`,
dataRouteRegex: normalizeRegEx(
`^\\/_next\\/data\\/${escapeRegex(
next.buildId
)}\\/catchall\\-explicit\\/(.+?)\\.json$`
),
page: '/catchall-explicit/[...slug]',
routeKeys: {
nextParamslug: 'nextParamslug',
nxtPslug: 'nxtPslug',
},
},
{
namedDataRouteRegex: `^/_next/data/${escapeRegex(
next.buildId
)}/catchall\\-optional(?:/(?<nextParamslug>.+?))?\\.json$`,
)}/catchall\\-optional(?:/(?<nxtPslug>.+?))?\\.json$`,
dataRouteRegex: normalizeRegEx(
`^\\/_next\\/data\\/${escapeRegex(
next.buildId
)}\\/catchall\\-optional(?:\\/(.+?))?\\.json$`
),
page: '/catchall-optional/[[...slug]]',
routeKeys: {
nextParamslug: 'nextParamslug',
nxtPslug: 'nxtPslug',
},
},
{
Expand All @@ -1467,10 +1467,10 @@ describe('Prerender', () => {
),
namedDataRouteRegex: `^/_next/data/${escapeRegex(
next.buildId
)}/dynamic/(?<nextParamslug>[^/]+?)\\.json$`,
)}/dynamic/(?<nxtPslug>[^/]+?)\\.json$`,
page: '/dynamic/[slug]',
routeKeys: {
nextParamslug: 'nextParamslug',
nxtPslug: 'nxtPslug',
},
},
{
Expand All @@ -1481,10 +1481,10 @@ describe('Prerender', () => {
),
namedDataRouteRegex: `^/_next/data/${escapeRegex(
next.buildId
)}/fallback\\-only/(?<nextParamslug>[^/]+?)\\.json$`,
)}/fallback\\-only/(?<nxtPslug>[^/]+?)\\.json$`,
page: '/fallback-only/[slug]',
routeKeys: {
nextParamslug: 'nextParamslug',
nxtPslug: 'nxtPslug',
},
},
// TODO: investigate index/index
Expand All @@ -1499,15 +1499,15 @@ describe('Prerender', () => {
{
namedDataRouteRegex: `^/_next/data/${escapeRegex(
next.buildId
)}/lang/(?<nextParamlang>[^/]+?)/about\\.json$`,
)}/lang/(?<nxtPlang>[^/]+?)/about\\.json$`,
dataRouteRegex: normalizeRegEx(
`^\\/_next\\/data\\/${escapeRegex(
next.buildId
)}\\/lang\\/([^\\/]+?)\\/about\\.json$`
),
page: '/lang/[lang]/about',
routeKeys: {
nextParamlang: 'nextParamlang',
nxtPlang: 'nxtPlang',
},
},
{
Expand All @@ -1525,29 +1525,29 @@ describe('Prerender', () => {
{
namedDataRouteRegex: `^/_next/data/${escapeRegex(
next.buildId
)}/non\\-json/(?<nextParamp>[^/]+?)\\.json$`,
)}/non\\-json/(?<nxtPp>[^/]+?)\\.json$`,
dataRouteRegex: normalizeRegEx(
`^\\/_next\\/data\\/${escapeRegex(
next.buildId
)}\\/non\\-json\\/([^\\/]+?)\\.json$`
),
page: '/non-json/[p]',
routeKeys: {
nextParamp: 'nextParamp',
nxtPp: 'nxtPp',
},
},
{
namedDataRouteRegex: `^/_next/data/${escapeRegex(
next.buildId
)}/non\\-json\\-blocking/(?<nextParamp>[^/]+?)\\.json$`,
)}/non\\-json\\-blocking/(?<nxtPp>[^/]+?)\\.json$`,
dataRouteRegex: normalizeRegEx(
`^\\/_next\\/data\\/${escapeRegex(
next.buildId
)}\\/non\\-json\\-blocking\\/([^\\/]+?)\\.json$`
),
page: '/non-json-blocking/[p]',
routeKeys: {
nextParamp: 'nextParamp',
nxtPp: 'nxtPp',
},
},
{
Expand Down Expand Up @@ -1575,15 +1575,15 @@ describe('Prerender', () => {
{
namedDataRouteRegex: `^/_next/data/${escapeRegex(
next.buildId
)}/user/(?<nextParamuser>[^/]+?)/profile\\.json$`,
)}/user/(?<nxtPuser>[^/]+?)/profile\\.json$`,
dataRouteRegex: normalizeRegEx(
`^\\/_next\\/data\\/${escapeRegex(
next.buildId
)}\\/user\\/([^\\/]+?)\\/profile\\.json$`
),
page: '/user/[user]/profile',
routeKeys: {
nextParamuser: 'nextParamuser',
nxtPuser: 'nxtPuser',
},
},
])
Expand Down
Loading