@@ -6,6 +6,16 @@ import { PagesRouteModuleOptions } from '../../../../server/future/route-modules
6
6
import { RouteKind } from '../../../../server/future/route-kind'
7
7
import { normalizePagePath } from '../../../../shared/lib/page-path/normalize-page-path'
8
8
import { MiddlewareConfig } from '../../../analysis/get-page-static-info'
9
+ import { decodeFromBase64 , encodeToBase64 } from '../utils'
10
+ import { isInstrumentationHookFile } from '../../../worker'
11
+
12
+ type RouteLoaderOptionsInput = {
13
+ page : string
14
+ pages : { [ page : string ] : string }
15
+ preferredRegion : string | string [ ] | undefined
16
+ absolutePagePath : string
17
+ middlewareConfig : MiddlewareConfig
18
+ }
9
19
10
20
/**
11
21
* The options for the route loader.
@@ -25,17 +35,29 @@ type RouteLoaderOptions = {
25
35
* The absolute path to the userland page file.
26
36
*/
27
37
absolutePagePath : string
28
-
29
- middlewareConfig : string
38
+ absoluteAppPath : string
39
+ absoluteDocumentPath : string
40
+ middlewareConfigBase64 : string
30
41
}
31
42
32
43
/**
33
44
* Returns the loader entry for a given page.
34
45
*
35
- * @param query the options to create the loader entry
46
+ * @param options the options to create the loader entry
36
47
* @returns the encoded loader entry
37
48
*/
38
- export function getRouteLoaderEntry ( query : RouteLoaderOptions ) : string {
49
+ export function getRouteLoaderEntry ( options : RouteLoaderOptionsInput ) : string {
50
+ const query : RouteLoaderOptions = {
51
+ page : options . page ,
52
+ preferredRegion : options . preferredRegion ,
53
+ absolutePagePath : options . absolutePagePath ,
54
+ // These are the path references to the internal components that may be
55
+ // overridden by userland components.
56
+ absoluteAppPath : options . pages [ '/_app' ] ,
57
+ absoluteDocumentPath : options . pages [ '/_document' ] ,
58
+ middlewareConfigBase64 : encodeToBase64 ( options . middlewareConfig ) ,
59
+ }
60
+
39
61
return `next-route-loader?${ stringify ( query ) } !`
40
62
}
41
63
@@ -49,16 +71,18 @@ const loader: webpack.LoaderDefinitionFunction<RouteLoaderOptions> =
49
71
page,
50
72
preferredRegion,
51
73
absolutePagePath,
52
- middlewareConfig : middlewareConfigBase64 ,
74
+ absoluteAppPath,
75
+ absoluteDocumentPath,
76
+ middlewareConfigBase64,
53
77
} = this . getOptions ( )
54
78
55
79
// Ensure we only run this loader for as a module.
56
80
if ( ! this . _module ) {
57
81
throw new Error ( 'Invariant: expected this to reference a module' )
58
82
}
59
83
60
- const middlewareConfig : MiddlewareConfig = JSON . parse (
61
- Buffer . from ( middlewareConfigBase64 , 'base64' ) . toString ( )
84
+ const middlewareConfig : MiddlewareConfig = decodeFromBase64 (
85
+ middlewareConfigBase64
62
86
)
63
87
64
88
// Attach build info to the module.
@@ -70,7 +94,7 @@ const loader: webpack.LoaderDefinitionFunction<RouteLoaderOptions> =
70
94
middlewareConfig,
71
95
}
72
96
73
- const options : Omit < PagesRouteModuleOptions , 'userland' > = {
97
+ const options : Omit < PagesRouteModuleOptions , 'userland' | 'components' > = {
74
98
definition : {
75
99
kind : RouteKind . PAGES ,
76
100
page : normalizePagePath ( page ) ,
@@ -86,6 +110,10 @@ const loader: webpack.LoaderDefinitionFunction<RouteLoaderOptions> =
86
110
import RouteModule from "next/dist/server/future/route-modules/pages/module"
87
111
import { hoist } from "next/dist/build/webpack/loaders/next-route-loader/helpers"
88
112
113
+ // Import the app and document modules.
114
+ import * as moduleDocument from ${ JSON . stringify ( absoluteDocumentPath ) }
115
+ import * as moduleApp from ${ JSON . stringify ( absoluteAppPath ) }
116
+
89
117
// Import the userland code.
90
118
import * as userland from ${ JSON . stringify ( absolutePagePath ) }
91
119
@@ -98,6 +126,14 @@ const loader: webpack.LoaderDefinitionFunction<RouteLoaderOptions> =
98
126
export const getServerSideProps = hoist(userland, "getServerSideProps")
99
127
export const config = hoist(userland, "config")
100
128
export const reportWebVitals = hoist(userland, "reportWebVitals")
129
+ ${
130
+ // When we're building the instrumentation page (only when the
131
+ // instrumentation file conflicts with a page also labeled
132
+ // /instrumentation) hoist the `register` method.
133
+ isInstrumentationHookFile ( page )
134
+ ? 'export const register = hoist(userland, "register")'
135
+ : ''
136
+ }
101
137
102
138
// Re-export legacy methods.
103
139
export const unstable_getStaticProps = hoist(userland, "unstable_getStaticProps")
@@ -108,7 +144,14 @@ const loader: webpack.LoaderDefinitionFunction<RouteLoaderOptions> =
108
144
109
145
// Create and export the route module that will be consumed.
110
146
const options = ${ JSON . stringify ( options ) }
111
- const routeModule = new RouteModule({ ...options, userland })
147
+ const routeModule = new RouteModule({
148
+ ...options,
149
+ components: {
150
+ App: moduleApp.default,
151
+ Document: moduleDocument.default,
152
+ },
153
+ userland,
154
+ })
112
155
113
156
export { routeModule }
114
157
`
0 commit comments