Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: external redirects not being matched correctly closes #3770 #4109

Merged
merged 4 commits into from
Feb 4, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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