Skip to content

Commit 2b5b6f8

Browse files
committed
test: add test case
1 parent 20d9a3b commit 2b5b6f8

File tree

4 files changed

+32
-2
lines changed

4 files changed

+32
-2
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

+10-2
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}`
@@ -36,7 +38,8 @@ export async function middleware(req: NextRequest) {
3638
return res
3739
}
3840

39-
if (pathname.startsWith('/request-rewrite')) {
41+
// skipMiddlewareUrlNormalize next config option is used so we have to try to match both html path and data blob path
42+
if (pathname.startsWith('/request-rewrite') || pathname.endsWith('/request-rewrite.json')) {
4043
// request.rewrite() should return the MiddlewareResponse object instead of the Response object.
4144
const res = await request.rewrite('/static-rewrite')
4245
const message = `This was static (& escaping test &) but has been transformed in ${req.geo?.city}`
@@ -100,6 +103,10 @@ export async function middleware(req: NextRequest) {
100103
return response
101104
}
102105

106+
if (pathname.includes('locale-preserving-rewrite')) {
107+
return NextResponse.rewrite(new URL('/locale-test', req.url))
108+
}
109+
103110
if (pathname.startsWith('/shows')) {
104111
if (pathname.startsWith('/shows/222')) {
105112
response = NextResponse.next()
@@ -151,6 +158,7 @@ export const config = {
151158
matcher: [
152159
'/api/:all*',
153160
'/headers',
161+
'/:all*/locale-preserving-rewrite',
154162
'/cookies/:path*',
155163
{ source: '/static' },
156164
{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)