Skip to content

Commit 9de97eb

Browse files
committed
test: add test case
1 parent 20d9a3b commit 9de97eb

File tree

4 files changed

+30
-1
lines changed

4 files changed

+30
-1
lines changed

cypress/e2e/middleware/standard.cy.ts

+6
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ describe('Standard middleware', () => {
4545
expect(response.headers).to.have.property('x-foo', 'bar')
4646
})
4747
})
48+
49+
it('preserves locale on rewrites (skipMiddlewareUrlNormalize: true)', () => {
50+
cy.visit('/de-de/locale-preserving-rewrite')
51+
cy.get('div').should('contain', 'Locale: de-DE')
52+
cy.url().should('eq', `${Cypress.config().baseUrl}/de-de/locale-preserving-rewrite`)
53+
})
4854
})
4955

5056
describe('Middleware matchers', () => {

demos/middleware/middleware.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ export async function middleware(req: NextRequest) {
2121
}
2222

2323
const request = new MiddlewareRequest(req)
24-
if (pathname.startsWith('/static')) {
24+
25+
// skipMiddlewareUrlNormalize next config option is used so we have to try to match both html path and data blob path
26+
if (pathname.startsWith('/static') || pathname.endsWith('static.json')) {
2527
// Unlike NextResponse.next(), this actually sends the request to the origin
2628
const res = await request.next()
2729
const message = `This was static (& escaping test &) but has been transformed in ${req.geo?.city}`
@@ -100,6 +102,10 @@ export async function middleware(req: NextRequest) {
100102
return response
101103
}
102104

105+
if (pathname.includes('locale-preserving-rewrite')) {
106+
return NextResponse.rewrite(new URL('/locale-test', req.url))
107+
}
108+
103109
if (pathname.startsWith('/shows')) {
104110
if (pathname.startsWith('/shows/222')) {
105111
response = NextResponse.next()
@@ -151,6 +157,7 @@ export const config = {
151157
matcher: [
152158
'/api/:all*',
153159
'/headers',
160+
'/:all*/locale-preserving-rewrite',
154161
'/cookies/:path*',
155162
{ source: '/static' },
156163
{source: '/request-rewrite' },

demos/middleware/next.config.js

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const nextConfig = {
1111
defaultLocale: 'en',
1212
locales: ['en', 'de-DE'],
1313
},
14+
skipMiddlewareUrlNormalize: true,
1415
}
1516

1617
module.exports = nextConfig

demos/middleware/pages/locale-test.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import * as React from 'react'
2+
3+
const Page = ({ pageLocale }) => {
4+
return <div>Locale: {pageLocale}</div>
5+
}
6+
7+
export async function getServerSideProps({ locale }) {
8+
return {
9+
props: {
10+
pageLocale: locale,
11+
},
12+
}
13+
}
14+
15+
export default Page

0 commit comments

Comments
 (0)