File tree 3 files changed +13
-1
lines changed
packages/runtime/src/templates/edge-shared
3 files changed +13
-1
lines changed Original file line number Diff line number Diff line change @@ -24,6 +24,13 @@ describe('Standard middleware', () => {
24
24
cy . url ( ) . should ( 'eq' , `${ Cypress . config ( ) . baseUrl } /shows/rewrite-external` )
25
25
} )
26
26
27
+ it ( 'doesnt follow redirects from rewritten page' , ( ) => {
28
+ cy . request ( { url : '/rewrite-to-redirect' , followRedirect : false } ) . then ( ( response ) => {
29
+ expect ( response . status ) . to . eq ( 302 )
30
+ expect ( response . redirectedToUrl ) . to . eq ( 'https://example.com/' )
31
+ } )
32
+ } )
33
+
27
34
it ( 'adds headers to static pages' , ( ) => {
28
35
cy . request ( '/shows/static/3' ) . then ( ( response ) => {
29
36
expect ( response . headers ) . to . have . property ( 'x-middleware-date' )
Original file line number Diff line number Diff line change @@ -38,6 +38,10 @@ export async function middleware(req: NextRequest) {
38
38
return res
39
39
}
40
40
41
+ if ( pathname . startsWith ( "/rewrite-to-redirect" ) ) {
42
+ return NextResponse . rewrite ( 'https://httpbin.org/redirect-to?url=https://example.com' )
43
+ }
44
+
41
45
// skipMiddlewareUrlNormalize next config option is used so we have to try to match both html path and data blob path
42
46
if ( pathname . startsWith ( '/request-rewrite' ) || pathname . endsWith ( '/request-rewrite.json' ) ) {
43
47
// request.rewrite() should return the MiddlewareResponse object instead of the Response object.
@@ -174,6 +178,7 @@ export const config = {
174
178
'/cookies/:path*' ,
175
179
{ source : '/static' } ,
176
180
{ source : '/request-rewrite' } ,
181
+ { source : '/rewrite-to-redirect' } ,
177
182
{ source : '/matcher-cookie' } ,
178
183
{ source : '/shows/((?!99|88).*)' } ,
179
184
{
Original file line number Diff line number Diff line change @@ -265,7 +265,7 @@ export const buildResponse = async ({
265
265
}
266
266
if ( rewriteUrl . hostname !== baseUrl . hostname ) {
267
267
// Netlify Edge Functions don't support proxying to external domains, but Next middleware does
268
- const proxied = fetch ( new Request ( rewriteUrl . toString ( ) , request ) )
268
+ const proxied = fetch ( new Request ( rewriteUrl . toString ( ) , request ) , { redirect : 'manual' } )
269
269
return addMiddlewareHeaders ( proxied , res )
270
270
}
271
271
res . headers . set ( 'x-middleware-rewrite' , relativeUrl )
You can’t perform that action at this time.
0 commit comments