Skip to content

Commit ec9698c

Browse files
committed
fix: use requireHooks based on next -v and appDir
1 parent 5f08690 commit ec9698c

File tree

3 files changed

+36
-6
lines changed

3 files changed

+36
-6
lines changed

packages/runtime/src/templates/getHandler.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ const {
2424
getMultiValueHeaders,
2525
getPrefetchResponse,
2626
normalizePath,
27+
nextVersionNum,
2728
} = require('./handlerUtils')
2829
const { overrideRequireHooks, applyRequireHooks } = require('./requireHooks')
2930
const { getNetlifyNextServer } = require('./server')
@@ -61,10 +62,15 @@ const makeHandler = ({ conf, app, pageRoot, NextServer, staticManifest = [], mod
6162
const { appDir }: ExperimentalConfigWithLegacy = conf.experimental
6263
// Next 13.4 conditionally uses different React versions and we need to make sure we use the same one
6364
// With the release of 13.5 experimental.appDir is no longer used.
64-
// we will need to check if appDir exists to run requireHooks for older versions
65-
if (appDir) overrideRequireHooks(conf.experimental)
65+
// we will need to check if appDir is set and Next version before running requireHooks
66+
const runRequireHooks = async (hook) =>
67+
await nextVersionNum()
68+
.then((version) => (appDir && version ? hook : null))
69+
.catch(() => ({}))
70+
71+
runRequireHooks(overrideRequireHooks(conf.experimental))
6672
const NetlifyNextServer: NetlifyNextServerType = getNetlifyNextServer(NextServer)
67-
if (appDir) applyRequireHooks()
73+
runRequireHooks(applyRequireHooks())
6874

6975
const ONE_YEAR_IN_SECONDS = 31536000
7076

@@ -222,7 +228,7 @@ export const getHandler = ({
222228
const { promises } = require("fs");
223229
// We copy the file here rather than requiring from the node module
224230
const { Bridge } = require("./bridge");
225-
const { augmentFsModule, getMaxAge, getMultiValueHeaders, getPrefetchResponse, normalizePath } = require('./handlerUtils')
231+
const { augmentFsModule, getMaxAge, getMultiValueHeaders, getPrefetchResponse, normalizePath, nextVersionNum } = require('./handlerUtils')
226232
const { overrideRequireHooks, applyRequireHooks } = require("./requireHooks")
227233
const { getNetlifyNextServer } = require("./server")
228234
const NextServer = require(${JSON.stringify(nextServerModuleRelativeLocation)}).default

packages/runtime/src/templates/handlerUtils.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import fs, { createWriteStream, existsSync } from 'fs'
22
import { ServerResponse } from 'http'
33
import { tmpdir } from 'os'
4-
import path from 'path'
4+
import path, { dirname, join, relative } from 'path'
55
import { pipeline } from 'stream'
66
import { promisify } from 'util'
77

88
import { HandlerEvent, HandlerResponse } from '@netlify/functions'
99
import { http, https } from 'follow-redirects'
10+
import { readJSON } from 'fs-extra'
1011
import NextNodeServer from 'next/dist/server/next-server'
12+
import { satisfies } from 'semver'
1113

1214
import type { StaticRoute } from '../helpers/types'
1315

@@ -274,6 +276,25 @@ export const localizeDataRoute = (dataRoute: string, localizedRoute: string): st
274276
.replace(/\/index\.json$/, '.json')
275277
}
276278

279+
export const resolveModuleRoot = (moduleName) => {
280+
try {
281+
return dirname(relative(process.cwd(), require.resolve(`${moduleName}/package.json`, { paths: [process.cwd()] })))
282+
} catch {
283+
return null
284+
}
285+
}
286+
// Had to copy nextPluginVersion logic from functionsMetaData for it to work within server.ts and getHandler.ts
287+
const nextPluginVersion = async (module: string) => {
288+
const moduleRoot = resolveModuleRoot(module)
289+
if (!existsSync(moduleRoot)) {
290+
return
291+
}
292+
const packagePlugin = await readJSON(join(moduleRoot, 'package.json'))
293+
return packagePlugin?.version
294+
}
295+
296+
export const nextVersionNum = async () => satisfies(await nextPluginVersion('next'), '13.3.3 - 13.4.9')
297+
277298
export const getMatchedRoute = (
278299
paths: string,
279300
routesManifest: Array<StaticRoute>,

packages/runtime/src/templates/server.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
localizeDataRoute,
1818
unlocalizeRoute,
1919
getMatchedRoute,
20+
nextVersionNum,
2021
} from './handlerUtils'
2122

2223
interface NetlifyConfig {
@@ -64,8 +65,10 @@ const getNetlifyNextServer = (NextServer: NextServerType) => {
6465
const { url, headers } = req
6566

6667
// conditionally use the prebundled React module
68+
// PrebundledReact should only apply when appDir is set it falls between the specified Next versions
6769
const { experimental }: NextConfigWithAppDir = this.nextConfig
68-
if (experimental?.appDir) this.netlifyPrebundleReact(url, this.nextConfig, parsedUrl)
70+
const version = await nextVersionNum()
71+
if (experimental?.appDir && version) this.netlifyPrebundleReact(url, this.nextConfig, parsedUrl)
6972

7073
// intercept on-demand revalidation requests and handle with the Netlify API
7174
if (headers['x-prerender-revalidate'] && this.netlifyConfig.revalidateToken) {

0 commit comments

Comments
 (0)