next
Version:
The React Framework
1,042 lines • 83.2 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
0 && (module.exports = {
createKey: null,
default: null,
matchesMiddleware: null
});
function _export(target, all) {
for(var name in all)Object.defineProperty(target, name, {
enumerable: true,
get: all[name]
});
}
_export(exports, {
createKey: function() {
return createKey;
},
default: function() {
return Router;
},
matchesMiddleware: function() {
return matchesMiddleware;
}
});
const _interop_require_default = require("@swc/helpers/_/_interop_require_default");
const _interop_require_wildcard = require("@swc/helpers/_/_interop_require_wildcard");
const _removetrailingslash = require("./utils/remove-trailing-slash");
const _routeloader = require("../../../client/route-loader");
const _script = require("../../../client/script");
const _iserror = /*#__PURE__*/ _interop_require_wildcard._(require("../../../lib/is-error"));
const _denormalizepagepath = require("../page-path/denormalize-page-path");
const _normalizelocalepath = require("../i18n/normalize-locale-path");
const _mitt = /*#__PURE__*/ _interop_require_default._(require("../mitt"));
const _utils = require("../utils");
const _isdynamic = require("./utils/is-dynamic");
const _parserelativeurl = require("./utils/parse-relative-url");
const _routematcher = require("./utils/route-matcher");
const _routeregex = require("./utils/route-regex");
const _formaturl = require("./utils/format-url");
const _detectdomainlocale = require("../../../client/detect-domain-locale");
const _parsepath = require("./utils/parse-path");
const _addlocale = require("../../../client/add-locale");
const _removelocale = require("../../../client/remove-locale");
const _removebasepath = require("../../../client/remove-base-path");
const _addbasepath = require("../../../client/add-base-path");
const _hasbasepath = require("../../../client/has-base-path");
const _resolvehref = require("../../../client/resolve-href");
const _isapiroute = require("../../../lib/is-api-route");
const _getnextpathnameinfo = require("./utils/get-next-pathname-info");
const _formatnextpathnameinfo = require("./utils/format-next-pathname-info");
const _comparestates = require("./utils/compare-states");
const _islocalurl = require("./utils/is-local-url");
const _isbot = require("./utils/is-bot");
const _omit = require("./utils/omit");
const _interpolateas = require("./utils/interpolate-as");
const _disablesmoothscroll = require("./utils/disable-smooth-scroll");
const _constants = require("../../../lib/constants");
const _deploymentid = require("../deployment-id");
let resolveRewrites;
if (process.env.__NEXT_HAS_REWRITES) {
resolveRewrites = require('./utils/resolve-rewrites').default;
}
function buildCancellationError() {
return Object.assign(Object.defineProperty(new Error('Route Cancelled'), "__NEXT_ERROR_CODE", {
value: "E315",
enumerable: false,
configurable: true
}), {
cancelled: true
});
}
async function matchesMiddleware(options) {
const matchers = await Promise.resolve(options.router.pageLoader.getMiddleware());
if (!matchers) return false;
const { pathname: asPathname } = (0, _parsepath.parsePath)(options.asPath);
// remove basePath first since path prefix has to be in the order of `/${basePath}/${locale}`
const cleanedAs = (0, _hasbasepath.hasBasePath)(asPathname) ? (0, _removebasepath.removeBasePath)(asPathname) : asPathname;
const asWithBasePathAndLocale = (0, _addbasepath.addBasePath)((0, _addlocale.addLocale)(cleanedAs, options.locale));
// Check only path match on client. Matching "has" should be done on server
// where we can access more info such as headers, HttpOnly cookie, etc.
return matchers.some((m)=>new RegExp(m.regexp).test(asWithBasePathAndLocale));
}
function stripOrigin(url) {
const origin = (0, _utils.getLocationOrigin)();
return url.startsWith(origin) ? url.substring(origin.length) : url;
}
function prepareUrlAs(router, url, as) {
// If url and as provided as an object representation,
// we'll format them into the string version here.
let [resolvedHref, resolvedAs] = (0, _resolvehref.resolveHref)(router, url, true);
const origin = (0, _utils.getLocationOrigin)();
const hrefWasAbsolute = resolvedHref.startsWith(origin);
const asWasAbsolute = resolvedAs && resolvedAs.startsWith(origin);
resolvedHref = stripOrigin(resolvedHref);
resolvedAs = resolvedAs ? stripOrigin(resolvedAs) : resolvedAs;
const preparedUrl = hrefWasAbsolute ? resolvedHref : (0, _addbasepath.addBasePath)(resolvedHref);
const preparedAs = as ? stripOrigin((0, _resolvehref.resolveHref)(router, as)) : resolvedAs || resolvedHref;
return {
url: preparedUrl,
as: asWasAbsolute ? preparedAs : (0, _addbasepath.addBasePath)(preparedAs)
};
}
function resolveDynamicRoute(pathname, pages) {
const cleanPathname = (0, _removetrailingslash.removeTrailingSlash)((0, _denormalizepagepath.denormalizePagePath)(pathname));
if (cleanPathname === '/404' || cleanPathname === '/_error') {
return pathname;
}
// handle resolving href for dynamic routes
if (!pages.includes(cleanPathname)) {
// eslint-disable-next-line array-callback-return
pages.some((page)=>{
if ((0, _isdynamic.isDynamicRoute)(page) && (0, _routeregex.getRouteRegex)(page).re.test(cleanPathname)) {
pathname = page;
return true;
}
});
}
return (0, _removetrailingslash.removeTrailingSlash)(pathname);
}
function getMiddlewareData(source, response, options) {
const nextConfig = {
basePath: options.router.basePath,
i18n: {
locales: options.router.locales
},
trailingSlash: Boolean(process.env.__NEXT_TRAILING_SLASH)
};
const rewriteHeader = response.headers.get('x-nextjs-rewrite');
let rewriteTarget = rewriteHeader || response.headers.get('x-nextjs-matched-path');
const matchedPath = response.headers.get(_constants.MATCHED_PATH_HEADER);
if (matchedPath && !rewriteTarget && !matchedPath.includes('__next_data_catchall') && !matchedPath.includes('/_error') && !matchedPath.includes('/404')) {
// leverage x-matched-path to detect next.config.js rewrites
rewriteTarget = matchedPath;
}
if (rewriteTarget) {
if (rewriteTarget.startsWith('/') || process.env.__NEXT_EXTERNAL_MIDDLEWARE_REWRITE_RESOLVE) {
const parsedRewriteTarget = (0, _parserelativeurl.parseRelativeUrl)(rewriteTarget);
const pathnameInfo = (0, _getnextpathnameinfo.getNextPathnameInfo)(parsedRewriteTarget.pathname, {
nextConfig,
parseData: true
});
let fsPathname = (0, _removetrailingslash.removeTrailingSlash)(pathnameInfo.pathname);
return Promise.all([
options.router.pageLoader.getPageList(),
(0, _routeloader.getClientBuildManifest)()
]).then(([pages, { __rewrites: rewrites }])=>{
let as = (0, _addlocale.addLocale)(pathnameInfo.pathname, pathnameInfo.locale);
if ((0, _isdynamic.isDynamicRoute)(as) || !rewriteHeader && pages.includes((0, _normalizelocalepath.normalizeLocalePath)((0, _removebasepath.removeBasePath)(as), options.router.locales).pathname)) {
const parsedSource = (0, _getnextpathnameinfo.getNextPathnameInfo)((0, _parserelativeurl.parseRelativeUrl)(source).pathname, {
nextConfig: process.env.__NEXT_HAS_REWRITES ? undefined : nextConfig,
parseData: true
});
as = (0, _addbasepath.addBasePath)(parsedSource.pathname);
parsedRewriteTarget.pathname = as;
}
if (process.env.__NEXT_HAS_REWRITES) {
const result = resolveRewrites(as, pages, rewrites, parsedRewriteTarget.query, (path)=>resolveDynamicRoute(path, pages), options.router.locales);
if (result.matchedPage) {
parsedRewriteTarget.pathname = result.parsedAs.pathname;
as = parsedRewriteTarget.pathname;
Object.assign(parsedRewriteTarget.query, result.parsedAs.query);
}
} else if (!pages.includes(fsPathname)) {
const resolvedPathname = resolveDynamicRoute(fsPathname, pages);
if (resolvedPathname !== fsPathname) {
fsPathname = resolvedPathname;
}
}
const resolvedHref = !pages.includes(fsPathname) ? resolveDynamicRoute((0, _normalizelocalepath.normalizeLocalePath)((0, _removebasepath.removeBasePath)(parsedRewriteTarget.pathname), options.router.locales).pathname, pages) : fsPathname;
if ((0, _isdynamic.isDynamicRoute)(resolvedHref)) {
const matches = (0, _routematcher.getRouteMatcher)((0, _routeregex.getRouteRegex)(resolvedHref))(as);
Object.assign(parsedRewriteTarget.query, matches || {});
}
return {
type: 'rewrite',
parsedAs: parsedRewriteTarget,
resolvedHref
};
});
}
const src = (0, _parsepath.parsePath)(source);
const pathname = (0, _formatnextpathnameinfo.formatNextPathnameInfo)({
...(0, _getnextpathnameinfo.getNextPathnameInfo)(src.pathname, {
nextConfig,
parseData: true
}),
defaultLocale: options.router.defaultLocale,
buildId: ''
});
return Promise.resolve({
type: 'redirect-external',
destination: `${pathname}${src.query}${src.hash}`
});
}
const redirectTarget = response.headers.get('x-nextjs-redirect');
if (redirectTarget) {
if (redirectTarget.startsWith('/')) {
const src = (0, _parsepath.parsePath)(redirectTarget);
const pathname = (0, _formatnextpathnameinfo.formatNextPathnameInfo)({
...(0, _getnextpathnameinfo.getNextPathnameInfo)(src.pathname, {
nextConfig,
parseData: true
}),
defaultLocale: options.router.defaultLocale,
buildId: ''
});
return Promise.resolve({
type: 'redirect-internal',
newAs: `${pathname}${src.query}${src.hash}`,
newUrl: `${pathname}${src.query}${src.hash}`
});
}
return Promise.resolve({
type: 'redirect-external',
destination: redirectTarget
});
}
return Promise.resolve({
type: 'next'
});
}
async function withMiddlewareEffects(options) {
const matches = await matchesMiddleware(options);
if (!matches || !options.fetchData) {
return null;
}
const data = await options.fetchData();
const effect = await getMiddlewareData(data.dataHref, data.response, options);
return {
dataHref: data.dataHref,
json: data.json,
response: data.response,
text: data.text,
cacheKey: data.cacheKey,
effect
};
}
const manualScrollRestoration = process.env.__NEXT_SCROLL_RESTORATION && typeof window !== 'undefined' && 'scrollRestoration' in window.history && !!function() {
try {
let v = '__next';
return sessionStorage.setItem(v, v), sessionStorage.removeItem(v), true;
} catch (n) {}
}();
const SSG_DATA_NOT_FOUND = Symbol('SSG_DATA_NOT_FOUND');
function fetchRetry(url, attempts, options) {
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/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',
method: options.method || 'GET',
headers: Object.assign({}, options.headers, {
'x-nextjs-data': '1'
})
}).then((response)=>{
return !response.ok && attempts > 1 && response.status >= 500 ? fetchRetry(url, attempts - 1, options) : response;
});
}
function tryToParseAsJSON(text) {
try {
return JSON.parse(text);
} catch (error) {
return null;
}
}
function fetchNextData({ dataHref, inflightCache, isPrefetch, hasMiddleware, isServerRender, parseJSON, persistCache, isBackground, unstable_skipClientCache }) {
const { href: cacheKey } = new URL(dataHref, window.location.href);
const deploymentId = (0, _deploymentid.getDeploymentId)();
const getData = (params)=>fetchRetry(dataHref, isServerRender ? 3 : 1, {
headers: Object.assign({}, isPrefetch ? {
purpose: 'prefetch'
} : {}, isPrefetch && hasMiddleware ? {
'x-middleware-prefetch': '1'
} : {}, deploymentId ? {
'x-deployment-id': deploymentId
} : {}),
method: params?.method ?? 'GET'
}).then((response)=>{
if (response.ok && params?.method === 'HEAD') {
return {
dataHref,
response,
text: '',
json: {},
cacheKey
};
}
return response.text().then((text)=>{
if (!response.ok) {
/**
* When the data response is a redirect because of a middleware
* we do not consider it an error. The headers must bring the
* mapped location.
* TODO: Change the status code in the handler.
*/ if (hasMiddleware && [
301,
302,
307,
308
].includes(response.status)) {
return {
dataHref,
response,
text,
json: {},
cacheKey
};
}
if (response.status === 404) {
if (tryToParseAsJSON(text)?.notFound) {
return {
dataHref,
json: {
notFound: SSG_DATA_NOT_FOUND
},
response,
text,
cacheKey
};
}
}
const error = Object.defineProperty(new Error(`Failed to load static props`), "__NEXT_ERROR_CODE", {
value: "E124",
enumerable: false,
configurable: true
});
/**
* 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.
*/ if (!isServerRender) {
(0, _routeloader.markAssetError)(error);
}
throw error;
}
return {
dataHref,
json: parseJSON ? tryToParseAsJSON(text) : null,
response,
text,
cacheKey
};
});
}).then((data)=>{
if (!persistCache || process.env.NODE_ENV !== 'production' || data.response.headers.get('x-middleware-cache') === 'no-cache') {
delete inflightCache[cacheKey];
}
return data;
}).catch((err)=>{
if (!unstable_skipClientCache) {
delete inflightCache[cacheKey];
}
if (// chrome
err.message === 'Failed to fetch' || // firefox
err.message === 'NetworkError when attempting to fetch resource.' || // safari
err.message === 'Load failed') {
(0, _routeloader.markAssetError)(err);
}
throw err;
});
// when skipping client cache we wait to update
// inflight cache until successful data response
// this allows racing click event with fetching newer data
// without blocking navigation when stale data is available
if (unstable_skipClientCache && persistCache) {
return getData({}).then((data)=>{
if (data.response.headers.get('x-middleware-cache') !== 'no-cache') {
// only update cache if not marked as no-cache
inflightCache[cacheKey] = Promise.resolve(data);
}
return data;
});
}
if (inflightCache[cacheKey] !== undefined) {
return inflightCache[cacheKey];
}
return inflightCache[cacheKey] = getData(isBackground ? {
method: 'HEAD'
} : {});
}
function createKey() {
return Math.random().toString(36).slice(2, 10);
}
function handleHardNavigation({ url, router }) {
// ensure we don't trigger a hard navigation to the same
// URL as this can end up with an infinite refresh
if (url === (0, _addbasepath.addBasePath)((0, _addlocale.addLocale)(router.asPath, router.locale))) {
throw Object.defineProperty(new Error(`Invariant: attempted to hard navigate to the same URL ${url} ${location.href}`), "__NEXT_ERROR_CODE", {
value: "E282",
enumerable: false,
configurable: true
});
}
window.location.href = url;
}
const getCancelledHandler = ({ route, router })=>{
let cancelled = false;
const cancel = router.clc = ()=>{
cancelled = true;
};
const handleCancelled = ()=>{
if (cancelled) {
const error = Object.defineProperty(new Error(`Abort fetching component for route: "${route}"`), "__NEXT_ERROR_CODE", {
value: "E483",
enumerable: false,
configurable: true
});
error.cancelled = true;
throw error;
}
if (cancel === router.clc) {
router.clc = null;
}
};
return handleCancelled;
};
class Router {
static{
this.events = (0, _mitt.default)();
}
constructor(pathname, query, as, { initialProps, pageLoader, App, wrapApp, Component, err, subscription, isFallback, locale, locales, defaultLocale, domainLocales, isPreview }){
// Server Data Cache (full data requests)
this.sdc = {};
// Server Background Cache (HEAD requests)
this.sbc = {};
this.isFirstPopStateEvent = true;
this._key = createKey();
this.onPopState = (e)=>{
const { isFirstPopStateEvent } = this;
this.isFirstPopStateEvent = false;
const state = e.state;
if (!state) {
// 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;
this.changeState('replaceState', (0, _formaturl.formatWithValidation)({
pathname: (0, _addbasepath.addBasePath)(pathname),
query
}), (0, _utils.getURL)());
return;
}
// __NA is used to identify if the history entry can be handled by the app-router.
if (state.__NA) {
window.location.reload();
return;
}
if (!state.__N) {
return;
}
// Safari fires popstateevent when reopening the browser.
if (isFirstPopStateEvent && this.locale === state.options.locale && state.as === this.asPath) {
return;
}
let forcedScroll;
const { url, as, options, key } = state;
if (process.env.__NEXT_SCROLL_RESTORATION) {
if (manualScrollRestoration) {
if (this._key !== key) {
// Snapshot current scroll position:
try {
sessionStorage.setItem('__next_scroll_' + this._key, JSON.stringify({
x: self.pageXOffset,
y: self.pageYOffset
}));
} catch {}
// Restore old scroll position:
try {
const v = sessionStorage.getItem('__next_scroll_' + key);
forcedScroll = JSON.parse(v);
} catch {
forcedScroll = {
x: 0,
y: 0
};
}
}
}
}
this._key = key;
const { pathname } = (0, _parserelativeurl.parseRelativeUrl)(url);
// Make sure we don't re-render on initial load,
// can be caused by navigating back from an external site
if (this.isSsr && as === (0, _addbasepath.addBasePath)(this.asPath) && pathname === (0, _addbasepath.addBasePath)(this.pathname)) {
return;
}
// If the downstream application returns falsy, return.
// They will then be responsible for handling the event.
if (this._bps && !this._bps(state)) {
return;
}
this.change('replaceState', url, as, Object.assign({}, options, {
shallow: options.shallow && this._shallow,
locale: options.locale || this.defaultLocale,
// @ts-ignore internal value not exposed on types
_h: 0
}), forcedScroll);
};
// represents the current component key
const route = (0, _removetrailingslash.removeTrailingSlash)(pathname);
// set up the component cache (by route keys)
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.
if (pathname !== '/_error') {
this.components[route] = {
Component,
initial: true,
props: initialProps,
err,
__N_SSG: initialProps && initialProps.__N_SSG,
__N_SSP: initialProps && initialProps.__N_SSP
};
}
this.components['/_app'] = {
Component: App,
styleSheets: []
};
// Backwards compat for Router.router.events
// TODO: Should be remove the following major version as it was never documented
this.events = Router.events;
this.pageLoader = pageLoader;
// if auto prerendered and dynamic route wait to update asPath
// until after mount to prevent hydration mismatch
const autoExportDynamic = (0, _isdynamic.isDynamicRoute)(pathname) && self.__NEXT_DATA__.autoExport;
this.basePath = process.env.__NEXT_ROUTER_BASEPATH || '';
this.sub = subscription;
this.clc = null;
this._wrapApp = wrapApp;
// make sure to ignore extra popState in safari on navigating
// back from external site
this.isSsr = true;
this.isLocaleDomain = false;
this.isReady = !!(self.__NEXT_DATA__.gssp || self.__NEXT_DATA__.gip || self.__NEXT_DATA__.isExperimentalCompile || self.__NEXT_DATA__.appGip && !self.__NEXT_DATA__.gsp || !autoExportDynamic && !self.location.search && !process.env.__NEXT_HAS_REWRITES);
if (process.env.__NEXT_I18N_SUPPORT) {
this.locales = locales;
this.defaultLocale = defaultLocale;
this.domainLocales = domainLocales;
this.isLocaleDomain = !!(0, _detectdomainlocale.detectDomainLocale)(domainLocales, self.location.hostname);
}
this.state = {
route,
pathname,
query,
asPath: autoExportDynamic ? pathname : as,
isPreview: !!isPreview,
locale: process.env.__NEXT_I18N_SUPPORT ? locale : undefined,
isFallback
};
this._initialMatchesMiddlewarePromise = Promise.resolve(false);
if (typeof window !== 'undefined') {
// make sure "as" doesn't start with double slashes or else it can
// throw an error as it's considered invalid
if (!as.startsWith('//')) {
// in order for `e.state` to work on the `onpopstate` event
// we have to register the initial route upon initialization
const options = {
locale
};
const asPath = (0, _utils.getURL)();
this._initialMatchesMiddlewarePromise = matchesMiddleware({
router: this,
locale,
asPath
}).then((matches)=>{
// if middleware matches we leave resolving to the change function
// as the server needs to resolve for correct priority
;
options._shouldResolveHref = as !== pathname;
this.changeState('replaceState', matches ? asPath : (0, _formaturl.formatWithValidation)({
pathname: (0, _addbasepath.addBasePath)(pathname),
query
}), asPath, options);
return matches;
});
}
window.addEventListener('popstate', this.onPopState);
// 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';
}
}
}
}
reload() {
window.location.reload();
}
/**
* Go back in history
*/ back() {
window.history.back();
}
/**
* Go forward in history
*/ forward() {
window.history.forward();
}
/**
* 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
*/ push(url, as, options = {}) {
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._key, JSON.stringify({
x: self.pageXOffset,
y: self.pageYOffset
}));
} catch {}
}
}
;
({ url, as } = prepareUrlAs(this, url, as));
return this.change('pushState', url, as, options);
}
/**
* 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
*/ replace(url, as, options = {}) {
;
({ url, as } = prepareUrlAs(this, url, as));
return this.change('replaceState', url, as, options);
}
async _bfl(as, resolvedAs, locale, skipNavigate) {
if (process.env.__NEXT_CLIENT_ROUTER_FILTER_ENABLED) {
if (!this._bfl_s && !this._bfl_d) {
const { BloomFilter } = require('../../lib/bloom-filter');
let staticFilterData;
let dynamicFilterData;
try {
;
({ __routerFilterStatic: staticFilterData, __routerFilterDynamic: dynamicFilterData } = await (0, _routeloader.getClientBuildManifest)());
} catch (err) {
// failed to load build manifest hard navigate
// to be safe
console.error(err);
if (skipNavigate) {
return true;
}
handleHardNavigation({
url: (0, _addbasepath.addBasePath)((0, _addlocale.addLocale)(as, locale || this.locale, this.defaultLocale)),
router: this
});
return new Promise(()=>{});
}
const routerFilterSValue = process.env.__NEXT_CLIENT_ROUTER_S_FILTER;
if (!staticFilterData && routerFilterSValue) {
staticFilterData = routerFilterSValue ? routerFilterSValue : undefined;
}
const routerFilterDValue = process.env.__NEXT_CLIENT_ROUTER_D_FILTER;
if (!dynamicFilterData && routerFilterDValue) {
dynamicFilterData = routerFilterDValue ? routerFilterDValue : undefined;
}
if (staticFilterData?.numHashes) {
this._bfl_s = new BloomFilter(staticFilterData.numItems, staticFilterData.errorRate);
this._bfl_s.import(staticFilterData);
}
if (dynamicFilterData?.numHashes) {
this._bfl_d = new BloomFilter(dynamicFilterData.numItems, dynamicFilterData.errorRate);
this._bfl_d.import(dynamicFilterData);
}
}
let matchesBflStatic = false;
let matchesBflDynamic = false;
const pathsToCheck = [
{
as
},
{
as: resolvedAs
}
];
for (const { as: curAs, allowMatchCurrent } of pathsToCheck){
if (curAs) {
const asNoSlash = (0, _removetrailingslash.removeTrailingSlash)(new URL(curAs, 'http://n').pathname);
const asNoSlashLocale = (0, _addbasepath.addBasePath)((0, _addlocale.addLocale)(asNoSlash, locale || this.locale));
if (allowMatchCurrent || asNoSlash !== (0, _removetrailingslash.removeTrailingSlash)(new URL(this.asPath, 'http://n').pathname)) {
matchesBflStatic = matchesBflStatic || !!this._bfl_s?.contains(asNoSlash) || !!this._bfl_s?.contains(asNoSlashLocale);
for (const normalizedAS of [
asNoSlash,
asNoSlashLocale
]){
// if any sub-path of as matches a dynamic filter path
// it should be hard navigated
const curAsParts = normalizedAS.split('/');
for(let i = 0; !matchesBflDynamic && i < curAsParts.length + 1; i++){
const currentPart = curAsParts.slice(0, i).join('/');
if (currentPart && this._bfl_d?.contains(currentPart)) {
matchesBflDynamic = true;
break;
}
}
}
// if the client router filter is matched then we trigger
// a hard navigation
if (matchesBflStatic || matchesBflDynamic) {
if (skipNavigate) {
return true;
}
handleHardNavigation({
url: (0, _addbasepath.addBasePath)((0, _addlocale.addLocale)(as, locale || this.locale, this.defaultLocale)),
router: this
});
return new Promise(()=>{});
}
}
}
}
}
return false;
}
async change(method, url, as, options, forcedScroll) {
if (!(0, _islocalurl.isLocalURL)(url)) {
handleHardNavigation({
url,
router: this
});
return false;
}
// 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.
const isQueryUpdating = options._h === 1;
if (!isQueryUpdating && !options.shallow) {
await this._bfl(as, undefined, options.locale);
}
let shouldResolveHref = isQueryUpdating || options._shouldResolveHref || (0, _parsepath.parsePath)(url).pathname === (0, _parsepath.parsePath)(as).pathname;
const nextState = {
...this.state
};
// for static pages with query params in the URL we delay
// marking the router ready until after the query is updated
// or a navigation has occurred
const readyStateChange = this.isReady !== true;
this.isReady = true;
const isSsr = this.isSsr;
if (!isQueryUpdating) {
this.isSsr = false;
}
// if a route transition is already in progress before
// the query updating is triggered ignore query updating
if (isQueryUpdating && this.clc) {
return false;
}
const prevLocale = nextState.locale;
if (process.env.__NEXT_I18N_SUPPORT) {
nextState.locale = options.locale === false ? this.defaultLocale : options.locale || nextState.locale;
if (typeof options.locale === 'undefined') {
options.locale = nextState.locale;
}
const parsedAs = (0, _parserelativeurl.parseRelativeUrl)((0, _hasbasepath.hasBasePath)(as) ? (0, _removebasepath.removeBasePath)(as) : as);
const localePathResult = (0, _normalizelocalepath.normalizeLocalePath)(parsedAs.pathname, this.locales);
if (localePathResult.detectedLocale) {
nextState.locale = localePathResult.detectedLocale;
parsedAs.pathname = (0, _addbasepath.addBasePath)(parsedAs.pathname);
as = (0, _formaturl.formatWithValidation)(parsedAs);
url = (0, _addbasepath.addBasePath)((0, _normalizelocalepath.normalizeLocalePath)((0, _hasbasepath.hasBasePath)(url) ? (0, _removebasepath.removeBasePath)(url) : url, this.locales).pathname);
}
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(nextState.locale)) {
parsedAs.pathname = (0, _addlocale.addLocale)(parsedAs.pathname, nextState.locale);
handleHardNavigation({
url: (0, _formaturl.formatWithValidation)(parsedAs),
router: this
});
// this was previously a return but was removed in favor
// of better dead code elimination with regenerator runtime
didNavigate = true;
}
}
const detectedDomain = (0, _detectdomainlocale.detectDomainLocale)(this.domainLocales, undefined, nextState.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 && this.isLocaleDomain && self.location.hostname !== detectedDomain.domain) {
const asNoBasePath = (0, _removebasepath.removeBasePath)(as);
handleHardNavigation({
url: `http${detectedDomain.http ? '' : 's'}://${detectedDomain.domain}${(0, _addbasepath.addBasePath)(`${nextState.locale === detectedDomain.defaultLocale ? '' : `/${nextState.locale}`}${asNoBasePath === '/' ? '' : asNoBasePath}` || '/')}`,
router: this
});
// this was previously a return but was removed in favor
// of better dead code elimination with regenerator runtime
didNavigate = true;
}
}
if (didNavigate) {
return new Promise(()=>{});
}
}
// marking route changes as a navigation start entry
if (_utils.ST) {
performance.mark('routeChange');
}
const { shallow = false, scroll = true } = options;
const routeProps = {
shallow
};
if (this._inFlightRoute && this.clc) {
if (!isSsr) {
Router.events.emit('routeChangeError', buildCancellationError(), this._inFlightRoute, routeProps);
}
this.clc();
this.clc = null;
}
as = (0, _addbasepath.addBasePath)((0, _addlocale.addLocale)((0, _hasbasepath.hasBasePath)(as) ? (0, _removebasepath.removeBasePath)(as) : as, options.locale, this.defaultLocale));
const cleanedAs = (0, _removelocale.removeLocale)((0, _hasbasepath.hasBasePath)(as) ? (0, _removebasepath.removeBasePath)(as) : as, nextState.locale);
this._inFlightRoute = as;
const localeChange = prevLocale !== nextState.locale;
// If the url change is only related to a hash change
// We should not proceed. We should only change the state.
if (!isQueryUpdating && this.onlyAHashChange(cleanedAs) && !localeChange) {
nextState.asPath = cleanedAs;
Router.events.emit('hashChangeStart', as, routeProps);
// TODO: do we need the resolved href when only a hash change?
this.changeState(method, url, as, {
...options,
scroll: false
});
if (scroll) {
this.scrollToHash(cleanedAs);
}
try {
await this.set(nextState, this.components[nextState.route], null);
} catch (err) {
if ((0, _iserror.default)(err) && err.cancelled) {
Router.events.emit('routeChangeError', err, cleanedAs, routeProps);
}
throw err;
}
Router.events.emit('hashChangeComplete', as, routeProps);
return true;
}
let parsed = (0, _parserelativeurl.parseRelativeUrl)(url);
let { pathname, query } = parsed;
// 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
let pages, rewrites;
try {
;
[pages, { __rewrites: rewrites }] = await Promise.all([
this.pageLoader.getPageList(),
(0, _routeloader.getClientBuildManifest)(),
this.pageLoader.getMiddleware()
]);
} catch (err) {
// If we fail to resolve the page list or client-build manifest, we must
// do a server-side transition:
handleHardNavigation({
url: as,
router: this
});
return false;
}
// 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
if (!this.urlIsNew(cleanedAs) && !localeChange) {
method = 'replaceState';
}
// we need to resolve the as value using rewrites for dynamic SSG
// pages to allow building the data URL correctly
let resolvedAs = as;
// 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 ? (0, _removetrailingslash.removeTrailingSlash)((0, _removebasepath.removeBasePath)(pathname)) : pathname;
let route = (0, _removetrailingslash.removeTrailingSlash)(pathname);
const parsedAsPathname = as.startsWith('/') && (0, _parserelativeurl.parseRelativeUrl)(as).pathname;
// if we detected the path as app route during prefetching
// trigger hard navigation
if (this.components[pathname]?.__appRouter) {
handleHardNavigation({
url: as,
router: this
});
return new Promise(()=>{});
}
const isMiddlewareRewrite = !!(parsedAsPathname && route !== parsedAsPathname && (!(0, _isdynamic.isDynamicRoute)(route) || !(0, _routematcher.getRouteMatcher)((0, _routeregex.getRouteRegex)(route))(parsedAsPathname)));
// we don't attempt resolve asPath when we need to execute
// middleware as the resolving will occur server-side
const isMiddlewareMatch = !options.shallow && await matchesMiddleware({
asPath: as,
locale: nextState.locale,
router: this
});
if (isQueryUpdating && isMiddlewareMatch) {
shouldResolveHref = false;
}
if (shouldResolveHref && pathname !== '/_error') {
;
options._shouldResolveHref = true;
if (process.env.__NEXT_HAS_REWRITES && as.startsWith('/')) {
const rewritesResult = resolveRewrites((0, _addbasepath.addBasePath)((0, _addlocale.addLocale)(cleanedAs, nextState.locale), true), pages, rewrites, query, (p)=>resolveDynamicRoute(p, pages), this.locales);
if (rewritesResult.externalDest) {
handleHardNavigation({
url: as,
router: this
});
return true;
}
if (!isMiddlewareMatch) {
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;
parsed.pathname = (0, _addbasepath.addBasePath)(pathname);
if (!isMiddlewareMatch) {
url = (0, _formaturl.formatWithValidation)(parsed);
}
}
} else {
parsed.pathname = resolveDynamicRoute(pathname, pages);
if (parsed.pathname !== pathname) {
pathname = parsed.pathname;
parsed.pathname = (0, _addbasepath.addBasePath)(pathname);
if (!isMiddlewareMatch) {
url = (0, _formaturl.formatWithValidation)(parsed);
}
}
}
}
if (!(0, _islocalurl.isLocalURL)(as)) {
if (process.env.NODE_ENV !== 'production') {
throw Object.defineProperty(new Error(`Invalid href: "${url}" and as: "${as}", received relative href and external as` + `\nSee more info: https://nextjs.org/docs/messages/invalid-relative-url-external-as`), "__NEXT_ERROR_CODE", {
value: "E380",
enumerable: false,
configurable: true
});
}
handleHardNavigation({
url: as,
router: this
});
return false;
}
resolvedAs = (0, _removelocale.removeLocale)((0, _removebasepath.removeBasePath)(resolvedAs), nextState.locale);
route = (0, _removetrailingslash.removeTrailingSlash)(pathname);
let routeMatch = false;
if ((0, _isdynamic.isDynamicRoute)(route)) {
const parsedAs = (0, _parserelativeurl.parseRelativeUrl)(resolvedAs);
const asPathname = parsedAs.pathname;
const routeRegex = (0, _routeregex.getRouteRegex)(route);
routeMatch = (0, _routematcher.getRouteMatcher)(routeRegex)(asPathname);
const shouldInterpolate = route === asPathname;
const interpolatedAs = shouldInterpolate ? (0, _interpolateas.interpolateAs)(route, asPathname, query) : {};
if (!routeMatch || shouldInterpolate && !interpolatedAs.result) {
const missingParams = Object.keys(routeRegex.groups).filter((param)=>!query[param] && !routeRegex.groups[param].optional);
if (missingParams.length > 0 && !isMiddlewareMatch) {
if (process.env.NODE_ENV !== 'production') {
console.warn(`${shouldInterpolate ? `Interpolating href` : `Mismatching \`as\` and \`href\``} failed to manually provide ` + `the params: ${missingParams.join(', ')} in the \`href\`'s \`query\``);
}
throw Object.defineProperty(new Error((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}). `) + `Read more: https://nextjs.org/docs/messages/${shouldInterpolate ? 'href-interpolation-failed' : 'incompatible-href-as'}`), "__NEXT_ERROR_CODE", {
value: "E344",
enumerable: false,
configurable: true
});
}
} else if (shouldInterpolate) {
as = (0, _formaturl.formatWithValidation)(Object.assign({}, parsedAs, {
pathname: interpolatedAs.result,
query: (0, _omit.omit)(query, interpolatedAs.params)
}));
} else {
// Merge params into `query`, overwriting any specified in search
Object.assign(query, routeMatch);
}
}
if (!isQueryUpdating) {
Router.events.emit('routeChangeStart', as, routeProps);
}
const isErrorRoute = this.pathname === '/404' || this.pathname === '/_error';
try {
let routeInfo = await this.getRouteInfo({
route,
pathname,
query,
as,
resolvedAs,
routeProps,
locale: nextState.locale,
isPreview: nextState.isPreview,
hasMiddleware: isMiddlewareMatch,
unstable_skipClientCache: options.unstable_skipClientCache,
isQueryUpdating: isQueryUpdating && !this.isFallback,
isMiddlewareRewrite
});
if (!isQueryUpdating && !options.shallow) {
await this._bfl(as, 'resolvedAs' in routeInfo ? routeInfo.resolvedAs : undefined, nextState.locale);
}
if ('route' in routeInfo && isMiddlewareMatch) {
pathname = routeInfo.route || route;
route = pathname;
if (!routeProps.shallow) {
query = Object.assign({}, routeInfo.query || {}, query);
}
const cleanedParsedPathname = (0, _hasbasepath.hasBasePath)(parsed.pathname) ? (0, _removebasepath.removeBasePath)(parsed.pathname) : parsed.pathname;
if (routeMatch && pathname !== cleanedParsedPathname) {
Object.keys(routeMatch).forEach((key)=>{
if (routeMatch && query[key] === routeMatch[key]) {
delete query[key];
}
});
}
if ((0, _isdynamic.isDynamicRoute)(pathname)) {
const prefi