Skip to content

Commit 5181234

Browse files
authored
fix: support skipProxyUrlNormalize (#3190)
1 parent 91ab33e commit 5181234

File tree

7 files changed

+136
-2
lines changed

7 files changed

+136
-2
lines changed

src/build/functions/edge.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,8 @@ const writeHandlerFile = async (
117117
basePath: nextConfig.basePath,
118118
i18n: nextConfig.i18n,
119119
trailingSlash: nextConfig.trailingSlash,
120-
skipMiddlewareUrlNormalize: nextConfig.skipMiddlewareUrlNormalize,
120+
skipMiddlewareUrlNormalize:
121+
nextConfig.skipProxyUrlNormalize ?? nextConfig.skipMiddlewareUrlNormalize,
121122
}
122123

123124
await writeFile(

tests/fixtures/middleware-i18n-skip-normalize/middleware-shared.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ const getResponse = (request: NextRequest) => {
2323
return NextResponse.next()
2424
}
2525

26+
// this is needed for tests to assert next
27+
if (url.pathname.includes('dynamic/test')) {
28+
const response = NextResponse.next()
29+
response.headers.set('x-next-url-pathname', request.nextUrl.pathname)
30+
return response
31+
}
32+
2633
if (url.pathname === '/old-home') {
2734
if (url.searchParams.get('override') === 'external') {
2835
return NextResponse.redirect('https://example.vercel.sh')
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
module.exports = {
2+
output: 'standalone',
3+
distDir: '.next',
4+
generateBuildId: () => 'build-id',
5+
i18n: {
6+
locales: ['en', 'fr', 'nl', 'es'],
7+
defaultLocale: 'en',
8+
},
9+
skipProxyUrlNormalize: true,
10+
experimental: {
11+
clientRouterFilter: true,
12+
clientRouterFilterRedirects: true,
13+
},
14+
redirects() {
15+
return [
16+
{
17+
source: '/to-new',
18+
destination: '/dynamic/new',
19+
permanent: false,
20+
},
21+
]
22+
},
23+
outputFileTracingRoot: __dirname,
24+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "proxy-i18n-skip-normalize",
3+
"version": "0.1.0",
4+
"private": true,
5+
"scripts": {
6+
"postinstall": "npm run build",
7+
"dev": "next dev",
8+
"build": "next build"
9+
},
10+
"dependencies": {
11+
"next": "latest",
12+
"react": "18.2.0",
13+
"react-dom": "18.2.0"
14+
},
15+
"devDependencies": {
16+
"@types/react": "18.2.47"
17+
},
18+
"test": {
19+
"dependencies": {
20+
"next": ">=16.0.0-alpha.0"
21+
}
22+
}
23+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export default function Account({ slug }) {
2+
return (
3+
<p id="dynamic" className="title">
4+
Welcome to a /dynamic/[slug]: {slug}
5+
</p>
6+
)
7+
}
8+
9+
export function getServerSideProps({ params }) {
10+
return {
11+
props: {
12+
slug: params.slug,
13+
},
14+
}
15+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import type { NextRequest } from 'next/server'
2+
import { NextResponse } from 'next/server'
3+
4+
export async function proxy(request: NextRequest) {
5+
const response = NextResponse.next()
6+
7+
if (response) {
8+
response.headers.append('Deno' in globalThis ? 'x-deno' : 'x-node', Date.now().toString())
9+
// report Next.js Middleware Runtime (not the execution runtime, but target runtime)
10+
// @ts-expect-error EdgeRuntime global not declared
11+
response.headers.append('x-runtime', typeof EdgeRuntime !== 'undefined' ? EdgeRuntime : 'node')
12+
response.headers.set('x-hello-from-middleware-res', 'hello')
13+
14+
response.headers.set('x-next-url-pathname', request.nextUrl.pathname)
15+
16+
return response
17+
}
18+
}
19+
20+
export const config = {
21+
runtime: 'nodejs',
22+
}

tests/integration/middleware.test.ts

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
} from '../utils/fixture.js'
1212
import { generateRandomObjectID, startMockBlobStore } from '../utils/helpers.js'
1313
import { LocalServer } from '../utils/local-server.js'
14-
import { hasNodeMiddlewareSupport } from '../utils/next-version-helpers.mjs'
14+
import { hasNodeMiddlewareSupport, nextVersionSatisfies } from '../utils/next-version-helpers.mjs'
1515

1616
beforeEach<FixtureTestContext>(async (ctx) => {
1717
// set for each test a new deployID and siteID
@@ -693,6 +693,17 @@ for (const {
693693
expect(bodyFr.requestUrlPathname).toBe('/fr/json')
694694
expect(bodyFr.nextUrlPathname).toBe('/json')
695695
expect(bodyFr.nextUrlLocale).toBe('fr')
696+
697+
const responseData = await invokeEdgeFunction(ctx, {
698+
functions: [edgeFunctionNameRoot],
699+
origin,
700+
url: `/_next/data/build_id/en/dynamic/test.json?slug=test`,
701+
})
702+
703+
expect(
704+
responseData.headers.get('x-next-url-pathname'),
705+
'nextUrl.pathname should not be normalized due to skipMiddlewareUrlNormalize',
706+
).toEqual('/_next/data/build_id/en/dynamic/test.json')
696707
})
697708
})
698709

@@ -719,6 +730,37 @@ for (const {
719730
)
720731
})
721732
})
733+
734+
describe('Proxy specific', () => {
735+
test.skipIf(!nextVersionSatisfies('>=16.0.0-alpha.0'))<FixtureTestContext>(
736+
'skipProxyUrlNormalize in proxy.ts is supported',
737+
async (ctx) => {
738+
await createFixture('proxy-i18n-skip-normalize', ctx)
739+
await runPlugin(ctx)
740+
const origin = await LocalServer.run(async (req, res) => {
741+
res.write(
742+
JSON.stringify({
743+
url: req.url,
744+
headers: req.headers,
745+
}),
746+
)
747+
res.end()
748+
})
749+
ctx.cleanup?.push(() => origin.stop())
750+
751+
const responseData = await invokeEdgeFunction(ctx, {
752+
functions: [edgeFunctionNameRoot],
753+
origin,
754+
url: `/_next/data/build_id/en/dynamic/test.json`,
755+
})
756+
757+
expect(
758+
responseData.headers.get('x-next-url-pathname'),
759+
'nextUrl.pathname should not be normalized due to skipProxyUrlNormalize',
760+
).toEqual('/_next/data/build_id/en/dynamic/test.json')
761+
},
762+
)
763+
})
722764
}
723765
})
724766
}

0 commit comments

Comments
 (0)