Skip to content

Commit

Permalink
Fix test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
ijjk committed Apr 4, 2023
1 parent 397b02a commit eb3f6dd
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 29 deletions.
28 changes: 13 additions & 15 deletions packages/next/src/server/base-server.ts
Expand Up @@ -783,25 +783,23 @@ export default abstract class Server<ServerOptions extends Options = Options> {
addRequestMeta(req, '_nextDidRewrite', true)
}

for (const key of Object.keys(parsedUrl.query)) {
const value = parsedUrl.query[key]

if (key.startsWith(NEXT_QUERY_PARAM_PREFIX)) {
parsedUrl.query[key.substring(NEXT_QUERY_PARAM_PREFIX.length)] =
value
delete parsedUrl.query[key]
}
}

// interpolate dynamic params and normalize URL if needed
if (pageIsDynamic) {
let params: ParsedUrlQuery | false = {}

const queryRouteParams: Record<string, string | string[]> = {}

for (const key of Object.keys(parsedUrl.query)) {
const value = parsedUrl.query[key]

if (key.startsWith(NEXT_QUERY_PARAM_PREFIX) && value) {
queryRouteParams[
key.substring(NEXT_QUERY_PARAM_PREFIX.length)
] = value
delete parsedUrl.query[key]
}
}

let paramsResult =
utils.normalizeDynamicRouteParams(queryRouteParams)
let paramsResult = utils.normalizeDynamicRouteParams(
parsedUrl.query
)

// for prerendered ISR paths we attempt parsing the route
// params from the URL directly as route-matches may not
Expand Down
10 changes: 8 additions & 2 deletions packages/next/src/server/server-utils.ts
Expand Up @@ -23,6 +23,7 @@ import { TEMPORARY_REDIRECT_STATUS } from '../shared/lib/constants'
import { addRequestMeta } from './request-meta'
import { removeTrailingSlash } from '../shared/lib/router/utils/remove-trailing-slash'
import { normalizeRscPath } from '../shared/lib/router/utils/app-paths'
import { NEXT_QUERY_PARAM_PREFIX } from '../lib/constants'

export function normalizeVercelUrl(
req: BaseNextRequest | IncomingMessage,
Expand All @@ -37,8 +38,13 @@ export function normalizeVercelUrl(
const _parsedUrl = parseUrl(req.url!, true)
delete (_parsedUrl as any).search

for (const param of paramKeys || Object.keys(defaultRouteRegex.groups)) {
delete _parsedUrl.query[param]
for (const key of Object.keys(_parsedUrl.query)) {
if (
key.startsWith(NEXT_QUERY_PARAM_PREFIX) ||
(paramKeys || Object.keys(defaultRouteRegex.groups)).includes(key)
) {
delete _parsedUrl.query[key]
}
}
req.url = formatUrl(_parsedUrl)
}
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/edge-render-getserversideprops/index.test.ts
Expand Up @@ -140,7 +140,7 @@ describe('edge-render-getserversideprops', () => {
),
namedDataRouteRegex: `^/_next/data/${escapeStringRegexp(
next.buildId
)}/(?<id>[^/]+?)\\.json$`,
)}/(?<nextPrivid>[^/]+?)\\.json$`,
page: '/[id]',
routeKeys: {
nextPrivid: 'nextPrivid',
Expand Down
Expand Up @@ -193,7 +193,7 @@ describe('Required Server Files', () => {
it('should render dynamic SSR page correctly with x-matched-path', async () => {
const html = await renderViaHTTP(
appPort,
'/some-other-path?slug=first',
'/some-other-path?nextPrivslug=first',
undefined,
{
headers: {
Expand Down Expand Up @@ -259,7 +259,7 @@ describe('Required Server Files', () => {
it('should return data correctly with x-matched-path', async () => {
const res = await fetchViaHTTP(
appPort,
`/_next/data/${buildId}/dynamic/first.json?slug=first`,
`/_next/data/${buildId}/dynamic/first.json?nextPrivslug=first`,
undefined,
{
headers: {
Expand Down
Expand Up @@ -313,7 +313,7 @@ describe('should set-up next', () => {
it('should render dynamic SSR page correctly with x-matched-path', async () => {
const html = await renderViaHTTP(
appPort,
'/some-other-path?slug=first',
'/some-other-path?nextPrivslug=first',
undefined,
{
headers: {
Expand All @@ -330,7 +330,7 @@ describe('should set-up next', () => {

const html2 = await renderViaHTTP(
appPort,
'/some-other-path?slug=second',
'/some-other-path?nextPrivslug=second',
undefined,
{
headers: {
Expand Down Expand Up @@ -393,7 +393,7 @@ describe('should set-up next', () => {
it('should return data correctly with x-matched-path', async () => {
const res = await fetchViaHTTP(
appPort,
`/_next/data/${next.buildId}/en/dynamic/first.json?slug=first`,
`/_next/data/${next.buildId}/en/dynamic/first.json?nextPrivslug=first`,
undefined,
{
headers: {
Expand Down Expand Up @@ -640,7 +640,7 @@ describe('should set-up next', () => {
const res = await fetchViaHTTP(
appPort,
'/optional-ssp',
{ rest: '', another: 'value' },
{ nextPrivrest: '', another: 'value' },
{
headers: {
'x-matched-path': '/optional-ssp/[[...rest]]',
Expand Down Expand Up @@ -722,7 +722,7 @@ describe('should set-up next', () => {
const res = await fetchViaHTTP(
appPort,
'/api/optional',
{ rest: '', another: 'value' },
{ nextPrivrest: '', another: 'value' },
{
headers: {
'x-matched-path': '/api/optional/[[...rest]]',
Expand Down
Expand Up @@ -570,7 +570,7 @@ describe('should set-up next', () => {
it('should render dynamic SSR page correctly with x-matched-path', async () => {
const html = await renderViaHTTP(
appPort,
'/some-other-path?slug=first',
'/some-other-path?nextPrivslug=first',
undefined,
{
headers: {
Expand All @@ -587,7 +587,7 @@ describe('should set-up next', () => {

const html2 = await renderViaHTTP(
appPort,
'/some-other-path?slug=second',
'/some-other-path?nextPrivslug=second',
undefined,
{
headers: {
Expand Down Expand Up @@ -703,7 +703,7 @@ describe('should set-up next', () => {
it('should return data correctly with x-matched-path', async () => {
const res = await fetchViaHTTP(
appPort,
`/_next/data/${next.buildId}/dynamic/first.json?slug=first`,
`/_next/data/${next.buildId}/dynamic/first.json?nextPrivslug=first`,
undefined,
{
headers: {
Expand Down Expand Up @@ -1145,7 +1145,7 @@ describe('should set-up next', () => {
const res = await fetchViaHTTP(
appPort,
'/api/optional/index',
{ rest: 'index', another: 'value' },
{ nextPrivrest: 'index', another: 'value' },
{
headers: {
'x-matched-path': '/api/optional/[[...rest]]',
Expand Down

1 comment on commit eb3f6dd

@Podfelix
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test/production/standalone-mode/required-server-files/required-server-files.test.ts

Please sign in to comment.