2019-03-13 15:56:20 +01:00
|
|
|
|
// tslint:disable:no-console
|
2019-05-29 13:57:26 +02:00
|
|
|
|
import { ParsedUrlQuery } from 'querystring'
|
|
|
|
|
import { ComponentType } from 'react'
|
2020-07-13 18:08:12 +02:00
|
|
|
|
import { UrlObject } from 'url'
|
2020-08-13 07:24:57 +02:00
|
|
|
|
import {
|
|
|
|
|
normalizePathTrailingSlash,
|
|
|
|
|
removePathTrailingSlash,
|
|
|
|
|
} from '../../../client/normalize-trailing-slash'
|
2020-08-26 18:34:53 +02:00
|
|
|
|
import { GoodPageCache, StyleSheetTuple } from '../../../client/page-loader'
|
2020-11-11 19:13:16 +01:00
|
|
|
|
import {
|
|
|
|
|
getClientBuildManifest,
|
|
|
|
|
isAssetError,
|
|
|
|
|
markAssetError,
|
|
|
|
|
} from '../../../client/route-loader'
|
2020-12-30 07:44:07 +01:00
|
|
|
|
import { DomainLocales } from '../../server/config'
|
2020-08-18 16:06:10 +02:00
|
|
|
|
import { denormalizePagePath } from '../../server/denormalize-page-path'
|
2020-12-12 15:06:46 +01:00
|
|
|
|
import { normalizeLocalePath } from '../i18n/normalize-locale-path'
|
2019-05-29 13:57:26 +02:00
|
|
|
|
import mitt, { MittEmitter } from '../mitt'
|
2019-05-27 20:20:33 +02:00
|
|
|
|
import {
|
2019-05-29 13:57:26 +02:00
|
|
|
|
AppContextType,
|
|
|
|
|
formatWithValidation,
|
2020-08-13 07:24:57 +02:00
|
|
|
|
getLocationOrigin,
|
2019-05-29 13:57:26 +02:00
|
|
|
|
getURL,
|
|
|
|
|
loadGetInitialProps,
|
|
|
|
|
NextPageContext,
|
2020-01-04 21:53:33 +01:00
|
|
|
|
ST,
|
2020-12-31 17:54:32 +01:00
|
|
|
|
NEXT_DATA,
|
2019-05-29 13:57:26 +02:00
|
|
|
|
} from '../utils'
|
2019-11-11 04:24:53 +01:00
|
|
|
|
import { isDynamicRoute } from './utils/is-dynamic'
|
2020-08-13 07:24:57 +02:00
|
|
|
|
import { parseRelativeUrl } from './utils/parse-relative-url'
|
|
|
|
|
import { searchParamsToUrlQuery } from './utils/querystring'
|
2020-08-18 16:06:10 +02:00
|
|
|
|
import resolveRewrites from './utils/resolve-rewrites'
|
2019-05-31 00:44:27 +02:00
|
|
|
|
import { getRouteMatcher } from './utils/route-matcher'
|
2019-05-30 22:42:45 +02:00
|
|
|
|
import { getRouteRegex } from './utils/route-regex'
|
2019-03-23 23:00:46 +01:00
|
|
|
|
|
2020-12-31 17:54:32 +01:00
|
|
|
|
declare global {
|
|
|
|
|
interface Window {
|
|
|
|
|
/* prod */
|
|
|
|
|
__NEXT_DATA__: NEXT_DATA
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-17 10:14:50 +01:00
|
|
|
|
interface RouteProperties {
|
|
|
|
|
shallow: boolean
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-01 20:13:03 +02:00
|
|
|
|
interface TransitionOptions {
|
|
|
|
|
shallow?: boolean
|
2020-10-22 17:08:01 +02:00
|
|
|
|
locale?: string | false
|
2020-12-30 05:33:08 +01:00
|
|
|
|
scroll?: boolean
|
2020-08-01 20:13:03 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface NextHistoryState {
|
|
|
|
|
url: string
|
|
|
|
|
as: string
|
|
|
|
|
options: TransitionOptions
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-31 17:08:12 +01:00
|
|
|
|
type HistoryState =
|
|
|
|
|
| null
|
|
|
|
|
| { __N: false }
|
|
|
|
|
| ({ __N: true; idx: number } & NextHistoryState)
|
2020-08-01 20:13:03 +02:00
|
|
|
|
|
2020-12-30 07:44:07 +01:00
|
|
|
|
let detectDomainLocale: typeof import('../i18n/detect-domain-locale').detectDomainLocale
|
|
|
|
|
|
|
|
|
|
if (process.env.__NEXT_I18N_SUPPORT) {
|
|
|
|
|
detectDomainLocale = require('../i18n/detect-domain-locale')
|
|
|
|
|
.detectDomainLocale
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-15 01:12:02 +02:00
|
|
|
|
const basePath = (process.env.__NEXT_ROUTER_BASEPATH as string) || ''
|
2020-04-14 09:50:39 +02:00
|
|
|
|
|
2020-07-21 19:33:11 +02:00
|
|
|
|
function buildCancellationError() {
|
|
|
|
|
return Object.assign(new Error('Route Cancelled'), {
|
|
|
|
|
cancelled: true,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-07 23:11:01 +02:00
|
|
|
|
function addPathPrefix(path: string, prefix?: string) {
|
|
|
|
|
return prefix && path.startsWith('/')
|
|
|
|
|
? path === '/'
|
|
|
|
|
? normalizePathTrailingSlash(prefix)
|
2021-01-06 02:20:04 +01:00
|
|
|
|
: `${prefix}${pathNoQueryHash(path) === '/' ? path.substring(1) : path}`
|
2020-10-07 23:11:01 +02:00
|
|
|
|
: path
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-31 09:07:51 +01:00
|
|
|
|
export function getDomainLocale(
|
|
|
|
|
path: string,
|
|
|
|
|
locale?: string | false,
|
|
|
|
|
locales?: string[],
|
|
|
|
|
domainLocales?: DomainLocales
|
|
|
|
|
) {
|
|
|
|
|
if (process.env.__NEXT_I18N_SUPPORT) {
|
|
|
|
|
locale = locale || normalizeLocalePath(path, locales).detectedLocale
|
|
|
|
|
|
|
|
|
|
const detectedDomain = detectDomainLocale(domainLocales, undefined, locale)
|
|
|
|
|
|
|
|
|
|
if (detectedDomain) {
|
|
|
|
|
return `http${detectedDomain.http ? '' : 's'}://${detectedDomain.domain}${
|
|
|
|
|
basePath || ''
|
|
|
|
|
}${locale === detectedDomain.defaultLocale ? '' : `/${locale}`}${path}`
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-08 13:12:17 +02:00
|
|
|
|
export function addLocale(
|
|
|
|
|
path: string,
|
2020-10-22 17:08:01 +02:00
|
|
|
|
locale?: string | false,
|
2020-10-08 13:12:17 +02:00
|
|
|
|
defaultLocale?: string
|
|
|
|
|
) {
|
2020-10-20 12:23:11 +02:00
|
|
|
|
if (process.env.__NEXT_I18N_SUPPORT) {
|
2021-04-22 05:06:26 +02:00
|
|
|
|
const pathname = pathNoQueryHash(path)
|
|
|
|
|
const pathLower = pathname.toLowerCase()
|
2021-04-18 11:01:01 +02:00
|
|
|
|
const localeLower = locale && locale.toLowerCase()
|
|
|
|
|
|
2020-11-03 00:32:56 +01:00
|
|
|
|
return locale &&
|
|
|
|
|
locale !== defaultLocale &&
|
2021-04-18 11:01:01 +02:00
|
|
|
|
!pathLower.startsWith('/' + localeLower + '/') &&
|
|
|
|
|
pathLower !== '/' + localeLower
|
2020-10-07 23:11:01 +02:00
|
|
|
|
? addPathPrefix(path, '/' + locale)
|
|
|
|
|
: path
|
|
|
|
|
}
|
|
|
|
|
return path
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function delLocale(path: string, locale?: string) {
|
2020-10-20 12:23:11 +02:00
|
|
|
|
if (process.env.__NEXT_I18N_SUPPORT) {
|
2021-04-06 12:25:04 +02:00
|
|
|
|
const pathname = pathNoQueryHash(path)
|
2021-04-18 11:01:01 +02:00
|
|
|
|
const pathLower = pathname.toLowerCase()
|
|
|
|
|
const localeLower = locale && locale.toLowerCase()
|
|
|
|
|
|
2020-11-03 00:32:56 +01:00
|
|
|
|
return locale &&
|
2021-04-18 11:01:01 +02:00
|
|
|
|
(pathLower.startsWith('/' + localeLower + '/') ||
|
|
|
|
|
pathLower === '/' + localeLower)
|
2021-04-06 12:25:04 +02:00
|
|
|
|
? (pathname.length === locale.length + 1 ? '/' : '') +
|
|
|
|
|
path.substr(locale.length + 1)
|
2020-10-07 23:11:01 +02:00
|
|
|
|
: path
|
|
|
|
|
}
|
|
|
|
|
return path
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-06 02:20:04 +01:00
|
|
|
|
function pathNoQueryHash(path: string) {
|
2020-12-30 06:37:26 +01:00
|
|
|
|
const queryIndex = path.indexOf('?')
|
|
|
|
|
const hashIndex = path.indexOf('#')
|
|
|
|
|
|
|
|
|
|
if (queryIndex > -1 || hashIndex > -1) {
|
|
|
|
|
path = path.substring(0, queryIndex > -1 ? queryIndex : hashIndex)
|
|
|
|
|
}
|
2021-01-06 02:20:04 +01:00
|
|
|
|
return path
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function hasBasePath(path: string): boolean {
|
|
|
|
|
path = pathNoQueryHash(path)
|
2020-08-01 13:51:47 +02:00
|
|
|
|
return path === basePath || path.startsWith(basePath + '/')
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-14 09:50:39 +02:00
|
|
|
|
export function addBasePath(path: string): string {
|
2020-08-05 20:12:17 +02:00
|
|
|
|
// we only add the basepath on relative urls
|
2020-10-07 23:11:01 +02:00
|
|
|
|
return addPathPrefix(path, basePath)
|
2020-04-14 09:50:39 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-26 17:14:39 +02:00
|
|
|
|
export function delBasePath(path: string): string {
|
2021-01-06 02:20:04 +01:00
|
|
|
|
path = path.slice(basePath.length)
|
|
|
|
|
if (!path.startsWith('/')) path = `/${path}`
|
|
|
|
|
return path
|
2019-03-23 23:00:46 +01:00
|
|
|
|
}
|
2019-03-13 15:56:20 +01:00
|
|
|
|
|
2020-08-05 20:12:17 +02:00
|
|
|
|
/**
|
|
|
|
|
* Detects whether a given url is routable by the Next.js router (browser only).
|
|
|
|
|
*/
|
|
|
|
|
export function isLocalURL(url: string): boolean {
|
2021-01-15 17:53:10 +01:00
|
|
|
|
// prevent a hydration mismatch on href for url with anchor refs
|
2021-05-28 14:51:41 +02:00
|
|
|
|
if (url.startsWith('/') || url.startsWith('#') || url.startsWith('?'))
|
|
|
|
|
return true
|
2020-08-05 20:12:17 +02:00
|
|
|
|
try {
|
|
|
|
|
// absolute urls can be local if they are on the same origin
|
|
|
|
|
const locationOrigin = getLocationOrigin()
|
|
|
|
|
const resolved = new URL(url, locationOrigin)
|
|
|
|
|
return resolved.origin === locationOrigin && hasBasePath(resolved.pathname)
|
|
|
|
|
} catch (_) {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-10 06:10:44 +02:00
|
|
|
|
type Url = UrlObject | string
|
|
|
|
|
|
2020-09-02 18:23:26 +02:00
|
|
|
|
export function interpolateAs(
|
|
|
|
|
route: string,
|
|
|
|
|
asPathname: string,
|
|
|
|
|
query: ParsedUrlQuery
|
|
|
|
|
) {
|
|
|
|
|
let interpolatedRoute = ''
|
|
|
|
|
|
|
|
|
|
const dynamicRegex = getRouteRegex(route)
|
|
|
|
|
const dynamicGroups = dynamicRegex.groups
|
|
|
|
|
const dynamicMatches =
|
|
|
|
|
// Try to match the dynamic route against the asPath
|
|
|
|
|
(asPathname !== route ? getRouteMatcher(dynamicRegex)(asPathname) : '') ||
|
|
|
|
|
// Fall back to reading the values from the href
|
|
|
|
|
// TODO: should this take priority; also need to change in the router.
|
|
|
|
|
query
|
|
|
|
|
|
|
|
|
|
interpolatedRoute = route
|
2020-09-07 13:09:02 +02:00
|
|
|
|
const params = Object.keys(dynamicGroups)
|
|
|
|
|
|
2020-09-02 18:23:26 +02:00
|
|
|
|
if (
|
2020-09-07 13:09:02 +02:00
|
|
|
|
!params.every((param) => {
|
2020-09-02 18:23:26 +02:00
|
|
|
|
let value = dynamicMatches[param] || ''
|
|
|
|
|
const { repeat, optional } = dynamicGroups[param]
|
|
|
|
|
|
|
|
|
|
// support single-level catch-all
|
|
|
|
|
// TODO: more robust handling for user-error (passing `/`)
|
|
|
|
|
let replaced = `[${repeat ? '...' : ''}${param}]`
|
|
|
|
|
if (optional) {
|
|
|
|
|
replaced = `${!value ? '/' : ''}[${replaced}]`
|
|
|
|
|
}
|
|
|
|
|
if (repeat && !Array.isArray(value)) value = [value]
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
(optional || param in dynamicMatches) &&
|
|
|
|
|
// Interpolate group into data URL if present
|
|
|
|
|
(interpolatedRoute =
|
|
|
|
|
interpolatedRoute!.replace(
|
|
|
|
|
replaced,
|
|
|
|
|
repeat
|
2020-12-28 21:08:58 +01:00
|
|
|
|
? (value as string[])
|
|
|
|
|
.map(
|
|
|
|
|
// these values should be fully encoded instead of just
|
|
|
|
|
// path delimiter escaped since they are being inserted
|
|
|
|
|
// into the URL and we expect URL encoded segments
|
|
|
|
|
// when parsing dynamic route params
|
|
|
|
|
(segment) => encodeURIComponent(segment)
|
|
|
|
|
)
|
|
|
|
|
.join('/')
|
|
|
|
|
: encodeURIComponent(value as string)
|
2020-09-02 18:23:26 +02:00
|
|
|
|
) || '/')
|
|
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
) {
|
|
|
|
|
interpolatedRoute = '' // did not satisfy all requirements
|
|
|
|
|
|
|
|
|
|
// n.b. We ignore this error because we handle warning for this case in
|
|
|
|
|
// development in the `<Link>` component directly.
|
|
|
|
|
}
|
2020-09-07 13:09:02 +02:00
|
|
|
|
return {
|
|
|
|
|
params,
|
|
|
|
|
result: interpolatedRoute,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function omitParmsFromQuery(query: ParsedUrlQuery, params: string[]) {
|
|
|
|
|
const filteredQuery: ParsedUrlQuery = {}
|
|
|
|
|
|
|
|
|
|
Object.keys(query).forEach((key) => {
|
|
|
|
|
if (!params.includes(key)) {
|
|
|
|
|
filteredQuery[key] = query[key]
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
return filteredQuery
|
2020-09-02 18:23:26 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-13 18:08:12 +02:00
|
|
|
|
/**
|
|
|
|
|
* Resolves a given hyperlink with a certain router state (basePath not included).
|
|
|
|
|
* Preserves absolute urls.
|
|
|
|
|
*/
|
2020-09-02 18:23:26 +02:00
|
|
|
|
export function resolveHref(
|
2021-05-28 14:51:41 +02:00
|
|
|
|
router: NextRouter,
|
2020-09-02 18:23:26 +02:00
|
|
|
|
href: Url,
|
|
|
|
|
resolveAs?: boolean
|
|
|
|
|
): string {
|
2020-07-13 18:08:12 +02:00
|
|
|
|
// we use a dummy base url for relative urls
|
2021-04-30 18:34:23 +02:00
|
|
|
|
let base: URL
|
2021-05-28 14:51:41 +02:00
|
|
|
|
const urlAsString =
|
|
|
|
|
typeof href === 'string' ? href : formatWithValidation(href)
|
2021-04-30 18:34:23 +02:00
|
|
|
|
|
|
|
|
|
try {
|
2021-05-28 14:51:41 +02:00
|
|
|
|
base = new URL(
|
|
|
|
|
urlAsString.startsWith('#') ? router.asPath : router.pathname,
|
|
|
|
|
'http://n'
|
|
|
|
|
)
|
2021-04-30 18:34:23 +02:00
|
|
|
|
} catch (_) {
|
|
|
|
|
// fallback to / for invalid asPath values e.g. //
|
|
|
|
|
base = new URL('/', 'http://n')
|
|
|
|
|
}
|
2020-11-12 19:10:40 +01:00
|
|
|
|
// Return because it cannot be routed by the Next.js router
|
|
|
|
|
if (!isLocalURL(urlAsString)) {
|
|
|
|
|
return (resolveAs ? [urlAsString] : urlAsString) as string
|
|
|
|
|
}
|
2020-08-05 20:12:17 +02:00
|
|
|
|
try {
|
|
|
|
|
const finalUrl = new URL(urlAsString, base)
|
|
|
|
|
finalUrl.pathname = normalizePathTrailingSlash(finalUrl.pathname)
|
2020-09-02 18:23:26 +02:00
|
|
|
|
let interpolatedAs = ''
|
|
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
isDynamicRoute(finalUrl.pathname) &&
|
|
|
|
|
finalUrl.searchParams &&
|
|
|
|
|
resolveAs
|
|
|
|
|
) {
|
|
|
|
|
const query = searchParamsToUrlQuery(finalUrl.searchParams)
|
|
|
|
|
|
2020-09-07 13:09:02 +02:00
|
|
|
|
const { result, params } = interpolateAs(
|
2020-09-02 18:23:26 +02:00
|
|
|
|
finalUrl.pathname,
|
|
|
|
|
finalUrl.pathname,
|
|
|
|
|
query
|
|
|
|
|
)
|
2020-09-10 22:05:02 +02:00
|
|
|
|
|
|
|
|
|
if (result) {
|
|
|
|
|
interpolatedAs = formatWithValidation({
|
|
|
|
|
pathname: result,
|
|
|
|
|
hash: finalUrl.hash,
|
|
|
|
|
query: omitParmsFromQuery(query, params),
|
|
|
|
|
})
|
|
|
|
|
}
|
2020-09-02 18:23:26 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-05 20:12:17 +02:00
|
|
|
|
// if the origin didn't change, it means we received a relative href
|
2020-09-02 18:23:26 +02:00
|
|
|
|
const resolvedHref =
|
|
|
|
|
finalUrl.origin === base.origin
|
|
|
|
|
? finalUrl.href.slice(finalUrl.origin.length)
|
|
|
|
|
: finalUrl.href
|
|
|
|
|
|
2020-09-10 22:05:02 +02:00
|
|
|
|
return (resolveAs
|
|
|
|
|
? [resolvedHref, interpolatedAs || resolvedHref]
|
|
|
|
|
: resolvedHref) as string
|
2020-08-05 20:12:17 +02:00
|
|
|
|
} catch (_) {
|
2020-09-02 18:23:26 +02:00
|
|
|
|
return (resolveAs ? [urlAsString] : urlAsString) as string
|
2020-08-05 20:12:17 +02:00
|
|
|
|
}
|
2020-06-29 16:50:45 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-06 02:20:04 +01:00
|
|
|
|
function stripOrigin(url: string) {
|
|
|
|
|
const origin = getLocationOrigin()
|
|
|
|
|
|
|
|
|
|
return url.startsWith(origin) ? url.substring(origin.length) : url
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-28 21:08:58 +01:00
|
|
|
|
function prepareUrlAs(router: NextRouter, url: Url, as?: Url) {
|
2020-06-08 17:59:50 +02:00
|
|
|
|
// If url and as provided as an object representation,
|
|
|
|
|
// we'll format them into the string version here.
|
2021-05-28 14:51:41 +02:00
|
|
|
|
let [resolvedHref, resolvedAs] = resolveHref(router, url, true)
|
2021-01-06 02:20:04 +01:00
|
|
|
|
const origin = getLocationOrigin()
|
|
|
|
|
const hrefHadOrigin = resolvedHref.startsWith(origin)
|
|
|
|
|
const asHadOrigin = resolvedAs && resolvedAs.startsWith(origin)
|
|
|
|
|
|
|
|
|
|
resolvedHref = stripOrigin(resolvedHref)
|
|
|
|
|
resolvedAs = resolvedAs ? stripOrigin(resolvedAs) : resolvedAs
|
|
|
|
|
|
|
|
|
|
const preparedUrl = hrefHadOrigin ? resolvedHref : addBasePath(resolvedHref)
|
|
|
|
|
const preparedAs = as
|
2021-05-28 14:51:41 +02:00
|
|
|
|
? stripOrigin(resolveHref(router, as))
|
2021-01-06 02:20:04 +01:00
|
|
|
|
: resolvedAs || resolvedHref
|
|
|
|
|
|
2020-06-08 17:59:50 +02:00
|
|
|
|
return {
|
2021-01-06 02:20:04 +01:00
|
|
|
|
url: preparedUrl,
|
|
|
|
|
as: asHadOrigin ? preparedAs : addBasePath(preparedAs),
|
2020-07-13 18:08:12 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-26 16:19:48 +01:00
|
|
|
|
function resolveDynamicRoute(pathname: string, pages: string[]) {
|
|
|
|
|
const cleanPathname = removePathTrailingSlash(denormalizePagePath(pathname!))
|
2021-02-24 16:37:13 +01:00
|
|
|
|
|
|
|
|
|
if (cleanPathname === '/404' || cleanPathname === '/_error') {
|
2021-03-26 16:19:48 +01:00
|
|
|
|
return pathname
|
2021-02-24 16:37:13 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// handle resolving href for dynamic routes
|
|
|
|
|
if (!pages.includes(cleanPathname!)) {
|
|
|
|
|
// eslint-disable-next-line array-callback-return
|
|
|
|
|
pages.some((page) => {
|
|
|
|
|
if (isDynamicRoute(page) && getRouteRegex(page).re.test(cleanPathname!)) {
|
2021-03-26 16:19:48 +01:00
|
|
|
|
pathname = page
|
2021-02-24 16:37:13 +01:00
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
2021-03-26 16:19:48 +01:00
|
|
|
|
return removePathTrailingSlash(pathname)
|
2021-02-24 16:37:13 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-04-26 09:37:57 +02:00
|
|
|
|
export type BaseRouter = {
|
2019-03-13 15:56:20 +01:00
|
|
|
|
route: string
|
|
|
|
|
pathname: string
|
2019-05-20 17:21:15 +02:00
|
|
|
|
query: ParsedUrlQuery
|
2019-05-29 13:57:26 +02:00
|
|
|
|
asPath: string
|
2020-04-14 09:50:39 +02:00
|
|
|
|
basePath: string
|
2020-10-07 23:11:01 +02:00
|
|
|
|
locale?: string
|
|
|
|
|
locales?: string[]
|
2020-10-08 13:12:17 +02:00
|
|
|
|
defaultLocale?: string
|
2020-12-31 09:07:51 +01:00
|
|
|
|
domainLocales?: DomainLocales
|
2021-02-11 11:18:24 +01:00
|
|
|
|
isLocaleDomain: boolean
|
2019-03-13 15:56:20 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-11 19:35:39 +02:00
|
|
|
|
export type NextRouter = BaseRouter &
|
|
|
|
|
Pick<
|
|
|
|
|
Router,
|
|
|
|
|
| 'push'
|
|
|
|
|
| 'replace'
|
|
|
|
|
| 'reload'
|
|
|
|
|
| 'back'
|
|
|
|
|
| 'prefetch'
|
|
|
|
|
| 'beforePopState'
|
|
|
|
|
| 'events'
|
2020-02-15 19:01:10 +01:00
|
|
|
|
| 'isFallback'
|
2021-01-06 18:44:35 +01:00
|
|
|
|
| 'isReady'
|
2021-02-18 19:34:33 +01:00
|
|
|
|
| 'isPreview'
|
2019-07-11 19:35:39 +02:00
|
|
|
|
>
|
|
|
|
|
|
2020-02-17 21:46:15 +01:00
|
|
|
|
export type PrefetchOptions = {
|
|
|
|
|
priority?: boolean
|
2020-11-02 18:22:40 +01:00
|
|
|
|
locale?: string | false
|
2020-02-17 21:46:15 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-11 19:13:16 +01:00
|
|
|
|
export type PrivateRouteInfo =
|
|
|
|
|
| (Omit<CompletePrivateRouteInfo, 'styleSheets'> & { initial: true })
|
|
|
|
|
| CompletePrivateRouteInfo
|
|
|
|
|
|
|
|
|
|
export type CompletePrivateRouteInfo = {
|
2019-05-29 13:57:26 +02:00
|
|
|
|
Component: ComponentType
|
2020-08-26 18:34:53 +02:00
|
|
|
|
styleSheets: StyleSheetTuple[]
|
2020-03-06 05:15:10 +01:00
|
|
|
|
__N_SSG?: boolean
|
|
|
|
|
__N_SSP?: boolean
|
2020-08-13 19:39:33 +02:00
|
|
|
|
props?: Record<string, any>
|
2019-03-13 15:56:20 +01:00
|
|
|
|
err?: Error
|
2019-05-29 13:57:26 +02:00
|
|
|
|
error?: any
|
2019-03-13 15:56:20 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-11 19:13:16 +01:00
|
|
|
|
export type AppProps = Pick<CompletePrivateRouteInfo, 'Component' | 'err'> & {
|
2020-08-13 19:39:33 +02:00
|
|
|
|
router: Router
|
|
|
|
|
} & Record<string, any>
|
|
|
|
|
export type AppComponent = ComponentType<AppProps>
|
|
|
|
|
|
2020-12-30 05:33:08 +01:00
|
|
|
|
type Subscription = (
|
|
|
|
|
data: PrivateRouteInfo,
|
|
|
|
|
App: AppComponent,
|
2020-12-31 17:08:12 +01:00
|
|
|
|
resetScroll: { x: number; y: number } | null
|
2020-12-30 05:33:08 +01:00
|
|
|
|
) => Promise<void>
|
2017-05-14 02:25:57 +02:00
|
|
|
|
|
2020-08-01 20:13:03 +02:00
|
|
|
|
type BeforePopStateCallback = (state: NextHistoryState) => boolean
|
2016-10-06 01:52:50 +02:00
|
|
|
|
|
2019-06-05 20:15:42 +02:00
|
|
|
|
type ComponentLoadCancel = (() => void) | null
|
|
|
|
|
|
2020-02-17 22:16:19 +01:00
|
|
|
|
type HistoryMethod = 'replaceState' | 'pushState'
|
|
|
|
|
|
2020-07-06 16:27:45 +02:00
|
|
|
|
const manualScrollRestoration =
|
|
|
|
|
process.env.__NEXT_SCROLL_RESTORATION &&
|
|
|
|
|
typeof window !== 'undefined' &&
|
2020-12-31 17:08:12 +01:00
|
|
|
|
'scrollRestoration' in window.history &&
|
|
|
|
|
!!(function () {
|
|
|
|
|
try {
|
|
|
|
|
let v = '__next'
|
|
|
|
|
// eslint-disable-next-line no-sequences
|
|
|
|
|
return sessionStorage.setItem(v, v), sessionStorage.removeItem(v), true
|
|
|
|
|
} catch (n) {}
|
|
|
|
|
})()
|
2020-07-06 16:27:45 +02:00
|
|
|
|
|
2020-12-30 06:17:10 +01:00
|
|
|
|
const SSG_DATA_NOT_FOUND = Symbol('SSG_DATA_NOT_FOUND')
|
2020-10-15 23:55:38 +02:00
|
|
|
|
|
2020-07-19 06:02:01 +02:00
|
|
|
|
function fetchRetry(url: string, attempts: number): Promise<any> {
|
|
|
|
|
return fetch(url, {
|
|
|
|
|
// Cookies are required to be present for Next.js' SSG "Preview Mode".
|
|
|
|
|
// Cookies may also be required for `getServerSideProps`.
|
|
|
|
|
//
|
|
|
|
|
// > `fetch` won’t send cookies, unless you set the credentials init
|
|
|
|
|
// > option.
|
|
|
|
|
// https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch
|
|
|
|
|
//
|
|
|
|
|
// > For maximum browser compatibility when it comes to sending &
|
|
|
|
|
// > receiving cookies, always supply the `credentials: 'same-origin'`
|
|
|
|
|
// > option instead of relying on the default.
|
|
|
|
|
// https://github.com/github/fetch#caveats
|
|
|
|
|
credentials: 'same-origin',
|
|
|
|
|
}).then((res) => {
|
|
|
|
|
if (!res.ok) {
|
|
|
|
|
if (attempts > 1 && res.status >= 500) {
|
|
|
|
|
return fetchRetry(url, attempts - 1)
|
2020-01-27 23:50:59 +01:00
|
|
|
|
}
|
2020-10-15 23:55:38 +02:00
|
|
|
|
if (res.status === 404) {
|
2020-12-30 23:35:02 +01:00
|
|
|
|
return res.json().then((data) => {
|
|
|
|
|
if (data.notFound) {
|
|
|
|
|
return { notFound: SSG_DATA_NOT_FOUND }
|
|
|
|
|
}
|
|
|
|
|
throw new Error(`Failed to load static props`)
|
|
|
|
|
})
|
2020-10-15 23:55:38 +02:00
|
|
|
|
}
|
2020-07-19 06:02:01 +02:00
|
|
|
|
throw new Error(`Failed to load static props`)
|
|
|
|
|
}
|
|
|
|
|
return res.json()
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function fetchNextData(dataHref: string, isServerRender: boolean) {
|
|
|
|
|
return fetchRetry(dataHref, isServerRender ? 3 : 1).catch((err: Error) => {
|
|
|
|
|
// We should only trigger a server-side transition if this was caused
|
|
|
|
|
// on a client-side transition. Otherwise, we'd get into an infinite
|
|
|
|
|
// loop.
|
2020-10-15 23:55:38 +02:00
|
|
|
|
|
2020-10-22 16:54:38 +02:00
|
|
|
|
if (!isServerRender) {
|
2020-11-11 19:13:16 +01:00
|
|
|
|
markAssetError(err)
|
2020-07-19 06:02:01 +02:00
|
|
|
|
}
|
|
|
|
|
throw err
|
|
|
|
|
})
|
2020-01-27 23:50:59 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-04-26 09:37:57 +02:00
|
|
|
|
export default class Router implements BaseRouter {
|
2019-03-13 15:56:20 +01:00
|
|
|
|
route: string
|
|
|
|
|
pathname: string
|
2019-05-20 17:21:15 +02:00
|
|
|
|
query: ParsedUrlQuery
|
2019-03-13 15:56:20 +01:00
|
|
|
|
asPath: string
|
2020-04-14 09:50:39 +02:00
|
|
|
|
basePath: string
|
|
|
|
|
|
2019-05-23 21:31:22 +02:00
|
|
|
|
/**
|
|
|
|
|
* Map of all components loaded in `Router`
|
|
|
|
|
*/
|
2020-08-13 19:39:33 +02:00
|
|
|
|
components: { [pathname: string]: PrivateRouteInfo }
|
2019-12-30 23:57:06 +01:00
|
|
|
|
// Static Data Cache
|
|
|
|
|
sdc: { [asPath: string]: object } = {}
|
2021-03-05 18:16:02 +01:00
|
|
|
|
// In-flight Server Data Requests, for deduping
|
|
|
|
|
sdr: { [asPath: string]: Promise<object> } = {}
|
|
|
|
|
|
2019-06-05 20:15:42 +02:00
|
|
|
|
sub: Subscription
|
|
|
|
|
clc: ComponentLoadCancel
|
2019-03-13 15:56:20 +01:00
|
|
|
|
pageLoader: any
|
2019-03-14 19:43:29 +01:00
|
|
|
|
_bps: BeforePopStateCallback | undefined
|
2019-07-11 19:35:39 +02:00
|
|
|
|
events: MittEmitter
|
2020-08-13 19:39:33 +02:00
|
|
|
|
_wrapApp: (App: AppComponent) => any
|
2019-11-12 22:37:47 +01:00
|
|
|
|
isSsr: boolean
|
2020-02-15 19:01:10 +01:00
|
|
|
|
isFallback: boolean
|
2020-07-20 22:03:49 +02:00
|
|
|
|
_inFlightRoute?: string
|
2020-08-22 23:51:14 +02:00
|
|
|
|
_shallow?: boolean
|
2020-10-07 23:11:01 +02:00
|
|
|
|
locale?: string
|
|
|
|
|
locales?: string[]
|
2020-10-08 13:12:17 +02:00
|
|
|
|
defaultLocale?: string
|
2020-12-30 07:44:07 +01:00
|
|
|
|
domainLocales?: DomainLocales
|
2020-12-31 17:54:32 +01:00
|
|
|
|
isReady: boolean
|
2021-02-18 19:34:33 +01:00
|
|
|
|
isPreview: boolean
|
2021-02-11 11:18:24 +01:00
|
|
|
|
isLocaleDomain: boolean
|
2019-03-13 15:56:20 +01:00
|
|
|
|
|
2020-12-31 17:08:12 +01:00
|
|
|
|
private _idx: number = 0
|
|
|
|
|
|
2019-03-13 15:56:20 +01:00
|
|
|
|
static events: MittEmitter = mitt()
|
|
|
|
|
|
2019-05-29 13:57:26 +02:00
|
|
|
|
constructor(
|
|
|
|
|
pathname: string,
|
|
|
|
|
query: ParsedUrlQuery,
|
|
|
|
|
as: string,
|
|
|
|
|
{
|
|
|
|
|
initialProps,
|
|
|
|
|
pageLoader,
|
|
|
|
|
App,
|
2019-07-30 20:00:19 +02:00
|
|
|
|
wrapApp,
|
2019-05-29 13:57:26 +02:00
|
|
|
|
Component,
|
|
|
|
|
err,
|
2019-06-05 20:15:42 +02:00
|
|
|
|
subscription,
|
2020-02-15 19:01:10 +01:00
|
|
|
|
isFallback,
|
2020-10-07 23:11:01 +02:00
|
|
|
|
locale,
|
|
|
|
|
locales,
|
2020-10-08 13:12:17 +02:00
|
|
|
|
defaultLocale,
|
2020-12-30 07:44:07 +01:00
|
|
|
|
domainLocales,
|
2021-02-18 19:34:33 +01:00
|
|
|
|
isPreview,
|
2019-05-29 13:57:26 +02:00
|
|
|
|
}: {
|
2019-06-05 20:15:42 +02:00
|
|
|
|
subscription: Subscription
|
2019-05-29 13:57:26 +02:00
|
|
|
|
initialProps: any
|
|
|
|
|
pageLoader: any
|
|
|
|
|
Component: ComponentType
|
2020-08-13 19:39:33 +02:00
|
|
|
|
App: AppComponent
|
2021-04-25 20:34:36 +02:00
|
|
|
|
wrapApp: (WrapAppComponent: AppComponent) => any
|
2019-05-29 13:57:26 +02:00
|
|
|
|
err?: Error
|
2020-02-15 19:01:10 +01:00
|
|
|
|
isFallback: boolean
|
2020-10-07 23:11:01 +02:00
|
|
|
|
locale?: string
|
|
|
|
|
locales?: string[]
|
2020-10-08 13:12:17 +02:00
|
|
|
|
defaultLocale?: string
|
2020-12-30 07:44:07 +01:00
|
|
|
|
domainLocales?: DomainLocales
|
2021-02-18 19:34:33 +01:00
|
|
|
|
isPreview?: boolean
|
2019-05-29 13:57:26 +02:00
|
|
|
|
}
|
|
|
|
|
) {
|
2016-10-08 07:12:51 +02:00
|
|
|
|
// represents the current component key
|
2020-07-02 05:48:10 +02:00
|
|
|
|
this.route = removePathTrailingSlash(pathname)
|
2016-10-06 01:52:50 +02:00
|
|
|
|
|
|
|
|
|
// set up the component cache (by route keys)
|
2017-04-18 06:18:43 +02:00
|
|
|
|
this.components = {}
|
|
|
|
|
// We should not keep the cache, if there's an error
|
|
|
|
|
// Otherwise, this cause issues when when going back and
|
|
|
|
|
// come again to the errored page.
|
2019-01-31 21:45:36 +01:00
|
|
|
|
if (pathname !== '/_error') {
|
2020-03-06 05:15:10 +01:00
|
|
|
|
this.components[this.route] = {
|
|
|
|
|
Component,
|
2020-11-11 19:13:16 +01:00
|
|
|
|
initial: true,
|
2020-03-06 05:15:10 +01:00
|
|
|
|
props: initialProps,
|
|
|
|
|
err,
|
|
|
|
|
__N_SSG: initialProps && initialProps.__N_SSG,
|
|
|
|
|
__N_SSP: initialProps && initialProps.__N_SSP,
|
|
|
|
|
}
|
2017-04-18 06:18:43 +02:00
|
|
|
|
}
|
2017-02-15 20:29:42 +01:00
|
|
|
|
|
2020-08-17 23:20:05 +02:00
|
|
|
|
this.components['/_app'] = {
|
|
|
|
|
Component: App as ComponentType,
|
|
|
|
|
styleSheets: [
|
|
|
|
|
/* /_app does not need its stylesheets managed */
|
|
|
|
|
],
|
|
|
|
|
}
|
2018-05-31 11:47:29 +02:00
|
|
|
|
|
2018-07-31 21:04:14 +02:00
|
|
|
|
// Backwards compat for Router.router.events
|
|
|
|
|
// TODO: Should be remove the following major version as it was never documented
|
|
|
|
|
this.events = Router.events
|
2017-03-13 05:34:30 +01:00
|
|
|
|
|
2017-04-18 06:18:43 +02:00
|
|
|
|
this.pageLoader = pageLoader
|
2016-12-16 21:33:08 +01:00
|
|
|
|
this.pathname = pathname
|
|
|
|
|
this.query = query
|
2019-09-16 18:15:18 +02:00
|
|
|
|
// if auto prerendered and dynamic route wait to update asPath
|
|
|
|
|
// until after mount to prevent hydration mismatch
|
2021-01-04 09:58:52 +01:00
|
|
|
|
const autoExportDynamic =
|
|
|
|
|
isDynamicRoute(pathname) && self.__NEXT_DATA__.autoExport
|
|
|
|
|
|
|
|
|
|
this.asPath = autoExportDynamic ? pathname : as
|
2020-04-14 09:50:39 +02:00
|
|
|
|
this.basePath = basePath
|
2019-06-05 20:15:42 +02:00
|
|
|
|
this.sub = subscription
|
|
|
|
|
this.clc = null
|
2019-07-30 20:00:19 +02:00
|
|
|
|
this._wrapApp = wrapApp
|
2019-11-12 22:37:47 +01:00
|
|
|
|
// make sure to ignore extra popState in safari on navigating
|
|
|
|
|
// back from external site
|
|
|
|
|
this.isSsr = true
|
2016-10-08 07:12:51 +02:00
|
|
|
|
|
2020-02-15 19:01:10 +01:00
|
|
|
|
this.isFallback = isFallback
|
|
|
|
|
|
2020-12-31 17:54:32 +01:00
|
|
|
|
this.isReady = !!(
|
|
|
|
|
self.__NEXT_DATA__.gssp ||
|
|
|
|
|
self.__NEXT_DATA__.gip ||
|
2021-04-18 12:00:04 +02:00
|
|
|
|
(!autoExportDynamic &&
|
|
|
|
|
!self.location.search &&
|
|
|
|
|
!process.env.__NEXT_HAS_REWRITES)
|
2020-12-31 17:54:32 +01:00
|
|
|
|
)
|
2021-02-18 19:34:33 +01:00
|
|
|
|
this.isPreview = !!isPreview
|
2021-02-11 11:18:24 +01:00
|
|
|
|
this.isLocaleDomain = false
|
2020-12-31 17:54:32 +01:00
|
|
|
|
|
2020-10-20 12:23:11 +02:00
|
|
|
|
if (process.env.__NEXT_I18N_SUPPORT) {
|
2020-10-07 23:11:01 +02:00
|
|
|
|
this.locale = locale
|
|
|
|
|
this.locales = locales
|
2020-10-08 13:12:17 +02:00
|
|
|
|
this.defaultLocale = defaultLocale
|
2020-12-30 07:44:07 +01:00
|
|
|
|
this.domainLocales = domainLocales
|
2021-02-11 11:18:24 +01:00
|
|
|
|
this.isLocaleDomain = !!detectDomainLocale(
|
|
|
|
|
domainLocales,
|
|
|
|
|
self.location.hostname
|
|
|
|
|
)
|
2020-10-07 23:11:01 +02:00
|
|
|
|
}
|
|
|
|
|
|
2016-10-17 02:00:17 +02:00
|
|
|
|
if (typeof window !== 'undefined') {
|
2020-04-22 17:56:49 +02:00
|
|
|
|
// make sure "as" doesn't start with double slashes or else it can
|
|
|
|
|
// throw an error as it's considered invalid
|
|
|
|
|
if (as.substr(0, 2) !== '//') {
|
|
|
|
|
// in order for `e.state` to work on the `onpopstate` event
|
|
|
|
|
// we have to register the initial route upon initialization
|
2021-06-01 23:29:43 +02:00
|
|
|
|
const options: TransitionOptions = { locale }
|
|
|
|
|
;(options as any)._shouldResolveHref = as !== pathname
|
|
|
|
|
|
2020-04-22 17:56:49 +02:00
|
|
|
|
this.changeState(
|
|
|
|
|
'replaceState',
|
2020-06-08 17:59:50 +02:00
|
|
|
|
formatWithValidation({ pathname: addBasePath(pathname), query }),
|
2020-11-12 06:26:48 +01:00
|
|
|
|
getURL(),
|
2021-06-01 23:29:43 +02:00
|
|
|
|
options
|
2020-04-22 17:56:49 +02:00
|
|
|
|
)
|
|
|
|
|
}
|
2016-12-24 18:29:23 +01:00
|
|
|
|
|
2016-10-08 12:16:22 +02:00
|
|
|
|
window.addEventListener('popstate', this.onPopState)
|
2020-07-06 16:27:45 +02:00
|
|
|
|
|
|
|
|
|
// enable custom scroll restoration handling when available
|
|
|
|
|
// otherwise fallback to browser's default handling
|
|
|
|
|
if (process.env.__NEXT_SCROLL_RESTORATION) {
|
|
|
|
|
if (manualScrollRestoration) {
|
|
|
|
|
window.history.scrollRestoration = 'manual'
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-10-08 12:16:22 +02:00
|
|
|
|
}
|
2016-10-06 01:52:50 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-14 19:43:29 +01:00
|
|
|
|
onPopState = (e: PopStateEvent): void => {
|
2020-08-01 20:13:03 +02:00
|
|
|
|
const state = e.state as HistoryState
|
|
|
|
|
|
|
|
|
|
if (!state) {
|
2017-02-05 09:58:44 +01:00
|
|
|
|
// We get state as undefined for two reasons.
|
|
|
|
|
// 1. With older safari (< 8) and older chrome (< 34)
|
|
|
|
|
// 2. When the URL changed with #
|
|
|
|
|
//
|
|
|
|
|
// In the both cases, we don't need to proceed and change the route.
|
|
|
|
|
// (as it's already changed)
|
|
|
|
|
// But we can simply replace the state with the new changes.
|
|
|
|
|
// Actually, for (1) we don't need to nothing. But it's hard to detect that event.
|
|
|
|
|
// So, doing the following for (1) does no harm.
|
|
|
|
|
const { pathname, query } = this
|
2019-05-29 13:57:26 +02:00
|
|
|
|
this.changeState(
|
|
|
|
|
'replaceState',
|
2020-06-08 17:59:50 +02:00
|
|
|
|
formatWithValidation({ pathname: addBasePath(pathname), query }),
|
2019-05-29 13:57:26 +02:00
|
|
|
|
getURL()
|
|
|
|
|
)
|
2017-01-25 17:58:26 +01:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-01 20:13:03 +02:00
|
|
|
|
if (!state.__N) {
|
2020-07-28 12:02:00 +02:00
|
|
|
|
return
|
|
|
|
|
}
|
2020-08-01 20:13:03 +02:00
|
|
|
|
|
2020-12-31 17:08:12 +01:00
|
|
|
|
let forcedScroll: { x: number; y: number } | undefined
|
|
|
|
|
const { url, as, options, idx } = state
|
|
|
|
|
if (process.env.__NEXT_SCROLL_RESTORATION) {
|
|
|
|
|
if (manualScrollRestoration) {
|
|
|
|
|
if (this._idx !== idx) {
|
|
|
|
|
// Snapshot current scroll position:
|
|
|
|
|
try {
|
|
|
|
|
sessionStorage.setItem(
|
|
|
|
|
'__next_scroll_' + this._idx,
|
|
|
|
|
JSON.stringify({ x: self.pageXOffset, y: self.pageYOffset })
|
|
|
|
|
)
|
|
|
|
|
} catch {}
|
|
|
|
|
|
|
|
|
|
// Restore old scroll position:
|
|
|
|
|
try {
|
|
|
|
|
const v = sessionStorage.getItem('__next_scroll_' + idx)
|
|
|
|
|
forcedScroll = JSON.parse(v!)
|
|
|
|
|
} catch {
|
|
|
|
|
forcedScroll = { x: 0, y: 0 }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
this._idx = idx
|
2020-08-01 20:13:03 +02:00
|
|
|
|
|
2020-07-13 18:08:12 +02:00
|
|
|
|
const { pathname } = parseRelativeUrl(url)
|
|
|
|
|
|
2019-04-09 23:58:44 +02:00
|
|
|
|
// Make sure we don't re-render on initial load,
|
|
|
|
|
// can be caused by navigating back from an external site
|
2020-07-13 18:08:12 +02:00
|
|
|
|
if (this.isSsr && as === this.asPath && pathname === this.pathname) {
|
2019-04-09 23:58:44 +02:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-31 16:21:51 +02:00
|
|
|
|
// If the downstream application returns falsy, return.
|
|
|
|
|
// They will then be responsible for handling the event.
|
2020-08-01 20:13:03 +02:00
|
|
|
|
if (this._bps && !this._bps(state)) {
|
2018-03-31 16:21:51 +02:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-22 23:51:14 +02:00
|
|
|
|
this.change(
|
|
|
|
|
'replaceState',
|
|
|
|
|
url,
|
|
|
|
|
as,
|
2020-12-31 17:08:12 +01:00
|
|
|
|
Object.assign<{}, TransitionOptions, TransitionOptions>({}, options, {
|
2020-08-22 23:51:14 +02:00
|
|
|
|
shallow: options.shallow && this._shallow,
|
2020-10-22 17:08:01 +02:00
|
|
|
|
locale: options.locale || this.defaultLocale,
|
2020-12-31 17:08:12 +01:00
|
|
|
|
}),
|
|
|
|
|
forcedScroll
|
2020-08-22 23:51:14 +02:00
|
|
|
|
)
|
2019-05-29 13:57:26 +02:00
|
|
|
|
}
|
2016-10-06 01:52:50 +02:00
|
|
|
|
|
2019-05-05 22:22:02 +02:00
|
|
|
|
reload(): void {
|
|
|
|
|
window.location.reload()
|
2016-10-24 09:22:15 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-23 21:31:22 +02:00
|
|
|
|
/**
|
|
|
|
|
* Go back in history
|
|
|
|
|
*/
|
2019-03-13 15:56:20 +01:00
|
|
|
|
back() {
|
2016-10-17 02:00:17 +02:00
|
|
|
|
window.history.back()
|
2016-10-06 01:52:50 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-23 21:31:22 +02:00
|
|
|
|
/**
|
|
|
|
|
* Performs a `pushState` with arguments
|
|
|
|
|
* @param url of the route
|
|
|
|
|
* @param as masks `url` for the browser
|
|
|
|
|
* @param options object you can define `shallow` and other options
|
|
|
|
|
*/
|
2020-12-28 21:08:58 +01:00
|
|
|
|
push(url: Url, as?: Url, options: TransitionOptions = {}) {
|
2020-12-31 17:08:12 +01:00
|
|
|
|
if (process.env.__NEXT_SCROLL_RESTORATION) {
|
|
|
|
|
// TODO: remove in the future when we update history before route change
|
|
|
|
|
// is complete, as the popstate event should handle this capture.
|
|
|
|
|
if (manualScrollRestoration) {
|
|
|
|
|
try {
|
|
|
|
|
// Snapshot scroll position right before navigating to a new page:
|
|
|
|
|
sessionStorage.setItem(
|
|
|
|
|
'__next_scroll_' + this._idx,
|
|
|
|
|
JSON.stringify({ x: self.pageXOffset, y: self.pageYOffset })
|
|
|
|
|
)
|
|
|
|
|
} catch {}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-07-13 18:08:12 +02:00
|
|
|
|
;({ url, as } = prepareUrlAs(this, url, as))
|
2017-03-06 17:48:35 +01:00
|
|
|
|
return this.change('pushState', url, as, options)
|
2016-10-06 01:52:50 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-23 21:31:22 +02:00
|
|
|
|
/**
|
|
|
|
|
* Performs a `replaceState` with arguments
|
|
|
|
|
* @param url of the route
|
|
|
|
|
* @param as masks `url` for the browser
|
|
|
|
|
* @param options object you can define `shallow` and other options
|
|
|
|
|
*/
|
2020-12-28 21:08:58 +01:00
|
|
|
|
replace(url: Url, as?: Url, options: TransitionOptions = {}) {
|
2020-07-13 18:08:12 +02:00
|
|
|
|
;({ url, as } = prepareUrlAs(this, url, as))
|
2017-03-06 17:48:35 +01:00
|
|
|
|
return this.change('replaceState', url, as, options)
|
2016-10-06 01:52:50 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-31 17:08:12 +01:00
|
|
|
|
private async change(
|
2020-02-17 22:16:19 +01:00
|
|
|
|
method: HistoryMethod,
|
2020-06-08 17:59:50 +02:00
|
|
|
|
url: string,
|
|
|
|
|
as: string,
|
2020-12-31 17:08:12 +01:00
|
|
|
|
options: TransitionOptions,
|
|
|
|
|
forcedScroll?: { x: number; y: number }
|
2020-02-17 22:16:19 +01:00
|
|
|
|
): Promise<boolean> {
|
2020-08-05 20:12:17 +02:00
|
|
|
|
if (!isLocalURL(url)) {
|
|
|
|
|
window.location.href = url
|
|
|
|
|
return false
|
|
|
|
|
}
|
2021-06-01 20:01:23 +02:00
|
|
|
|
const shouldResolveHref =
|
|
|
|
|
url === as || (options as any)._h || (options as any)._shouldResolveHref
|
2020-12-30 17:10:59 +01:00
|
|
|
|
|
2020-12-31 17:54:32 +01:00
|
|
|
|
// for static pages with query params in the URL we delay
|
|
|
|
|
// marking the router ready until after the query is updated
|
|
|
|
|
if ((options as any)._h) {
|
|
|
|
|
this.isReady = true
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-11 21:33:44 +01:00
|
|
|
|
let localeChange = options.locale !== this.locale
|
2020-10-22 17:08:01 +02:00
|
|
|
|
|
|
|
|
|
if (process.env.__NEXT_I18N_SUPPORT) {
|
2020-11-20 22:50:26 +01:00
|
|
|
|
this.locale =
|
|
|
|
|
options.locale === false
|
|
|
|
|
? this.defaultLocale
|
|
|
|
|
: options.locale || this.locale
|
2020-10-22 17:08:01 +02:00
|
|
|
|
|
|
|
|
|
if (typeof options.locale === 'undefined') {
|
|
|
|
|
options.locale = this.locale
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-12 06:26:48 +01:00
|
|
|
|
const parsedAs = parseRelativeUrl(hasBasePath(as) ? delBasePath(as) : as)
|
2020-11-11 21:33:44 +01:00
|
|
|
|
const localePathResult = normalizeLocalePath(
|
|
|
|
|
parsedAs.pathname,
|
|
|
|
|
this.locales
|
|
|
|
|
)
|
2020-12-28 18:58:07 +01:00
|
|
|
|
|
2020-10-22 17:08:01 +02:00
|
|
|
|
if (localePathResult.detectedLocale) {
|
|
|
|
|
this.locale = localePathResult.detectedLocale
|
2020-12-28 18:58:07 +01:00
|
|
|
|
parsedAs.pathname = addBasePath(parsedAs.pathname)
|
|
|
|
|
as = formatWithValidation(parsedAs)
|
|
|
|
|
url = addBasePath(
|
|
|
|
|
normalizeLocalePath(
|
|
|
|
|
hasBasePath(url) ? delBasePath(url) : url,
|
|
|
|
|
this.locales
|
|
|
|
|
).pathname
|
|
|
|
|
)
|
2020-10-22 17:08:01 +02:00
|
|
|
|
}
|
2020-12-30 07:44:07 +01:00
|
|
|
|
let didNavigate = false
|
|
|
|
|
|
|
|
|
|
// we need to wrap this in the env check again since regenerator runtime
|
|
|
|
|
// moves this on its own due to the return
|
|
|
|
|
if (process.env.__NEXT_I18N_SUPPORT) {
|
|
|
|
|
// if the locale isn't configured hard navigate to show 404 page
|
|
|
|
|
if (!this.locales?.includes(this.locale!)) {
|
|
|
|
|
parsedAs.pathname = addLocale(parsedAs.pathname, this.locale)
|
|
|
|
|
window.location.href = formatWithValidation(parsedAs)
|
|
|
|
|
// this was previously a return but was removed in favor
|
|
|
|
|
// of better dead code elimination with regenerator runtime
|
|
|
|
|
didNavigate = true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const detectedDomain = detectDomainLocale(
|
|
|
|
|
this.domainLocales,
|
|
|
|
|
undefined,
|
|
|
|
|
this.locale
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// we need to wrap this in the env check again since regenerator runtime
|
|
|
|
|
// moves this on its own due to the return
|
|
|
|
|
if (process.env.__NEXT_I18N_SUPPORT) {
|
|
|
|
|
// if we are navigating to a domain locale ensure we redirect to the
|
|
|
|
|
// correct domain
|
|
|
|
|
if (
|
|
|
|
|
!didNavigate &&
|
|
|
|
|
detectedDomain &&
|
2021-02-11 11:18:24 +01:00
|
|
|
|
this.isLocaleDomain &&
|
2020-12-30 07:44:07 +01:00
|
|
|
|
self.location.hostname !== detectedDomain.domain
|
|
|
|
|
) {
|
|
|
|
|
const asNoBasePath = delBasePath(as)
|
|
|
|
|
window.location.href = `http${detectedDomain.http ? '' : 's'}://${
|
|
|
|
|
detectedDomain.domain
|
|
|
|
|
}${addBasePath(
|
|
|
|
|
`${
|
|
|
|
|
this.locale === detectedDomain.defaultLocale
|
|
|
|
|
? ''
|
|
|
|
|
: `/${this.locale}`
|
|
|
|
|
}${asNoBasePath === '/' ? '' : asNoBasePath}` || '/'
|
|
|
|
|
)}`
|
|
|
|
|
// this was previously a return but was removed in favor
|
|
|
|
|
// of better dead code elimination with regenerator runtime
|
|
|
|
|
didNavigate = true
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-11-20 16:07:00 +01:00
|
|
|
|
|
2020-12-30 07:44:07 +01:00
|
|
|
|
if (didNavigate) {
|
2020-11-20 16:07:00 +01:00
|
|
|
|
return new Promise(() => {})
|
|
|
|
|
}
|
2020-10-22 17:08:01 +02:00
|
|
|
|
}
|
2020-08-05 20:12:17 +02:00
|
|
|
|
|
2020-08-01 20:13:03 +02:00
|
|
|
|
if (!(options as any)._h) {
|
2020-07-20 22:03:49 +02:00
|
|
|
|
this.isSsr = false
|
|
|
|
|
}
|
|
|
|
|
// marking route changes as a navigation start entry
|
|
|
|
|
if (ST) {
|
|
|
|
|
performance.mark('routeChange')
|
|
|
|
|
}
|
2019-07-16 01:07:04 +02:00
|
|
|
|
|
2020-12-17 10:14:50 +01:00
|
|
|
|
const { shallow = false } = options
|
|
|
|
|
const routeProps = { shallow }
|
|
|
|
|
|
2020-07-20 22:03:49 +02:00
|
|
|
|
if (this._inFlightRoute) {
|
2020-12-17 10:14:50 +01:00
|
|
|
|
this.abortComponentLoad(this._inFlightRoute, routeProps)
|
2020-07-20 22:03:49 +02:00
|
|
|
|
}
|
2020-07-13 18:08:12 +02:00
|
|
|
|
|
2020-11-12 06:26:48 +01:00
|
|
|
|
as = addBasePath(
|
|
|
|
|
addLocale(
|
|
|
|
|
hasBasePath(as) ? delBasePath(as) : as,
|
|
|
|
|
options.locale,
|
|
|
|
|
this.defaultLocale
|
|
|
|
|
)
|
|
|
|
|
)
|
2020-10-07 23:11:01 +02:00
|
|
|
|
const cleanedAs = delLocale(
|
|
|
|
|
hasBasePath(as) ? delBasePath(as) : as,
|
|
|
|
|
this.locale
|
|
|
|
|
)
|
2020-07-20 22:03:49 +02:00
|
|
|
|
this._inFlightRoute = as
|
|
|
|
|
|
|
|
|
|
// If the url change is only related to a hash change
|
|
|
|
|
// We should not proceed. We should only change the state.
|
|
|
|
|
|
|
|
|
|
// WARNING: `_h` is an internal option for handing Next.js client-side
|
|
|
|
|
// hydration. Your app should _never_ use this property. It may change at
|
|
|
|
|
// any time without notice.
|
2020-08-01 20:13:03 +02:00
|
|
|
|
if (!(options as any)._h && this.onlyAHashChange(cleanedAs)) {
|
2020-07-20 22:03:49 +02:00
|
|
|
|
this.asPath = cleanedAs
|
2020-12-17 10:14:50 +01:00
|
|
|
|
Router.events.emit('hashChangeStart', as, routeProps)
|
2020-08-13 14:39:36 +02:00
|
|
|
|
// TODO: do we need the resolved href when only a hash change?
|
2020-07-20 22:03:49 +02:00
|
|
|
|
this.changeState(method, url, as, options)
|
|
|
|
|
this.scrollToHash(cleanedAs)
|
2020-12-31 17:08:12 +01:00
|
|
|
|
this.notify(this.components[this.route], null)
|
2020-12-17 10:14:50 +01:00
|
|
|
|
Router.events.emit('hashChangeComplete', as, routeProps)
|
2020-07-20 22:03:49 +02:00
|
|
|
|
return true
|
|
|
|
|
}
|
2020-07-13 18:08:12 +02:00
|
|
|
|
|
2020-11-11 19:13:16 +01:00
|
|
|
|
let parsed = parseRelativeUrl(url)
|
|
|
|
|
let { pathname, query } = parsed
|
|
|
|
|
|
2020-08-13 14:39:36 +02:00
|
|
|
|
// The build manifest needs to be loaded before auto-static dynamic pages
|
|
|
|
|
// get their query parameters to allow ensuring they can be parsed properly
|
|
|
|
|
// when rewritten to
|
2020-11-11 19:13:16 +01:00
|
|
|
|
let pages: any, rewrites: any
|
|
|
|
|
try {
|
|
|
|
|
pages = await this.pageLoader.getPageList()
|
|
|
|
|
;({ __rewrites: rewrites } = await getClientBuildManifest())
|
|
|
|
|
} catch (err) {
|
|
|
|
|
// If we fail to resolve the page list or client-build manifest, we must
|
|
|
|
|
// do a server-side transition:
|
|
|
|
|
window.location.href = as
|
|
|
|
|
return false
|
|
|
|
|
}
|
2020-08-13 14:39:36 +02:00
|
|
|
|
|
2020-07-20 22:03:49 +02:00
|
|
|
|
// If asked to change the current URL we should reload the current page
|
|
|
|
|
// (not location.reload() but reload getInitialProps and other Next.js stuffs)
|
|
|
|
|
// We also need to set the method = replaceState always
|
|
|
|
|
// as this should not go into the history (That's how browsers work)
|
|
|
|
|
// We should compare the new asPath to the current asPath, not the url
|
2020-11-11 21:33:44 +01:00
|
|
|
|
if (!this.urlIsNew(cleanedAs) && !localeChange) {
|
2020-07-20 22:03:49 +02:00
|
|
|
|
method = 'replaceState'
|
|
|
|
|
}
|
2016-10-08 07:12:51 +02:00
|
|
|
|
|
2020-08-13 14:39:36 +02:00
|
|
|
|
// we need to resolve the as value using rewrites for dynamic SSG
|
|
|
|
|
// pages to allow building the data URL correctly
|
|
|
|
|
let resolvedAs = as
|
|
|
|
|
|
2021-03-26 16:19:48 +01:00
|
|
|
|
// url and as should always be prefixed with basePath by this
|
|
|
|
|
// point by either next/link or router.push/replace so strip the
|
|
|
|
|
// basePath from the pathname to match the pages dir 1-to-1
|
|
|
|
|
pathname = pathname
|
|
|
|
|
? removePathTrailingSlash(delBasePath(pathname))
|
|
|
|
|
: pathname
|
2020-09-10 02:55:22 +02:00
|
|
|
|
|
2021-05-14 12:50:41 +02:00
|
|
|
|
if (shouldResolveHref && pathname !== '/_error') {
|
2021-06-01 20:01:23 +02:00
|
|
|
|
;(options as any)._shouldResolveHref = true
|
|
|
|
|
|
2021-03-26 16:19:48 +01:00
|
|
|
|
if (process.env.__NEXT_HAS_REWRITES && as.startsWith('/')) {
|
|
|
|
|
const rewritesResult = resolveRewrites(
|
2021-04-06 12:25:04 +02:00
|
|
|
|
addBasePath(addLocale(cleanedAs, this.locale)),
|
2021-03-26 16:19:48 +01:00
|
|
|
|
pages,
|
|
|
|
|
rewrites,
|
|
|
|
|
query,
|
|
|
|
|
(p: string) => resolveDynamicRoute(p, pages),
|
|
|
|
|
this.locales
|
|
|
|
|
)
|
|
|
|
|
resolvedAs = rewritesResult.asPath
|
|
|
|
|
|
|
|
|
|
if (rewritesResult.matchedPage && rewritesResult.resolvedHref) {
|
|
|
|
|
// if this directly matches a page we need to update the href to
|
|
|
|
|
// allow the correct page chunk to be loaded
|
|
|
|
|
pathname = rewritesResult.resolvedHref
|
2021-05-26 09:58:05 +02:00
|
|
|
|
parsed.pathname = addBasePath(pathname)
|
2021-03-26 16:19:48 +01:00
|
|
|
|
url = formatWithValidation(parsed)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
parsed.pathname = resolveDynamicRoute(pathname, pages)
|
|
|
|
|
|
|
|
|
|
if (parsed.pathname !== pathname) {
|
|
|
|
|
pathname = parsed.pathname
|
2021-05-26 09:58:05 +02:00
|
|
|
|
parsed.pathname = addBasePath(pathname)
|
2021-03-26 16:19:48 +01:00
|
|
|
|
url = formatWithValidation(parsed)
|
|
|
|
|
}
|
2020-09-10 02:55:22 +02:00
|
|
|
|
}
|
2020-08-13 14:39:36 +02:00
|
|
|
|
}
|
2021-01-01 19:14:42 +01:00
|
|
|
|
|
2021-03-26 16:19:48 +01:00
|
|
|
|
const route = removePathTrailingSlash(pathname)
|
|
|
|
|
|
2021-01-01 19:14:42 +01:00
|
|
|
|
if (!isLocalURL(as)) {
|
|
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
|
throw new Error(
|
|
|
|
|
`Invalid href: "${url}" and as: "${as}", received relative href and external as` +
|
2021-03-29 10:25:00 +02:00
|
|
|
|
`\nSee more info: https://nextjs.org/docs/messages/invalid-relative-url-external-as`
|
2021-01-01 19:14:42 +01:00
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
window.location.href = as
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-07 23:11:01 +02:00
|
|
|
|
resolvedAs = delLocale(delBasePath(resolvedAs), this.locale)
|
2020-08-13 14:39:36 +02:00
|
|
|
|
|
2020-07-20 22:03:49 +02:00
|
|
|
|
if (isDynamicRoute(route)) {
|
2020-09-07 13:09:02 +02:00
|
|
|
|
const parsedAs = parseRelativeUrl(resolvedAs)
|
|
|
|
|
const asPathname = parsedAs.pathname
|
|
|
|
|
|
2020-07-20 22:03:49 +02:00
|
|
|
|
const routeRegex = getRouteRegex(route)
|
|
|
|
|
const routeMatch = getRouteMatcher(routeRegex)(asPathname)
|
2020-09-10 22:05:02 +02:00
|
|
|
|
const shouldInterpolate = route === asPathname
|
|
|
|
|
const interpolatedAs = shouldInterpolate
|
|
|
|
|
? interpolateAs(route, asPathname, query)
|
|
|
|
|
: ({} as { result: undefined; params: undefined })
|
|
|
|
|
|
|
|
|
|
if (!routeMatch || (shouldInterpolate && !interpolatedAs.result)) {
|
2020-07-20 22:03:49 +02:00
|
|
|
|
const missingParams = Object.keys(routeRegex.groups).filter(
|
|
|
|
|
(param) => !query[param]
|
|
|
|
|
)
|
2020-01-20 23:47:52 +01:00
|
|
|
|
|
2020-07-20 22:03:49 +02:00
|
|
|
|
if (missingParams.length > 0) {
|
|
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
|
console.warn(
|
2020-09-10 22:05:02 +02:00
|
|
|
|
`${
|
|
|
|
|
shouldInterpolate
|
|
|
|
|
? `Interpolating href`
|
|
|
|
|
: `Mismatching \`as\` and \`href\``
|
|
|
|
|
} failed to manually provide ` +
|
2020-07-20 22:03:49 +02:00
|
|
|
|
`the params: ${missingParams.join(
|
|
|
|
|
', '
|
|
|
|
|
)} in the \`href\`'s \`query\``
|
2020-01-20 23:47:52 +01:00
|
|
|
|
)
|
2019-10-11 11:49:18 +02:00
|
|
|
|
}
|
2020-07-20 22:03:49 +02:00
|
|
|
|
|
|
|
|
|
throw new Error(
|
2020-09-10 22:05:02 +02:00
|
|
|
|
(shouldInterpolate
|
|
|
|
|
? `The provided \`href\` (${url}) value is missing query values (${missingParams.join(
|
|
|
|
|
', '
|
|
|
|
|
)}) to be interpolated properly. `
|
|
|
|
|
: `The provided \`as\` value (${asPathname}) is incompatible with the \`href\` value (${route}). `) +
|
2021-03-29 10:25:00 +02:00
|
|
|
|
`Read more: https://nextjs.org/docs/messages/${
|
2020-09-10 22:05:02 +02:00
|
|
|
|
shouldInterpolate
|
|
|
|
|
? 'href-interpolation-failed'
|
|
|
|
|
: 'incompatible-href-as'
|
|
|
|
|
}`
|
2020-07-20 22:03:49 +02:00
|
|
|
|
)
|
2019-05-27 20:20:33 +02:00
|
|
|
|
}
|
2020-09-10 22:05:02 +02:00
|
|
|
|
} else if (shouldInterpolate) {
|
2020-09-07 13:09:02 +02:00
|
|
|
|
as = formatWithValidation(
|
|
|
|
|
Object.assign({}, parsedAs, {
|
2020-09-10 22:05:02 +02:00
|
|
|
|
pathname: interpolatedAs.result,
|
|
|
|
|
query: omitParmsFromQuery(query, interpolatedAs.params!),
|
2020-09-07 13:09:02 +02:00
|
|
|
|
})
|
|
|
|
|
)
|
2020-07-20 22:03:49 +02:00
|
|
|
|
} else {
|
|
|
|
|
// Merge params into `query`, overwriting any specified in search
|
|
|
|
|
Object.assign(query, routeMatch)
|
2019-05-27 20:20:33 +02:00
|
|
|
|
}
|
2020-07-20 22:03:49 +02:00
|
|
|
|
}
|
2019-05-27 20:20:33 +02:00
|
|
|
|
|
2020-12-17 10:14:50 +01:00
|
|
|
|
Router.events.emit('routeChangeStart', as, routeProps)
|
2017-03-06 17:48:35 +01:00
|
|
|
|
|
2020-07-21 19:33:11 +02:00
|
|
|
|
try {
|
2020-12-30 06:17:10 +01:00
|
|
|
|
let routeInfo = await this.getRouteInfo(
|
2020-07-21 19:33:11 +02:00
|
|
|
|
route,
|
|
|
|
|
pathname,
|
|
|
|
|
query,
|
2021-01-22 00:40:23 +01:00
|
|
|
|
as,
|
|
|
|
|
resolvedAs,
|
2020-12-17 10:14:50 +01:00
|
|
|
|
routeProps
|
2020-07-21 19:33:11 +02:00
|
|
|
|
)
|
2020-09-08 09:23:21 +02:00
|
|
|
|
let { error, props, __N_SSG, __N_SSP } = routeInfo
|
|
|
|
|
|
|
|
|
|
// handle redirect on client-transition
|
2020-12-30 06:17:10 +01:00
|
|
|
|
if ((__N_SSG || __N_SSP) && props) {
|
|
|
|
|
if ((props as any).pageProps && (props as any).pageProps.__N_REDIRECT) {
|
|
|
|
|
const destination = (props as any).pageProps.__N_REDIRECT
|
|
|
|
|
|
|
|
|
|
// check if destination is internal (resolves to a page) and attempt
|
|
|
|
|
// client-navigation if it is falling back to hard navigation if
|
|
|
|
|
// it's not
|
|
|
|
|
if (destination.startsWith('/')) {
|
|
|
|
|
const parsedHref = parseRelativeUrl(destination)
|
2021-03-26 16:19:48 +01:00
|
|
|
|
parsedHref.pathname = resolveDynamicRoute(
|
|
|
|
|
parsedHref.pathname,
|
|
|
|
|
pages
|
|
|
|
|
)
|
2020-12-30 06:17:10 +01:00
|
|
|
|
|
2021-06-16 21:50:10 +02:00
|
|
|
|
const { url: newUrl, as: newAs } = prepareUrlAs(
|
|
|
|
|
this,
|
|
|
|
|
destination,
|
|
|
|
|
destination
|
|
|
|
|
)
|
|
|
|
|
return this.change(method, newUrl, newAs, options)
|
2020-09-08 09:23:21 +02:00
|
|
|
|
}
|
2020-12-30 06:17:10 +01:00
|
|
|
|
|
|
|
|
|
window.location.href = destination
|
|
|
|
|
return new Promise(() => {})
|
2020-09-08 09:23:21 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-02-18 19:34:33 +01:00
|
|
|
|
this.isPreview = !!props.__N_PREVIEW
|
|
|
|
|
|
2020-12-30 06:17:10 +01:00
|
|
|
|
// handle SSG data 404
|
|
|
|
|
if (props.notFound === SSG_DATA_NOT_FOUND) {
|
|
|
|
|
let notFoundRoute
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
await this.fetchComponent('/404')
|
|
|
|
|
notFoundRoute = '/404'
|
|
|
|
|
} catch (_) {
|
|
|
|
|
notFoundRoute = '/_error'
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
routeInfo = await this.getRouteInfo(
|
|
|
|
|
notFoundRoute,
|
|
|
|
|
notFoundRoute,
|
|
|
|
|
query,
|
|
|
|
|
as,
|
2021-01-22 00:40:23 +01:00
|
|
|
|
resolvedAs,
|
2020-12-30 06:17:10 +01:00
|
|
|
|
{ shallow: false }
|
|
|
|
|
)
|
|
|
|
|
}
|
2020-09-08 09:23:21 +02:00
|
|
|
|
}
|
2016-11-03 11:15:03 +01:00
|
|
|
|
|
2020-12-17 10:14:50 +01:00
|
|
|
|
Router.events.emit('beforeHistoryChange', as, routeProps)
|
2020-11-12 06:26:48 +01:00
|
|
|
|
this.changeState(method, url, as, options)
|
2019-07-03 02:42:41 +02:00
|
|
|
|
|
2020-07-21 19:33:11 +02:00
|
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
|
const appComp: any = this.components['/_app'].Component
|
|
|
|
|
;(window as any).next.isPrerendered =
|
|
|
|
|
appComp.getInitialProps === appComp.origGetInitialProps &&
|
|
|
|
|
!(routeInfo.Component as any).getInitialProps
|
|
|
|
|
}
|
2020-02-07 14:09:06 +01:00
|
|
|
|
|
2021-02-23 23:15:46 +01:00
|
|
|
|
if (
|
|
|
|
|
(options as any)._h &&
|
|
|
|
|
pathname === '/_error' &&
|
|
|
|
|
self.__NEXT_DATA__.props?.pageProps?.statusCode === 500 &&
|
|
|
|
|
props?.pageProps
|
|
|
|
|
) {
|
|
|
|
|
// ensure statusCode is still correct for static 500 page
|
|
|
|
|
// when updating query information
|
|
|
|
|
props.pageProps.statusCode = 500
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-08 18:48:15 +02:00
|
|
|
|
// shallow routing is only allowed for same page URL changes.
|
|
|
|
|
const isValidShallowRoute = options.shallow && this.route === route
|
|
|
|
|
|
|
|
|
|
const shouldScroll = options.scroll ?? !isValidShallowRoute
|
|
|
|
|
const resetScroll = shouldScroll ? { x: 0, y: 0 } : null
|
2020-12-30 05:33:08 +01:00
|
|
|
|
await this.set(
|
|
|
|
|
route,
|
|
|
|
|
pathname!,
|
|
|
|
|
query,
|
|
|
|
|
cleanedAs,
|
|
|
|
|
routeInfo,
|
2021-06-08 18:48:15 +02:00
|
|
|
|
forcedScroll ?? resetScroll
|
2020-12-30 05:33:08 +01:00
|
|
|
|
).catch((e) => {
|
|
|
|
|
if (e.cancelled) error = error || e
|
|
|
|
|
else throw e
|
|
|
|
|
})
|
2020-02-07 14:09:06 +01:00
|
|
|
|
|
2020-07-21 19:33:11 +02:00
|
|
|
|
if (error) {
|
2020-12-17 10:14:50 +01:00
|
|
|
|
Router.events.emit('routeChangeError', error, cleanedAs, routeProps)
|
2020-07-21 19:33:11 +02:00
|
|
|
|
throw error
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-02 19:54:20 +01:00
|
|
|
|
if (process.env.__NEXT_I18N_SUPPORT) {
|
|
|
|
|
if (this.locale) {
|
|
|
|
|
document.documentElement.lang = this.locale
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-12-17 10:14:50 +01:00
|
|
|
|
Router.events.emit('routeChangeComplete', as, routeProps)
|
2020-07-20 22:03:49 +02:00
|
|
|
|
|
2020-07-21 19:33:11 +02:00
|
|
|
|
return true
|
|
|
|
|
} catch (err) {
|
|
|
|
|
if (err.cancelled) {
|
|
|
|
|
return false
|
2020-07-20 22:03:49 +02:00
|
|
|
|
}
|
2020-07-21 19:33:11 +02:00
|
|
|
|
throw err
|
|
|
|
|
}
|
2017-02-15 19:27:41 +01:00
|
|
|
|
}
|
2016-12-30 20:55:00 +01:00
|
|
|
|
|
2020-02-17 22:16:19 +01:00
|
|
|
|
changeState(
|
|
|
|
|
method: HistoryMethod,
|
|
|
|
|
url: string,
|
|
|
|
|
as: string,
|
2020-08-01 20:13:03 +02:00
|
|
|
|
options: TransitionOptions = {}
|
2020-02-17 22:16:19 +01:00
|
|
|
|
): void {
|
2018-09-27 21:10:53 +02:00
|
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
|
if (typeof window.history === 'undefined') {
|
|
|
|
|
console.error(`Warning: window.history is not available.`)
|
|
|
|
|
return
|
|
|
|
|
}
|
2020-02-17 22:16:19 +01:00
|
|
|
|
|
2018-09-27 21:10:53 +02:00
|
|
|
|
if (typeof window.history[method] === 'undefined') {
|
|
|
|
|
console.error(`Warning: window.history.${method} is not available`)
|
|
|
|
|
return
|
|
|
|
|
}
|
2018-02-25 15:19:38 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (method !== 'pushState' || getURL() !== as) {
|
2020-08-22 23:51:14 +02:00
|
|
|
|
this._shallow = options.shallow
|
2019-10-30 03:50:16 +01:00
|
|
|
|
window.history[method](
|
|
|
|
|
{
|
|
|
|
|
url,
|
|
|
|
|
as,
|
2019-11-12 22:37:47 +01:00
|
|
|
|
options,
|
2020-07-28 12:02:00 +02:00
|
|
|
|
__N: true,
|
2020-12-31 17:08:12 +01:00
|
|
|
|
idx: this._idx = method !== 'pushState' ? this._idx : this._idx + 1,
|
2020-08-01 20:13:03 +02:00
|
|
|
|
} as HistoryState,
|
2020-02-17 22:16:19 +01:00
|
|
|
|
// Most browsers currently ignores this parameter, although they may use it in the future.
|
|
|
|
|
// Passing the empty string here should be safe against future changes to the method.
|
|
|
|
|
// https://developer.mozilla.org/en-US/docs/Web/API/History/replaceState
|
|
|
|
|
'',
|
2019-10-30 03:50:16 +01:00
|
|
|
|
as
|
|
|
|
|
)
|
2016-12-30 20:55:00 +01:00
|
|
|
|
}
|
2016-10-06 01:52:50 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-21 19:33:11 +02:00
|
|
|
|
async handleRouteInfoError(
|
|
|
|
|
err: Error & { code: any; cancelled: boolean },
|
2019-05-29 13:57:26 +02:00
|
|
|
|
pathname: string,
|
2020-08-01 20:13:03 +02:00
|
|
|
|
query: ParsedUrlQuery,
|
2019-05-29 13:57:26 +02:00
|
|
|
|
as: string,
|
2020-12-17 10:14:50 +01:00
|
|
|
|
routeProps: RouteProperties,
|
2020-07-21 19:33:11 +02:00
|
|
|
|
loadErrorFail?: boolean
|
2020-11-11 19:13:16 +01:00
|
|
|
|
): Promise<CompletePrivateRouteInfo> {
|
2020-07-21 19:33:11 +02:00
|
|
|
|
if (err.cancelled) {
|
|
|
|
|
// bubble up cancellation errors
|
|
|
|
|
throw err
|
|
|
|
|
}
|
2020-02-20 22:25:41 +01:00
|
|
|
|
|
2020-11-11 19:13:16 +01:00
|
|
|
|
if (isAssetError(err) || loadErrorFail) {
|
2020-12-17 10:14:50 +01:00
|
|
|
|
Router.events.emit('routeChangeError', err, as, routeProps)
|
2020-02-20 22:25:41 +01:00
|
|
|
|
|
2020-07-21 19:33:11 +02:00
|
|
|
|
// If we can't load the page it could be one of following reasons
|
|
|
|
|
// 1. Page doesn't exists
|
|
|
|
|
// 2. Page does exist in a different zone
|
|
|
|
|
// 3. Internal error while loading the page
|
|
|
|
|
|
|
|
|
|
// So, doing a hard reload is the proper way to deal with this.
|
|
|
|
|
window.location.href = as
|
|
|
|
|
|
|
|
|
|
// Changing the URL doesn't block executing the current code path.
|
|
|
|
|
// So let's throw a cancellation error stop the routing logic.
|
|
|
|
|
throw buildCancellationError()
|
2020-02-20 22:25:41 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-21 19:33:11 +02:00
|
|
|
|
try {
|
2020-10-22 16:54:38 +02:00
|
|
|
|
let Component: ComponentType
|
|
|
|
|
let styleSheets: StyleSheetTuple[]
|
2020-10-27 10:40:17 +01:00
|
|
|
|
let props: Record<string, any> | undefined
|
2020-10-22 16:54:38 +02:00
|
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
typeof Component! === 'undefined' ||
|
|
|
|
|
typeof styleSheets! === 'undefined'
|
|
|
|
|
) {
|
|
|
|
|
;({ page: Component, styleSheets } = await this.fetchComponent(
|
|
|
|
|
'/_error'
|
|
|
|
|
))
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-11 19:13:16 +01:00
|
|
|
|
const routeInfo: CompletePrivateRouteInfo = {
|
2020-10-27 10:40:17 +01:00
|
|
|
|
props,
|
2020-08-17 23:20:05 +02:00
|
|
|
|
Component,
|
|
|
|
|
styleSheets,
|
2020-12-30 06:17:10 +01:00
|
|
|
|
err,
|
|
|
|
|
error: err,
|
2020-08-17 23:20:05 +02:00
|
|
|
|
}
|
2020-07-21 19:33:11 +02:00
|
|
|
|
|
2020-10-27 10:40:17 +01:00
|
|
|
|
if (!routeInfo.props) {
|
|
|
|
|
try {
|
|
|
|
|
routeInfo.props = await this.getInitialProps(Component, {
|
|
|
|
|
err,
|
|
|
|
|
pathname,
|
|
|
|
|
query,
|
|
|
|
|
} as any)
|
|
|
|
|
} catch (gipErr) {
|
|
|
|
|
console.error('Error in error page `getInitialProps`: ', gipErr)
|
|
|
|
|
routeInfo.props = {}
|
2020-10-22 16:54:38 +02:00
|
|
|
|
}
|
2017-02-21 00:48:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-21 19:33:11 +02:00
|
|
|
|
return routeInfo
|
|
|
|
|
} catch (routeInfoErr) {
|
2020-12-17 10:14:50 +01:00
|
|
|
|
return this.handleRouteInfoError(
|
|
|
|
|
routeInfoErr,
|
|
|
|
|
pathname,
|
|
|
|
|
query,
|
|
|
|
|
as,
|
|
|
|
|
routeProps,
|
|
|
|
|
true
|
|
|
|
|
)
|
2020-07-21 19:33:11 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async getRouteInfo(
|
|
|
|
|
route: string,
|
|
|
|
|
pathname: string,
|
|
|
|
|
query: any,
|
|
|
|
|
as: string,
|
2021-01-22 00:40:23 +01:00
|
|
|
|
resolvedAs: string,
|
2020-12-17 10:14:50 +01:00
|
|
|
|
routeProps: RouteProperties
|
2020-08-13 19:39:33 +02:00
|
|
|
|
): Promise<PrivateRouteInfo> {
|
2020-07-21 19:33:11 +02:00
|
|
|
|
try {
|
2020-11-11 19:13:16 +01:00
|
|
|
|
const existingRouteInfo: PrivateRouteInfo | undefined = this.components[
|
|
|
|
|
route
|
|
|
|
|
]
|
2020-12-17 10:14:50 +01:00
|
|
|
|
if (routeProps.shallow && existingRouteInfo && this.route === route) {
|
2020-11-11 19:13:16 +01:00
|
|
|
|
return existingRouteInfo
|
2020-07-21 19:33:11 +02:00
|
|
|
|
}
|
2017-12-06 00:35:30 +01:00
|
|
|
|
|
2020-11-11 19:13:16 +01:00
|
|
|
|
const cachedRouteInfo: CompletePrivateRouteInfo | undefined =
|
|
|
|
|
existingRouteInfo && 'initial' in existingRouteInfo
|
|
|
|
|
? undefined
|
|
|
|
|
: existingRouteInfo
|
|
|
|
|
const routeInfo: CompletePrivateRouteInfo = cachedRouteInfo
|
2020-07-21 19:33:11 +02:00
|
|
|
|
? cachedRouteInfo
|
2020-08-13 07:24:57 +02:00
|
|
|
|
: await this.fetchComponent(route).then((res) => ({
|
|
|
|
|
Component: res.page,
|
2020-08-17 23:20:05 +02:00
|
|
|
|
styleSheets: res.styleSheets,
|
2020-08-13 07:24:57 +02:00
|
|
|
|
__N_SSG: res.mod.__N_SSG,
|
|
|
|
|
__N_SSP: res.mod.__N_SSP,
|
|
|
|
|
}))
|
2020-07-21 19:33:11 +02:00
|
|
|
|
|
|
|
|
|
const { Component, __N_SSG, __N_SSP } = routeInfo
|
2020-06-29 17:14:45 +02:00
|
|
|
|
|
2020-07-21 19:33:11 +02:00
|
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
|
const { isValidElementType } = require('react-is')
|
|
|
|
|
if (!isValidElementType(Component)) {
|
|
|
|
|
throw new Error(
|
|
|
|
|
`The default export is not a React Component in page: "${pathname}"`
|
2020-06-29 17:14:45 +02:00
|
|
|
|
)
|
|
|
|
|
}
|
2020-07-21 19:33:11 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let dataHref: string | undefined
|
2020-06-29 17:14:45 +02:00
|
|
|
|
|
2020-07-21 19:33:11 +02:00
|
|
|
|
if (__N_SSG || __N_SSP) {
|
|
|
|
|
dataHref = this.pageLoader.getDataHref(
|
|
|
|
|
formatWithValidation({ pathname, query }),
|
2021-01-22 00:40:23 +01:00
|
|
|
|
resolvedAs,
|
2020-10-07 23:11:01 +02:00
|
|
|
|
__N_SSG,
|
2020-10-10 12:22:45 +02:00
|
|
|
|
this.locale
|
2020-07-21 19:33:11 +02:00
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-11 19:13:16 +01:00
|
|
|
|
const props = await this._getData<CompletePrivateRouteInfo>(() =>
|
2020-07-21 19:33:11 +02:00
|
|
|
|
__N_SSG
|
|
|
|
|
? this._getStaticData(dataHref!)
|
|
|
|
|
: __N_SSP
|
|
|
|
|
? this._getServerData(dataHref!)
|
|
|
|
|
: this.getInitialProps(
|
|
|
|
|
Component,
|
|
|
|
|
// we provide AppTree later so this needs to be `any`
|
|
|
|
|
{
|
|
|
|
|
pathname,
|
|
|
|
|
query,
|
|
|
|
|
asPath: as,
|
2021-05-10 14:11:19 +02:00
|
|
|
|
locale: this.locale,
|
|
|
|
|
locales: this.locales,
|
|
|
|
|
defaultLocale: this.defaultLocale,
|
2020-07-21 19:33:11 +02:00
|
|
|
|
} as any
|
|
|
|
|
)
|
|
|
|
|
)
|
2020-09-08 09:23:21 +02:00
|
|
|
|
|
2020-07-21 19:33:11 +02:00
|
|
|
|
routeInfo.props = props
|
|
|
|
|
this.components[route] = routeInfo
|
|
|
|
|
return routeInfo
|
|
|
|
|
} catch (err) {
|
2020-12-17 10:14:50 +01:00
|
|
|
|
return this.handleRouteInfoError(err, pathname, query, as, routeProps)
|
2020-07-21 19:33:11 +02:00
|
|
|
|
}
|
2016-12-31 02:15:22 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-29 13:57:26 +02:00
|
|
|
|
set(
|
|
|
|
|
route: string,
|
|
|
|
|
pathname: string,
|
2020-08-01 20:13:03 +02:00
|
|
|
|
query: ParsedUrlQuery,
|
2019-05-29 13:57:26 +02:00
|
|
|
|
as: string,
|
2020-12-30 05:33:08 +01:00
|
|
|
|
data: PrivateRouteInfo,
|
2020-12-31 17:08:12 +01:00
|
|
|
|
resetScroll: { x: number; y: number } | null
|
2020-05-23 23:54:11 +02:00
|
|
|
|
): Promise<void> {
|
2020-02-15 19:01:10 +01:00
|
|
|
|
this.isFallback = false
|
|
|
|
|
|
2017-03-06 17:48:35 +01:00
|
|
|
|
this.route = route
|
2016-12-30 20:55:00 +01:00
|
|
|
|
this.pathname = pathname
|
|
|
|
|
this.query = query
|
2017-05-03 18:40:09 +02:00
|
|
|
|
this.asPath = as
|
2020-12-30 05:33:08 +01:00
|
|
|
|
return this.notify(data, resetScroll)
|
2016-10-06 01:52:50 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-23 21:31:22 +02:00
|
|
|
|
/**
|
|
|
|
|
* Callback to execute before replacing router state
|
|
|
|
|
* @param cb callback to be executed
|
|
|
|
|
*/
|
2019-03-13 15:56:20 +01:00
|
|
|
|
beforePopState(cb: BeforePopStateCallback) {
|
2019-03-14 19:43:29 +01:00
|
|
|
|
this._bps = cb
|
2018-03-31 16:21:51 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-13 15:56:20 +01:00
|
|
|
|
onlyAHashChange(as: string): boolean {
|
2017-05-03 18:40:09 +02:00
|
|
|
|
if (!this.asPath) return false
|
2019-05-29 13:57:26 +02:00
|
|
|
|
const [oldUrlNoHash, oldHash] = this.asPath.split('#')
|
|
|
|
|
const [newUrlNoHash, newHash] = as.split('#')
|
2017-02-28 18:31:17 +01:00
|
|
|
|
|
2018-06-28 20:56:18 +02:00
|
|
|
|
// Makes sure we scroll to the provided hash if the url/hash are the same
|
2019-05-29 13:57:26 +02:00
|
|
|
|
if (newHash && oldUrlNoHash === newUrlNoHash && oldHash === newHash) {
|
2018-06-28 20:56:18 +02:00
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-28 18:31:17 +01:00
|
|
|
|
// If the urls are change, there's more than a hash change
|
|
|
|
|
if (oldUrlNoHash !== newUrlNoHash) {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-27 21:55:49 +02:00
|
|
|
|
// If the hash has changed, then it's a hash only change.
|
|
|
|
|
// This check is necessary to handle both the enter and
|
|
|
|
|
// leave hash === '' cases. The identity case falls through
|
|
|
|
|
// and is treated as a next reload.
|
|
|
|
|
return oldHash !== newHash
|
2017-02-28 18:31:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-13 15:56:20 +01:00
|
|
|
|
scrollToHash(as: string): void {
|
2019-05-29 13:57:26 +02:00
|
|
|
|
const [, hash] = as.split('#')
|
2021-01-30 05:32:00 +01:00
|
|
|
|
// Scroll to top if the hash is just `#` with no value or `#top`
|
|
|
|
|
// To mirror browsers
|
|
|
|
|
if (hash === '' || hash === 'top') {
|
2018-06-28 20:56:18 +02:00
|
|
|
|
window.scrollTo(0, 0)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-23 20:20:45 +02:00
|
|
|
|
// First we check if the element by id is found
|
|
|
|
|
const idEl = document.getElementById(hash)
|
|
|
|
|
if (idEl) {
|
|
|
|
|
idEl.scrollIntoView()
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
// If there's no element with the id, we check the `name` property
|
|
|
|
|
// To mirror browsers
|
|
|
|
|
const nameEl = document.getElementsByName(hash)[0]
|
|
|
|
|
if (nameEl) {
|
|
|
|
|
nameEl.scrollIntoView()
|
2017-06-09 21:32:28 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-13 15:56:20 +01:00
|
|
|
|
urlIsNew(asPath: string): boolean {
|
2019-02-28 19:53:29 +01:00
|
|
|
|
return this.asPath !== asPath
|
2016-10-06 01:52:50 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-23 21:31:22 +02:00
|
|
|
|
/**
|
2020-02-17 21:46:15 +01:00
|
|
|
|
* Prefetch page code, you may wait for the data during page rendering.
|
2019-05-23 21:31:22 +02:00
|
|
|
|
* This feature only works in production!
|
2020-02-17 21:46:15 +01:00
|
|
|
|
* @param url the href of prefetched page
|
|
|
|
|
* @param asPath the as path of the prefetched page
|
2019-05-23 21:31:22 +02:00
|
|
|
|
*/
|
2020-07-25 06:36:43 +02:00
|
|
|
|
async prefetch(
|
2020-02-17 21:46:15 +01:00
|
|
|
|
url: string,
|
|
|
|
|
asPath: string = url,
|
|
|
|
|
options: PrefetchOptions = {}
|
|
|
|
|
): Promise<void> {
|
2020-08-28 04:49:40 +02:00
|
|
|
|
let parsed = parseRelativeUrl(url)
|
2020-07-13 18:08:12 +02:00
|
|
|
|
|
2020-08-13 14:39:36 +02:00
|
|
|
|
let { pathname } = parsed
|
|
|
|
|
|
2020-11-02 18:22:40 +01:00
|
|
|
|
if (process.env.__NEXT_I18N_SUPPORT) {
|
|
|
|
|
if (options.locale === false) {
|
|
|
|
|
pathname = normalizeLocalePath!(pathname, this.locales).pathname
|
|
|
|
|
parsed.pathname = pathname
|
|
|
|
|
url = formatWithValidation(parsed)
|
|
|
|
|
|
|
|
|
|
let parsedAs = parseRelativeUrl(asPath)
|
|
|
|
|
const localePathResult = normalizeLocalePath!(
|
|
|
|
|
parsedAs.pathname,
|
|
|
|
|
this.locales
|
|
|
|
|
)
|
|
|
|
|
parsedAs.pathname = localePathResult.pathname
|
2020-11-20 22:50:26 +01:00
|
|
|
|
options.locale = localePathResult.detectedLocale || this.defaultLocale
|
2020-11-02 18:22:40 +01:00
|
|
|
|
asPath = formatWithValidation(parsedAs)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-13 14:39:36 +02:00
|
|
|
|
const pages = await this.pageLoader.getPageList()
|
2021-02-24 16:37:13 +01:00
|
|
|
|
let resolvedAs = asPath
|
|
|
|
|
|
|
|
|
|
if (process.env.__NEXT_HAS_REWRITES && asPath.startsWith('/')) {
|
2021-03-26 16:19:48 +01:00
|
|
|
|
let rewrites: any
|
2021-02-24 16:37:13 +01:00
|
|
|
|
;({ __rewrites: rewrites } = await getClientBuildManifest())
|
|
|
|
|
|
|
|
|
|
const rewritesResult = resolveRewrites(
|
2021-03-26 16:19:48 +01:00
|
|
|
|
addBasePath(addLocale(asPath, this.locale)),
|
2021-02-24 16:37:13 +01:00
|
|
|
|
pages,
|
|
|
|
|
rewrites,
|
|
|
|
|
parsed.query,
|
2021-03-26 16:19:48 +01:00
|
|
|
|
(p: string) => resolveDynamicRoute(p, pages),
|
2021-02-24 16:37:13 +01:00
|
|
|
|
this.locales
|
|
|
|
|
)
|
2021-03-26 16:19:48 +01:00
|
|
|
|
resolvedAs = delLocale(delBasePath(rewritesResult.asPath), this.locale)
|
2021-02-24 16:37:13 +01:00
|
|
|
|
|
|
|
|
|
if (rewritesResult.matchedPage && rewritesResult.resolvedHref) {
|
|
|
|
|
// if this directly matches a page we need to update the href to
|
|
|
|
|
// allow the correct page chunk to be loaded
|
|
|
|
|
pathname = rewritesResult.resolvedHref
|
|
|
|
|
parsed.pathname = pathname
|
|
|
|
|
url = formatWithValidation(parsed)
|
2021-03-26 16:19:48 +01:00
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
parsed.pathname = resolveDynamicRoute(parsed.pathname, pages)
|
|
|
|
|
|
|
|
|
|
if (parsed.pathname !== pathname) {
|
|
|
|
|
pathname = parsed.pathname
|
2021-05-26 09:58:05 +02:00
|
|
|
|
parsed.pathname = pathname
|
2021-03-26 16:19:48 +01:00
|
|
|
|
url = formatWithValidation(parsed)
|
2021-02-24 16:37:13 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-03-26 16:19:48 +01:00
|
|
|
|
const route = removePathTrailingSlash(pathname)
|
2019-12-20 22:30:58 +01:00
|
|
|
|
|
2020-07-25 06:36:43 +02:00
|
|
|
|
// Prefetch is not supported in development mode because it would trigger on-demand-entries
|
|
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await Promise.all([
|
2021-03-26 16:19:48 +01:00
|
|
|
|
this.pageLoader._isSsg(route).then((isSsg: boolean) => {
|
2020-11-05 10:51:24 +01:00
|
|
|
|
return isSsg
|
|
|
|
|
? this._getStaticData(
|
|
|
|
|
this.pageLoader.getDataHref(
|
|
|
|
|
url,
|
2021-02-24 16:37:13 +01:00
|
|
|
|
resolvedAs,
|
2020-11-05 10:51:24 +01:00
|
|
|
|
true,
|
|
|
|
|
typeof options.locale !== 'undefined'
|
|
|
|
|
? options.locale
|
|
|
|
|
: this.locale
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
: false
|
|
|
|
|
}),
|
2020-07-25 06:36:43 +02:00
|
|
|
|
this.pageLoader[options.priority ? 'loadPage' : 'prefetch'](route),
|
|
|
|
|
])
|
2017-02-15 09:52:22 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-13 07:24:57 +02:00
|
|
|
|
async fetchComponent(route: string): Promise<GoodPageCache> {
|
2017-02-15 09:52:22 +01:00
|
|
|
|
let cancelled = false
|
2019-06-05 20:15:42 +02:00
|
|
|
|
const cancel = (this.clc = () => {
|
2017-02-15 09:52:22 +01:00
|
|
|
|
cancelled = true
|
2019-05-29 13:57:26 +02:00
|
|
|
|
})
|
2017-02-15 09:52:22 +01:00
|
|
|
|
|
2020-03-06 05:15:10 +01:00
|
|
|
|
const componentResult = await this.pageLoader.loadPage(route)
|
2017-02-15 09:52:22 +01:00
|
|
|
|
|
2018-01-30 16:40:52 +01:00
|
|
|
|
if (cancelled) {
|
2019-05-29 13:57:26 +02:00
|
|
|
|
const error: any = new Error(
|
|
|
|
|
`Abort fetching component for route: "${route}"`
|
|
|
|
|
)
|
2018-01-30 16:40:52 +01:00
|
|
|
|
error.cancelled = true
|
|
|
|
|
throw error
|
|
|
|
|
}
|
2017-02-15 09:52:22 +01:00
|
|
|
|
|
2019-06-05 20:15:42 +02:00
|
|
|
|
if (cancel === this.clc) {
|
|
|
|
|
this.clc = null
|
2017-07-18 09:00:23 +02:00
|
|
|
|
}
|
2018-01-30 16:40:52 +01:00
|
|
|
|
|
2020-03-06 05:15:10 +01:00
|
|
|
|
return componentResult
|
2016-10-08 07:12:51 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-29 20:01:46 +01:00
|
|
|
|
_getData<T>(fn: () => Promise<T>): Promise<T> {
|
2016-10-08 07:12:51 +02:00
|
|
|
|
let cancelled = false
|
2019-05-29 13:57:26 +02:00
|
|
|
|
const cancel = () => {
|
|
|
|
|
cancelled = true
|
|
|
|
|
}
|
2019-06-05 20:15:42 +02:00
|
|
|
|
this.clc = cancel
|
2020-05-18 21:24:37 +02:00
|
|
|
|
return fn().then((data) => {
|
2019-12-29 20:01:46 +01:00
|
|
|
|
if (cancel === this.clc) {
|
|
|
|
|
this.clc = null
|
|
|
|
|
}
|
2016-10-08 07:12:51 +02:00
|
|
|
|
|
2019-12-29 20:01:46 +01:00
|
|
|
|
if (cancelled) {
|
|
|
|
|
const err: any = new Error('Loading initial props cancelled')
|
|
|
|
|
err.cancelled = true
|
|
|
|
|
throw err
|
|
|
|
|
}
|
2019-08-12 03:56:57 +02:00
|
|
|
|
|
2019-12-29 20:01:46 +01:00
|
|
|
|
return data
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-19 06:02:01 +02:00
|
|
|
|
_getStaticData(dataHref: string): Promise<object> {
|
|
|
|
|
const { href: cacheKey } = new URL(dataHref, window.location.href)
|
2021-02-18 19:34:33 +01:00
|
|
|
|
if (
|
|
|
|
|
process.env.NODE_ENV === 'production' &&
|
|
|
|
|
!this.isPreview &&
|
|
|
|
|
this.sdc[cacheKey]
|
|
|
|
|
) {
|
2020-07-19 06:02:01 +02:00
|
|
|
|
return Promise.resolve(this.sdc[cacheKey])
|
|
|
|
|
}
|
|
|
|
|
return fetchNextData(dataHref, this.isSsr).then((data) => {
|
|
|
|
|
this.sdc[cacheKey] = data
|
|
|
|
|
return data
|
|
|
|
|
})
|
2020-01-27 23:50:59 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-19 06:02:01 +02:00
|
|
|
|
_getServerData(dataHref: string): Promise<object> {
|
2021-03-05 18:16:02 +01:00
|
|
|
|
const { href: resourceKey } = new URL(dataHref, window.location.href)
|
|
|
|
|
if (this.sdr[resourceKey]) {
|
|
|
|
|
return this.sdr[resourceKey]
|
|
|
|
|
}
|
|
|
|
|
return (this.sdr[resourceKey] = fetchNextData(dataHref, this.isSsr)
|
|
|
|
|
.then((data) => {
|
|
|
|
|
delete this.sdr[resourceKey]
|
|
|
|
|
return data
|
|
|
|
|
})
|
|
|
|
|
.catch((err) => {
|
|
|
|
|
delete this.sdr[resourceKey]
|
|
|
|
|
throw err
|
|
|
|
|
}))
|
2019-12-30 19:40:23 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-29 20:01:46 +01:00
|
|
|
|
getInitialProps(
|
|
|
|
|
Component: ComponentType,
|
|
|
|
|
ctx: NextPageContext
|
|
|
|
|
): Promise<any> {
|
|
|
|
|
const { Component: App } = this.components['/_app']
|
2020-08-13 19:39:33 +02:00
|
|
|
|
const AppTree = this._wrapApp(App as AppComponent)
|
2019-12-29 20:01:46 +01:00
|
|
|
|
ctx.AppTree = AppTree
|
2019-12-30 19:40:23 +01:00
|
|
|
|
return loadGetInitialProps<AppContextType<Router>>(App, {
|
|
|
|
|
AppTree,
|
|
|
|
|
Component,
|
|
|
|
|
router: this,
|
|
|
|
|
ctx,
|
2019-12-29 20:01:46 +01:00
|
|
|
|
})
|
2016-10-06 01:52:50 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-17 10:14:50 +01:00
|
|
|
|
abortComponentLoad(as: string, routeProps: RouteProperties): void {
|
2019-06-05 20:15:42 +02:00
|
|
|
|
if (this.clc) {
|
2020-12-17 10:14:50 +01:00
|
|
|
|
Router.events.emit(
|
|
|
|
|
'routeChangeError',
|
|
|
|
|
buildCancellationError(),
|
|
|
|
|
as,
|
|
|
|
|
routeProps
|
|
|
|
|
)
|
2019-06-05 20:15:42 +02:00
|
|
|
|
this.clc()
|
|
|
|
|
this.clc = null
|
2016-10-06 01:52:50 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-31 17:08:12 +01:00
|
|
|
|
notify(
|
|
|
|
|
data: PrivateRouteInfo,
|
|
|
|
|
resetScroll: { x: number; y: number } | null
|
|
|
|
|
): Promise<void> {
|
2020-12-30 05:33:08 +01:00
|
|
|
|
return this.sub(
|
|
|
|
|
data,
|
|
|
|
|
this.components['/_app'].Component as AppComponent,
|
|
|
|
|
resetScroll
|
|
|
|
|
)
|
2016-10-06 01:52:50 +02:00
|
|
|
|
}
|
|
|
|
|
}
|