/
next-dev-server.ts
828 lines (754 loc) · 25.4 KB
/
next-dev-server.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
import type { CustomRoutes } from '../../lib/load-custom-routes'
import type { FindComponentsResult } from '../next-server'
import type { LoadComponentsReturnType } from '../load-components'
import type { Options as ServerOptions } from '../next-server'
import type { Params } from '../../shared/lib/router/utils/route-matcher'
import type { ParsedUrl } from '../../shared/lib/router/utils/parse-url'
import type { ParsedUrlQuery } from 'querystring'
import type { UrlWithParsedQuery } from 'url'
import type { BaseNextRequest, BaseNextResponse } from '../base-http'
import type { MiddlewareRoutingItem } from '../base-server'
import type { FunctionComponent } from 'react'
import type { RouteMatch } from '../future/route-matches/route-match'
import { Worker } from 'next/dist/compiled/jest-worker'
import { join as pathJoin } from 'path'
import { ampValidation } from '../../build/output'
import {
INSTRUMENTATION_HOOK_FILENAME,
PUBLIC_DIR_MIDDLEWARE_CONFLICT,
} from '../../lib/constants'
import { fileExists } from '../../lib/file-exists'
import { findPagesDir } from '../../lib/find-pages-dir'
import {
PHASE_DEVELOPMENT_SERVER,
PAGES_MANIFEST,
APP_PATHS_MANIFEST,
} from '../../shared/lib/constants'
import Server, { WrappedBuildError } from '../next-server'
import { normalizePagePath } from '../../shared/lib/page-path/normalize-page-path'
import { pathHasPrefix } from '../../shared/lib/router/utils/path-has-prefix'
import { removePathPrefix } from '../../shared/lib/router/utils/remove-path-prefix'
import { Telemetry } from '../../telemetry/storage'
import { setGlobal } from '../../trace'
import { findPageFile } from '../lib/find-page-file'
import { getNodeOptionsWithoutInspect } from '../lib/utils'
import {
UnwrapPromise,
withCoalescedInvoke,
} from '../../lib/coalesced-function'
import { loadDefaultErrorComponents } from '../load-components'
import { DecodeError, MiddlewareNotFoundError } from '../../shared/lib/utils'
import * as Log from '../../build/output/log'
import isError, { getProperError } from '../../lib/is-error'
import { NodeNextResponse, NodeNextRequest } from '../base-http/node'
import { isMiddlewareFile } from '../../build/utils'
import { formatServerError } from '../../lib/format-server-error'
import {
DevRouteMatcherManager,
RouteEnsurer,
} from '../future/route-matcher-managers/dev-route-matcher-manager'
import { DevPagesRouteMatcherProvider } from '../future/route-matcher-providers/dev/dev-pages-route-matcher-provider'
import { DevPagesAPIRouteMatcherProvider } from '../future/route-matcher-providers/dev/dev-pages-api-route-matcher-provider'
import { DevAppPageRouteMatcherProvider } from '../future/route-matcher-providers/dev/dev-app-page-route-matcher-provider'
import { DevAppRouteRouteMatcherProvider } from '../future/route-matcher-providers/dev/dev-app-route-route-matcher-provider'
import { PagesManifest } from '../../build/webpack/plugins/pages-manifest-plugin'
import { NodeManifestLoader } from '../future/route-matcher-providers/helpers/manifest-loaders/node-manifest-loader'
import { CachedFileReader } from '../future/route-matcher-providers/dev/helpers/file-reader/cached-file-reader'
import { DefaultFileReader } from '../future/route-matcher-providers/dev/helpers/file-reader/default-file-reader'
import { NextBuildContext } from '../../build/build-context'
import { IncrementalCache } from '../lib/incremental-cache'
import LRUCache from 'next/dist/compiled/lru-cache'
import { NextUrlWithParsedQuery } from '../request-meta'
import { errorToJSON } from '../render'
import { getMiddlewareRouteMatcher } from '../../shared/lib/router/utils/middleware-route-matcher'
import {
deserializeErr,
invokeIpcMethod,
} from '../lib/server-ipc/request-utils'
// Load ReactDevOverlay only when needed
let ReactDevOverlayImpl: FunctionComponent
const ReactDevOverlay = (props: any) => {
if (ReactDevOverlayImpl === undefined) {
ReactDevOverlayImpl =
require('next/dist/compiled/@next/react-dev-overlay/dist/client').ReactDevOverlay
}
return ReactDevOverlayImpl(props)
}
export interface Options extends ServerOptions {
/**
* Tells of Next.js is running from the `next dev` command
*/
isNextDevCommand?: boolean
}
export default class DevServer extends Server {
private devReady: Promise<void>
private setDevReady?: Function
protected sortedRoutes?: string[]
private pagesDir?: string
private appDir?: string
private actualMiddlewareFile?: string
private actualInstrumentationHookFile?: string
private middleware?: MiddlewareRoutingItem
private originalFetch: typeof fetch
private staticPathsCache: LRUCache<
string,
UnwrapPromise<ReturnType<DevServer['getStaticPaths']>>
>
protected staticPathsWorker?: { [key: string]: any } & {
loadStaticPaths: typeof import('./static-paths-worker').loadStaticPaths
}
private getStaticPathsWorker(): { [key: string]: any } & {
loadStaticPaths: typeof import('./static-paths-worker').loadStaticPaths
} {
const worker = new Worker(require.resolve('./static-paths-worker'), {
maxRetries: 1,
// For dev server, it's not necessary to spin up too many workers as long as you are not doing a load test.
// This helps reusing the memory a lot.
numWorkers: 1,
enableWorkerThreads: this.nextConfig.experimental.workerThreads,
forkOptions: {
env: {
...process.env,
// discard --inspect/--inspect-brk flags from process.env.NODE_OPTIONS. Otherwise multiple Node.js debuggers
// would be started if user launch Next.js in debugging mode. The number of debuggers is linked to
// the number of workers Next.js tries to launch. The only worker users are interested in debugging
// is the main Next.js one
NODE_OPTIONS: getNodeOptionsWithoutInspect(),
},
},
}) as Worker & {
loadStaticPaths: typeof import('./static-paths-worker').loadStaticPaths
}
worker.getStdout().pipe(process.stdout)
worker.getStderr().pipe(process.stderr)
return worker
}
constructor(options: Options) {
try {
// Increase the number of stack frames on the server
Error.stackTraceLimit = 50
} catch {}
super({ ...options, dev: true })
this.originalFetch = global.fetch
this.renderOpts.dev = true
this.renderOpts.appDirDevErrorLogger = (err: any) =>
this.logErrorWithOriginalStack(err, 'app-dir')
;(this.renderOpts as any).ErrorDebug = ReactDevOverlay
this.devReady = new Promise((resolve) => {
this.setDevReady = resolve
})
this.staticPathsCache = new LRUCache({
// 5MB
max: 5 * 1024 * 1024,
length(value) {
return JSON.stringify(value.staticPaths).length
},
})
;(this.renderOpts as any).ampSkipValidation =
this.nextConfig.experimental?.amp?.skipValidation ?? false
;(this.renderOpts as any).ampValidator = (
html: string,
pathname: string
) => {
const validatorPath =
this.nextConfig.experimental &&
this.nextConfig.experimental.amp &&
this.nextConfig.experimental.amp.validator
const AmpHtmlValidator =
require('next/dist/compiled/amphtml-validator') as typeof import('next/dist/compiled/amphtml-validator')
return AmpHtmlValidator.getInstance(validatorPath).then((validator) => {
const result = validator.validateString(html)
ampValidation(
pathname,
result.errors
.filter((e) => e.severity === 'ERROR')
.filter((e) => this._filterAmpDevelopmentScript(html, e)),
result.errors.filter((e) => e.severity !== 'ERROR')
)
})
}
const { pagesDir, appDir } = findPagesDir(
this.dir,
!!this.nextConfig.experimental.appDir
)
this.pagesDir = pagesDir
this.appDir = appDir
}
protected getRoutes() {
const { pagesDir, appDir } = findPagesDir(
this.dir,
!!this.nextConfig.experimental.appDir
)
const ensurer: RouteEnsurer = {
ensure: async (match) => {
await this.ensurePage({
match,
page: match.definition.page,
clientOnly: false,
})
},
}
const routes = super.getRoutes()
const matchers = new DevRouteMatcherManager(
routes.matchers,
ensurer,
this.dir
)
const extensions = this.nextConfig.pageExtensions
const fileReader = new CachedFileReader(new DefaultFileReader())
// If the pages directory is available, then configure those matchers.
if (pagesDir) {
matchers.push(
new DevPagesRouteMatcherProvider(
pagesDir,
extensions,
fileReader,
this.localeNormalizer
)
)
matchers.push(
new DevPagesAPIRouteMatcherProvider(
pagesDir,
extensions,
fileReader,
this.localeNormalizer
)
)
}
if (appDir) {
matchers.push(
new DevAppPageRouteMatcherProvider(appDir, extensions, fileReader)
)
matchers.push(
new DevAppRouteRouteMatcherProvider(appDir, extensions, fileReader)
)
}
return { matchers }
}
protected getBuildId(): string {
return 'development'
}
protected async prepareImpl(): Promise<void> {
setGlobal('distDir', this.distDir)
setGlobal('phase', PHASE_DEVELOPMENT_SERVER)
const telemetry = new Telemetry({ distDir: this.distDir })
await super.prepareImpl()
await this.runInstrumentationHookIfAvailable()
await this.matchers.reload()
this.setDevReady!()
// This is required by the tracing subsystem.
setGlobal('appDir', this.appDir)
setGlobal('pagesDir', this.pagesDir)
setGlobal('telemetry', telemetry)
process.on('unhandledRejection', (reason) => {
this.logErrorWithOriginalStack(reason, 'unhandledRejection').catch(
() => {}
)
})
process.on('uncaughtException', (err) => {
this.logErrorWithOriginalStack(err, 'uncaughtException').catch(() => {})
})
}
protected async close(): Promise<void> {}
protected async hasPage(pathname: string): Promise<boolean> {
let normalizedPath: string
try {
normalizedPath = normalizePagePath(pathname)
} catch (err) {
console.error(err)
// if normalizing the page fails it means it isn't valid
// so it doesn't exist so don't throw and return false
// to ensure we return 404 instead of 500
return false
}
if (isMiddlewareFile(normalizedPath)) {
return findPageFile(
this.dir,
normalizedPath,
this.nextConfig.pageExtensions,
false
).then(Boolean)
}
let appFile: string | null = null
let pagesFile: string | null = null
if (this.appDir) {
appFile = await findPageFile(
this.appDir,
normalizedPath + '/page',
this.nextConfig.pageExtensions,
true
)
}
if (this.pagesDir) {
pagesFile = await findPageFile(
this.pagesDir,
normalizedPath,
this.nextConfig.pageExtensions,
false
)
}
if (appFile && pagesFile) {
return false
}
return Boolean(appFile || pagesFile)
}
async runMiddleware(params: {
request: BaseNextRequest
response: BaseNextResponse
parsedUrl: ParsedUrl
parsed: UrlWithParsedQuery
middlewareList: MiddlewareRoutingItem[]
}) {
try {
const result = await super.runMiddleware({
...params,
onWarning: (warn) => {
this.logErrorWithOriginalStack(warn, 'warning')
},
})
if ('finished' in result) {
return result
}
result.waitUntil.catch((error) => {
this.logErrorWithOriginalStack(error, 'unhandledRejection')
})
return result
} catch (error) {
if (error instanceof DecodeError) {
throw error
}
/**
* We only log the error when it is not a MiddlewareNotFound error as
* in that case we should be already displaying a compilation error
* which is what makes the module not found.
*/
if (!(error instanceof MiddlewareNotFoundError)) {
this.logErrorWithOriginalStack(error)
}
const err = getProperError(error)
;(err as any).middleware = true
const { request, response, parsedUrl } = params
/**
* When there is a failure for an internal Next.js request from
* middleware we bypass the error without finishing the request
* so we can serve the required chunks to render the error.
*/
if (
request.url.includes('/_next/static') ||
request.url.includes('/__nextjs_original-stack-frame')
) {
return { finished: false }
}
response.statusCode = 500
this.renderError(err, request, response, parsedUrl.pathname)
return { finished: true }
}
}
async runEdgeFunction(params: {
req: BaseNextRequest
res: BaseNextResponse
query: ParsedUrlQuery
params: Params | undefined
page: string
appPaths: string[] | null
isAppPath: boolean
}) {
try {
return super.runEdgeFunction({
...params,
onWarning: (warn) => {
this.logErrorWithOriginalStack(warn, 'warning')
},
})
} catch (error) {
if (error instanceof DecodeError) {
throw error
}
this.logErrorWithOriginalStack(error, 'warning')
const err = getProperError(error)
const { req, res, page } = params
res.statusCode = 500
this.renderError(err, req, res, page)
return null
}
}
public async handleRequest(
req: BaseNextRequest,
res: BaseNextResponse,
parsedUrl?: NextUrlWithParsedQuery
): Promise<void> {
await this.devReady
return await super.handleRequest(req, res, parsedUrl)
}
async run(
req: NodeNextRequest,
res: NodeNextResponse,
parsedUrl: UrlWithParsedQuery
): Promise<void> {
await this.devReady
const { basePath } = this.nextConfig
let originalPathname: string | null = null
// TODO: see if we can remove this in the future
if (basePath && pathHasPrefix(parsedUrl.pathname || '/', basePath)) {
// strip basePath before handling dev bundles
// If replace ends up replacing the full url it'll be `undefined`, meaning we have to default it to `/`
originalPathname = parsedUrl.pathname
parsedUrl.pathname = removePathPrefix(parsedUrl.pathname || '/', basePath)
}
const { pathname } = parsedUrl
if (pathname!.startsWith('/_next')) {
if (await fileExists(pathJoin(this.publicDir, '_next'))) {
throw new Error(PUBLIC_DIR_MIDDLEWARE_CONFLICT)
}
}
if (originalPathname) {
// restore the path before continuing so that custom-routes can accurately determine
// if they should match against the basePath or not
parsedUrl.pathname = originalPathname
}
try {
return await super.run(req, res, parsedUrl)
} catch (error) {
const err = getProperError(error)
formatServerError(err)
this.logErrorWithOriginalStack(err).catch(() => {})
if (!res.sent) {
res.statusCode = 500
try {
return await this.renderError(err, req, res, pathname!, {
__NEXT_PAGE: (isError(err) && err.page) || pathname || '',
})
} catch (internalErr) {
console.error(internalErr)
res.body('Internal Server Error').send()
}
}
}
}
protected async logErrorWithOriginalStack(
err?: unknown,
type?: 'unhandledRejection' | 'uncaughtException' | 'warning' | 'app-dir'
) {
if (this.isRenderWorker) {
await invokeIpcMethod({
hostname: this.hostname,
method: 'logErrorWithOriginalStack',
args: [errorToJSON(err as Error), type],
ipcPort: process.env.__NEXT_PRIVATE_ROUTER_IPC_PORT,
ipcKey: process.env.__NEXT_PRIVATE_ROUTER_IPC_KEY,
})
return
}
throw new Error(
'Invariant logErrorWithOriginalStack called outside render worker'
)
}
// override production loading of routes-manifest
protected getCustomRoutes(): CustomRoutes {
// actual routes will be loaded asynchronously during .prepare()
return {
redirects: [],
rewrites: { beforeFiles: [], afterFiles: [], fallback: [] },
headers: [],
}
}
protected getPagesManifest(): PagesManifest | undefined {
return (
NodeManifestLoader.require(
pathJoin(this.serverDistDir, PAGES_MANIFEST)
) ?? undefined
)
}
protected getAppPathsManifest(): PagesManifest | undefined {
if (!this.hasAppDir) return undefined
return (
NodeManifestLoader.require(
pathJoin(this.serverDistDir, APP_PATHS_MANIFEST)
) ?? undefined
)
}
protected getMiddleware() {
// We need to populate the match
// field as it isn't serializable
if (this.middleware?.match === null) {
this.middleware.match = getMiddlewareRouteMatcher(
this.middleware.matchers || []
)
}
return this.middleware
}
protected getNextFontManifest() {
return undefined
}
protected async hasMiddleware(): Promise<boolean> {
return this.hasPage(this.actualMiddlewareFile!)
}
protected async ensureMiddleware() {
return this.ensurePage({
page: this.actualMiddlewareFile!,
clientOnly: false,
})
}
private async runInstrumentationHookIfAvailable() {
if (
this.actualInstrumentationHookFile &&
(await this.ensurePage({
page: this.actualInstrumentationHookFile!,
clientOnly: false,
})
.then(() => true)
.catch(() => false))
) {
NextBuildContext!.hasInstrumentationHook = true
try {
const instrumentationHook = await require(pathJoin(
this.distDir,
'server',
INSTRUMENTATION_HOOK_FILENAME
))
await instrumentationHook.register()
} catch (err: any) {
err.message = `An error occurred while loading instrumentation hook: ${err.message}`
throw err
}
}
}
protected async ensureEdgeFunction({
page,
appPaths,
}: {
page: string
appPaths: string[] | null
}) {
return this.ensurePage({ page, appPaths, clientOnly: false })
}
generateRoutes(_dev?: boolean) {
// In development we expose all compiled files for react-error-overlay's line show feature
// We use unshift so that we're sure the routes is defined before Next's default routes
// routes.unshift({
// match: getPathMatch('/_next/development/:path*'),
// type: 'route',
// name: '_next/development catchall',
// fn: async (req, res, params) => {
// const p = pathJoin(this.distDir, ...(params.path || []))
// await this.serveStatic(req, res, p)
// return {
// finished: true,
// }
// },
// })
}
_filterAmpDevelopmentScript(
html: string,
event: { line: number; col: number; code: string }
): boolean {
if (event.code !== 'DISALLOWED_SCRIPT_TAG') {
return true
}
const snippetChunks = html.split('\n')
let snippet
if (
!(snippet = html.split('\n')[event.line - 1]) ||
!(snippet = snippet.substring(event.col))
) {
return true
}
snippet = snippet + snippetChunks.slice(event.line).join('\n')
snippet = snippet.substring(0, snippet.indexOf('</script>'))
return !snippet.includes('data-amp-development-mode-only')
}
protected async getStaticPaths({
pathname,
originalAppPath,
requestHeaders,
}: {
pathname: string
originalAppPath?: string
requestHeaders: IncrementalCache['requestHeaders']
}): Promise<{
staticPaths?: string[]
fallbackMode?: false | 'static' | 'blocking'
}> {
const isAppPath = Boolean(originalAppPath)
// we lazy load the staticPaths to prevent the user
// from waiting on them for the page to load in dev mode
const __getStaticPaths = async () => {
const {
configFileName,
publicRuntimeConfig,
serverRuntimeConfig,
httpAgentOptions,
} = this.nextConfig
const { locales, defaultLocale } = this.nextConfig.i18n || {}
const staticPathsWorker = this.getStaticPathsWorker()
try {
const pathsResult = await staticPathsWorker.loadStaticPaths({
distDir: this.distDir,
pathname,
config: {
configFileName,
publicRuntimeConfig,
serverRuntimeConfig,
},
httpAgentOptions,
locales,
defaultLocale,
originalAppPath,
isAppPath,
requestHeaders,
incrementalCacheHandlerPath:
this.nextConfig.experimental.incrementalCacheHandlerPath,
fetchCacheKeyPrefix: this.nextConfig.experimental.fetchCacheKeyPrefix,
isrFlushToDisk: this.nextConfig.experimental.isrFlushToDisk,
maxMemoryCacheSize: this.nextConfig.experimental.isrMemoryCacheSize,
})
return pathsResult
} finally {
// we don't re-use workers so destroy the used one
staticPathsWorker.end()
}
}
const result = this.staticPathsCache.get(pathname)
const nextInvoke = withCoalescedInvoke(__getStaticPaths)(
`staticPaths-${pathname}`,
[]
)
.then((res) => {
const { paths: staticPaths = [], fallback } = res.value
if (!isAppPath && this.nextConfig.output === 'export') {
if (fallback === 'blocking') {
throw new Error(
'getStaticPaths with "fallback: blocking" cannot be used with "output: export". See more info here: https://nextjs.org/docs/advanced-features/static-html-export'
)
} else if (fallback === true) {
throw new Error(
'getStaticPaths with "fallback: true" cannot be used with "output: export". See more info here: https://nextjs.org/docs/advanced-features/static-html-export'
)
}
}
const value: {
staticPaths: string[]
fallbackMode: 'blocking' | 'static' | false | undefined
} = {
staticPaths,
fallbackMode:
fallback === 'blocking'
? 'blocking'
: fallback === true
? 'static'
: fallback,
}
this.staticPathsCache.set(pathname, value)
return value
})
.catch((err) => {
this.staticPathsCache.del(pathname)
if (!result) throw err
Log.error(`Failed to generate static paths for ${pathname}:`)
console.error(err)
})
if (result) {
return result
}
return nextInvoke as NonNullable<typeof result>
}
private restorePatchedGlobals(): void {
global.fetch = this.originalFetch
}
protected async ensurePage(opts: {
page: string
clientOnly: boolean
appPaths?: string[] | null
match?: RouteMatch
}) {
if (this.isRenderWorker) {
await invokeIpcMethod({
hostname: this.hostname,
method: 'ensurePage',
args: [opts],
ipcPort: process.env.__NEXT_PRIVATE_ROUTER_IPC_PORT,
ipcKey: process.env.__NEXT_PRIVATE_ROUTER_IPC_KEY,
})
return
}
throw new Error('Invariant ensurePage called outside render worker')
}
protected async findPageComponents({
pathname,
query,
params,
isAppPath,
appPaths = null,
shouldEnsure,
}: {
pathname: string
query: ParsedUrlQuery
params: Params
isAppPath: boolean
appPaths?: string[] | null
shouldEnsure: boolean
}): Promise<FindComponentsResult | null> {
await this.devReady
const compilationErr = await this.getCompilationError(pathname)
if (compilationErr) {
// Wrap build errors so that they don't get logged again
throw new WrappedBuildError(compilationErr)
}
try {
if (shouldEnsure || this.renderOpts.customServer) {
await this.ensurePage({
page: pathname,
appPaths,
clientOnly: false,
})
}
this.nextFontManifest = super.getNextFontManifest()
// before we re-evaluate a route module, we want to restore globals that might
// have been patched previously to their original state so that we don't
// patch on top of the previous patch, which would keep the context of the previous
// patched global in memory, creating a memory leak.
this.restorePatchedGlobals()
return await super.findPageComponents({
pathname,
query,
params,
isAppPath,
})
} catch (err) {
if ((err as any).code !== 'ENOENT') {
throw err
}
return null
}
}
protected async getFallbackErrorComponents(): Promise<LoadComponentsReturnType | null> {
if (this.isRenderWorker) {
await invokeIpcMethod({
hostname: this.hostname,
method: 'getFallbackErrorComponents',
args: [],
ipcPort: process.env.__NEXT_PRIVATE_ROUTER_IPC_PORT,
ipcKey: process.env.__NEXT_PRIVATE_ROUTER_IPC_KEY,
})
return await loadDefaultErrorComponents(this.distDir)
}
throw new Error(
`Invariant getFallbackErrorComponents called outside render worker`
)
}
async getCompilationError(page: string): Promise<any> {
if (this.isRenderWorker) {
const err = await invokeIpcMethod({
hostname: this.hostname,
method: 'getCompilationError',
args: [page],
ipcPort: process.env.__NEXT_PRIVATE_ROUTER_IPC_PORT,
ipcKey: process.env.__NEXT_PRIVATE_ROUTER_IPC_KEY,
})
return deserializeErr(err)
}
throw new Error(
'Invariant getCompilationError called outside render worker'
)
}
}