Skip to content

Commit 0043411

Browse files
Fixed Host Issue (#273)
1 parent ee79b7a commit 0043411

11 files changed

Lines changed: 84 additions & 21 deletions

File tree

packages/stats-generator/src/clientSideRendered/index.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ import { spawn } from 'node:child_process'
22
import { join } from 'node:path'
33
import { fileURLToPath } from 'node:url'
44
import { packagesDir } from '../constants.ts'
5+
import { getHost } from '../serve/common.ts'
56
import { runBenchmark } from './run-benchmark.ts'
67
import type { ClientSideRenderedBenchmarkResult } from './types.ts'
78

9+
const CLIENT_SIDE_RENDERED_HOST = getHost()
810
const CLIENT_SIDE_RENDERED_PORT = 3001
911

1012
interface ClientSideRenderedFrameworkConfig {
@@ -85,6 +87,7 @@ async function spawnServer(
8587
const proc = spawn('node', [scriptPath, appDir], {
8688
env: {
8789
...process.env,
90+
HOST: CLIENT_SIDE_RENDERED_HOST,
8891
NODE_ENV: 'production',
8992
PORT: String(CLIENT_SIDE_RENDERED_PORT),
9093
},
@@ -116,7 +119,7 @@ async function spawnServer(
116119

117120
await Promise.race([
118121
waitForServer(
119-
`http://localhost:${CLIENT_SIDE_RENDERED_PORT}/client-side-rendered`,
122+
`http://${CLIENT_SIDE_RENDERED_HOST}:${CLIENT_SIDE_RENDERED_PORT}/client-side-rendered`,
120123
),
121124
exitPromise,
122125
])
@@ -148,7 +151,7 @@ export async function runClientSideRenderedBenchmark(
148151
`Running client-side rendered benchmark for ${config.displayName}...`,
149152
)
150153
return await runBenchmark(
151-
`http://localhost:${CLIENT_SIDE_RENDERED_PORT}`,
154+
`http://${CLIENT_SIDE_RENDERED_HOST}:${CLIENT_SIDE_RENDERED_PORT}`,
152155
config.name,
153156
config.displayName,
154157
runs,
Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
import { join } from 'node:path'
2-
import { getPort, parseAppDir, spawnProductionServer } from './common.ts'
2+
import {
3+
getHost,
4+
getPort,
5+
parseAppDir,
6+
spawnProductionServer,
7+
} from './common.ts'
38

49
const appDir = parseAppDir()
10+
const HOST = getHost()
511
const PORT = getPort(4321)
612
const entryPath = join(appDir, 'dist', 'server', 'entry.mjs')
713

8-
spawnProductionServer([entryPath], appDir, { PORT: String(PORT) })
14+
spawnProductionServer([entryPath], appDir, {
15+
HOST,
16+
PORT: String(PORT),
17+
})
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { createServer } from 'node:http'
22
import { testData } from '../../../testdata/src/ssr.ts'
33
import { renderBaselineHtml } from '../baseline-html.ts'
4-
import { getPort, registerShutdown } from './common.ts'
4+
import { getHost, getPort, registerShutdown } from './common.ts'
55

6+
const HOST = getHost()
67
const PORT = getPort()
78

89
const server = createServer(async (_req, res) => {
@@ -11,8 +12,8 @@ const server = createServer(async (_req, res) => {
1112

1213
res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' })
1314
res.end(html)
14-
}).listen(PORT, () => {
15-
console.log(`Ready at http://localhost:${PORT}`)
15+
}).listen(PORT, HOST, () => {
16+
console.log(`Ready at http://${HOST}:${PORT}`)
1617
})
1718

1819
registerShutdown(server)

packages/stats-generator/src/serve/common.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ export function getPort(defaultPort = 3000): number {
66
return Number.parseInt(process.env.PORT ?? String(defaultPort), 10)
77
}
88

9+
export function getHost(defaultHost = '127.0.0.1'): string {
10+
return process.env.HOST ?? defaultHost
11+
}
12+
913
export function parseAppDir(): string {
1014
const appDir = process.argv[2]
1115
if (!appDir) {
Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
import { createRequire } from 'node:module'
22
import { join } from 'node:path'
3-
import { getPort, parseAppDir, spawnProductionServer } from './common.ts'
3+
import {
4+
getHost,
5+
getPort,
6+
parseAppDir,
7+
spawnProductionServer,
8+
} from './common.ts'
49

510
const appDir = parseAppDir()
11+
const HOST = getHost()
612
const PORT = getPort()
713

814
const appRequire = createRequire(join(appDir, 'package.json'))
915
const nextBinPath = appRequire.resolve('next/dist/bin/next')
1016

1117
spawnProductionServer(
12-
[nextBinPath, 'start', '--port', String(PORT), '--hostname', 'localhost'],
18+
[nextBinPath, 'start', '--port', String(PORT), '--hostname', HOST],
1319
appDir,
14-
{ PORT: String(PORT) },
20+
{ HOST, PORT: String(PORT) },
1521
)
Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
import { join } from 'node:path'
2-
import { getPort, parseAppDir, spawnProductionServer } from './common.ts'
2+
import {
3+
getHost,
4+
getPort,
5+
parseAppDir,
6+
spawnProductionServer,
7+
} from './common.ts'
38

49
const appDir = parseAppDir()
10+
const HOST = getHost()
511
const PORT = getPort()
612
const entryPath = join(appDir, '.output', 'server', 'index.mjs')
713

8-
spawnProductionServer([entryPath], appDir, { PORT: String(PORT) })
14+
spawnProductionServer([entryPath], appDir, {
15+
HOST,
16+
PORT: String(PORT),
17+
})
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
import { createRequire } from 'node:module'
22
import { dirname, join } from 'node:path'
3-
import { getPort, parseAppDir, spawnProductionServer } from './common.ts'
3+
import {
4+
getHost,
5+
getPort,
6+
parseAppDir,
7+
spawnProductionServer,
8+
} from './common.ts'
49

510
const appDir = parseAppDir()
11+
const HOST = getHost()
612
const PORT = getPort()
713
const buildPath = join(appDir, 'build', 'server', 'index.js')
814

@@ -11,5 +17,6 @@ const servePackagePath = appRequire.resolve('@react-router/serve/package.json')
1117
const serveBinPath = join(dirname(servePackagePath), 'bin.js')
1218

1319
spawnProductionServer([serveBinPath, buildPath], appDir, {
20+
HOST,
1421
PORT: String(PORT),
1522
})
Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
import { join } from 'node:path'
2-
import { getPort, parseAppDir, spawnProductionServer } from './common.ts'
2+
import {
3+
getHost,
4+
getPort,
5+
parseAppDir,
6+
spawnProductionServer,
7+
} from './common.ts'
38

49
const appDir = parseAppDir()
10+
const HOST = getHost()
511
const PORT = getPort()
612
const entryPath = join(appDir, 'build', 'index.js')
713

8-
spawnProductionServer([entryPath], appDir, { PORT: String(PORT) })
14+
spawnProductionServer([entryPath], appDir, {
15+
HOST,
16+
PORT: String(PORT),
17+
})
Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
import { join } from 'node:path'
2-
import { getPort, parseAppDir, spawnProductionServer } from './common.ts'
2+
import {
3+
getHost,
4+
getPort,
5+
parseAppDir,
6+
spawnProductionServer,
7+
} from './common.ts'
38

49
const appDir = parseAppDir()
10+
const HOST = getHost()
511
const PORT = getPort()
612
const entryPath = join(appDir, '.output', 'server', 'index.mjs')
713

8-
spawnProductionServer([entryPath], appDir, { PORT: String(PORT) })
14+
spawnProductionServer([entryPath], appDir, {
15+
HOST,
16+
PORT: String(PORT),
17+
})

packages/stats-generator/src/serverSideRendered/index.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ import { spawn } from 'node:child_process'
22
import { join } from 'node:path'
33
import { fileURLToPath } from 'node:url'
44
import { packagesDir } from '../constants.ts'
5+
import { getHost } from '../serve/common.ts'
56
import { runBenchmark } from './run-benchmark.ts'
67
import type { ServerSideRenderedBenchmarkResult } from './types.ts'
78

9+
const SERVER_SIDE_RENDERED_HOST = getHost()
810
const SERVER_SIDE_RENDERED_PORT = 3002
911

1012
interface ServerSideRenderedFrameworkConfig {
@@ -84,6 +86,7 @@ async function spawnServer(
8486
const proc = spawn('node', [scriptPath, appDir], {
8587
env: {
8688
...process.env,
89+
HOST: SERVER_SIDE_RENDERED_HOST,
8790
NODE_ENV: 'production',
8891
PORT: String(SERVER_SIDE_RENDERED_PORT),
8992
},
@@ -115,7 +118,7 @@ async function spawnServer(
115118

116119
await Promise.race([
117120
waitForServer(
118-
`http://localhost:${SERVER_SIDE_RENDERED_PORT}/server-side-rendered`,
121+
`http://${SERVER_SIDE_RENDERED_HOST}:${SERVER_SIDE_RENDERED_PORT}/server-side-rendered`,
119122
),
120123
exitPromise,
121124
])
@@ -147,7 +150,7 @@ export async function runServerSideRenderedBenchmark(
147150
`Running server side rendered benchmark for ${config.displayName}...`,
148151
)
149152
return await runBenchmark(
150-
`http://localhost:${SERVER_SIDE_RENDERED_PORT}`,
153+
`http://${SERVER_SIDE_RENDERED_HOST}:${SERVER_SIDE_RENDERED_PORT}`,
151154
config.name,
152155
config.displayName,
153156
runs,

0 commit comments

Comments
 (0)