Skip to content

Commit 30b091c

Browse files
authored
fix: rewrites shouldn't be following redirects (#253)
1 parent 65c6b88 commit 30b091c

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

edge-runtime/lib/response.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,8 @@ export const buildResponse = async ({
163163
method: request.method,
164164
})
165165
}
166-
return addMiddlewareHeaders(fetch(proxyRequest), res)
166+
167+
return addMiddlewareHeaders(fetch(proxyRequest, { redirect: 'manual' }), res)
167168
} else if (isDataReq) {
168169
rewriteUrl.pathname = rewriteDataPath({
169170
dataUrl: originalPath,

tests/integration/edge-handler.test.ts

+28
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,34 @@ describe('rewrite', () => {
153153
expect(external.calls).toBe(1)
154154
expect(origin.calls).toBe(0)
155155
})
156+
157+
test<FixtureTestContext>('rewriting to external URL that redirects should return said redirect', async (ctx) => {
158+
await createFixture('middleware', ctx)
159+
await runPlugin(ctx)
160+
161+
const external = await LocalServer.run(async (req, res) => {
162+
res.writeHead(302, {
163+
location: 'http://example.com/redirected',
164+
})
165+
res.end()
166+
})
167+
ctx.cleanup?.push(() => external.stop())
168+
169+
const origin = new LocalServer()
170+
ctx.cleanup?.push(() => origin.stop())
171+
172+
const response = await invokeEdgeFunction(ctx, {
173+
functions: ['___netlify-edge-handler-middleware'],
174+
origin,
175+
url: `/test/rewrite-external?external-url=http://localhost:${external.port}/some-path`,
176+
redirect: 'manual',
177+
})
178+
179+
expect(await response.text()).toBe('')
180+
181+
expect(response.status).toBe(302)
182+
expect(response.headers.get('location')).toBe('http://example.com/redirected')
183+
})
156184
})
157185

158186
describe("aborts middleware execution when the matcher conditions don't match the request", () => {

0 commit comments

Comments
 (0)