Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/build/functions/edge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ const writeHandlerFile = async (
basePath: nextConfig.basePath,
i18n: nextConfig.i18n,
trailingSlash: nextConfig.trailingSlash,
skipMiddlewareUrlNormalize: nextConfig.skipMiddlewareUrlNormalize,
skipMiddlewareUrlNormalize:
nextConfig.skipProxyUrlNormalize ?? nextConfig.skipMiddlewareUrlNormalize,
}

await writeFile(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ const getResponse = (request: NextRequest) => {
return NextResponse.next()
}

// this is needed for tests to assert next
if (url.pathname.includes('dynamic/test')) {
const response = NextResponse.next()
response.headers.set('x-next-url-pathname', request.nextUrl.pathname)
return response
}

if (url.pathname === '/old-home') {
if (url.searchParams.get('override') === 'external') {
return NextResponse.redirect('https://example.vercel.sh')
Expand Down
24 changes: 24 additions & 0 deletions tests/fixtures/proxy-i18n-skip-normalize/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module.exports = {
output: 'standalone',
distDir: '.next',
generateBuildId: () => 'build-id',
i18n: {
locales: ['en', 'fr', 'nl', 'es'],
defaultLocale: 'en',
},
skipProxyUrlNormalize: true,
experimental: {
clientRouterFilter: true,
clientRouterFilterRedirects: true,
},
redirects() {
return [
{
source: '/to-new',
destination: '/dynamic/new',
permanent: false,
},
]
},
outputFileTracingRoot: __dirname,
}
23 changes: 23 additions & 0 deletions tests/fixtures/proxy-i18n-skip-normalize/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "proxy-i18n-skip-normalize",
"version": "0.1.0",
"private": true,
"scripts": {
"postinstall": "npm run build",
"dev": "next dev",
"build": "next build"
},
"dependencies": {
"next": "latest",
"react": "18.2.0",
"react-dom": "18.2.0"
},
"devDependencies": {
"@types/react": "18.2.47"
},
"test": {
"dependencies": {
"next": ">=16.0.0-alpha.0"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export default function Account({ slug }) {
return (
<p id="dynamic" className="title">
Welcome to a /dynamic/[slug]: {slug}
</p>
)
}

export function getServerSideProps({ params }) {
return {
props: {
slug: params.slug,
},
}
}
22 changes: 22 additions & 0 deletions tests/fixtures/proxy-i18n-skip-normalize/proxy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import type { NextRequest } from 'next/server'
import { NextResponse } from 'next/server'

export async function proxy(request: NextRequest) {
const response = NextResponse.next()

if (response) {
response.headers.append('Deno' in globalThis ? 'x-deno' : 'x-node', Date.now().toString())
// report Next.js Middleware Runtime (not the execution runtime, but target runtime)
// @ts-expect-error EdgeRuntime global not declared
response.headers.append('x-runtime', typeof EdgeRuntime !== 'undefined' ? EdgeRuntime : 'node')
response.headers.set('x-hello-from-middleware-res', 'hello')

response.headers.set('x-next-url-pathname', request.nextUrl.pathname)

return response
}
}

export const config = {
runtime: 'nodejs',
}
Comment on lines +20 to +22
Copy link
Contributor Author

Choose a reason for hiding this comment

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

It seems like Proxy will be Node.js only (tho it's not as of now), so I do add this explicitly here in hope the test setup won't break when Next.js ships the change to make Proxy Node.js only. If that change end up not shipping, I'll adjust the setup to run in both Edge and Node.js runtimes as we do for most of our middleware fixtures

44 changes: 43 additions & 1 deletion tests/integration/middleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
} from '../utils/fixture.js'
import { generateRandomObjectID, startMockBlobStore } from '../utils/helpers.js'
import { LocalServer } from '../utils/local-server.js'
import { hasNodeMiddlewareSupport } from '../utils/next-version-helpers.mjs'
import { hasNodeMiddlewareSupport, nextVersionSatisfies } from '../utils/next-version-helpers.mjs'

beforeEach<FixtureTestContext>(async (ctx) => {
// set for each test a new deployID and siteID
Expand Down Expand Up @@ -693,6 +693,17 @@ for (const {
expect(bodyFr.requestUrlPathname).toBe('/fr/json')
expect(bodyFr.nextUrlPathname).toBe('/json')
expect(bodyFr.nextUrlLocale).toBe('fr')

const responseData = await invokeEdgeFunction(ctx, {
functions: [edgeFunctionNameRoot],
origin,
url: `/_next/data/build_id/en/dynamic/test.json?slug=test`,
})

expect(
responseData.headers.get('x-next-url-pathname'),
'nextUrl.pathname should not be normalized due to skipMiddlewareUrlNormalize',
).toEqual('/_next/data/build_id/en/dynamic/test.json')
Comment on lines +696 to +706
Copy link
Contributor Author

@pieh pieh Oct 21, 2025

Choose a reason for hiding this comment

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

uff, we actually didn't have any assertion that skipMiddlewareUrlNormalize was supported - so this adds assertion for now deprecated config option mainly for older Next.js versions

})
})

Expand All @@ -719,6 +730,37 @@ for (const {
)
})
})

describe('Proxy specific', () => {
test.skipIf(!nextVersionSatisfies('>=16.0.0-alpha.0'))<FixtureTestContext>(
'skipProxyUrlNormalize in proxy.ts is supported',
async (ctx) => {
await createFixture('proxy-i18n-skip-normalize', ctx)
await runPlugin(ctx)
const origin = await LocalServer.run(async (req, res) => {
res.write(
JSON.stringify({
url: req.url,
headers: req.headers,
}),
)
res.end()
})
ctx.cleanup?.push(() => origin.stop())

const responseData = await invokeEdgeFunction(ctx, {
functions: [edgeFunctionNameRoot],
origin,
url: `/_next/data/build_id/en/dynamic/test.json`,
})

expect(
responseData.headers.get('x-next-url-pathname'),
'nextUrl.pathname should not be normalized due to skipProxyUrlNormalize',
).toEqual('/_next/data/build_id/en/dynamic/test.json')
},
)
})
}
})
}
Expand Down
Loading