Skip to content

Commit

Permalink
Fix middleware i18n rewrites (#31174)
Browse files Browse the repository at this point in the history
Fixes #30897

This PR fixes the linked issue where rewrites are not being applied for locale. It adds the corresponding test but also, as it was added in debugging process, it introduces a helper to read/write into the `request` object.

We are currently writing directly into the request by casting to `any` and then using flags like `_nextRewrote`. Instead, this PR puts all of this metadata under a symbol so it is not directly accessible. This also allows to have a single place where all of this metadata is listed so we can add comments describing the purpose of each flag.

In the same way, there is metadata written in the querystring. This is adding some types for it in order to throw some visibility on where is this metadata accessed. In an upcoming PR we can move all of it to the `request` object if possible to simplify the system.

## Bug

- [x] Related issues linked using `fixes #number`
- [x] Integration tests added
- [x] Errors have helpful link attached, see `contributing.md`
  • Loading branch information
javivelasco committed Nov 9, 2021
1 parent 91a6e3a commit 5fc4325
Show file tree
Hide file tree
Showing 12 changed files with 383 additions and 183 deletions.
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

0 comments on commit 5fc4325

Please sign in to comment.