Skip to content

Commit e06157d

Browse files
committed
fix: restore trying to resolve from plugin and not appdir
1 parent 1933824 commit e06157d

File tree

3 files changed

+53
-5
lines changed

3 files changed

+53
-5
lines changed

packages/runtime/src/helpers/files.ts

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ const patchFile = async ({
330330
* The file we need has moved around a bit over the past few versions,
331331
* so we iterate through the options until we find it
332332
*/
333-
export const getServerFile = (root: string, includeBase = true) => {
333+
const getServerFile = (root: string, includeBase = true) => {
334334
const candidates = ['next/dist/server/next-server', 'next/dist/next-server/server/next-server']
335335

336336
if (includeBase) {
@@ -340,6 +340,54 @@ export const getServerFile = (root: string, includeBase = true) => {
340340
return findModuleFromBase({ candidates, paths: [root] })
341341
}
342342

343+
/**
344+
* Try to find next-server module in few locations (to support different next versions) and in few context (try to resolve from app location and from this module)
345+
*/
346+
export const getNextServerModulePath = (root: string): string | null => {
347+
// first let's try to use app location directory to find next-server
348+
try {
349+
const nextServerModuleLocation = getServerFile(root, false)
350+
if (nextServerModuleLocation) {
351+
return nextServerModuleLocation
352+
}
353+
} catch (error) {
354+
if (!error.message.includes('Cannot find module')) {
355+
// A different error, so rethrow it
356+
throw error
357+
}
358+
}
359+
360+
// if we didn't find it, let's try to resolve "next" package from this module
361+
try {
362+
// next >= 11.0.1. Yay breaking changes in patch releases!
363+
const nextServerModuleLocation = require.resolve('next/dist/server/next-server')
364+
if (nextServerModuleLocation) {
365+
return nextServerModuleLocation
366+
}
367+
} catch (error) {
368+
if (!error.message.includes("Cannot find module 'next/dist/server/next-server'")) {
369+
// A different error, so rethrow it
370+
throw error
371+
}
372+
// Probably an old version of next, so fall through and find it elsewhere.
373+
}
374+
375+
try {
376+
// next < 11.0.1
377+
// eslint-disable-next-line n/no-missing-require
378+
const nextServerModuleLocation = require.resolve('next/dist/next-server/server/next-server')
379+
if (nextServerModuleLocation) {
380+
return nextServerModuleLocation
381+
}
382+
} catch (error) {
383+
if (!error.message.includes("Cannot find module 'next/dist/next-server/server/next-server'")) {
384+
throw error
385+
}
386+
}
387+
388+
return null
389+
}
390+
343391
/**
344392
* Find the source file for a given page route
345393
*/

packages/runtime/src/templates/getApiHandler.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { outdent as javascript } from 'outdent'
55

66
import { ApiConfig, ApiRouteType } from '../helpers/analysis'
77
import type { NextConfig } from '../helpers/config'
8-
import { getServerFile } from '../helpers/files'
8+
import { getNextServerModulePath } from '../helpers/files'
99

1010
import type { NextServerType } from './handlerUtils'
1111

@@ -126,7 +126,7 @@ export const getApiHandler = ({
126126
appDir?: string
127127
appDirAbsolute: string
128128
}): string => {
129-
const nextServerModuleLocation = getServerFile(appDirAbsolute, false)
129+
const nextServerModuleLocation = getNextServerModulePath(appDirAbsolute)
130130

131131
// This is a string, but if you have the right editor plugin it should format as js
132132
return javascript/* javascript */ `

packages/runtime/src/templates/getHandler.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { Bridge as NodeBridge } from '@vercel/node-bridge/bridge'
44
import { outdent as javascript } from 'outdent'
55

66
import type { NextConfig } from '../helpers/config'
7-
import { getServerFile } from '../helpers/files'
7+
import { getNextServerModulePath } from '../helpers/files'
88

99
import { NextServerType } from './handlerUtils'
1010
import type { NetlifyNextServerType } from './server'
@@ -197,7 +197,7 @@ export const getHandler = ({
197197
appDir = '../../..',
198198
appDirAbsolute = process.cwd(),
199199
}): string => {
200-
const nextServerModuleLocation = getServerFile(appDirAbsolute, false)
200+
const nextServerModuleLocation = getNextServerModulePath(appDirAbsolute)
201201

202202
// This is a string, but if you have the right editor plugin it should format as js
203203
return javascript/* javascript */ `

0 commit comments

Comments
 (0)