Skip to content

Commit 5fc044d

Browse files
authored
fix: skip checking for endpoint existence for hidden redirects (#7067)
1 parent 150fcc3 commit 5fc044d

File tree

2 files changed

+61
-6
lines changed

2 files changed

+61
-6
lines changed

src/utils/proxy.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -349,10 +349,14 @@ const serveRedirect = async function ({
349349
}
350350

351351
const reqUrl = reqToURL(req, req.url)
352+
const isHiddenProxy =
353+
match.proxyHeaders &&
354+
Object.entries(match.proxyHeaders).some(([key, val]) => key.toLowerCase() === 'x-nf-hidden-proxy' && val === 'true')
352355

353356
const staticFile = await getStatic(decodeURIComponent(reqUrl.pathname), options.publicFolder)
354357
const endpointExists =
355358
!staticFile &&
359+
!isHiddenProxy &&
356360
process.env.NETLIFY_DEV_SERVER_CHECK_SSG_ENDPOINTS &&
357361
(await isEndpointExists(decodeURIComponent(reqUrl.pathname), options.target))
358362
if (staticFile || endpointExists) {
@@ -388,11 +392,6 @@ const serveRedirect = async function ({
388392
// This is a redirect, so we set the complete external URL as destination
389393
destURL = `${dest}`
390394
} else {
391-
const isHiddenProxy =
392-
match.proxyHeaders &&
393-
Object.entries(match.proxyHeaders).some(
394-
([key, val]) => key.toLowerCase() === 'x-nf-hidden-proxy' && val === 'true',
395-
)
396395
if (!isHiddenProxy) {
397396
console.log(`${NETLIFYDEVLOG} Proxying to ${dest}`)
398397
}

tests/integration/commands/dev/redirects.test.ts

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
import fetch from 'node-fetch'
12
import { describe, expect, test } from 'vitest'
23

4+
import { withDevServer } from '../../utils/dev-server.js'
35
import { FixtureTestContext, setupFixtureTests } from '../../utils/fixture.js'
4-
import fetch from 'node-fetch'
6+
import { withSiteBuilder } from '../../utils/site-builder.js'
57

68
describe('redirects', () => {
79
setupFixtureTests('dev-server-with-functions', { devServer: true }, () => {
@@ -50,4 +52,58 @@ describe('redirects', () => {
5052
expect(result.toLowerCase()).not.toContain('netlify')
5153
})
5254
})
55+
56+
test('should not check the endpoint existence for hidden proxies', async (t) => {
57+
await withSiteBuilder(t, async (builder) => {
58+
await builder
59+
.withContentFile({
60+
path: './index.js',
61+
content: `
62+
const http = require('http')
63+
const server = http.createServer((req, res) => {
64+
console.log('Got request main server', req.method, req.url)
65+
res.end()
66+
})
67+
server.listen(6125)
68+
69+
const proxyServer = http.createServer((req, res) => {
70+
console.log('Got request proxy server', req.method, req.url)
71+
res.end()
72+
})
73+
proxyServer.listen(6126)
74+
`,
75+
})
76+
.withNetlifyToml({
77+
config: {
78+
dev: {
79+
targetPort: 6125,
80+
command: 'node index.js',
81+
},
82+
redirects: [
83+
{
84+
from: '/from-hidden',
85+
to: 'http://localhost:6126/to',
86+
status: 200,
87+
headers: { 'x-nf-hidden-proxy': 'true' },
88+
},
89+
{ from: '/from', to: 'http://localhost:6126/to', status: 200 },
90+
],
91+
},
92+
})
93+
.build()
94+
95+
await withDevServer(
96+
{ cwd: builder.directory, env: { NETLIFY_DEV_SERVER_CHECK_SSG_ENDPOINTS: '1' } },
97+
async ({ outputBuffer, url }) => {
98+
await fetch(new URL('/from-hidden', url))
99+
t.expect(String(outputBuffer)).not.toContain('Got request main server')
100+
t.expect(String(outputBuffer)).toContain('Got request proxy server GET /to')
101+
await fetch(new URL('/from', url))
102+
t.expect(String(outputBuffer.join(''))).toContain(
103+
'Got request main server HEAD /from\nGot request main server GET /from',
104+
)
105+
},
106+
)
107+
})
108+
})
53109
})

0 commit comments

Comments
 (0)