From ddaa498943eaf87ebc8ab4ba9239b2a72711633a Mon Sep 17 00:00:00 2001 From: Steven Date: Wed, 5 Apr 2023 17:32:10 -0400 Subject: [PATCH 1/2] fix: show error when "next start" is used with "output" config --- docs/api-reference/cli.md | 6 ++++-- packages/next/src/server/next.ts | 11 +++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/docs/api-reference/cli.md b/docs/api-reference/cli.md index 208d7c9383b0a..cb40bb937a766 100644 --- a/docs/api-reference/cli.md +++ b/docs/api-reference/cli.md @@ -82,7 +82,7 @@ Or using the `PORT` environment variable: PORT=4000 npx next dev ``` -> Note: `PORT` can not be set in `.env` as booting up the HTTP server happens before any other code is initialized. +> Note: `PORT` cannot be set in `.env` as booting up the HTTP server happens before any other code is initialized. You can also set the hostname to be different from the default of `0.0.0.0`, this can be useful for making the application available for other devices on the network. The default hostname can be changed with `-H`, like so: @@ -106,7 +106,9 @@ Or using the `PORT` environment variable: PORT=4000 npx next start ``` -> Note: `PORT` can not be set in `.env` as booting up the HTTP server happens before any other code is initialized. +> Note: `PORT` cannot be set in `.env` as booting up the HTTP server happens before any other code is initialized. + +> Note: `next start` cannot be used with `output: 'standalone'` or `output: 'export'`. ### Keep Alive Timeout diff --git a/packages/next/src/server/next.ts b/packages/next/src/server/next.ts index d3c0830a72304..44c91e0d5a195 100644 --- a/packages/next/src/server/next.ts +++ b/packages/next/src/server/next.ts @@ -164,6 +164,17 @@ export class NextServer { private async getServer() { if (!this.serverPromise) { this.serverPromise = this.loadConfig().then(async (conf) => { + if (!this.options.dev) { + if (conf.output === 'standalone') { + log.warn( + `"next start" does not work with "output: standalone" configuration. Use "node .next/standalone/server.js" instead.` + ) + } else if (conf.output === 'export') { + throw new Error( + `"next start" does not work with "output: export" configuration. Use "npx serve@latest out" instead.` + ) + } + } if (conf.experimental.appDir) { const useExperimentalReact = !!conf.experimental.experimentalReact process.env.NEXT_PREBUNDLED_REACT = useExperimentalReact From b57ef9f318c10f9cf7164ef843fb8d44450da161 Mon Sep 17 00:00:00 2001 From: Steven Date: Thu, 6 Apr 2023 17:54:29 -0400 Subject: [PATCH 2/2] Add tests --- .../app-dir-export/test/index.test.ts | 2 +- .../app-dir-export/test/start.test.ts | 60 +++++++++++++++++++ 2 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 test/integration/app-dir-export/test/start.test.ts diff --git a/test/integration/app-dir-export/test/index.test.ts b/test/integration/app-dir-export/test/index.test.ts index 05c7274ce975c..ccf4606983909 100644 --- a/test/integration/app-dir-export/test/index.test.ts +++ b/test/integration/app-dir-export/test/index.test.ts @@ -201,7 +201,7 @@ async function runTests({ } } -describe('app dir with output export', () => { +describe('app dir with output export (next dev / next build)', () => { it.each([ { isDev: true, trailingSlash: false }, { isDev: true, trailingSlash: true }, diff --git a/test/integration/app-dir-export/test/start.test.ts b/test/integration/app-dir-export/test/start.test.ts new file mode 100644 index 0000000000000..6f4aa4870c0c3 --- /dev/null +++ b/test/integration/app-dir-export/test/start.test.ts @@ -0,0 +1,60 @@ +/* eslint-env jest */ + +import { join } from 'path' +import fs from 'fs-extra' +import { + check, + File, + findPort, + killApp, + nextBuild, + nextStart, +} from 'next-test-utils' + +const appDir = join(__dirname, '..') +const distDir = join(appDir, '.next') +const exportDir = join(appDir, 'out') +const nextConfig = new File(join(appDir, 'next.config.js')) +let app + +describe('app dir with output export (next start)', () => { + afterEach(async () => { + await killApp(app) + nextConfig.restore() + await fs.remove(distDir) + await fs.remove(exportDir) + }) + + it('should error during next start with output export', async () => { + const { code } = await nextBuild(appDir) + expect(code).toBe(0) + const port = await findPort() + let stderr = '' + app = await nextStart(appDir, port, { + onStderr(msg: string) { + stderr += msg || '' + }, + }) + await check(() => stderr, /error/i) + expect(stderr).toContain( + '"next start" does not work with "output: export" configuration. Use "npx serve@latest out" instead.' + ) + }) + + it('should warn during next start with output standalone', async () => { + nextConfig.replace(`output: 'export'`, `output: 'standalone'`) + const { code } = await nextBuild(appDir) + expect(code).toBe(0) + const port = await findPort() + let stderr = '' + app = await nextStart(appDir, port, { + onStderr(msg: string) { + stderr += msg || '' + }, + }) + await check(() => stderr, /warn/i) + expect(stderr).toContain( + `warn - "next start" does not work with "output: standalone" configuration. Use "node .next/standalone/server.js" instead.` + ) + }) +})