Unverified Commit 7f063886 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Fix skipMiddlewareUrlNormalize with i18n (#47773)

This ensures we don't normalize locales in the URL with the
skipMiddlewareUrlNormalize flag enabled so that casing redirects can be
applied correctly for locales.
parent 82787dba
......@@ -99,7 +99,9 @@ export async function adapter(
const request = new NextRequestHint({
page: params.page,
input: String(requestUrl),
input: process.env.__NEXT_NO_MIDDLEWARE_URL_NORMALIZE
? params.request.url
: String(requestUrl),
init: {
body: params.request.body,
geo: params.request.geo,
......
......@@ -12,7 +12,8 @@ export class NextRequest extends Request {
cookies: RequestCookies
geo: RequestData['geo']
ip?: string
url: NextURL
url: string
nextUrl: NextURL
}
constructor(input: URL | RequestInfo, init: RequestInit = {}) {
......@@ -20,14 +21,18 @@ export class NextRequest extends Request {
typeof input !== 'string' && 'url' in input ? input.url : String(input)
validateURL(url)
super(url, init)
const nextUrl = new NextURL(url, {
headers: toNodeHeaders(this.headers),
nextConfig: init.nextConfig,
})
this[INTERNALS] = {
cookies: new RequestCookies(this.headers),
geo: init.geo || {},
ip: init.ip,
url: new NextURL(url, {
headers: toNodeHeaders(this.headers),
nextConfig: init.nextConfig,
}),
nextUrl,
url: process.env.__NEXT_NO_MIDDLEWARE_URL_NORMALIZE
? url
: nextUrl.toString(),
}
}
......@@ -68,7 +73,7 @@ export class NextRequest extends Request {
}
public get nextUrl() {
return this[INTERNALS].url
return this[INTERNALS].nextUrl
}
/**
......@@ -90,7 +95,7 @@ export class NextRequest extends Request {
}
public get url() {
return this[INTERNALS].url.toString()
return this[INTERNALS].url
}
}
......
import { NextResponse } from 'next/server'
export default function handler(req) {
const parsedOrigUrl = new URL(req.url)
const origPathname = parsedOrigUrl.pathname
const lowerPathname = origPathname.toLowerCase()
// ensure locale casing can be redirected
if (
(lowerPathname.startsWith('/en') || lowerPathname.startsWith('/ja-jp')) &&
origPathname !== lowerPathname &&
!lowerPathname.includes('_next')
) {
parsedOrigUrl.pathname = lowerPathname
return NextResponse.redirect(parsedOrigUrl)
}
console.log(req.nextUrl)
let { pathname } = req.nextUrl
if (pathname.includes('docs') || pathname.includes('chained-rewrite')) {
if (pathname.startsWith('/en')) {
pathname = pathname.replace(/^\/en/, '') || '/'
}
if (pathname === '/docs') {
pathname = '/'
}
console.log(
`rewriting to https://middleware-external-rewrite-target-epsp8idgo-uncurated-tests.vercel.app${pathname}`
)
return NextResponse.rewrite(
`https://middleware-external-rewrite-target-epsp8idgo-uncurated-tests.vercel.app${pathname}`
)
......
......@@ -5,6 +5,10 @@ const nextConfig = {
experimental: {
externalMiddlewareRewritesResolve: true,
},
i18n: {
locales: ['en', 'ja-jp'],
defaultLocale: 'en',
},
async redirects() {
return [
{
......
......@@ -16,6 +16,17 @@ describe('skip-trailing-slash-redirect', () => {
})
afterAll(() => next.destroy())
it.each(['EN', 'JA-JP'])(
'should be able to redirect locale casing $1',
async (locale) => {
const res = await next.fetch(`/${locale}`, { redirect: 'manual' })
expect(res.status).toBe(307)
expect(new URL(res.headers.get('location'), 'http://n').pathname).toBe(
`/${locale.toLowerCase()}`
)
}
)
it.each([
{ pathname: '/chained-rewrite-ssg' },
{ pathname: '/chained-rewrite-static' },
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment