Skip to content

Commit 4774797

Browse files
authored
fix: dev server fix ip when skipping watch (#6977)
* fix: fix ip when skipping watch * fix: use node version to determine default ip ver
1 parent a4f3778 commit 4774797

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed

src/utils/framework-server.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ export const startFrameworkServer = async function ({
4949
let port: { open: boolean; ipVersion?: 4 | 6 } | undefined
5050
try {
5151
if (settings.skipWaitPort) {
52-
port = { open: true, ipVersion: 6 }
52+
// default ip version based on node version
53+
const ipVersion = parseInt(process.versions.node.split('.')[0]) >= 18 ? 6 : 4
54+
port = { open: true, ipVersion }
5355
} else {
5456
port = await waitPort({
5557
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,46 @@ describe.concurrent('command/dev', () => {
357357
})
358358
})
359359

360+
test('should follow 301 redirect to an external server', async (t) => {
361+
await withSiteBuilder(t, async (builder) => {
362+
const externalServer = startExternalServer()
363+
const { port } = externalServer.address() as AddressInfo
364+
builder.withRedirectsFile({
365+
redirects: [{ from: '/api/*', to: `http://localhost:${port}/:splat`, status: 301 }],
366+
})
367+
368+
await builder.build()
369+
370+
await withDevServer({ cwd: builder.directory }, async (server) => {
371+
const [response1, response2] = await Promise.all([
372+
fetch(`${server.url}/api/ping`, { follow: 0, redirect: 'manual' }),
373+
fetch(`${server.url}/api/ping`).then((res) => res.json()),
374+
])
375+
t.expect(response1.headers.get('location')).toEqual(`http://localhost:${port}/ping`)
376+
377+
t.expect(response2.body).toStrictEqual({})
378+
t.expect(response2.method).toEqual('GET')
379+
t.expect(response2.url).toEqual('/ping')
380+
})
381+
382+
externalServer.close()
383+
})
384+
})
385+
386+
test('should proxy server without waiting for port', async (t) => {
387+
await withSiteBuilder(t, async (builder) => {
388+
const externalServer = startExternalServer()
389+
await builder.build()
390+
391+
await withDevServer({ cwd: builder.directory, skipWaitPort: true }, async (server) => {
392+
const response = await fetch(`${server.url}/api/test`)
393+
t.expect(response.status).toBe(404)
394+
})
395+
396+
externalServer.close()
397+
})
398+
})
399+
360400
test('should rewrite POST request if content-type is missing and not crash dev server', async (t) => {
361401
await withSiteBuilder(t, async (builder) => {
362402
builder.withNetlifyToml({

tests/integration/utils/dev-server.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ interface DevServerOptions {
4747
offline?: boolean
4848
prompt?: $FIXME[]
4949
serve?: boolean
50+
skipWaitPort?: boolean
5051
}
5152

5253
// 240 seconds
@@ -62,14 +63,14 @@ const startServer = async ({
6263
offline = true,
6364
prompt,
6465
serve = false,
66+
skipWaitPort = false,
6567
}: DevServerOptions): Promise<DevServer | { timeout: boolean; output: string }> => {
6668
const port = await getPort()
6769
const staticPort = await getPort()
6870
const host = 'localhost'
6971
const url = `http://${host}:${port}`
7072

7173
console.log(`Starting dev server on port: ${port} in directory ${path.basename(cwd)}`)
72-
7374
const baseCommand = serve ? 'serve' : 'dev'
7475
const baseArgs = [
7576
baseCommand,
@@ -79,6 +80,7 @@ const startServer = async ({
7980
'--staticServerPort',
8081
staticPort,
8182
debug ? '--debug' : '',
83+
skipWaitPort ? '--skip-wait-port' : '',
8284
]
8385

8486
// We use `null` to override the default context and actually omit the flag

0 commit comments

Comments
 (0)