forked from graphile/starter
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add explicit system for handling 'upgrade' events (graphile#354)
- Loading branch information
Showing
5 changed files
with
137 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,33 @@ | ||
// TODO: fix to 'import next' when next fixes the bug | ||
import { Express } from "express"; | ||
import { createServer } from "http"; | ||
import next from "next"; | ||
import { parse } from "url"; | ||
|
||
import { getUpgradeHandlers } from "../app"; | ||
|
||
if (!process.env.NODE_ENV) { | ||
throw new Error("No NODE_ENV envvar! Try `export NODE_ENV=development`"); | ||
} | ||
|
||
const isDev = process.env.NODE_ENV === "development"; | ||
|
||
export default async function installSSR(app: Express) { | ||
// @ts-ignore Next had a bad typing file, they claim `export default` but should have `export =` | ||
// Ref: https://unpkg.com/[email protected]/dist/server/next.js | ||
const fakeHttpServer = createServer(); | ||
const nextApp = next({ | ||
dev: isDev, | ||
dir: `${__dirname}/../../../client/src`, | ||
quiet: !isDev, | ||
// Don't specify 'conf' key | ||
|
||
// Trick Next.js into adding its upgrade handler here, so we can extract | ||
// it. Calling `getUpgradeHandler()` is insufficient because that doesn't | ||
// handle the assets. | ||
httpServer: fakeHttpServer, | ||
}); | ||
const handlerPromise = (async () => { | ||
await nextApp.prepare(); | ||
return nextApp.getRequestHandler(); | ||
})(); | ||
// Foo | ||
handlerPromise.catch((e) => { | ||
console.error("Error occurred starting Next.js; aborting process"); | ||
console.error(e); | ||
|
@@ -42,4 +47,24 @@ export default async function installSSR(app: Express) { | |
}, | ||
}); | ||
}); | ||
|
||
// Now handle websockets | ||
if (!(nextApp as any).getServer) { | ||
console.warn( | ||
`Our Next.js workaround for getting the upgrade handler without giving Next.js dominion over all websockets might no longer work - nextApp.getServer (private API) is no more.` | ||
); | ||
} else { | ||
await (nextApp as any).getServer(); | ||
} | ||
const nextJsUpgradeHandler = fakeHttpServer.listeners("upgrade")[0] as any; | ||
if (nextJsUpgradeHandler) { | ||
const upgradeHandlers = getUpgradeHandlers(app); | ||
upgradeHandlers.push({ | ||
name: "Next.js", | ||
check(req) { | ||
return req.url?.includes("/_next/") ?? false; | ||
}, | ||
upgrade: nextJsUpgradeHandler, | ||
}); | ||
} | ||
} |