From d2ac04cd54df00ab3cd84f1b03bfc358ee59be4b Mon Sep 17 00:00:00 2001 From: TodorTotev <51530311+TodorTotev@users.noreply.github.com> Date: Fri, 29 May 2020 11:52:28 +0300 Subject: [PATCH 1/5] added checks and error message --- errors/invalid-server-options.md | 25 +++++++++++++++++++++++++ packages/next/server/next.ts | 13 +++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 errors/invalid-server-options.md diff --git a/errors/invalid-server-options.md b/errors/invalid-server-options.md new file mode 100644 index 0000000000000..b688e53942e8a --- /dev/null +++ b/errors/invalid-server-options.md @@ -0,0 +1,25 @@ +# It looks like the next instance is being instantiated incorrectly. + +#### Why This Error Occurred + +You have passed a null or undefined parameter to the next() call. + +#### Possible Ways to Fix It + +Make sure you are passing the variables properly: + +```js +const app = next() +``` + +And make sure you're passing dev as shown in the examples below: + +```js +const app = next({ dev: boolean }) +``` + +### Useful Links + +- [custom-server-express](https://github.com/vercel/next.js/blob/6ca00bfe312c8d3fc5c20d25a9cd8d2741a29332/examples/custom-server-express/server.js#L6) +- [custom-server](https://github.com/vercel/next.js/blob/6ca00bfe312c8d3fc5c20d25a9cd8d2741a29332/examples/custom-server/server.js#L6) +- [custom-server-typescript](https://github.com/vercel/next.js/blob/6ca00bfe312c8d3fc5c20d25a9cd8d2741a29332/examples/custom-server-typescript/server/index.ts#L7) diff --git a/packages/next/server/next.ts b/packages/next/server/next.ts index b054d445040ec..0700f42ad75b0 100644 --- a/packages/next/server/next.ts +++ b/packages/next/server/next.ts @@ -13,6 +13,12 @@ type NextServerConstructor = Omit & { function createServer(options: NextServerConstructor): Server { const standardEnv = ['production', 'development', 'test'] + if (options == null) { + throw new Error( + 'The server has not been instantiated properly. https://err.sh/next.js/invalid-server-options' + ) + } + if ( !(options as any).isNextDevCommand && process.env.NODE_ENV && @@ -22,6 +28,13 @@ function createServer(options: NextServerConstructor): Server { } if (options.dev) { + // This is optional and not related to the issue, but I believe this PR can introduce a handy warning if the variable is not boolean + if (typeof options.dev !== 'boolean') { + console.warn( + "Warning: 'dev' is not a boolean which could introduce unexpected behavior. https://err.sh/next.js/invalid-server-options" + ) + } + const Server = require('./next-dev-server').default return new Server(options) } From f7573eae27daf8673435913e94409c7477dc463e Mon Sep 17 00:00:00 2001 From: Tim Neutkens Date: Fri, 29 May 2020 14:04:49 +0200 Subject: [PATCH 2/5] Update packages/next/server/next.ts --- packages/next/server/next.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/next/server/next.ts b/packages/next/server/next.ts index 0700f42ad75b0..79f313e4bd088 100644 --- a/packages/next/server/next.ts +++ b/packages/next/server/next.ts @@ -13,7 +13,7 @@ type NextServerConstructor = Omit & { function createServer(options: NextServerConstructor): Server { const standardEnv = ['production', 'development', 'test'] - if (options == null) { + if (typeof options === 'undefined') { throw new Error( 'The server has not been instantiated properly. https://err.sh/next.js/invalid-server-options' ) From c923f8bc7b96249e118c992f3a6db57e8135e364 Mon Sep 17 00:00:00 2001 From: Tim Neutkens Date: Fri, 29 May 2020 14:07:00 +0200 Subject: [PATCH 3/5] Update packages/next/server/next.ts --- packages/next/server/next.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/next/server/next.ts b/packages/next/server/next.ts index 79f313e4bd088..b60d568b1484c 100644 --- a/packages/next/server/next.ts +++ b/packages/next/server/next.ts @@ -28,7 +28,6 @@ function createServer(options: NextServerConstructor): Server { } if (options.dev) { - // This is optional and not related to the issue, but I believe this PR can introduce a handy warning if the variable is not boolean if (typeof options.dev !== 'boolean') { console.warn( "Warning: 'dev' is not a boolean which could introduce unexpected behavior. https://err.sh/next.js/invalid-server-options" From 004fa8b5654e141aefbe5b2b95e8b1c1b85a301c Mon Sep 17 00:00:00 2001 From: TodorTotev <51530311+TodorTotev@users.noreply.github.com> Date: Sat, 30 May 2020 09:17:39 +0300 Subject: [PATCH 4/5] added tests --- .../invalid-server-options/pages/index.js | 1 + .../invalid-server-options/test/index.test.js | 59 +++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 test/integration/invalid-server-options/pages/index.js create mode 100644 test/integration/invalid-server-options/test/index.test.js diff --git a/test/integration/invalid-server-options/pages/index.js b/test/integration/invalid-server-options/pages/index.js new file mode 100644 index 0000000000000..f40492ac06d85 --- /dev/null +++ b/test/integration/invalid-server-options/pages/index.js @@ -0,0 +1 @@ +export default () => 'test' diff --git a/test/integration/invalid-server-options/test/index.test.js b/test/integration/invalid-server-options/test/index.test.js new file mode 100644 index 0000000000000..aae3675faeb95 --- /dev/null +++ b/test/integration/invalid-server-options/test/index.test.js @@ -0,0 +1,59 @@ +import next from 'next' +import { join } from 'path' +const dir = join(__dirname, '../') +const warningMessage = + "Warning: 'dev' is not a boolean which could introduce unexpected behavior. https://err.sh/next.js/invalid-server-options" + +describe('Invalid server options', () => { + test('next() called with no parameters should throw error', () => { + expect(() => next()).toThrowError( + 'The server has not been instantiated properly. https://err.sh/next.js/invalid-server-options' + ) + }) + + test('next() called with undefined parameters should throw error', () => { + expect(() => next(undefined)).toThrowError( + 'The server has not been instantiated properly. https://err.sh/next.js/invalid-server-options' + ) + }) + + test('next() called with dev as string should send warning', () => { + const consoleSpy = jest.spyOn(console, 'warn') + const dev = 'string' + next({ dev, dir }) + + expect(consoleSpy).toHaveBeenCalledWith(warningMessage) + }) + + test('next() called with dev as number should send warning', () => { + const consoleSpy = jest.spyOn(console, 'warn') + const dev = 123 + next({ dev, dir }) + + expect(consoleSpy).toHaveBeenCalledWith(warningMessage) + }) + + test('next() called with dev as array should send warning', () => { + const consoleSpy = jest.spyOn(console, 'warn') + const dev = ['array'] + next({ dev, dir }) + + expect(consoleSpy).toHaveBeenCalledWith(warningMessage) + }) + + test('next() called with dev as object should send warning', () => { + const consoleSpy = jest.spyOn(console, 'warn') + const dev = { test: 'goes here' } + next({ dev, dir }) + + expect(consoleSpy).toHaveBeenCalledWith(warningMessage) + }) + + test('next() called with dev as function should send warning', () => { + const consoleSpy = jest.spyOn(console, 'warn') + const dev = () => console.log('test') + next({ dev, dir }) + + expect(consoleSpy).toHaveBeenCalledWith(warningMessage) + }) +}) From f9104826b0244ef1293c312ba0600f235f79653e Mon Sep 17 00:00:00 2001 From: Joe Haddad Date: Tue, 2 Jun 2020 22:46:34 -0400 Subject: [PATCH 5/5] Apply suggestions from code review --- packages/next/server/next.ts | 2 +- .../integration/invalid-server-options/test/index.test.js | 8 +++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/packages/next/server/next.ts b/packages/next/server/next.ts index 24cc4472c741d..6b17c6496c85c 100644 --- a/packages/next/server/next.ts +++ b/packages/next/server/next.ts @@ -13,7 +13,7 @@ type NextServerConstructor = Omit & { function createServer(options: NextServerConstructor): Server { const standardEnv = ['production', 'development', 'test'] - if (typeof options === 'undefined') { + if (options == null) { throw new Error( 'The server has not been instantiated properly. https://err.sh/next.js/invalid-server-options' ) diff --git a/test/integration/invalid-server-options/test/index.test.js b/test/integration/invalid-server-options/test/index.test.js index aae3675faeb95..9137fffdb31db 100644 --- a/test/integration/invalid-server-options/test/index.test.js +++ b/test/integration/invalid-server-options/test/index.test.js @@ -11,12 +11,18 @@ describe('Invalid server options', () => { ) }) - test('next() called with undefined parameters should throw error', () => { + test('next() called with undefined parameter should throw error', () => { expect(() => next(undefined)).toThrowError( 'The server has not been instantiated properly. https://err.sh/next.js/invalid-server-options' ) }) + test('next() called with null parameter should throw error', () => { + expect(() => next(null)).toThrowError( + 'The server has not been instantiated properly. https://err.sh/next.js/invalid-server-options' + ) + }) + test('next() called with dev as string should send warning', () => { const consoleSpy = jest.spyOn(console, 'warn') const dev = 'string'