1
1
// eslint-disable-next-line n/no-deprecated-api -- this is what Next.js uses as well
2
2
import { parse } from 'url'
3
3
4
+ import { NextConfig } from 'next'
4
5
import type { PrerenderManifest } from 'next/dist/build'
5
6
import type { BaseNextResponse } from 'next/dist/server/base-http'
6
7
import type { NodeRequestHandler , Options } from 'next/dist/server/next-server'
@@ -12,6 +13,7 @@ import {
12
13
localizeRoute ,
13
14
localizeDataRoute ,
14
15
unlocalizeRoute ,
16
+ getMatchedRoute ,
15
17
} from './handlerUtils'
16
18
17
19
interface NetlifyConfig {
@@ -24,6 +26,10 @@ const getNetlifyNextServer = (NextServer: NextServerType) => {
24
26
private netlifyConfig : NetlifyConfig
25
27
private netlifyPrerenderManifest : PrerenderManifest
26
28
29
+ public getAppRouterReactVersion ( ) : string {
30
+ return this . nextConfig . experimental ?. serverActions ? 'experimental' : 'next'
31
+ }
32
+
27
33
public constructor ( options : Options , netlifyConfig : NetlifyConfig ) {
28
34
super ( options )
29
35
this . netlifyConfig = netlifyConfig
@@ -47,7 +53,7 @@ const getNetlifyNextServer = (NextServer: NextServerType) => {
47
53
const { url, headers } = req
48
54
49
55
// conditionally use the prebundled React module
50
- this . netlifyPrebundleReact ( url )
56
+ this . netlifyPrebundleReact ( url , this . nextConfig , parsedUrl )
51
57
52
58
// intercept on-demand revalidation requests and handle with the Netlify API
53
59
if ( headers [ 'x-prerender-revalidate' ] && this . netlifyConfig . revalidateToken ) {
@@ -76,24 +82,20 @@ const getNetlifyNextServer = (NextServer: NextServerType) => {
76
82
}
77
83
78
84
// doing what they do in https://github.com/vercel/vercel/blob/1663db7ca34d3dd99b57994f801fb30b72fbd2f3/packages/next/src/server-build.ts#L576-L580
79
- private netlifyPrebundleReact ( path : string ) {
85
+ private async netlifyPrebundleReact ( path : string , { basePath , trailingSlash } : NextConfig , parsedUrl ) {
80
86
const routesManifest = this . getRoutesManifest ?.( )
81
87
const appPathsRoutes = this . getAppPathRoutes ?.( )
82
-
83
88
const routes = routesManifest && [ ...routesManifest . staticRoutes , ...routesManifest . dynamicRoutes ]
84
- const matchedRoute = routes ?. find ( ( route ) => new RegExp ( route . regex ) . test ( new URL ( path , 'http://n' ) . pathname ) )
89
+ const matchedRoute = await getMatchedRoute ( path , routes , parsedUrl , basePath , trailingSlash )
85
90
const isAppRoute = appPathsRoutes && matchedRoute ? appPathsRoutes [ matchedRoute . page ] : false
86
91
87
92
if ( isAppRoute ) {
88
93
// app routes should use prebundled React
89
94
// eslint-disable-next-line no-underscore-dangle
90
- process . env . __NEXT_PRIVATE_PREBUNDLED_REACT = this . nextConfig . experimental ?. serverActions
91
- ? 'experimental'
92
- : 'next'
95
+ process . env . __NEXT_PRIVATE_PREBUNDLED_REACT = this . getAppRouterReactVersion ( )
96
+
93
97
return
94
98
}
95
-
96
- // pages routes should use use node_modules React
97
99
// eslint-disable-next-line no-underscore-dangle
98
100
process . env . __NEXT_PRIVATE_PREBUNDLED_REACT = ''
99
101
}
0 commit comments