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

Fix middleware i18n rewrites #31174

Merged
merged 7 commits into from Nov 9, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -6,8 +6,9 @@ import { normalizeLocalePath } from '../../../../shared/lib/i18n/normalize-local
import pathMatch from '../../../../shared/lib/router/utils/path-match'
import { getRouteRegex } from '../../../../shared/lib/router/utils/route-regex'
import { getRouteMatcher } from '../../../../shared/lib/router/utils/route-matcher'
import prepareDestination, {
import {
matchHas,
prepareDestination,
} from '../../../../shared/lib/router/utils/prepare-destination'
import { __ApiPreviewProps } from '../../../../server/api-utils'
import { BuildManifest } from '../../../../server/get-page-files'
Expand All @@ -23,6 +24,7 @@ import { denormalizePagePath } from '../../../../server/denormalize-page-path'
import cookie from 'next/dist/compiled/cookie'
import { TEMPORARY_REDIRECT_STATUS } from '../../../../shared/lib/constants'
import { NextConfig } from '../../../../server/config'
import { addRequestMeta } from '../../../../server/request-meta'

const getCustomRouteMatcher = pathMatch(true)

Expand Down Expand Up @@ -99,12 +101,12 @@ export function getUtils({
}

if (params) {
const { parsedDestination } = prepareDestination(
rewrite.destination,
params,
parsedUrl.query,
true
)
const { parsedDestination } = prepareDestination({
appendParamsToQuery: true,
destination: rewrite.destination,
params: params,
query: parsedUrl.query,
})

Object.assign(parsedUrl.query, parsedDestination.query)
delete (parsedDestination as any).query
Expand Down Expand Up @@ -372,7 +374,7 @@ export function getUtils({
if (detectedDomain) {
defaultLocale = detectedDomain.defaultLocale
detectedLocale = defaultLocale
;(req as any).__nextIsLocaleDomain = true
addRequestMeta(req, '__nextIsLocaleDomain', true)
}

// if not domain specific locale use accept-language preferred
Expand All @@ -392,7 +394,7 @@ export function getUtils({
...parsedUrl,
pathname: localePathResult.pathname,
})
;(req as any).__nextStrippedLocale = true
addRequestMeta(req, '__nextStrippedLocale', true)
parsedUrl.pathname = localePathResult.pathname
}

Expand Down
15 changes: 8 additions & 7 deletions packages/next/export/worker.ts
@@ -1,3 +1,9 @@
import type { ComponentType } from 'react'
import type { FontManifest } from '../server/font-utils'
import type { GetStaticProps } from '../types'
import type { IncomingMessage, ServerResponse } from 'http'
import type { NextConfigComplete } from '../server/config-shared'
import type { NextParsedUrlQuery } from '../server/request-meta'
import url from 'url'
import { extname, join, dirname, sep } from 'path'
import { renderToHTML } from '../server/render'
Expand All @@ -10,15 +16,10 @@ import { getRouteRegex } from '../shared/lib/router/utils/route-regex'
import { normalizePagePath } from '../server/normalize-page-path'
import { SERVER_PROPS_EXPORT_ERROR } from '../lib/constants'
import '../server/node-polyfill-fetch'
import { IncomingMessage, ServerResponse } from 'http'
import { ComponentType } from 'react'
import { GetStaticProps } from '../types'
import { requireFontManifest } from '../server/require'
import { FontManifest } from '../server/font-utils'
import { normalizeLocalePath } from '../shared/lib/i18n/normalize-locale-path'
import { trace } from '../trace'
import { isInAmpMode } from '../shared/lib/amp'
import { NextConfigComplete } from '../server/config-shared'
import { setHttpAgentOptions } from '../server/config'
import RenderResult from '../server/render-result'
import isError from '../lib/is-error'
Expand All @@ -39,7 +40,7 @@ interface AmpValidation {

interface PathMap {
page: string
query?: { [key: string]: string | string[] }
query?: NextParsedUrlQuery
}

interface ExportPageInput {
Expand Down Expand Up @@ -128,7 +129,7 @@ export default async function exportPage({
let query = { ...originalQuery }
let params: { [key: string]: string | string[] } | undefined

let updatedPath = (query.__nextSsgPath as string) || path
let updatedPath = query.__nextSsgPath || path
let locale = query.__nextLocale || renderOpts.locale
delete query.__nextLocale
delete query.__nextSsgPath
Expand Down