Skip to content

Commit

Permalink
fix: external redirects not being matched correctly closes #3770 (#4109)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
3 people authored Feb 4, 2022
1 parent 5b5b360 commit 32714f0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/utils/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,10 @@ const serveRedirect = async function ({ match, options, proxy, req, res }) {

const destURL = stripOrigin(dest)

if (isExternal(match)) {
return proxyToExternalUrl({ req, res, dest, destURL })
}

if (isRedirect(match)) {
res.writeHead(match.status, {
Location: destURL,
Expand All @@ -222,10 +226,6 @@ const serveRedirect = async function ({ match, options, proxy, req, res }) {
return
}

if (isExternal(match)) {
return proxyToExternalUrl({ req, res, dest, destURL })
}

const ct = req.headers['content-type'] ? contentType.parse(req).type : ''
if (
req.method === 'POST' &&
Expand Down
19 changes: 19 additions & 0 deletions tests/integration/0.command.dev.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,25 @@ testMatrix.forEach(({ args }) => {
})
})

test(testName('should follow 301 redirect to an external server', args), async (t) => {
await withSiteBuilder('site-redirects-file-to-external-301', async (builder) => {
const externalServer = startExternalServer()
const { port } = externalServer.address()
builder.withRedirectsFile({
redirects: [{ from: '/api/*', to: `http://localhost:${port}/:splat`, status: 301 }],
})

await builder.buildAsync()

await withDevServer({ cwd: builder.directory, args }, async (server) => {
const response = await got(`${server.url}/api/ping`).json()
t.deepEqual(response, { body: {}, method: 'GET', url: '/ping' })
})

externalServer.close()
})
})

test(testName('should redirect POST request if content-type is missing', args), async (t) => {
await withSiteBuilder('site-with-post-no-content-type', async (builder) => {
builder.withNetlifyToml({
Expand Down

1 comment on commit 32714f0

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📊 Benchmark results

Package size: 362 MB

Please sign in to comment.