Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 013b210

Browse files
authored
Merge branch 'main' into rs/react-env-var-fix
2 parents 17371ad + 68daaba commit 013b210

File tree

12 files changed

+660
-1508
lines changed

12 files changed

+660
-1508
lines changed

cypress/e2e/middleware/enhanced.cy.ts

+6
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ describe('Enhanced middleware', () => {
1313
})
1414
})
1515

16+
it('passes in headers within request.rewrite()', () => {
17+
cy.request('/request-rewrite').then((response) => {
18+
expect(response.headers).to.have.property('x-rewrite-test', 'hello')
19+
})
20+
})
21+
1622
it('rewrites the response body using request.next()', () => {
1723
cy.visit('/static')
1824
cy.get('#message').contains('This was static (& escaping test &) but has been transformed in')

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

+17-3
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,9 +38,16 @@ 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.
41-
const res = await request.rewrite('/static-rewrite')
44+
const res = await request.rewrite('/static-rewrite',
45+
{
46+
headers: {
47+
'x-rewrite-test': 'hello',
48+
'x-rewrite-test-2': 'hello-2'
49+
}
50+
})
4251
const message = `This was static (& escaping test &) but has been transformed in ${req.geo?.city}`
4352

4453
// Transform the response HTML and props
@@ -100,6 +109,10 @@ export async function middleware(req: NextRequest) {
100109
return response
101110
}
102111

112+
if (pathname.includes('locale-preserving-rewrite')) {
113+
return NextResponse.rewrite(new URL('/locale-test', req.url))
114+
}
115+
103116
if (pathname.startsWith('/shows')) {
104117
if (pathname.startsWith('/shows/222')) {
105118
response = NextResponse.next()
@@ -151,6 +164,7 @@ export const config = {
151164
matcher: [
152165
'/api/:all*',
153166
'/headers',
167+
'/:all*/locale-preserving-rewrite',
154168
'/cookies/:path*',
155169
{ source: '/static' },
156170
{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)