Skip to content

Commit 83a4931

Browse files
authored
feat: handle old next version (#20)
1 parent 8a99754 commit 83a4931

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

Diff for: src/templates/getHandler.js

+13-4
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,24 @@ const { Server } = require('http')
22
const path = require('path')
33

44
const { Bridge } = require('@vercel/node/dist/bridge')
5-
// This path is specific to next@canary. In a live version we'd resolve various versions of next
6-
const NextServer = require('next/dist/server/next-server').default
75

86
const makeHandler =
97
() =>
108
// We return a function and then call `toString()` on it to serialise it as the launcher function
119
(conf, app) => {
10+
let nextServerPath
11+
12+
try {
13+
// next >= 11.0.1. Yay breaking changes in patch releases!
14+
nextServerPath = require.resolve('next/dist/server/next-server')
15+
} catch {
16+
// next < 11.0.1
17+
// eslint-disable-next-line node/no-missing-require
18+
nextServerPath = require.resolve('next/dist/next-server/server/next-server')
19+
}
20+
// eslint-disable-next-line import/no-dynamic-require
21+
const { default: NextServer } = require(nextServerPath)
22+
1223
const nextServer = new NextServer({
1324
conf,
1425
dir: path.resolve(__dirname, app),
@@ -61,8 +72,6 @@ const getHandler = ({ isODB = false, publishDir = '../../../.next', appDir = '..
6172
const { Server } = require("http");
6273
// We copy the file here rather than requiring from the node module
6374
const { Bridge } = require("./bridge");
64-
// Specific to this Next version
65-
const NextServer = require("next/dist/server/next-server").default;
6675
const { builder } = require("@netlify/functions");
6776
const { config } = require("${publishDir}/required-server-files.json")
6877
const path = require("path");

0 commit comments

Comments
 (0)