File tree Expand file tree Collapse file tree 3 files changed +13
-1
lines changed
packages/runtime/src/templates/edge-shared Expand file tree Collapse file tree 3 files changed +13
-1
lines changed Original file line number Diff line number Diff line change @@ -24,6 +24,13 @@ describe('Standard middleware', () => {
2424 cy . url ( ) . should ( 'eq' , `${ Cypress . config ( ) . baseUrl } /shows/rewrite-external` )
2525 } )
2626
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+
2734 it ( 'adds headers to static pages' , ( ) => {
2835 cy . request ( '/shows/static/3' ) . then ( ( response ) => {
2936 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) {
3838 return res
3939 }
4040
41+ if ( pathname . startsWith ( "/rewrite-to-redirect" ) ) {
42+ return NextResponse . rewrite ( 'https://httpbin.org/redirect-to?url=https://example.com' )
43+ }
44+
4145 // skipMiddlewareUrlNormalize next config option is used so we have to try to match both html path and data blob path
4246 if ( pathname . startsWith ( '/request-rewrite' ) || pathname . endsWith ( '/request-rewrite.json' ) ) {
4347 // request.rewrite() should return the MiddlewareResponse object instead of the Response object.
@@ -174,6 +178,7 @@ export const config = {
174178 '/cookies/:path*' ,
175179 { source : '/static' } ,
176180 { source : '/request-rewrite' } ,
181+ { source : '/rewrite-to-redirect' } ,
177182 { source : '/matcher-cookie' } ,
178183 { source : '/shows/((?!99|88).*)' } ,
179184 {
Original file line number Diff line number Diff line change @@ -265,7 +265,7 @@ export const buildResponse = async ({
265265 }
266266 if ( rewriteUrl . hostname !== baseUrl . hostname ) {
267267 // 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' } )
269269 return addMiddlewareHeaders ( proxied , res )
270270 }
271271 res . headers . set ( 'x-middleware-rewrite' , relativeUrl )
You can’t perform that action at this time.
0 commit comments