2022-04-27 11:50:29 +02:00
|
|
|
import type ws from 'ws'
|
|
|
|
import type { webpack5 as webpack } from 'next/dist/compiled/webpack/webpack'
|
|
|
|
import type { NextConfigComplete } from '../config-shared'
|
2017-02-26 20:45:16 +01:00
|
|
|
import { EventEmitter } from 'events'
|
2022-04-27 11:50:29 +02:00
|
|
|
import { findPageFile } from '../lib/find-page-file'
|
2022-05-20 14:24:00 +02:00
|
|
|
import { runDependingOnPageType } from '../../build/entries'
|
2019-03-19 04:24:21 +01:00
|
|
|
import { join, posix } from 'path'
|
2022-04-30 13:19:27 +02:00
|
|
|
import { normalizePathSep } from '../../shared/lib/page-path/normalize-path-sep'
|
|
|
|
import { normalizePagePath } from '../../shared/lib/page-path/normalize-page-path'
|
|
|
|
import { ensureLeadingSlash } from '../../shared/lib/page-path/ensure-leading-slash'
|
|
|
|
import { removePagePathTail } from '../../shared/lib/page-path/remove-page-path-tail'
|
2021-10-09 11:51:37 +02:00
|
|
|
import { reportTrigger } from '../../build/output'
|
2022-04-27 11:50:29 +02:00
|
|
|
import getRouteFromEntrypoint from '../get-route-from-entrypoint'
|
2022-05-13 19:48:53 +02:00
|
|
|
import { serverComponentRegex } from '../../build/webpack/loaders/utils'
|
2022-05-20 14:24:00 +02:00
|
|
|
import { getPageStaticInfo } from '../../build/analysis/get-page-static-info'
|
2022-06-08 16:10:05 +02:00
|
|
|
import { isMiddlewareFile, isMiddlewareFilename } from '../../build/utils'
|
2022-06-06 20:35:26 +02:00
|
|
|
import { PageNotFoundError } from '../../shared/lib/utils'
|
2022-07-11 14:34:10 +02:00
|
|
|
import { DynamicParamTypesShort, FlightRouterState } from '../app-render'
|
2022-07-10 19:18:48 +02:00
|
|
|
|
|
|
|
function treePathToEntrypoint(
|
|
|
|
segmentPath: string[],
|
|
|
|
parentPath?: string
|
|
|
|
): string {
|
|
|
|
const [parallelRouteKey, segment] = segmentPath
|
|
|
|
|
2022-07-11 14:02:46 +02:00
|
|
|
// TODO: modify this path to cover parallelRouteKey convention
|
2022-07-10 19:18:48 +02:00
|
|
|
const path =
|
|
|
|
(parentPath ? parentPath + '/' : '') +
|
|
|
|
(parallelRouteKey !== 'children' ? parallelRouteKey + '/' : '') +
|
|
|
|
(segment === '' ? 'page' : segment)
|
|
|
|
|
|
|
|
// Last segment
|
|
|
|
if (segmentPath.length === 2) {
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
|
|
|
|
const childSegmentPath = segmentPath.slice(2)
|
|
|
|
return treePathToEntrypoint(childSegmentPath, path)
|
|
|
|
}
|
|
|
|
|
2022-07-11 14:34:10 +02:00
|
|
|
function convertDynamicParamTypeToSyntax(
|
|
|
|
dynamicParamTypeShort: DynamicParamTypesShort,
|
|
|
|
param: string
|
|
|
|
) {
|
|
|
|
switch (dynamicParamTypeShort) {
|
|
|
|
case 'c':
|
|
|
|
return `[...${param}]`
|
|
|
|
case 'oc':
|
|
|
|
return `[[...${param}]]`
|
|
|
|
case 'd':
|
|
|
|
return `[${param}]`
|
|
|
|
default:
|
|
|
|
throw new Error('Unknown dynamic param type')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-10 19:18:48 +02:00
|
|
|
function getEntrypointsFromTree(
|
|
|
|
tree: FlightRouterState,
|
|
|
|
isFirst: boolean,
|
|
|
|
parentPath: string[] = []
|
|
|
|
) {
|
|
|
|
const [segment, parallelRoutes] = tree
|
|
|
|
|
2022-07-11 14:34:10 +02:00
|
|
|
const currentSegment = Array.isArray(segment)
|
|
|
|
? convertDynamicParamTypeToSyntax(segment[2], segment[0])
|
|
|
|
: segment
|
2022-07-10 19:18:48 +02:00
|
|
|
|
|
|
|
const currentPath = [...parentPath, currentSegment]
|
|
|
|
|
|
|
|
if (!isFirst && currentSegment === '') {
|
|
|
|
// TODO get rid of '' at the start of tree
|
|
|
|
return [treePathToEntrypoint(currentPath.slice(1))]
|
|
|
|
}
|
|
|
|
|
|
|
|
return Object.keys(parallelRoutes).reduce(
|
|
|
|
(paths: string[], key: string): string[] => {
|
|
|
|
const childTree = parallelRoutes[key]
|
|
|
|
const childPages = getEntrypointsFromTree(childTree, false, [
|
|
|
|
...currentPath,
|
|
|
|
key,
|
|
|
|
])
|
|
|
|
return [...paths, ...childPages]
|
|
|
|
},
|
|
|
|
[]
|
|
|
|
)
|
|
|
|
}
|
2017-02-26 20:45:16 +01:00
|
|
|
|
2020-06-26 06:26:09 +02:00
|
|
|
export const ADDED = Symbol('added')
|
|
|
|
export const BUILDING = Symbol('building')
|
|
|
|
export const BUILT = Symbol('built')
|
|
|
|
|
2021-11-06 12:27:40 +01:00
|
|
|
export const entries: {
|
2022-04-30 13:19:27 +02:00
|
|
|
/**
|
|
|
|
* The key composed of the compiler name and the page. For example:
|
|
|
|
* `edge-server/about`
|
|
|
|
*/
|
2020-06-26 06:26:09 +02:00
|
|
|
[page: string]: {
|
2022-04-30 13:19:27 +02:00
|
|
|
/**
|
|
|
|
* The absolute page to the page file. For example:
|
|
|
|
* `/Users/Rick/project/pages/about/index.js`
|
|
|
|
*/
|
2020-06-26 06:26:09 +02:00
|
|
|
absolutePagePath: string
|
2022-04-30 13:19:27 +02:00
|
|
|
/**
|
|
|
|
* Path to the page file relative to the dist folder with no extension.
|
|
|
|
* For example: `pages/about/index`
|
|
|
|
*/
|
|
|
|
bundlePath: string
|
2022-05-13 19:48:53 +02:00
|
|
|
/**
|
|
|
|
* Client entry loader and query parameters when RSC is enabled.
|
|
|
|
*/
|
|
|
|
clientLoader?: string
|
2022-04-30 13:19:27 +02:00
|
|
|
/**
|
|
|
|
* Tells if a page is scheduled to be disposed.
|
|
|
|
*/
|
2021-10-11 18:12:48 +02:00
|
|
|
dispose?: boolean
|
2022-04-30 13:19:27 +02:00
|
|
|
/**
|
|
|
|
* Timestamp with the last time the page was active.
|
|
|
|
*/
|
|
|
|
lastActiveTime?: number
|
|
|
|
/**
|
|
|
|
* Page build status.
|
|
|
|
*/
|
|
|
|
status?: typeof ADDED | typeof BUILDING | typeof BUILT
|
2020-06-26 06:26:09 +02:00
|
|
|
}
|
|
|
|
} = {}
|
2018-09-16 16:06:02 +02:00
|
|
|
|
2022-05-13 19:48:53 +02:00
|
|
|
let invalidator: Invalidator
|
|
|
|
export const getInvalidator = () => invalidator
|
|
|
|
|
2022-08-11 05:27:48 +02:00
|
|
|
const doneCallbacks: EventEmitter | null = new EventEmitter()
|
|
|
|
const lastClientAccessPages = ['']
|
|
|
|
const lastServerAccessPagesForAppDir = ['']
|
|
|
|
|
2022-04-30 13:19:27 +02:00
|
|
|
export function onDemandEntryHandler({
|
|
|
|
maxInactiveAge,
|
|
|
|
multiCompiler,
|
|
|
|
nextConfig,
|
|
|
|
pagesBufferLength,
|
|
|
|
pagesDir,
|
2022-05-19 17:46:21 +02:00
|
|
|
rootDir,
|
2022-05-25 11:46:26 +02:00
|
|
|
appDir,
|
2022-04-30 13:19:27 +02:00
|
|
|
}: {
|
|
|
|
maxInactiveAge: number
|
|
|
|
multiCompiler: webpack.MultiCompiler
|
|
|
|
nextConfig: NextConfigComplete
|
|
|
|
pagesBufferLength: number
|
|
|
|
pagesDir: string
|
2022-05-19 17:46:21 +02:00
|
|
|
rootDir: string
|
2022-05-25 11:46:26 +02:00
|
|
|
appDir?: string
|
2022-04-30 13:19:27 +02:00
|
|
|
}) {
|
2022-06-24 20:50:49 +02:00
|
|
|
invalidator = new Invalidator(multiCompiler)
|
2018-01-30 16:40:52 +01:00
|
|
|
|
2022-05-13 19:48:53 +02:00
|
|
|
const startBuilding = (_compilation: webpack.Compilation) => {
|
|
|
|
invalidator.startBuilding()
|
|
|
|
}
|
2022-04-30 13:19:27 +02:00
|
|
|
for (const compiler of multiCompiler.compilers) {
|
2022-05-13 19:48:53 +02:00
|
|
|
compiler.hooks.make.tap('NextJsOnDemandEntries', startBuilding)
|
2018-09-16 16:06:02 +02:00
|
|
|
}
|
2017-02-26 20:45:16 +01:00
|
|
|
|
2021-09-30 15:52:26 +02:00
|
|
|
function getPagePathsFromEntrypoints(
|
2022-04-30 13:19:27 +02:00
|
|
|
type: 'client' | 'server' | 'edge-server',
|
2022-05-03 12:37:23 +02:00
|
|
|
entrypoints: Map<string, { name?: string }>,
|
|
|
|
root?: boolean
|
2022-04-30 13:19:27 +02:00
|
|
|
) {
|
|
|
|
const pagePaths: string[] = []
|
2020-06-04 19:32:45 +02:00
|
|
|
for (const entrypoint of entrypoints.values()) {
|
2022-05-03 12:37:23 +02:00
|
|
|
const page = getRouteFromEntrypoint(entrypoint.name!, root)
|
2020-06-04 19:32:45 +02:00
|
|
|
if (page) {
|
2021-09-30 15:52:26 +02:00
|
|
|
pagePaths.push(`${type}${page}`)
|
2022-06-08 16:10:05 +02:00
|
|
|
} else if (
|
|
|
|
(root && entrypoint.name === 'root') ||
|
|
|
|
isMiddlewareFilename(entrypoint.name)
|
|
|
|
) {
|
2022-05-19 17:46:21 +02:00
|
|
|
pagePaths.push(`${type}/${entrypoint.name}`)
|
2018-09-16 16:06:02 +02:00
|
|
|
}
|
2019-05-28 15:48:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return pagePaths
|
|
|
|
}
|
|
|
|
|
2020-05-18 21:24:37 +02:00
|
|
|
multiCompiler.hooks.done.tap('NextJsOnDemandEntries', (multiStats) => {
|
2021-09-09 10:14:30 +02:00
|
|
|
if (invalidator.rebuildAgain) {
|
|
|
|
return invalidator.doneBuilding()
|
|
|
|
}
|
2022-02-08 14:16:46 +01:00
|
|
|
const [clientStats, serverStats, edgeServerStats] = multiStats.stats
|
2022-05-25 11:46:26 +02:00
|
|
|
const root = !!appDir
|
2021-09-30 15:52:26 +02:00
|
|
|
const pagePaths = [
|
|
|
|
...getPagePathsFromEntrypoints(
|
|
|
|
'client',
|
2022-05-03 12:37:23 +02:00
|
|
|
clientStats.compilation.entrypoints,
|
|
|
|
root
|
2021-09-30 15:52:26 +02:00
|
|
|
),
|
|
|
|
...getPagePathsFromEntrypoints(
|
|
|
|
'server',
|
2022-05-03 12:37:23 +02:00
|
|
|
serverStats.compilation.entrypoints,
|
|
|
|
root
|
2021-09-30 15:52:26 +02:00
|
|
|
),
|
2022-02-08 14:16:46 +01:00
|
|
|
...(edgeServerStats
|
2021-10-26 18:50:56 +02:00
|
|
|
? getPagePathsFromEntrypoints(
|
2022-02-08 14:16:46 +01:00
|
|
|
'edge-server',
|
2022-05-03 12:37:23 +02:00
|
|
|
edgeServerStats.compilation.entrypoints,
|
|
|
|
root
|
2021-10-26 18:50:56 +02:00
|
|
|
)
|
|
|
|
: []),
|
2021-09-30 15:52:26 +02:00
|
|
|
]
|
2019-05-28 15:48:13 +02:00
|
|
|
|
2020-06-04 19:32:45 +02:00
|
|
|
for (const page of pagePaths) {
|
2018-09-16 16:06:02 +02:00
|
|
|
const entry = entries[page]
|
|
|
|
if (!entry) {
|
|
|
|
continue
|
|
|
|
}
|
2017-06-07 00:32:02 +02:00
|
|
|
|
2018-09-16 16:06:02 +02:00
|
|
|
if (entry.status !== BUILDING) {
|
|
|
|
continue
|
2017-02-26 20:45:16 +01:00
|
|
|
}
|
2018-09-16 16:06:02 +02:00
|
|
|
|
|
|
|
entry.status = BUILT
|
2019-10-04 18:11:39 +02:00
|
|
|
doneCallbacks!.emit(page)
|
2018-09-16 16:06:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
invalidator.doneBuilding()
|
2017-02-26 20:45:16 +01:00
|
|
|
})
|
|
|
|
|
2021-10-11 19:49:32 +02:00
|
|
|
const pingIntervalTime = Math.max(1000, Math.min(5000, maxInactiveAge))
|
|
|
|
|
2022-04-30 13:19:27 +02:00
|
|
|
setInterval(function () {
|
2022-08-11 05:27:48 +02:00
|
|
|
disposeInactiveEntries(maxInactiveAge)
|
2022-04-30 13:19:27 +02:00
|
|
|
}, pingIntervalTime + 1000).unref()
|
2018-01-31 12:37:41 +01:00
|
|
|
|
2022-07-10 19:18:48 +02:00
|
|
|
function handleAppDirPing(
|
|
|
|
tree: FlightRouterState
|
|
|
|
): { success: true } | { invalid: true } {
|
|
|
|
const pages = getEntrypointsFromTree(tree, true)
|
|
|
|
|
|
|
|
for (const page of pages) {
|
|
|
|
const pageKey = `server/${page}`
|
|
|
|
const entryInfo = entries[pageKey]
|
|
|
|
|
|
|
|
// If there's no entry, it may have been invalidated and needs to be re-built.
|
|
|
|
if (!entryInfo) {
|
|
|
|
// if (page !== lastEntry) client pings, but there's no entry for page
|
|
|
|
return { invalid: true }
|
|
|
|
}
|
|
|
|
|
|
|
|
// We don't need to maintain active state of anything other than BUILT entries
|
|
|
|
if (entryInfo.status !== BUILT) continue
|
|
|
|
|
|
|
|
// If there's an entryInfo
|
|
|
|
if (!lastServerAccessPagesForAppDir.includes(pageKey)) {
|
|
|
|
lastServerAccessPagesForAppDir.unshift(pageKey)
|
|
|
|
|
|
|
|
// Maintain the buffer max length
|
|
|
|
// TODO: verify that the current pageKey is not at the end of the array as multiple entrypoints can exist
|
|
|
|
if (lastServerAccessPagesForAppDir.length > pagesBufferLength) {
|
|
|
|
lastServerAccessPagesForAppDir.pop()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
entryInfo.lastActiveTime = Date.now()
|
|
|
|
entryInfo.dispose = false
|
|
|
|
}
|
|
|
|
|
|
|
|
return { success: true }
|
|
|
|
}
|
|
|
|
|
2019-10-04 18:11:39 +02:00
|
|
|
function handlePing(pg: string) {
|
2020-06-04 19:32:45 +02:00
|
|
|
const page = normalizePathSep(pg)
|
2021-09-30 15:52:26 +02:00
|
|
|
const pageKey = `client${page}`
|
|
|
|
const entryInfo = entries[pageKey]
|
2019-02-15 22:22:21 +01:00
|
|
|
|
2019-04-09 17:52:03 +02:00
|
|
|
// If there's no entry, it may have been invalidated and needs to be re-built.
|
2019-02-15 22:22:21 +01:00
|
|
|
if (!entryInfo) {
|
2020-01-03 11:43:36 +01:00
|
|
|
// if (page !== lastEntry) client pings, but there's no entry for page
|
2019-02-19 21:58:47 +01:00
|
|
|
return { invalid: true }
|
2019-02-15 22:22:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// 404 is an on demand entry but when a new page is added we have to refresh the page
|
2022-04-30 13:19:27 +02:00
|
|
|
const toSend = page === '/_error' ? { invalid: true } : { success: true }
|
2019-02-15 22:22:21 +01:00
|
|
|
|
|
|
|
// We don't need to maintain active state of anything other than BUILT entries
|
|
|
|
if (entryInfo.status !== BUILT) return
|
|
|
|
|
|
|
|
// If there's an entryInfo
|
2021-09-30 15:52:26 +02:00
|
|
|
if (!lastClientAccessPages.includes(pageKey)) {
|
|
|
|
lastClientAccessPages.unshift(pageKey)
|
2019-02-15 22:22:21 +01:00
|
|
|
|
|
|
|
// Maintain the buffer max length
|
2021-09-30 15:52:26 +02:00
|
|
|
if (lastClientAccessPages.length > pagesBufferLength) {
|
|
|
|
lastClientAccessPages.pop()
|
2019-02-15 22:22:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
entryInfo.lastActiveTime = Date.now()
|
2021-10-11 18:12:48 +02:00
|
|
|
entryInfo.dispose = false
|
2019-02-19 21:58:47 +01:00
|
|
|
return toSend
|
2019-02-15 22:22:21 +01:00
|
|
|
}
|
|
|
|
|
2017-02-26 20:45:16 +01:00
|
|
|
return {
|
2022-04-30 13:19:27 +02:00
|
|
|
async ensurePage(page: string, clientOnly: boolean) {
|
|
|
|
const pagePathData = await findPagePathData(
|
2022-05-19 17:46:21 +02:00
|
|
|
rootDir,
|
2019-05-29 13:57:26 +02:00
|
|
|
pagesDir,
|
2022-04-30 13:19:27 +02:00
|
|
|
page,
|
2022-05-03 12:37:23 +02:00
|
|
|
nextConfig.pageExtensions,
|
2022-05-25 11:46:26 +02:00
|
|
|
appDir
|
2022-04-30 13:19:27 +02:00
|
|
|
)
|
2017-02-26 20:45:16 +01:00
|
|
|
|
2022-04-30 13:19:27 +02:00
|
|
|
let entryAdded = false
|
2022-04-27 11:50:29 +02:00
|
|
|
|
2022-02-08 14:16:46 +01:00
|
|
|
const addPageEntry = (type: 'client' | 'server' | 'edge-server') => {
|
2021-09-30 15:52:26 +02:00
|
|
|
return new Promise<void>((resolve, reject) => {
|
2022-05-13 19:48:53 +02:00
|
|
|
const isServerComponent = serverComponentRegex.test(
|
|
|
|
pagePathData.absolutePagePath
|
|
|
|
)
|
2022-06-09 16:58:10 +02:00
|
|
|
const isInsideAppDir =
|
|
|
|
appDir && pagePathData.absolutePagePath.startsWith(appDir)
|
2022-05-13 19:48:53 +02:00
|
|
|
|
2022-04-30 13:19:27 +02:00
|
|
|
const pageKey = `${type}${pagePathData.page}`
|
2022-05-13 19:48:53 +02:00
|
|
|
|
2022-04-30 13:19:27 +02:00
|
|
|
if (entries[pageKey]) {
|
|
|
|
entries[pageKey].dispose = false
|
|
|
|
entries[pageKey].lastActiveTime = Date.now()
|
|
|
|
if (entries[pageKey].status === BUILT) {
|
2021-09-30 15:52:26 +02:00
|
|
|
resolve()
|
|
|
|
return
|
|
|
|
}
|
2022-04-30 13:19:27 +02:00
|
|
|
} else {
|
2022-06-09 16:58:10 +02:00
|
|
|
if (type === 'client' && (isServerComponent || isInsideAppDir)) {
|
2022-05-13 19:48:53 +02:00
|
|
|
// Skip adding the client entry here.
|
|
|
|
} else {
|
|
|
|
entryAdded = true
|
|
|
|
entries[pageKey] = {
|
|
|
|
absolutePagePath: pagePathData.absolutePagePath,
|
|
|
|
bundlePath: pagePathData.bundlePath,
|
|
|
|
dispose: false,
|
|
|
|
lastActiveTime: Date.now(),
|
|
|
|
status: ADDED,
|
|
|
|
}
|
2022-04-30 13:19:27 +02:00
|
|
|
}
|
2021-09-30 15:52:26 +02:00
|
|
|
}
|
2017-02-26 20:45:16 +01:00
|
|
|
|
2022-04-30 13:19:27 +02:00
|
|
|
doneCallbacks!.once(pageKey, (err: Error) => {
|
2021-09-30 15:52:26 +02:00
|
|
|
if (err) return reject(err)
|
|
|
|
resolve()
|
2022-04-30 13:19:27 +02:00
|
|
|
})
|
2021-09-30 15:52:26 +02:00
|
|
|
})
|
|
|
|
}
|
2017-02-26 20:45:16 +01:00
|
|
|
|
2022-05-20 14:24:00 +02:00
|
|
|
const staticInfo = await getPageStaticInfo({
|
|
|
|
pageFilePath: pagePathData.absolutePagePath,
|
|
|
|
nextConfig,
|
|
|
|
})
|
|
|
|
|
2022-06-24 20:50:49 +02:00
|
|
|
const result = runDependingOnPageType({
|
2022-04-30 13:19:27 +02:00
|
|
|
page: pagePathData.page,
|
2022-05-20 14:24:00 +02:00
|
|
|
pageRuntime: staticInfo.runtime,
|
2022-04-27 11:50:29 +02:00
|
|
|
onClient: () => addPageEntry('client'),
|
|
|
|
onServer: () => addPageEntry('server'),
|
|
|
|
onEdgeServer: () => addPageEntry('edge-server'),
|
|
|
|
})
|
2021-09-30 15:52:26 +02:00
|
|
|
|
2022-06-24 20:50:49 +02:00
|
|
|
const promises = Object.values(result)
|
2022-04-30 13:19:27 +02:00
|
|
|
if (entryAdded) {
|
2021-10-09 11:51:37 +02:00
|
|
|
reportTrigger(
|
2022-04-27 11:50:29 +02:00
|
|
|
!clientOnly && promises.length > 1
|
2022-04-30 13:19:27 +02:00
|
|
|
? `${pagePathData.page} (client and server)`
|
|
|
|
: pagePathData.page
|
2021-09-30 15:52:26 +02:00
|
|
|
)
|
2022-06-24 20:50:49 +02:00
|
|
|
invalidator.invalidate(Object.keys(result))
|
2021-09-30 15:52:26 +02:00
|
|
|
}
|
2017-02-26 20:45:16 +01:00
|
|
|
|
2022-04-27 11:50:29 +02:00
|
|
|
return Promise.all(promises)
|
2017-02-26 20:45:16 +01:00
|
|
|
},
|
|
|
|
|
2021-10-15 09:09:54 +02:00
|
|
|
onHMR(client: ws) {
|
|
|
|
client.addEventListener('message', ({ data }) => {
|
|
|
|
try {
|
2022-04-30 13:19:27 +02:00
|
|
|
const parsedData = JSON.parse(
|
|
|
|
typeof data !== 'string' ? data.toString() : data
|
|
|
|
)
|
2021-10-15 09:09:54 +02:00
|
|
|
|
|
|
|
if (parsedData.event === 'ping') {
|
2022-07-10 19:18:48 +02:00
|
|
|
const result = parsedData.appDirRoute
|
|
|
|
? handleAppDirPing(parsedData.tree)
|
|
|
|
: handlePing(parsedData.page)
|
2021-10-15 09:09:54 +02:00
|
|
|
client.send(
|
|
|
|
JSON.stringify({
|
|
|
|
...result,
|
2022-07-10 19:18:48 +02:00
|
|
|
[parsedData.appDirRoute ? 'action' : 'event']: 'pong',
|
2021-10-15 09:09:54 +02:00
|
|
|
})
|
|
|
|
)
|
|
|
|
}
|
|
|
|
} catch (_) {}
|
2020-07-01 17:34:00 +02:00
|
|
|
})
|
2019-10-04 18:11:39 +02:00
|
|
|
},
|
2017-02-26 20:45:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-11 05:27:48 +02:00
|
|
|
function disposeInactiveEntries(maxInactiveAge: number) {
|
2020-05-18 21:24:37 +02:00
|
|
|
Object.keys(entries).forEach((page) => {
|
2022-07-10 19:18:48 +02:00
|
|
|
const { lastActiveTime, status, dispose, bundlePath } = entries[page]
|
|
|
|
|
|
|
|
const isClientComponentsEntry =
|
|
|
|
bundlePath.startsWith('app/') && page.startsWith('client/')
|
|
|
|
|
|
|
|
// Disposing client component entry is handled when disposing server component entry
|
|
|
|
if (isClientComponentsEntry) return
|
2021-10-11 18:12:48 +02:00
|
|
|
|
|
|
|
// Skip pages already scheduled for disposing
|
|
|
|
if (dispose) return
|
2017-02-26 20:45:16 +01:00
|
|
|
|
|
|
|
// This means this entry is currently building or just added
|
|
|
|
// We don't need to dispose those entries.
|
|
|
|
if (status !== BUILT) return
|
|
|
|
|
|
|
|
// We should not build the last accessed page even we didn't get any pings
|
|
|
|
// Sometimes, it's possible our XHR ping to wait before completing other requests.
|
|
|
|
// In that case, we should not dispose the current viewing page
|
2022-07-10 19:18:48 +02:00
|
|
|
if (
|
|
|
|
lastClientAccessPages.includes(page) ||
|
|
|
|
lastServerAccessPagesForAppDir.includes(page)
|
|
|
|
)
|
|
|
|
return
|
2017-02-26 20:45:16 +01:00
|
|
|
|
2020-06-26 06:26:09 +02:00
|
|
|
if (lastActiveTime && Date.now() - lastActiveTime > maxInactiveAge) {
|
2022-07-10 19:18:48 +02:00
|
|
|
const isServerComponentsEntry =
|
|
|
|
bundlePath.startsWith('app/') && page.startsWith('server/')
|
|
|
|
|
|
|
|
// Dispose client component entrypoint when server component entrypoint is disposed.
|
|
|
|
if (isServerComponentsEntry) {
|
|
|
|
entries[page.replace('server/', 'client/')].dispose = true
|
|
|
|
}
|
2021-10-11 18:12:48 +02:00
|
|
|
entries[page].dispose = true
|
2017-02-26 20:45:16 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-07-10 19:18:48 +02:00
|
|
|
// Make sure only one invalidation happens at a time∫
|
2017-02-27 21:05:10 +01:00
|
|
|
// Otherwise, webpack hash gets changed and it'll force the client to reload.
|
|
|
|
class Invalidator {
|
2022-06-24 20:50:49 +02:00
|
|
|
private multiCompiler: webpack.MultiCompiler
|
2019-10-04 18:11:39 +02:00
|
|
|
private building: boolean
|
2021-09-09 10:14:30 +02:00
|
|
|
public rebuildAgain: boolean
|
2019-10-04 18:11:39 +02:00
|
|
|
|
2022-06-24 20:50:49 +02:00
|
|
|
constructor(multiCompiler: webpack.MultiCompiler) {
|
|
|
|
this.multiCompiler = multiCompiler
|
2018-01-30 16:40:52 +01:00
|
|
|
// contains an array of types of compilers currently building
|
2017-02-27 21:05:10 +01:00
|
|
|
this.building = false
|
|
|
|
this.rebuildAgain = false
|
|
|
|
}
|
|
|
|
|
2022-06-24 20:50:49 +02:00
|
|
|
invalidate(keys: string[] = []) {
|
2017-02-27 21:05:10 +01:00
|
|
|
// If there's a current build is processing, we won't abort it by invalidating.
|
|
|
|
// (If aborted, it'll cause a client side hard reload)
|
|
|
|
// But let it to invalidate just after the completion.
|
|
|
|
// So, it can re-build the queued pages at once.
|
|
|
|
if (this.building) {
|
|
|
|
this.rebuildAgain = true
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
this.building = true
|
2022-06-24 20:50:49 +02:00
|
|
|
|
|
|
|
if (!keys || keys.length === 0) {
|
2022-08-11 05:27:48 +02:00
|
|
|
this.multiCompiler.compilers[0].watching?.invalidate()
|
|
|
|
this.multiCompiler.compilers[1].watching?.invalidate()
|
|
|
|
this.multiCompiler.compilers[2].watching?.invalidate()
|
2022-06-24 20:50:49 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const key of keys) {
|
|
|
|
if (key === 'client') {
|
2022-08-11 05:27:48 +02:00
|
|
|
this.multiCompiler.compilers[0].watching?.invalidate()
|
2022-06-24 20:50:49 +02:00
|
|
|
} else if (key === 'server') {
|
2022-08-11 05:27:48 +02:00
|
|
|
this.multiCompiler.compilers[1].watching?.invalidate()
|
2022-06-24 20:50:49 +02:00
|
|
|
} else if (key === 'edgeServer') {
|
2022-08-11 05:27:48 +02:00
|
|
|
this.multiCompiler.compilers[2].watching?.invalidate()
|
2022-06-24 20:50:49 +02:00
|
|
|
}
|
|
|
|
}
|
2017-02-27 21:05:10 +01:00
|
|
|
}
|
|
|
|
|
2019-10-04 18:11:39 +02:00
|
|
|
startBuilding() {
|
2017-02-27 21:05:10 +01:00
|
|
|
this.building = true
|
|
|
|
}
|
|
|
|
|
2019-10-04 18:11:39 +02:00
|
|
|
doneBuilding() {
|
2017-02-27 21:05:10 +01:00
|
|
|
this.building = false
|
2018-01-30 16:40:52 +01:00
|
|
|
|
2017-02-27 21:05:10 +01:00
|
|
|
if (this.rebuildAgain) {
|
|
|
|
this.rebuildAgain = false
|
|
|
|
this.invalidate()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-04-27 11:50:29 +02:00
|
|
|
|
2022-04-30 13:19:27 +02:00
|
|
|
/**
|
|
|
|
* Attempts to find a page file path from the given pages absolute directory,
|
|
|
|
* a page and allowed extensions. If the page can't be found it will throw an
|
|
|
|
* error. It defaults the `/_error` page to Next.js internal error page.
|
|
|
|
*
|
2022-05-19 17:46:21 +02:00
|
|
|
* @param rootDir Absolute path to the project root.
|
2022-04-30 13:19:27 +02:00
|
|
|
* @param pagesDir Absolute path to the pages folder with trailing `/pages`.
|
|
|
|
* @param normalizedPagePath The page normalized (it will be denormalized).
|
|
|
|
* @param pageExtensions Array of page extensions.
|
|
|
|
*/
|
|
|
|
async function findPagePathData(
|
2022-05-19 17:46:21 +02:00
|
|
|
rootDir: string,
|
2022-04-30 13:19:27 +02:00
|
|
|
pagesDir: string,
|
|
|
|
page: string,
|
2022-05-03 12:37:23 +02:00
|
|
|
extensions: string[],
|
2022-05-25 11:46:26 +02:00
|
|
|
appDir?: string
|
2022-04-30 13:19:27 +02:00
|
|
|
) {
|
|
|
|
const normalizedPagePath = tryToNormalizePagePath(page)
|
2022-05-03 12:37:23 +02:00
|
|
|
let pagePath: string | null = null
|
2022-05-05 13:15:32 +02:00
|
|
|
|
2022-06-08 16:10:05 +02:00
|
|
|
if (isMiddlewareFile(normalizedPagePath)) {
|
2022-05-19 17:46:21 +02:00
|
|
|
pagePath = await findPageFile(rootDir, normalizedPagePath, extensions)
|
|
|
|
|
|
|
|
if (!pagePath) {
|
2022-06-06 20:35:26 +02:00
|
|
|
throw new PageNotFoundError(normalizedPagePath)
|
2022-05-19 17:46:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const pageUrl = ensureLeadingSlash(
|
|
|
|
removePagePathTail(normalizePathSep(pagePath), {
|
|
|
|
extensions,
|
|
|
|
})
|
|
|
|
)
|
|
|
|
|
|
|
|
return {
|
|
|
|
absolutePagePath: join(rootDir, pagePath),
|
|
|
|
bundlePath: normalizedPagePath.slice(1),
|
|
|
|
page: posix.normalize(pageUrl),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-25 11:46:26 +02:00
|
|
|
// Check appDir first falling back to pagesDir
|
|
|
|
if (appDir) {
|
|
|
|
pagePath = await findPageFile(appDir, normalizedPagePath, extensions)
|
2022-05-03 12:37:23 +02:00
|
|
|
if (pagePath) {
|
2022-05-19 17:46:21 +02:00
|
|
|
const pageUrl = ensureLeadingSlash(
|
|
|
|
removePagePathTail(normalizePathSep(pagePath), {
|
|
|
|
keepIndex: true,
|
|
|
|
extensions,
|
|
|
|
})
|
|
|
|
)
|
|
|
|
|
|
|
|
return {
|
2022-05-25 11:46:26 +02:00
|
|
|
absolutePagePath: join(appDir, pagePath),
|
|
|
|
bundlePath: posix.join('app', normalizePagePath(pageUrl)),
|
2022-05-19 17:46:21 +02:00
|
|
|
page: posix.normalize(pageUrl),
|
|
|
|
}
|
2022-05-03 12:37:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!pagePath) {
|
|
|
|
pagePath = await findPageFile(pagesDir, normalizedPagePath, extensions)
|
|
|
|
}
|
|
|
|
|
2022-04-30 13:19:27 +02:00
|
|
|
if (pagePath !== null) {
|
|
|
|
const pageUrl = ensureLeadingSlash(
|
2022-05-19 17:46:21 +02:00
|
|
|
removePagePathTail(normalizePathSep(pagePath), {
|
|
|
|
extensions,
|
|
|
|
})
|
2022-04-30 13:19:27 +02:00
|
|
|
)
|
2022-04-27 11:50:29 +02:00
|
|
|
|
2022-04-30 13:19:27 +02:00
|
|
|
return {
|
2022-05-19 17:46:21 +02:00
|
|
|
absolutePagePath: join(pagesDir, pagePath),
|
|
|
|
bundlePath: posix.join('pages', normalizePagePath(pageUrl)),
|
2022-04-30 13:19:27 +02:00
|
|
|
page: posix.normalize(pageUrl),
|
2022-04-27 11:50:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-30 13:19:27 +02:00
|
|
|
if (page === '/_error') {
|
2022-04-27 11:50:29 +02:00
|
|
|
return {
|
2022-04-30 13:19:27 +02:00
|
|
|
absolutePagePath: require.resolve('next/dist/pages/_error'),
|
2022-04-27 11:50:29 +02:00
|
|
|
bundlePath: page,
|
2022-04-30 13:19:27 +02:00
|
|
|
page: normalizePathSep(page),
|
2022-04-27 11:50:29 +02:00
|
|
|
}
|
2022-04-30 13:19:27 +02:00
|
|
|
} else {
|
2022-06-06 20:35:26 +02:00
|
|
|
throw new PageNotFoundError(normalizedPagePath)
|
2022-04-27 11:50:29 +02:00
|
|
|
}
|
2022-04-30 13:19:27 +02:00
|
|
|
}
|
2022-04-27 11:50:29 +02:00
|
|
|
|
2022-04-30 13:19:27 +02:00
|
|
|
function tryToNormalizePagePath(page: string) {
|
|
|
|
try {
|
|
|
|
return normalizePagePath(page)
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err)
|
2022-06-06 20:35:26 +02:00
|
|
|
throw new PageNotFoundError(page)
|
2022-04-27 11:50:29 +02:00
|
|
|
}
|
|
|
|
}
|