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: dev server fix ip when skipping watch #6977

Merged
merged 9 commits into from
Jan 7, 2025
Merged
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
Loading