From 3571c27da93c727783581867e1c8d5669206efd5 Mon Sep 17 00:00:00 2001 From: Apteryx Date: Sun, 5 Nov 2023 16:07:08 +1300 Subject: [PATCH] Reorder sections of `useHttpServer` function --- packages/core/package.json | 2 +- packages/core/src/server/utilities/server.ts | 11 ++++++----- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/packages/core/package.json b/packages/core/package.json index 7eb1830..d3290ee 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -1,6 +1,6 @@ { "name": "next-ws", - "version": "1.0.1-canary.1", + "version": "1.0.1-experimental.1", "description": "Add support for WebSockets in Next.js 13 app directory", "keywords": [ "next", diff --git a/packages/core/src/server/utilities/server.ts b/packages/core/src/server/utilities/server.ts index d056911..598e442 100644 --- a/packages/core/src/server/utilities/server.ts +++ b/packages/core/src/server/utilities/server.ts @@ -13,6 +13,12 @@ export const CustomHttpServer = Symbol('NextWS::CustomHttpServer'); * @returns The HTTP Server instance. */ export function useHttpServer(nextServer: NextNodeServer) { + // NextNodeServer is always required, so check for it before attempting to use custom server + if (!nextServer || !(nextServer instanceof NextNodeServer)) { + Log.error('[next-ws] could not find the NextNodeServer instance'); + process.exit(1); + } + const existing = Reflect.get(globalThis, CustomHttpServer) as Server; if (existing) { Log.warnOnce( @@ -21,11 +27,6 @@ export function useHttpServer(nextServer: NextNodeServer) { return existing; } - if (!nextServer || !(nextServer instanceof NextNodeServer)) { - Log.error('[next-ws] could not find the NextNodeServer instance'); - process.exit(1); - } - // @ts-expect-error - serverOptions is protected const httpServer = nextServer.serverOptions?.httpServer; if (!httpServer || !(httpServer instanceof Server)) {