2020-08-18 16:06:10 +02:00
|
|
|
import { ComponentType } from 'react'
|
2020-12-28 23:04:51 +01:00
|
|
|
import { ClientSsgManifest } from '../build'
|
2020-09-02 18:23:26 +02:00
|
|
|
import {
|
|
|
|
addBasePath,
|
2020-10-07 23:11:01 +02:00
|
|
|
addLocale,
|
2020-11-03 13:50:15 +01:00
|
|
|
interpolateAs,
|
2021-06-30 11:43:31 +02:00
|
|
|
} from '../shared/lib/router/router'
|
|
|
|
import getAssetPathFromRoute from '../shared/lib/router/utils/get-asset-path-from-route'
|
|
|
|
import { isDynamicRoute } from '../shared/lib/router/utils/is-dynamic'
|
|
|
|
import { parseRelativeUrl } from '../shared/lib/router/utils/parse-relative-url'
|
2021-03-14 13:58:34 +01:00
|
|
|
import { removePathTrailingSlash } from './normalize-trailing-slash'
|
2021-07-14 20:12:04 +02:00
|
|
|
import {
|
|
|
|
createRouteLoader,
|
2020-11-11 19:13:16 +01:00
|
|
|
getClientBuildManifest,
|
2021-10-20 19:52:11 +02:00
|
|
|
getMiddlewareManifest,
|
2020-11-11 19:13:16 +01:00
|
|
|
RouteLoader,
|
|
|
|
} from './route-loader'
|
2019-12-24 16:07:44 +01:00
|
|
|
|
2021-01-05 16:11:37 +01:00
|
|
|
function normalizeRoute(route: string): string {
|
2020-01-04 17:58:32 +01:00
|
|
|
if (route[0] !== '/') {
|
|
|
|
throw new Error(`Route name should start with a "/", got "${route}"`)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (route === '/') return route
|
|
|
|
return route.replace(/\/$/, '')
|
|
|
|
}
|
|
|
|
|
2020-08-26 18:34:53 +02:00
|
|
|
export type StyleSheetTuple = { href: string; text: string }
|
2020-08-17 23:20:05 +02:00
|
|
|
export type GoodPageCache = {
|
|
|
|
page: ComponentType
|
|
|
|
mod: any
|
2020-08-26 18:34:53 +02:00
|
|
|
styleSheets: StyleSheetTuple[]
|
2020-08-17 23:20:05 +02:00
|
|
|
}
|
2020-08-12 22:42:05 +02:00
|
|
|
|
2017-04-03 20:10:24 +02:00
|
|
|
export default class PageLoader {
|
2020-08-12 22:42:05 +02:00
|
|
|
private buildId: string
|
|
|
|
private assetPrefix: string
|
2020-11-11 19:13:16 +01:00
|
|
|
|
2020-08-12 22:42:05 +02:00
|
|
|
private promisedSsgManifest?: Promise<ClientSsgManifest>
|
2020-08-13 14:39:36 +02:00
|
|
|
private promisedDevPagesManifest?: Promise<any>
|
2021-10-26 18:50:56 +02:00
|
|
|
private promisedMiddlewareManifest?: Promise<[string, boolean][]>
|
2020-11-11 19:13:16 +01:00
|
|
|
public routeLoader: RouteLoader
|
2020-08-12 22:42:05 +02:00
|
|
|
|
2020-11-11 19:13:16 +01:00
|
|
|
constructor(buildId: string, assetPrefix: string) {
|
|
|
|
this.routeLoader = createRouteLoader(assetPrefix)
|
2020-08-17 23:20:05 +02:00
|
|
|
|
2017-04-03 20:10:24 +02:00
|
|
|
this.buildId = buildId
|
2017-04-18 06:18:43 +02:00
|
|
|
this.assetPrefix = assetPrefix
|
|
|
|
|
2020-03-02 18:14:40 +01:00
|
|
|
/** @type {Promise<Set<string>>} */
|
2020-05-18 21:24:37 +02:00
|
|
|
this.promisedSsgManifest = new Promise((resolve) => {
|
2020-08-12 22:42:05 +02:00
|
|
|
if ((window as any).__SSG_MANIFEST) {
|
|
|
|
resolve((window as any).__SSG_MANIFEST)
|
2020-03-02 18:14:40 +01:00
|
|
|
} else {
|
2020-08-12 22:42:05 +02:00
|
|
|
;(window as any).__SSG_MANIFEST_CB = () => {
|
|
|
|
resolve((window as any).__SSG_MANIFEST)
|
2020-03-02 18:14:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2019-08-08 19:14:33 +02:00
|
|
|
}
|
|
|
|
|
2020-08-13 14:39:36 +02:00
|
|
|
getPageList() {
|
|
|
|
if (process.env.NODE_ENV === 'production') {
|
2020-11-11 19:13:16 +01:00
|
|
|
return getClientBuildManifest().then((manifest) => manifest.sortedPages)
|
2020-08-13 14:39:36 +02:00
|
|
|
} else {
|
|
|
|
if ((window as any).__DEV_PAGES_MANIFEST) {
|
|
|
|
return (window as any).__DEV_PAGES_MANIFEST.pages
|
|
|
|
} else {
|
|
|
|
if (!this.promisedDevPagesManifest) {
|
|
|
|
this.promisedDevPagesManifest = fetch(
|
|
|
|
`${this.assetPrefix}/_next/static/development/_devPagesManifest.json`
|
|
|
|
)
|
|
|
|
.then((res) => res.json())
|
|
|
|
.then((manifest) => {
|
|
|
|
;(window as any).__DEV_PAGES_MANIFEST = manifest
|
|
|
|
return manifest.pages
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
console.log(`Failed to fetch devPagesManifest`, err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return this.promisedDevPagesManifest
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-26 18:50:56 +02:00
|
|
|
getMiddlewareList(): Promise<[string, boolean][]> {
|
2021-10-22 08:40:57 +02:00
|
|
|
if (process.env.NODE_ENV === 'production') {
|
|
|
|
return getMiddlewareManifest()
|
|
|
|
} else {
|
|
|
|
if ((window as any).__DEV_MIDDLEWARE_MANIFEST) {
|
|
|
|
return (window as any).__DEV_MIDDLEWARE_MANIFEST
|
2021-10-20 19:52:11 +02:00
|
|
|
} else {
|
2021-10-22 08:40:57 +02:00
|
|
|
if (!this.promisedMiddlewareManifest) {
|
|
|
|
this.promisedMiddlewareManifest = fetch(
|
|
|
|
`${this.assetPrefix}/_next/static/${this.buildId}/_devMiddlewareManifest.json`
|
|
|
|
)
|
|
|
|
.then((res) => res.json())
|
|
|
|
.then((manifest) => {
|
|
|
|
;(window as any).__DEV_MIDDLEWARE_MANIFEST = manifest
|
|
|
|
return manifest
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
console.log(`Failed to fetch _devMiddlewareManifest`, err)
|
|
|
|
})
|
2021-10-20 19:52:11 +02:00
|
|
|
}
|
2021-10-22 08:40:57 +02:00
|
|
|
return this.promisedMiddlewareManifest
|
2021-10-20 19:52:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-02 18:14:40 +01:00
|
|
|
/**
|
|
|
|
* @param {string} href the route href (file-system path)
|
|
|
|
* @param {string} asPath the URL as shown in browser (virtual path); used for dynamic routes
|
2021-01-05 16:11:37 +01:00
|
|
|
* @returns {string}
|
2020-03-02 18:14:40 +01:00
|
|
|
*/
|
2021-10-26 18:50:56 +02:00
|
|
|
getDataHref({
|
|
|
|
href,
|
|
|
|
asPath,
|
|
|
|
ssg,
|
|
|
|
rsc,
|
|
|
|
locale,
|
|
|
|
}: {
|
|
|
|
href: string
|
|
|
|
asPath: string
|
|
|
|
ssg: boolean
|
|
|
|
rsc?: boolean
|
2020-11-02 18:22:40 +01:00
|
|
|
locale?: string | false
|
2021-10-26 18:50:56 +02:00
|
|
|
}): string {
|
2020-09-03 20:26:52 +02:00
|
|
|
const { pathname: hrefPathname, query, search } = parseRelativeUrl(href)
|
2020-07-13 18:08:12 +02:00
|
|
|
const { pathname: asPathname } = parseRelativeUrl(asPath)
|
2020-06-29 17:14:45 +02:00
|
|
|
const route = normalizeRoute(hrefPathname)
|
|
|
|
|
2020-08-12 22:42:05 +02:00
|
|
|
const getHrefForSlug = (path: string) => {
|
2021-10-26 18:50:56 +02:00
|
|
|
if (rsc) return path + '?__flight__'
|
|
|
|
|
2021-03-14 13:58:34 +01:00
|
|
|
const dataRoute = getAssetPathFromRoute(
|
|
|
|
removePathTrailingSlash(addLocale(path, locale)),
|
|
|
|
'.json'
|
|
|
|
)
|
2020-07-29 18:51:51 +02:00
|
|
|
return addBasePath(
|
|
|
|
`/_next/data/${this.buildId}${dataRoute}${ssg ? '' : search}`
|
|
|
|
)
|
2020-04-26 17:14:39 +02:00
|
|
|
}
|
2020-03-02 18:14:40 +01:00
|
|
|
|
2020-09-02 18:23:26 +02:00
|
|
|
const isDynamic: boolean = isDynamicRoute(route)
|
|
|
|
const interpolatedRoute = isDynamic
|
2020-09-07 13:09:02 +02:00
|
|
|
? interpolateAs(hrefPathname, asPathname, query).result
|
2020-09-02 18:23:26 +02:00
|
|
|
: ''
|
2020-03-02 18:14:40 +01:00
|
|
|
|
|
|
|
return isDynamic
|
|
|
|
? interpolatedRoute && getHrefForSlug(interpolatedRoute)
|
|
|
|
: getHrefForSlug(route)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-03-26 16:19:48 +01:00
|
|
|
* @param {string} route - the route (file-system path)
|
2020-03-02 18:14:40 +01:00
|
|
|
*/
|
2021-03-26 16:19:48 +01:00
|
|
|
_isSsg(route: string): Promise<boolean> {
|
2020-11-05 10:51:24 +01:00
|
|
|
return this.promisedSsgManifest!.then((s: ClientSsgManifest) =>
|
|
|
|
s.has(route)
|
2020-08-12 22:42:05 +02:00
|
|
|
)
|
2020-03-02 18:14:40 +01:00
|
|
|
}
|
|
|
|
|
2020-08-13 06:01:15 +02:00
|
|
|
loadPage(route: string): Promise<GoodPageCache> {
|
2020-11-11 19:13:16 +01:00
|
|
|
return this.routeLoader.loadRoute(route).then((res) => {
|
|
|
|
if ('component' in res) {
|
|
|
|
return {
|
|
|
|
page: res.component,
|
|
|
|
mod: res.exports,
|
|
|
|
styleSheets: res.styles.map((o) => ({
|
|
|
|
href: o.href,
|
|
|
|
text: o.content,
|
|
|
|
})),
|
2019-08-08 19:14:33 +02:00
|
|
|
}
|
2017-04-11 18:37:59 +02:00
|
|
|
}
|
2020-11-11 19:13:16 +01:00
|
|
|
throw res.error
|
2017-04-03 20:10:24 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-11-11 19:13:16 +01:00
|
|
|
prefetch(route: string): Promise<void> {
|
|
|
|
return this.routeLoader.prefetch(route)
|
2018-11-25 00:47:39 +01:00
|
|
|
}
|
2017-04-03 20:10:24 +02:00
|
|
|
}
|