Skip to content

Commit

Permalink
fix: dev server fix ip when skipping watch (#6977)
Browse files Browse the repository at this point in the history
* fix: fix ip when skipping watch
* fix: use node version to determine default ip ver
  • Loading branch information
davbree authored Jan 7, 2025
1 parent a4f3778 commit 4774797
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/utils/framework-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ export const startFrameworkServer = async function ({
let port: { open: boolean; ipVersion?: 4 | 6 } | undefined
try {
if (settings.skipWaitPort) {
port = { open: true, ipVersion: 6 }
// default ip version based on node version
const ipVersion = parseInt(process.versions.node.split('.')[0]) >= 18 ? 6 : 4
port = { open: true, ipVersion }
} else {
port = await waitPort({
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
Expand Down
40 changes: 40 additions & 0 deletions tests/integration/commands/dev/dev.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,46 @@ describe.concurrent('command/dev', () => {
})
})

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

await builder.build()

await withDevServer({ cwd: builder.directory }, async (server) => {
const [response1, response2] = await Promise.all([
fetch(`${server.url}/api/ping`, { follow: 0, redirect: 'manual' }),
fetch(`${server.url}/api/ping`).then((res) => res.json()),
])
t.expect(response1.headers.get('location')).toEqual(`http://localhost:${port}/ping`)

t.expect(response2.body).toStrictEqual({})
t.expect(response2.method).toEqual('GET')
t.expect(response2.url).toEqual('/ping')
})

externalServer.close()
})
})

test('should proxy server without waiting for port', async (t) => {
await withSiteBuilder(t, async (builder) => {
const externalServer = startExternalServer()
await builder.build()

await withDevServer({ cwd: builder.directory, skipWaitPort: true }, async (server) => {
const response = await fetch(`${server.url}/api/test`)
t.expect(response.status).toBe(404)
})

externalServer.close()
})
})

test('should rewrite POST request if content-type is missing and not crash dev server', async (t) => {
await withSiteBuilder(t, async (builder) => {
builder.withNetlifyToml({
Expand Down
4 changes: 3 additions & 1 deletion tests/integration/utils/dev-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ interface DevServerOptions {
offline?: boolean
prompt?: $FIXME[]
serve?: boolean
skipWaitPort?: boolean
}

// 240 seconds
Expand All @@ -62,14 +63,14 @@ const startServer = async ({
offline = true,
prompt,
serve = false,
skipWaitPort = false,
}: DevServerOptions): Promise<DevServer | { timeout: boolean; output: string }> => {
const port = await getPort()
const staticPort = await getPort()
const host = 'localhost'
const url = `http://${host}:${port}`

console.log(`Starting dev server on port: ${port} in directory ${path.basename(cwd)}`)

const baseCommand = serve ? 'serve' : 'dev'
const baseArgs = [
baseCommand,
Expand All @@ -79,6 +80,7 @@ const startServer = async ({
'--staticServerPort',
staticPort,
debug ? '--debug' : '',
skipWaitPort ? '--skip-wait-port' : '',
]

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

2 comments on commit 4774797

@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

  • Dependency count: 1,210
  • Package size: 317 MB
  • Number of ts-expect-error directives: 830

@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

  • Dependency count: 1,210
  • Package size: 317 MB
  • Number of ts-expect-error directives: 830

Please sign in to comment.