Skip to content

Commit 32714f0

Browse files
tinfoil-knighterezrokahkodiakhq[bot]
authoredFeb 4, 2022
fix: external redirects not being matched correctly closes #3770 (#4109)
* test: add failing test to reproduce issue with 301 redirects * fix: external redirects not being matched correctly Co-authored-by: Erez Rokah <erezrokah@users.noreply.github.com> Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
1 parent 5b5b360 commit 32714f0

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed
 

Diff for: ‎src/utils/proxy.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,10 @@ const serveRedirect = async function ({ match, options, proxy, req, res }) {
213213

214214
const destURL = stripOrigin(dest)
215215

216+
if (isExternal(match)) {
217+
return proxyToExternalUrl({ req, res, dest, destURL })
218+
}
219+
216220
if (isRedirect(match)) {
217221
res.writeHead(match.status, {
218222
Location: destURL,
@@ -222,10 +226,6 @@ const serveRedirect = async function ({ match, options, proxy, req, res }) {
222226
return
223227
}
224228

225-
if (isExternal(match)) {
226-
return proxyToExternalUrl({ req, res, dest, destURL })
227-
}
228-
229229
const ct = req.headers['content-type'] ? contentType.parse(req).type : ''
230230
if (
231231
req.method === 'POST' &&

Diff for: ‎tests/integration/0.command.dev.test.js

+19
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,25 @@ testMatrix.forEach(({ args }) => {
199199
})
200200
})
201201

202+
test(testName('should follow 301 redirect to an external server', args), async (t) => {
203+
await withSiteBuilder('site-redirects-file-to-external-301', async (builder) => {
204+
const externalServer = startExternalServer()
205+
const { port } = externalServer.address()
206+
builder.withRedirectsFile({
207+
redirects: [{ from: '/api/*', to: `http://localhost:${port}/:splat`, status: 301 }],
208+
})
209+
210+
await builder.buildAsync()
211+
212+
await withDevServer({ cwd: builder.directory, args }, async (server) => {
213+
const response = await got(`${server.url}/api/ping`).json()
214+
t.deepEqual(response, { body: {}, method: 'GET', url: '/ping' })
215+
})
216+
217+
externalServer.close()
218+
})
219+
})
220+
202221
test(testName('should redirect POST request if content-type is missing', args), async (t) => {
203222
await withSiteBuilder('site-with-post-no-content-type', async (builder) => {
204223
builder.withNetlifyToml({

1 commit comments

Comments
 (1)

github-actions[bot] commented on Feb 4, 2022

@github-actions[bot]

📊 Benchmark results

Package size: 362 MB

Please sign in to comment.