@tanstack/router-core
Advanced tools
Comparing version 0.0.1-beta.54 to 0.0.1-beta.145
/** | ||
* router-core | ||
* @tanstack/router-core/src/index.ts | ||
* | ||
@@ -19,9 +19,42 @@ * Copyright (c) TanStack | ||
const pushStateEvent = 'pushstate'; | ||
const popStateEvent = 'popstate'; | ||
const beforeUnloadEvent = 'beforeunload'; | ||
const beforeUnloadListener = event => { | ||
event.preventDefault(); | ||
// @ts-ignore | ||
return event.returnValue = ''; | ||
}; | ||
const stopBlocking = () => { | ||
removeEventListener(beforeUnloadEvent, beforeUnloadListener, { | ||
capture: true | ||
}); | ||
}; | ||
function createHistory(opts) { | ||
let currentLocation = opts.getLocation(); | ||
let location = opts.getLocation(); | ||
let unsub = () => {}; | ||
let listeners = new Set(); | ||
let blockers = []; | ||
let queue = []; | ||
const tryFlush = () => { | ||
if (blockers.length) { | ||
blockers[0]?.(tryFlush, () => { | ||
blockers = []; | ||
stopBlocking(); | ||
}); | ||
return; | ||
} | ||
while (queue.length) { | ||
queue.shift()?.(); | ||
} | ||
if (!opts.listener) { | ||
onUpdate(); | ||
} | ||
}; | ||
const queueTask = task => { | ||
queue.push(task); | ||
tryFlush(); | ||
}; | ||
const onUpdate = () => { | ||
currentLocation = opts.getLocation(); | ||
location = opts.getLocation(); | ||
listeners.forEach(listener => listener()); | ||
@@ -31,7 +64,7 @@ }; | ||
get location() { | ||
return currentLocation; | ||
return location; | ||
}, | ||
listen: cb => { | ||
if (listeners.size === 0) { | ||
unsub = opts.listener(onUpdate); | ||
unsub = typeof opts.listener === 'function' ? opts.listener(onUpdate) : () => {}; | ||
} | ||
@@ -47,20 +80,40 @@ listeners.add(cb); | ||
push: (path, state) => { | ||
opts.pushState(path, state); | ||
onUpdate(); | ||
queueTask(() => { | ||
opts.pushState(path, state); | ||
}); | ||
}, | ||
replace: (path, state) => { | ||
opts.replaceState(path, state); | ||
onUpdate(); | ||
queueTask(() => { | ||
opts.replaceState(path, state); | ||
}); | ||
}, | ||
go: index => { | ||
opts.go(index); | ||
onUpdate(); | ||
queueTask(() => { | ||
opts.go(index); | ||
}); | ||
}, | ||
back: () => { | ||
opts.back(); | ||
onUpdate(); | ||
queueTask(() => { | ||
opts.back(); | ||
}); | ||
}, | ||
forward: () => { | ||
opts.forward(); | ||
onUpdate(); | ||
queueTask(() => { | ||
opts.forward(); | ||
}); | ||
}, | ||
createHref: str => opts.createHref(str), | ||
block: cb => { | ||
blockers.push(cb); | ||
if (blockers.length === 1) { | ||
addEventListener(beforeUnloadEvent, beforeUnloadListener, { | ||
capture: true | ||
}); | ||
} | ||
return () => { | ||
blockers = blockers.filter(b => b !== cb); | ||
if (!blockers.length) { | ||
stopBlocking(); | ||
} | ||
}; | ||
} | ||
@@ -70,3 +123,3 @@ }; | ||
function createBrowserHistory(opts) { | ||
const getHref = opts?.getHref ?? (() => `${window.location.pathname}${window.location.hash}${window.location.search}`); | ||
const getHref = opts?.getHref ?? (() => `${window.location.pathname}${window.location.search}${window.location.hash}`); | ||
const createHref = opts?.createHref ?? (path => path); | ||
@@ -77,4 +130,20 @@ const getLocation = () => parseLocation(getHref(), history.state); | ||
listener: onUpdate => { | ||
window.addEventListener(pushStateEvent, onUpdate); | ||
window.addEventListener(popStateEvent, onUpdate); | ||
var pushState = window.history.pushState; | ||
window.history.pushState = function () { | ||
let res = pushState.apply(history, arguments); | ||
onUpdate(); | ||
return res; | ||
}; | ||
var replaceState = window.history.replaceState; | ||
window.history.replaceState = function () { | ||
let res = replaceState.apply(history, arguments); | ||
onUpdate(); | ||
return res; | ||
}; | ||
return () => { | ||
window.history.pushState = pushState; | ||
window.history.replaceState = replaceState; | ||
window.removeEventListener(pushStateEvent, onUpdate); | ||
window.removeEventListener(popStateEvent, onUpdate); | ||
@@ -97,3 +166,4 @@ }; | ||
forward: () => window.history.forward(), | ||
go: n => window.history.go(n) | ||
go: n => window.history.go(n), | ||
createHref: path => createHref(path) | ||
}); | ||
@@ -116,5 +186,3 @@ } | ||
getLocation, | ||
listener: () => { | ||
return () => {}; | ||
}, | ||
listener: false, | ||
pushState: (path, state) => { | ||
@@ -141,3 +209,4 @@ currentState = { | ||
}, | ||
go: n => window.history.go(n) | ||
go: n => window.history.go(n), | ||
createHref: path => path | ||
}); | ||
@@ -151,4 +220,4 @@ } | ||
pathname: href.substring(0, hashIndex > 0 ? searchIndex > 0 ? Math.min(hashIndex, searchIndex) : hashIndex : searchIndex > 0 ? searchIndex : href.length), | ||
hash: hashIndex > -1 ? href.substring(hashIndex, searchIndex) : '', | ||
search: searchIndex > -1 ? href.substring(searchIndex) : '', | ||
hash: hashIndex > -1 ? href.substring(hashIndex) : '', | ||
search: searchIndex > -1 ? href.slice(searchIndex, hashIndex === -1 ? undefined : hashIndex) : '', | ||
state | ||
@@ -155,0 +224,0 @@ }; |
/** | ||
* router-core | ||
* @tanstack/router-core/src/index.ts | ||
* | ||
@@ -16,2 +16,3 @@ * Copyright (c) TanStack | ||
var invariant = require('tiny-invariant'); | ||
var tinyWarning = require('tiny-warning'); | ||
var history = require('./history.js'); | ||
@@ -21,10 +22,5 @@ var path = require('./path.js'); | ||
var route = require('./route.js'); | ||
var routeConfig = require('./routeConfig.js'); | ||
var routeMatch = require('./routeMatch.js'); | ||
var router = require('./router.js'); | ||
var searchParams = require('./searchParams.js'); | ||
var utils = require('./utils.js'); | ||
var interop = require('./interop.js'); | ||
var actions = require('./actions.js'); | ||
var store = require('./store.js'); | ||
@@ -34,2 +30,3 @@ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } | ||
var invariant__default = /*#__PURE__*/_interopDefaultLegacy(invariant); | ||
var tinyWarning__default = /*#__PURE__*/_interopDefaultLegacy(tinyWarning); | ||
@@ -42,2 +39,6 @@ | ||
}); | ||
Object.defineProperty(exports, 'warning', { | ||
enumerable: true, | ||
get: function () { return tinyWarning__default["default"]; } | ||
}); | ||
exports.createBrowserHistory = history.createBrowserHistory; | ||
@@ -58,8 +59,13 @@ exports.createHashHistory = history.createHashHistory; | ||
exports.encode = qss.encode; | ||
exports.RootRoute = route.RootRoute; | ||
exports.Route = route.Route; | ||
exports.createRouteConfig = routeConfig.createRouteConfig; | ||
exports.rootRouteId = routeConfig.rootRouteId; | ||
exports.RouteMatch = routeMatch.RouteMatch; | ||
exports.RouterContext = route.RouterContext; | ||
exports.rootRouteId = route.rootRouteId; | ||
exports.PathParamError = router.PathParamError; | ||
exports.Router = router.Router; | ||
exports.defaultFetchServerDataFn = router.defaultFetchServerDataFn; | ||
exports.SearchParamError = router.SearchParamError; | ||
exports.componentTypes = router.componentTypes; | ||
exports.isRedirect = router.isRedirect; | ||
exports.lazyFn = router.lazyFn; | ||
exports.redirect = router.redirect; | ||
exports.defaultParseSearch = searchParams.defaultParseSearch; | ||
@@ -70,10 +76,7 @@ exports.defaultStringifySearch = searchParams.defaultStringifySearch; | ||
exports.functionalUpdate = utils.functionalUpdate; | ||
exports.isPlainObject = utils.isPlainObject; | ||
exports.last = utils.last; | ||
exports.partialDeepEqual = utils.partialDeepEqual; | ||
exports.pick = utils.pick; | ||
exports.warning = utils.warning; | ||
exports.replaceEqualDeep = interop.replaceEqualDeep; | ||
exports.trackDeep = interop.trackDeep; | ||
exports.createAction = actions.createAction; | ||
exports.batch = store.batch; | ||
exports.createStore = store.createStore; | ||
exports.replaceEqualDeep = utils.replaceEqualDeep; | ||
//# sourceMappingURL=index.js.map |
/** | ||
* router-core | ||
* @tanstack/router-core/src/index.ts | ||
* | ||
@@ -82,3 +82,3 @@ * Copyright (c) TanStack | ||
segments.push(...split.map(part => { | ||
if (part.startsWith('*')) { | ||
if (part === '$' || part === '*') { | ||
return { | ||
@@ -109,7 +109,9 @@ type: 'wildcard', | ||
} | ||
function interpolatePath(path, params, leaveWildcard) { | ||
function interpolatePath(path, params, leaveWildcards = false) { | ||
const interpolatedPathSegments = parsePathname(path); | ||
return joinPaths(interpolatedPathSegments.map(segment => { | ||
if (segment.value === '*' && !leaveWildcard) { | ||
return ''; | ||
if (segment.type === 'wildcard') { | ||
const value = params[segment.value]; | ||
if (leaveWildcards) return `${segment.value}${value ?? ''}`; | ||
return value; | ||
} | ||
@@ -124,3 +126,3 @@ if (segment.type === 'param') { | ||
const pathParams = matchByPath(basepath, currentPathname, matchLocation); | ||
// const searchMatched = matchBySearch(currentLocation.search, matchLocation) | ||
// const searchMatched = matchBySearch(location.search, matchLocation) | ||
@@ -133,9 +135,21 @@ if (matchLocation.to && !pathParams) { | ||
function matchByPath(basepath, from, matchLocation) { | ||
if (!from.startsWith(basepath)) { | ||
return undefined; | ||
} | ||
// Remove the base path from the pathname | ||
from = basepath != '/' ? from.substring(basepath.length) : from; | ||
// Default to to $ (wildcard) | ||
const to = `${matchLocation.to ?? '$'}`; | ||
// Parse the from and to | ||
const baseSegments = parsePathname(from); | ||
const to = `${matchLocation.to ?? '*'}`; | ||
const routeSegments = parsePathname(to); | ||
if (!from.startsWith('/')) { | ||
baseSegments.unshift({ | ||
type: 'pathname', | ||
value: '/' | ||
}); | ||
} | ||
if (!to.startsWith('/')) { | ||
routeSegments.unshift({ | ||
type: 'pathname', | ||
value: '/' | ||
}); | ||
} | ||
const params = {}; | ||
@@ -146,4 +160,4 @@ let isMatch = (() => { | ||
const routeSegment = routeSegments[i]; | ||
const isLastRouteSegment = i === routeSegments.length - 1; | ||
const isLastBaseSegment = i === baseSegments.length - 1; | ||
const isLastBaseSegment = i >= baseSegments.length - 1; | ||
const isLastRouteSegment = i >= routeSegments.length - 1; | ||
if (routeSegment) { | ||
@@ -183,3 +197,3 @@ if (routeSegment.type === 'wildcard') { | ||
} | ||
if (isLastRouteSegment && !isLastBaseSegment) { | ||
if (!isLastBaseSegment && isLastRouteSegment) { | ||
return !!matchLocation.fuzzy; | ||
@@ -186,0 +200,0 @@ } |
/** | ||
* router-core | ||
* @tanstack/router-core/src/index.ts | ||
* | ||
@@ -44,4 +44,3 @@ * Copyright (c) TanStack | ||
if (str === 'true') return true; | ||
if (str.charAt(0) === '0') return str; | ||
return +str * 0 === 0 ? +str : str; | ||
return +str * 0 === 0 && +str + '' === str ? +str : str; | ||
} | ||
@@ -48,0 +47,0 @@ function decode(str) { |
/** | ||
* router-core | ||
* @tanstack/router-core/src/index.ts | ||
* | ||
@@ -15,20 +15,134 @@ * Copyright (c) TanStack | ||
var invariant = require('tiny-invariant'); | ||
var path = require('./path.js'); | ||
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } | ||
var invariant__default = /*#__PURE__*/_interopDefaultLegacy(invariant); | ||
const rootRouteId = '__root__'; | ||
// | ParseParamsObj<TPath, TParams> | ||
// The parse type here allows a zod schema to be passed directly to the validator | ||
class Route { | ||
constructor(routeConfig, options, originalIndex, parent, router) { | ||
Object.assign(this, { | ||
...routeConfig, | ||
originalIndex, | ||
options, | ||
getRouter: () => router, | ||
childRoutes: undefined, | ||
getParentRoute: () => parent | ||
}); | ||
router.options.createRoute?.({ | ||
router, | ||
route: this | ||
}); | ||
// Set up in this.init() | ||
// customId!: TCustomId | ||
// Optional | ||
constructor(options) { | ||
this.options = options || {}; | ||
this.isRoot = !options?.getParentRoute; | ||
Route.__onInit(this); | ||
} | ||
init = opts => { | ||
this.originalIndex = opts.originalIndex; | ||
this.router = opts.router; | ||
const options = this.options; | ||
const isRoot = !options?.path && !options?.id; | ||
this.parentRoute = this.options?.getParentRoute?.(); | ||
if (isRoot) { | ||
this.path = rootRouteId; | ||
} else { | ||
invariant__default["default"](this.parentRoute, `Child Route instances must pass a 'getParentRoute: () => ParentRoute' option that returns a Route instance.`); | ||
} | ||
let path$1 = isRoot ? rootRouteId : options.path; | ||
// If the path is anything other than an index path, trim it up | ||
if (path$1 && path$1 !== '/') { | ||
path$1 = path.trimPath(path$1); | ||
} | ||
const customId = options?.id || path$1; | ||
// Strip the parentId prefix from the first level of children | ||
let id = isRoot ? rootRouteId : path.joinPaths([this.parentRoute.id === rootRouteId ? '' : this.parentRoute.id, customId]); | ||
if (path$1 === rootRouteId) { | ||
path$1 = '/'; | ||
} | ||
if (id !== rootRouteId) { | ||
id = path.joinPaths(['/', id]); | ||
} | ||
const fullPath = id === rootRouteId ? '/' : path.joinPaths([this.parentRoute.fullPath, path$1]); | ||
this.path = path$1; | ||
this.id = id; | ||
// this.customId = customId as TCustomId | ||
this.fullPath = fullPath; | ||
this.to = fullPath; | ||
}; | ||
addChildren = children => { | ||
this.children = children; | ||
return this; | ||
}; | ||
update = options => { | ||
Object.assign(this.options, options); | ||
return this; | ||
}; | ||
static __onInit = route => { | ||
// This is a dummy static method that should get | ||
// replaced by a framework specific implementation if necessary | ||
}; | ||
} | ||
class RouterContext { | ||
constructor() {} | ||
createRootRoute = options => { | ||
return new RootRoute(options); | ||
}; | ||
} | ||
class RootRoute extends Route { | ||
constructor(options) { | ||
super(options); | ||
} | ||
} | ||
// const rootRoute = new RootRoute({ | ||
// validateSearch: () => null as unknown as { root?: boolean }, | ||
// }) | ||
// const aRoute = new Route({ | ||
// getParentRoute: () => rootRoute, | ||
// path: 'a', | ||
// validateSearch: () => null as unknown as { a?: string }, | ||
// }) | ||
// const bRoute = new Route({ | ||
// getParentRoute: () => aRoute, | ||
// path: 'b', | ||
// }) | ||
// const rootIsRoot = rootRoute.isRoot | ||
// // ^? | ||
// const aIsRoot = aRoute.isRoot | ||
// // ^? | ||
// const rId = rootRoute.id | ||
// // ^? | ||
// const aId = aRoute.id | ||
// // ^? | ||
// const bId = bRoute.id | ||
// // ^? | ||
// const rPath = rootRoute.fullPath | ||
// // ^? | ||
// const aPath = aRoute.fullPath | ||
// // ^? | ||
// const bPath = bRoute.fullPath | ||
// // ^? | ||
// const rSearch = rootRoute.__types.fullSearchSchema | ||
// // ^? | ||
// const aSearch = aRoute.__types.fullSearchSchema | ||
// // ^? | ||
// const bSearch = bRoute.__types.fullSearchSchema | ||
// // ^? | ||
// const config = rootRoute.addChildren([aRoute.addChildren([bRoute])]) | ||
// // ^? | ||
exports.RootRoute = RootRoute; | ||
exports.Route = Route; | ||
exports.RouterContext = RouterContext; | ||
exports.rootRouteId = rootRouteId; | ||
//# sourceMappingURL=route.js.map |
/** | ||
* router-core | ||
* @tanstack/router-core/src/index.ts | ||
* | ||
@@ -15,10 +15,7 @@ * Copyright (c) TanStack | ||
var reactStore = require('@tanstack/react-store'); | ||
var invariant = require('tiny-invariant'); | ||
var path = require('./path.js'); | ||
var route = require('./route.js'); | ||
var routeMatch = require('./routeMatch.js'); | ||
var searchParams = require('./searchParams.js'); | ||
var store = require('./store.js'); | ||
var utils = require('./utils.js'); | ||
var interop = require('./interop.js'); | ||
var history = require('./history.js'); | ||
@@ -30,33 +27,9 @@ | ||
const defaultFetchServerDataFn = async ({ | ||
router, | ||
routeMatch | ||
}) => { | ||
const next = router.buildNext({ | ||
to: '.', | ||
search: d => ({ | ||
...(d ?? {}), | ||
__data: { | ||
matchId: routeMatch.id | ||
} | ||
}) | ||
}); | ||
const res = await fetch(next.href, { | ||
method: 'GET', | ||
signal: routeMatch.abortController.signal | ||
}); | ||
if (res.ok) { | ||
return res.json(); | ||
} | ||
throw new Error('Failed to fetch match data'); | ||
}; | ||
// | ||
const componentTypes = ['component', 'errorComponent', 'pendingComponent']; | ||
class Router { | ||
#unsubHistory; | ||
startedLoadingAt = Date.now(); | ||
resolveNavigation = () => {}; | ||
constructor(options) { | ||
this.options = { | ||
defaultLoaderGcMaxAge: 5 * 60 * 1000, | ||
defaultLoaderMaxAge: 0, | ||
defaultPreloadMaxAge: 2000, | ||
defaultPreloadDelay: 50, | ||
@@ -66,46 +39,65 @@ context: undefined, | ||
stringifySearch: options?.stringifySearch ?? searchParams.defaultStringifySearch, | ||
parseSearch: options?.parseSearch ?? searchParams.defaultParseSearch, | ||
fetchServerDataFn: options?.fetchServerDataFn ?? defaultFetchServerDataFn | ||
parseSearch: options?.parseSearch ?? searchParams.defaultParseSearch | ||
// fetchServerDataFn: options?.fetchServerDataFn ?? defaultFetchServerDataFn, | ||
}; | ||
this.store = store.createStore(getInitialRouterState()); | ||
this.basepath = ''; | ||
this.__store = new reactStore.Store(getInitialRouterState(), { | ||
onUpdate: () => { | ||
const prev = this.state; | ||
this.state = this.__store.state; | ||
const matchesByIdChanged = prev.matchesById !== this.state.matchesById; | ||
let matchesChanged; | ||
let pendingMatchesChanged; | ||
if (!matchesByIdChanged) { | ||
matchesChanged = prev.matchIds.length !== this.state.matchIds.length || prev.matchIds.some((d, i) => d !== this.state.matchIds[i]); | ||
pendingMatchesChanged = prev.pendingMatchIds.length !== this.state.pendingMatchIds.length || prev.pendingMatchIds.some((d, i) => d !== this.state.pendingMatchIds[i]); | ||
} | ||
if (matchesByIdChanged || matchesChanged) { | ||
this.state.matches = this.state.matchIds.map(id => { | ||
return this.state.matchesById[id]; | ||
}); | ||
} | ||
if (matchesByIdChanged || pendingMatchesChanged) { | ||
this.state.pendingMatches = this.state.pendingMatchIds.map(id => { | ||
return this.state.matchesById[id]; | ||
}); | ||
} | ||
this.state.isFetching = [...this.state.matches, ...this.state.pendingMatches].some(d => d.isFetching); | ||
}, | ||
defaultPriority: 'low' | ||
}); | ||
this.state = this.__store.state; | ||
this.update(options); | ||
// Allow frameworks to hook into the router creation | ||
this.options.Router?.(this); | ||
const next = this.buildNext({ | ||
hash: true, | ||
fromCurrent: true, | ||
search: true, | ||
state: true | ||
}); | ||
if (this.state.location.href !== next.href) { | ||
this.#commitLocation({ | ||
...next, | ||
replace: true | ||
}); | ||
} | ||
} | ||
reset = () => { | ||
this.store.setState(s => Object.assign(s, getInitialRouterState())); | ||
this.__store.setState(s => Object.assign(s, getInitialRouterState())); | ||
}; | ||
mount = () => { | ||
// Mount only does anything on the client | ||
if (!isServer) { | ||
// If the router matches are empty, load the matches | ||
if (!this.store.state.currentMatches.length) { | ||
this.load(); | ||
} | ||
const visibilityChangeEvent = 'visibilitychange'; | ||
const focusEvent = 'focus'; | ||
// If the router matches are empty, start loading the matches | ||
// if (!this.state.matches.length) { | ||
this.safeLoad(); | ||
// } | ||
}; | ||
// addEventListener does not exist in React Native, but window does | ||
// In the future, we might need to invert control here for more adapters | ||
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition | ||
if (window.addEventListener) { | ||
// Listen to visibilitychange and focus | ||
window.addEventListener(visibilityChangeEvent, this.#onFocus, false); | ||
window.addEventListener(focusEvent, this.#onFocus, false); | ||
update = opts => { | ||
this.options = { | ||
...this.options, | ||
...opts, | ||
context: { | ||
...this.options.context, | ||
...opts?.context | ||
} | ||
return () => { | ||
if (window.removeEventListener) { | ||
// Be sure to unsubscribe if a new handler is set | ||
window.removeEventListener(visibilityChangeEvent, this.#onFocus); | ||
window.removeEventListener(focusEvent, this.#onFocus); | ||
} | ||
}; | ||
} | ||
return () => {}; | ||
}; | ||
update = opts => { | ||
Object.assign(this.options, opts); | ||
}; | ||
if (!this.history || this.options.history && this.options.history !== this.history) { | ||
@@ -116,8 +108,12 @@ if (this.#unsubHistory) { | ||
this.history = this.options.history ?? (isServer ? history.createMemoryHistory() : history.createBrowserHistory()); | ||
this.store.setState(s => { | ||
s.latestLocation = this.#parseLocation(); | ||
s.currentLocation = s.latestLocation; | ||
}); | ||
const parsedLocation = this.#parseLocation(); | ||
this.__store.setState(s => ({ | ||
...s, | ||
resolvedLocation: parsedLocation, | ||
location: parsedLocation | ||
})); | ||
this.#unsubHistory = this.history.listen(() => { | ||
this.load(this.#parseLocation(this.store.state.latestLocation)); | ||
this.safeLoad({ | ||
next: this.#parseLocation(this.state.location) | ||
}); | ||
}); | ||
@@ -127,8 +123,7 @@ } | ||
basepath, | ||
routeConfig | ||
routeTree | ||
} = this.options; | ||
this.basepath = `/${path.trimPath(basepath ?? '') ?? ''}`; | ||
if (routeConfig) { | ||
this.routesById = {}; | ||
this.routeTree = this.#buildRouteTree(routeConfig); | ||
if (routeTree && routeTree !== this.routeTree) { | ||
this.#buildRouteTree(routeTree); | ||
} | ||
@@ -139,137 +134,102 @@ return this; | ||
const next = this.#buildLocation(opts); | ||
const matches = this.matchRoutes(next.pathname); | ||
const __preSearchFilters = matches.map(match => match.route.options.preSearchFilters ?? []).flat().filter(Boolean); | ||
const __postSearchFilters = matches.map(match => match.route.options.postSearchFilters ?? []).flat().filter(Boolean); | ||
const __matches = this.matchRoutes(next.pathname, next.search); | ||
return this.#buildLocation({ | ||
...opts, | ||
__preSearchFilters, | ||
__postSearchFilters | ||
__matches | ||
}); | ||
}; | ||
cancelMatches = () => { | ||
[...this.store.state.currentMatches, ...(this.store.state.pendingMatches || [])].forEach(match => { | ||
match.cancel(); | ||
this.state.matches.forEach(match => { | ||
this.cancelMatch(match.id); | ||
}); | ||
}; | ||
load = async next => { | ||
let now = Date.now(); | ||
const startedAt = now; | ||
this.startedLoadingAt = startedAt; | ||
cancelMatch = id => { | ||
this.getRouteMatch(id)?.abortController?.abort(); | ||
}; | ||
safeLoad = opts => { | ||
return this.load(opts).catch(err => { | ||
// console.warn(err) | ||
// invariant(false, 'Encountered an error during router.load()! ☝️.') | ||
}); | ||
}; | ||
latestLoadPromise = Promise.resolve(); | ||
load = async opts => { | ||
const promise = new Promise(async (resolve, reject) => { | ||
let latestPromise; | ||
const checkLatest = () => { | ||
return this.latestLoadPromise !== promise ? this.latestLoadPromise : undefined; | ||
}; | ||
// Cancel any pending matches | ||
this.cancelMatches(); | ||
let matches; | ||
store.batch(() => { | ||
if (next) { | ||
// Ingest the new location | ||
this.store.setState(s => { | ||
s.latestLocation = next; | ||
}); | ||
} | ||
// Cancel any pending matches | ||
// this.cancelMatches() | ||
// Match the routes | ||
matches = this.matchRoutes(this.store.state.latestLocation.pathname, { | ||
strictParseParams: true | ||
}); | ||
this.store.setState(s => { | ||
s.status = 'loading'; | ||
s.pendingMatches = matches; | ||
s.pendingLocation = this.store.state.latestLocation; | ||
}); | ||
}); | ||
let pendingMatches; | ||
this.__store.batch(() => { | ||
if (opts?.next) { | ||
// Ingest the new location | ||
this.__store.setState(s => ({ | ||
...s, | ||
location: opts.next | ||
})); | ||
} | ||
// Load the matches | ||
try { | ||
await this.loadMatches(matches); | ||
} catch (err) { | ||
console.warn(err); | ||
invariant__default["default"](false, 'Matches failed to load due to error above ☝️. Navigation cancelled!'); | ||
} | ||
if (this.startedLoadingAt !== startedAt) { | ||
// Ignore side-effects of outdated side-effects | ||
return this.navigationPromise; | ||
} | ||
const previousMatches = this.store.state.currentMatches; | ||
const exiting = [], | ||
staying = []; | ||
previousMatches.forEach(d => { | ||
if (matches.find(dd => dd.id === d.id)) { | ||
staying.push(d); | ||
} else { | ||
exiting.push(d); | ||
} | ||
}); | ||
const entering = matches.filter(d => { | ||
return !previousMatches.find(dd => dd.id === d.id); | ||
}); | ||
now = Date.now(); | ||
exiting.forEach(d => { | ||
d.__onExit?.({ | ||
params: d.params, | ||
search: d.store.state.routeSearch | ||
}); | ||
// Clear non-loading error states when match leaves | ||
if (d.store.state.status === 'error' && !d.store.state.isFetching) { | ||
d.store.setState(s => { | ||
s.status = 'idle'; | ||
s.error = undefined; | ||
// Match the routes | ||
pendingMatches = this.matchRoutes(this.state.location.pathname, this.state.location.search, { | ||
throwOnError: opts?.throwOnError, | ||
debug: true | ||
}); | ||
} | ||
const gc = Math.max(d.route.options.loaderGcMaxAge ?? this.options.defaultLoaderGcMaxAge ?? 0, d.route.options.loaderMaxAge ?? this.options.defaultLoaderMaxAge ?? 0); | ||
if (gc > 0) { | ||
this.store.setState(s => { | ||
s.matchCache[d.id] = { | ||
gc: gc == Infinity ? Number.MAX_SAFE_INTEGER : now + gc, | ||
match: d | ||
}; | ||
}); | ||
} | ||
}); | ||
staying.forEach(d => { | ||
d.route.options.onTransition?.({ | ||
params: d.params, | ||
search: d.store.state.routeSearch | ||
this.__store.setState(s => ({ | ||
...s, | ||
status: 'pending', | ||
pendingMatchIds: pendingMatches.map(d => d.id), | ||
matchesById: this.#mergeMatches(s.matchesById, pendingMatches) | ||
})); | ||
}); | ||
}); | ||
entering.forEach(d => { | ||
d.__onExit = d.route.options.onLoaded?.({ | ||
params: d.params, | ||
search: d.store.state.search | ||
}); | ||
delete this.store.state.matchCache[d.id]; | ||
}); | ||
this.store.setState(s => { | ||
Object.assign(s, { | ||
status: 'idle', | ||
currentLocation: this.store.state.latestLocation, | ||
currentMatches: matches, | ||
pendingLocation: undefined, | ||
pendingMatches: undefined | ||
}); | ||
}); | ||
this.options.onRouteChange?.(); | ||
this.resolveNavigation(); | ||
}; | ||
cleanMatchCache = () => { | ||
const now = Date.now(); | ||
this.store.setState(s => { | ||
Object.keys(s.matchCache).forEach(matchId => { | ||
const entry = s.matchCache[matchId]; | ||
try { | ||
// Load the matches | ||
await this.loadMatches(pendingMatches); | ||
// Don't remove loading matches | ||
if (entry.match.store.state.status === 'loading') { | ||
return; | ||
// Only apply the latest transition | ||
if (latestPromise = checkLatest()) { | ||
return await latestPromise; | ||
} | ||
// Do not remove successful matches that are still valid | ||
if (entry.gc > 0 && entry.gc > now) { | ||
return; | ||
const prevLocation = this.state.resolvedLocation; | ||
this.__store.setState(s => ({ | ||
...s, | ||
status: 'idle', | ||
resolvedLocation: s.location, | ||
matchIds: s.pendingMatchIds, | ||
pendingMatchIds: [] | ||
})); | ||
if (prevLocation.href !== this.state.location.href) { | ||
this.options.onRouteChange?.(); | ||
} | ||
// Everything else gets removed | ||
delete s.matchCache[matchId]; | ||
}); | ||
resolve(); | ||
} catch (err) { | ||
// Only apply the latest transition | ||
if (latestPromise = checkLatest()) { | ||
return await latestPromise; | ||
} | ||
reject(err); | ||
} | ||
}); | ||
this.latestLoadPromise = promise; | ||
return this.latestLoadPromise; | ||
}; | ||
#mergeMatches = (prevMatchesById, nextMatches) => { | ||
const nextMatchesById = { | ||
...prevMatchesById | ||
}; | ||
let hadNew = false; | ||
nextMatches.forEach(match => { | ||
if (!nextMatchesById[match.id]) { | ||
hadNew = true; | ||
nextMatchesById[match.id] = match; | ||
} | ||
}); | ||
if (!hadNew) { | ||
return prevMatchesById; | ||
} | ||
return nextMatchesById; | ||
}; | ||
getRoute = id => { | ||
@@ -280,163 +240,343 @@ const route = this.routesById[id]; | ||
}; | ||
loadRoute = async (navigateOpts = this.store.state.latestLocation) => { | ||
preloadRoute = async (navigateOpts = this.state.location) => { | ||
const next = this.buildNext(navigateOpts); | ||
const matches = this.matchRoutes(next.pathname, { | ||
strictParseParams: true | ||
const matches = this.matchRoutes(next.pathname, next.search, { | ||
throwOnError: true | ||
}); | ||
await this.loadMatches(matches); | ||
return matches; | ||
}; | ||
preloadRoute = async (navigateOpts = this.store.state.latestLocation, loaderOpts) => { | ||
const next = this.buildNext(navigateOpts); | ||
const matches = this.matchRoutes(next.pathname, { | ||
strictParseParams: true | ||
this.__store.setState(s => { | ||
return { | ||
...s, | ||
matchesById: this.#mergeMatches(s.matchesById, matches) | ||
}; | ||
}); | ||
await this.loadMatches(matches, { | ||
preload: true, | ||
maxAge: loaderOpts.maxAge ?? this.options.defaultPreloadMaxAge ?? this.options.defaultLoaderMaxAge ?? 0, | ||
gcMaxAge: loaderOpts.gcMaxAge ?? this.options.defaultPreloadGcMaxAge ?? this.options.defaultLoaderGcMaxAge ?? 0 | ||
maxAge: navigateOpts.maxAge | ||
}); | ||
return matches; | ||
}; | ||
matchRoutes = (pathname, opts) => { | ||
const matches = []; | ||
if (!this.routeTree) { | ||
return matches; | ||
} | ||
const existingMatches = [...this.store.state.currentMatches, ...(this.store.state.pendingMatches ?? [])]; | ||
const recurse = async routes => { | ||
const parentMatch = utils.last(matches); | ||
let params = parentMatch?.params ?? {}; | ||
const filteredRoutes = this.options.filterRoutes?.(routes) ?? routes; | ||
let foundRoutes = []; | ||
const findMatchInRoutes = (parentRoutes, routes) => { | ||
routes.some(route => { | ||
if (!route.path && route.childRoutes?.length) { | ||
return findMatchInRoutes([...foundRoutes, route], route.childRoutes); | ||
} | ||
const fuzzy = !!(route.path !== '/' || route.childRoutes?.length); | ||
const matchParams = path.matchPathname(this.basepath, pathname, { | ||
to: route.fullPath, | ||
fuzzy, | ||
caseSensitive: route.options.caseSensitive ?? this.options.caseSensitive | ||
}); | ||
if (matchParams) { | ||
let parsedParams; | ||
try { | ||
parsedParams = route.options.parseParams?.(matchParams) ?? matchParams; | ||
} catch (err) { | ||
if (opts?.strictParseParams) { | ||
throw err; | ||
} | ||
} | ||
params = { | ||
...params, | ||
...parsedParams | ||
}; | ||
} | ||
if (!!matchParams) { | ||
foundRoutes = [...parentRoutes, route]; | ||
} | ||
return !!foundRoutes.length; | ||
cleanMatches = () => { | ||
const now = Date.now(); | ||
const outdatedMatchIds = Object.values(this.state.matchesById).filter(match => { | ||
const route = this.getRoute(match.routeId); | ||
return !this.state.matchIds.includes(match.id) && !this.state.pendingMatchIds.includes(match.id) && match.preloadInvalidAt < now && (route.options.gcMaxAge ? match.updatedAt + route.options.gcMaxAge < now : true); | ||
}).map(d => d.id); | ||
if (outdatedMatchIds.length) { | ||
this.__store.setState(s => { | ||
const matchesById = { | ||
...s.matchesById | ||
}; | ||
outdatedMatchIds.forEach(id => { | ||
delete matchesById[id]; | ||
}); | ||
return !!foundRoutes.length; | ||
}; | ||
findMatchInRoutes([], filteredRoutes); | ||
if (!foundRoutes.length) { | ||
return; | ||
} | ||
foundRoutes.forEach(foundRoute => { | ||
const interpolatedPath = path.interpolatePath(foundRoute.path, params); | ||
const matchId = path.interpolatePath(foundRoute.id, params, true); | ||
const match = existingMatches.find(d => d.id === matchId) || this.store.state.matchCache[matchId]?.match || new routeMatch.RouteMatch(this, foundRoute, { | ||
id: matchId, | ||
params, | ||
pathname: path.joinPaths([this.basepath, interpolatedPath]) | ||
}); | ||
matches.push(match); | ||
return { | ||
...s, | ||
matchesById | ||
}; | ||
}); | ||
const foundRoute = utils.last(foundRoutes); | ||
if (foundRoute.childRoutes?.length) { | ||
recurse(foundRoute.childRoutes); | ||
} | ||
}; | ||
matchRoutes = (pathname, locationSearch, opts) => { | ||
let routeParams = {}; | ||
let foundRoute = this.flatRoutes.find(route => { | ||
const matchedParams = path.matchPathname(this.basepath, pathname, { | ||
to: route.fullPath, | ||
caseSensitive: route.options.caseSensitive ?? this.options.caseSensitive | ||
}); | ||
if (matchedParams) { | ||
routeParams = matchedParams; | ||
return true; | ||
} | ||
}; | ||
recurse([this.routeTree]); | ||
linkMatches(matches); | ||
return matches; | ||
}; | ||
loadMatches = async (resolvedMatches, loaderOpts) => { | ||
this.cleanMatchCache(); | ||
resolvedMatches.forEach(async match => { | ||
// Validate the match (loads search params etc) | ||
match.__validate(); | ||
return false; | ||
}); | ||
let routeCursor = foundRoute || this.routesById['__root__']; | ||
let matchedRoutes = [routeCursor]; | ||
while (routeCursor?.parentRoute) { | ||
routeCursor = routeCursor.parentRoute; | ||
if (routeCursor) matchedRoutes.unshift(routeCursor); | ||
} | ||
// Check each match middleware to see if the route can be accessed | ||
await Promise.all(resolvedMatches.map(async match => { | ||
// Alright, by now we should have all of our | ||
// matching routes and their param pairs, let's | ||
// Turn them into actual `Match` objects and | ||
// accumulate the params into a single params bag | ||
let allParams = {}; | ||
// Existing matches are matches that are already loaded along with | ||
// pending matches that are still loading | ||
const matches = matchedRoutes.map(route => { | ||
let parsedParams; | ||
let parsedParamsError; | ||
try { | ||
await match.route.options.beforeLoad?.({ | ||
router: this, | ||
match | ||
parsedParams = route.options.parseParams?.(routeParams) ?? routeParams; | ||
// (typeof route.options.parseParams === 'object' && | ||
// route.options.parseParams.parse | ||
// ? route.options.parseParams.parse(routeParams) | ||
// : (route.options.parseParams as any)?.(routeParams!)) ?? routeParams | ||
} catch (err) { | ||
parsedParamsError = new PathParamError(err.message, { | ||
cause: err | ||
}); | ||
} catch (err) { | ||
if (!loaderOpts?.preload) { | ||
match.route.options.onLoadError?.(err); | ||
if (opts?.throwOnError) { | ||
throw parsedParamsError; | ||
} | ||
throw err; | ||
} | ||
})); | ||
const matchPromises = resolvedMatches.map(async (match, index) => { | ||
const prevMatch = resolvedMatches[1]; | ||
const search = match.store.state.search; | ||
if (search.__data?.matchId && search.__data.matchId !== match.id) { | ||
return; | ||
// Add the parsed params to the accumulated params bag | ||
Object.assign(allParams, parsedParams); | ||
const interpolatedPath = path.interpolatePath(route.path, allParams); | ||
const key = route.options.key ? route.options.key({ | ||
params: allParams, | ||
search: locationSearch | ||
}) ?? '' : ''; | ||
const stringifiedKey = key ? JSON.stringify(key) : ''; | ||
const matchId = path.interpolatePath(route.id, allParams, true) + stringifiedKey; | ||
// Waste not, want not. If we already have a match for this route, | ||
// reuse it. This is important for layout routes, which might stick | ||
// around between navigation actions that only change leaf routes. | ||
const existingMatch = this.getRouteMatch(matchId); | ||
if (existingMatch) { | ||
return { | ||
...existingMatch | ||
}; | ||
} | ||
match.load(loaderOpts); | ||
if (match.store.state.status !== 'success' && match.__loadPromise) { | ||
// Wait for the first sign of activity from the match | ||
await match.__loadPromise; | ||
} | ||
if (prevMatch) { | ||
await prevMatch.__loadPromise; | ||
} | ||
// Create a fresh route match | ||
const hasLoaders = !!(route.options.loader || componentTypes.some(d => route.options[d]?.preload)); | ||
const routeMatch = { | ||
id: matchId, | ||
key: stringifiedKey, | ||
routeId: route.id, | ||
params: allParams, | ||
pathname: path.joinPaths([this.basepath, interpolatedPath]), | ||
updatedAt: Date.now(), | ||
invalidAt: Infinity, | ||
preloadInvalidAt: Infinity, | ||
routeSearch: {}, | ||
search: {}, | ||
status: hasLoaders ? 'idle' : 'success', | ||
isFetching: false, | ||
invalid: false, | ||
error: undefined, | ||
paramsError: parsedParamsError, | ||
searchError: undefined, | ||
loaderData: undefined, | ||
loadPromise: Promise.resolve(), | ||
routeContext: undefined, | ||
context: undefined, | ||
abortController: new AbortController(), | ||
fetchedAt: 0 | ||
}; | ||
return routeMatch; | ||
}); | ||
await Promise.all(matchPromises); | ||
}; | ||
loadMatchData = async routeMatch => { | ||
if (isServer || !this.options.useServerData) { | ||
return (await routeMatch.route.options.loader?.({ | ||
// parentLoaderPromise: routeMatch.parentMatch.dataPromise, | ||
params: routeMatch.params, | ||
search: routeMatch.store.state.routeSearch, | ||
signal: routeMatch.abortController.signal | ||
})) || {}; | ||
} else { | ||
// Refresh: | ||
// '/dashboard' | ||
// '/dashboard/invoices/' | ||
// '/dashboard/invoices/123' | ||
// New: | ||
// '/dashboard/invoices/456' | ||
// TODO: batch requests when possible | ||
const res = await this.options.fetchServerDataFn({ | ||
router: this, | ||
routeMatch | ||
// Take each match and resolve its search params and context | ||
// This has to happen after the matches are created or found | ||
// so that we can use the parent match's search params and context | ||
matches.forEach((match, i) => { | ||
const parentMatch = matches[i - 1]; | ||
const route = this.getRoute(match.routeId); | ||
const searchInfo = (() => { | ||
// Validate the search params and stabilize them | ||
const parentSearchInfo = { | ||
search: parentMatch?.search ?? locationSearch, | ||
routeSearch: parentMatch?.routeSearch ?? locationSearch | ||
}; | ||
try { | ||
const validator = typeof route.options.validateSearch === 'object' ? route.options.validateSearch.parse : route.options.validateSearch; | ||
const routeSearch = validator?.(parentSearchInfo.search) ?? {}; | ||
const search = { | ||
...parentSearchInfo.search, | ||
...routeSearch | ||
}; | ||
return { | ||
routeSearch: utils.replaceEqualDeep(match.routeSearch, routeSearch), | ||
search: utils.replaceEqualDeep(match.search, search) | ||
}; | ||
} catch (err) { | ||
match.searchError = new SearchParamError(err.message, { | ||
cause: err | ||
}); | ||
if (opts?.throwOnError) { | ||
throw match.searchError; | ||
} | ||
return parentSearchInfo; | ||
} | ||
})(); | ||
const contextInfo = (() => { | ||
try { | ||
const routeContext = route.options.getContext?.({ | ||
parentContext: parentMatch?.routeContext ?? {}, | ||
context: parentMatch?.context ?? this?.options.context ?? {}, | ||
params: match.params, | ||
search: match.search | ||
}) || {}; | ||
const context = { | ||
...(parentMatch?.context ?? this?.options.context), | ||
...routeContext | ||
}; | ||
return { | ||
context, | ||
routeContext | ||
}; | ||
} catch (err) { | ||
route.options.onError?.(err); | ||
throw err; | ||
} | ||
})(); | ||
Object.assign(match, { | ||
...searchInfo, | ||
...contextInfo | ||
}); | ||
return res; | ||
} | ||
}); | ||
return matches; | ||
}; | ||
invalidateRoute = async opts => { | ||
const next = this.buildNext(opts); | ||
const unloadedMatchIds = this.matchRoutes(next.pathname).map(d => d.id); | ||
await Promise.allSettled([...this.store.state.currentMatches, ...(this.store.state.pendingMatches ?? [])].map(async match => { | ||
if (unloadedMatchIds.includes(match.id)) { | ||
return match.invalidate(); | ||
loadMatches = async (resolvedMatches, opts) => { | ||
this.cleanMatches(); | ||
let firstBadMatchIndex; | ||
// Check each match middleware to see if the route can be accessed | ||
try { | ||
await Promise.all(resolvedMatches.map(async (match, index) => { | ||
const route = this.getRoute(match.routeId); | ||
if (!opts?.preload) { | ||
// Update each match with its latest url data | ||
this.setRouteMatch(match.id, s => ({ | ||
...s, | ||
routeSearch: match.routeSearch, | ||
search: match.search, | ||
routeContext: match.routeContext, | ||
context: match.context, | ||
error: match.error, | ||
paramsError: match.paramsError, | ||
searchError: match.searchError, | ||
params: match.params | ||
})); | ||
} | ||
const handleError = (err, handler) => { | ||
firstBadMatchIndex = firstBadMatchIndex ?? index; | ||
handler = handler || route.options.onError; | ||
if (isRedirect(err)) { | ||
throw err; | ||
} | ||
try { | ||
handler?.(err); | ||
} catch (errorHandlerErr) { | ||
err = errorHandlerErr; | ||
if (isRedirect(errorHandlerErr)) { | ||
throw errorHandlerErr; | ||
} | ||
} | ||
this.setRouteMatch(match.id, s => ({ | ||
...s, | ||
error: err, | ||
status: 'error', | ||
updatedAt: Date.now() | ||
})); | ||
}; | ||
if (match.paramsError) { | ||
handleError(match.paramsError, route.options.onParseParamsError); | ||
} | ||
if (match.searchError) { | ||
handleError(match.searchError, route.options.onValidateSearchError); | ||
} | ||
try { | ||
await route.options.beforeLoad?.({ | ||
...match, | ||
preload: !!opts?.preload | ||
}); | ||
} catch (err) { | ||
handleError(err, route.options.onBeforeLoadError); | ||
} | ||
})); | ||
} catch (err) { | ||
if (!opts?.preload) { | ||
this.navigate(err); | ||
} | ||
})); | ||
throw err; | ||
} | ||
const validResolvedMatches = resolvedMatches.slice(0, firstBadMatchIndex); | ||
const matchPromises = []; | ||
validResolvedMatches.forEach((match, index) => { | ||
matchPromises.push((async () => { | ||
const parentMatchPromise = matchPromises[index - 1]; | ||
const route = this.getRoute(match.routeId); | ||
if (match.isFetching || match.status === 'success' && !this.getIsInvalid({ | ||
matchId: match.id, | ||
preload: opts?.preload | ||
})) { | ||
return this.getRouteMatch(match.id)?.loadPromise; | ||
} | ||
const fetchedAt = Date.now(); | ||
const checkLatest = () => { | ||
const latest = this.getRouteMatch(match.id); | ||
return latest && latest.fetchedAt !== fetchedAt ? latest.loadPromise : undefined; | ||
}; | ||
const loadPromise = (async () => { | ||
let latestPromise; | ||
const componentsPromise = Promise.all(componentTypes.map(async type => { | ||
const component = route.options[type]; | ||
if (component?.preload) { | ||
await component.preload(); | ||
} | ||
})); | ||
const loaderPromise = route.options.loader?.({ | ||
...match, | ||
preload: !!opts?.preload, | ||
parentMatchPromise | ||
}); | ||
const handleError = err => { | ||
if (isRedirect(err)) { | ||
if (!opts?.preload) { | ||
this.navigate(err); | ||
} | ||
return true; | ||
} | ||
return false; | ||
}; | ||
try { | ||
const [_, loader] = await Promise.all([componentsPromise, loaderPromise]); | ||
if (latestPromise = checkLatest()) return await latestPromise; | ||
this.setRouteMatchData(match.id, () => loader, opts); | ||
} catch (err) { | ||
if (latestPromise = checkLatest()) return await latestPromise; | ||
if (handleError(err)) { | ||
return; | ||
} | ||
const errorHandler = route.options.onLoadError ?? route.options.onError; | ||
let caughtError = err; | ||
try { | ||
errorHandler?.(err); | ||
} catch (errorHandlerErr) { | ||
caughtError = errorHandlerErr; | ||
if (handleError(errorHandlerErr)) { | ||
return; | ||
} | ||
} | ||
this.setRouteMatch(match.id, s => ({ | ||
...s, | ||
error: caughtError, | ||
status: 'error', | ||
isFetching: false, | ||
updatedAt: Date.now() | ||
})); | ||
} | ||
})(); | ||
this.setRouteMatch(match.id, s => ({ | ||
...s, | ||
status: s.status !== 'success' ? 'pending' : s.status, | ||
isFetching: true, | ||
loadPromise, | ||
fetchedAt, | ||
invalid: false | ||
})); | ||
await loadPromise; | ||
})()); | ||
}); | ||
await Promise.all(matchPromises); | ||
}; | ||
reload = () => { | ||
this.navigate({ | ||
return this.navigate({ | ||
fromCurrent: true, | ||
@@ -452,3 +592,3 @@ replace: true, | ||
from, | ||
to = '.', | ||
to = '', | ||
search, | ||
@@ -487,15 +627,20 @@ hash, | ||
const next = this.buildNext(location); | ||
if (opts?.pending) { | ||
if (!this.store.state.pendingLocation) { | ||
return false; | ||
} | ||
return path.matchPathname(this.basepath, this.store.state.pendingLocation.pathname, { | ||
...opts, | ||
to: next.pathname | ||
}); | ||
if (opts?.pending && this.state.status !== 'pending') { | ||
return false; | ||
} | ||
return path.matchPathname(this.basepath, this.store.state.currentLocation.pathname, { | ||
const baseLocation = opts?.pending ? this.state.location : this.state.resolvedLocation; | ||
if (!baseLocation) { | ||
return false; | ||
} | ||
const match = path.matchPathname(this.basepath, baseLocation.pathname, { | ||
...opts, | ||
to: next.pathname | ||
}); | ||
if (!match) { | ||
return false; | ||
} | ||
if (opts?.includeSearch ?? true) { | ||
return utils.partialDeepEqual(baseLocation.search, next.search) ? match : false; | ||
} | ||
return match; | ||
}; | ||
@@ -512,4 +657,2 @@ buildLink = ({ | ||
preload, | ||
preloadMaxAge: userPreloadMaxAge, | ||
preloadGcMaxAge: userPreloadGcMaxAge, | ||
preloadDelay: userPreloadDelay, | ||
@@ -544,13 +687,12 @@ disabled | ||
// Compare path/hash for matches | ||
const pathIsEqual = this.store.state.currentLocation.pathname === next.pathname; | ||
const currentPathSplit = this.store.state.currentLocation.pathname.split('/'); | ||
const currentPathSplit = this.state.location.pathname.split('/'); | ||
const nextPathSplit = next.pathname.split('/'); | ||
const pathIsFuzzyEqual = nextPathSplit.every((d, i) => d === currentPathSplit[i]); | ||
const hashIsEqual = this.store.state.currentLocation.hash === next.hash; | ||
// Combine the matches based on user options | ||
const pathTest = activeOptions?.exact ? pathIsEqual : pathIsFuzzyEqual; | ||
const hashTest = activeOptions?.includeHash ? hashIsEqual : true; | ||
const pathTest = activeOptions?.exact ? this.state.location.pathname === next.pathname : pathIsFuzzyEqual; | ||
const hashTest = activeOptions?.includeHash ? this.state.location.hash === next.hash : true; | ||
const searchTest = activeOptions?.includeSearch ?? true ? utils.partialDeepEqual(this.state.location.search, next.search) : true; | ||
// The final "active" test | ||
const isActive = pathTest && hashTest; | ||
const isActive = pathTest && hashTest && searchTest; | ||
@@ -561,5 +703,2 @@ // The click handler | ||
e.preventDefault(); | ||
if (pathIsEqual && !search && !hash) { | ||
this.invalidateRoute(nextOpts); | ||
} | ||
@@ -574,6 +713,3 @@ // All is well? Navigate! | ||
if (preload) { | ||
this.preloadRoute(nextOpts, { | ||
maxAge: userPreloadMaxAge, | ||
gcMaxAge: userPreloadGcMaxAge | ||
}).catch(err => { | ||
this.preloadRoute(nextOpts).catch(err => { | ||
console.warn(err); | ||
@@ -584,2 +720,8 @@ console.warn('Error preloading route! ☝️'); | ||
}; | ||
const handleTouchStart = e => { | ||
this.preloadRoute(nextOpts).catch(err => { | ||
console.warn(err); | ||
console.warn('Error preloading route! ☝️'); | ||
}); | ||
}; | ||
const handleEnter = e => { | ||
@@ -593,6 +735,3 @@ const target = e.target || {}; | ||
target.preloadTimeout = null; | ||
this.preloadRoute(nextOpts, { | ||
maxAge: userPreloadMaxAge, | ||
gcMaxAge: userPreloadGcMaxAge | ||
}).catch(err => { | ||
this.preloadRoute(nextOpts).catch(err => { | ||
console.warn(err); | ||
@@ -618,2 +757,3 @@ console.warn('Error preloading route! ☝️'); | ||
handleLeave, | ||
handleTouchStart, | ||
isActive, | ||
@@ -625,93 +765,144 @@ disabled | ||
return { | ||
state: { | ||
...utils.pick(this.store.state, ['latestLocation', 'currentLocation', 'status', 'lastUpdated']), | ||
currentMatches: this.store.state.currentMatches.map(match => ({ | ||
id: match.id, | ||
state: { | ||
...utils.pick(match.store.state, ['status', 'routeLoaderData', 'invalidAt', 'invalid']) | ||
} | ||
})) | ||
}, | ||
context: this.options.context | ||
state: utils.pick(this.state, ['location', 'status', 'lastUpdated']) | ||
}; | ||
}; | ||
hydrate = dehydratedRouter => { | ||
this.store.setState(s => { | ||
// Update the context TODO: make this part of state? | ||
this.options.context = dehydratedRouter.context; | ||
// Match the routes | ||
const currentMatches = this.matchRoutes(dehydratedRouter.state.latestLocation.pathname, { | ||
strictParseParams: true | ||
}); | ||
currentMatches.forEach((match, index) => { | ||
const dehydratedMatch = dehydratedRouter.state.currentMatches[index]; | ||
invariant__default["default"](dehydratedMatch && dehydratedMatch.id === match.id, 'Oh no! There was a hydration mismatch when attempting to rethis.store the state of the router! 😬'); | ||
match.store.setState(s => { | ||
Object.assign(s, dehydratedMatch.state); | ||
}); | ||
match.setLoaderData(dehydratedMatch.state.routeLoaderData); | ||
}); | ||
currentMatches.forEach(match => match.__validate()); | ||
Object.assign(s, { | ||
...dehydratedRouter.state, | ||
currentMatches | ||
}); | ||
hydrate = async __do_not_use_server_ctx => { | ||
let _ctx = __do_not_use_server_ctx; | ||
// Client hydrates from window | ||
if (typeof document !== 'undefined') { | ||
_ctx = window.__TSR_DEHYDRATED__; | ||
} | ||
invariant__default["default"](_ctx, 'Expected to find a __TSR_DEHYDRATED__ property on window... but we did not. Did you forget to render <DehydrateRouter /> in your app?'); | ||
const ctx = _ctx; | ||
this.dehydratedData = ctx.payload; | ||
this.options.hydrate?.(ctx.payload); | ||
this.__store.setState(s => { | ||
return { | ||
...s, | ||
...ctx.router.state, | ||
resolvedLocation: ctx.router.state.location | ||
}; | ||
}); | ||
await this.load(); | ||
return; | ||
}; | ||
getLoader = opts => { | ||
const id = opts.from || '/'; | ||
const route = this.getRoute(id); | ||
if (!route) return undefined; | ||
let loader = this.store.state.loaders[id] || (() => { | ||
this.store.setState(s => { | ||
s.loaders[id] = { | ||
pending: [], | ||
fetch: async loaderContext => { | ||
if (!route) { | ||
return; | ||
} | ||
const loaderState = { | ||
loadedAt: Date.now(), | ||
loaderContext | ||
}; | ||
this.store.setState(s => { | ||
s.loaders[id].current = loaderState; | ||
s.loaders[id].latest = loaderState; | ||
s.loaders[id].pending.push(loaderState); | ||
}); | ||
try { | ||
return await route.options.loader?.(loaderContext); | ||
} finally { | ||
this.store.setState(s => { | ||
s.loaders[id].pending = s.loaders[id].pending.filter(d => d !== loaderState); | ||
}); | ||
} | ||
} | ||
}; | ||
injectedHtml = []; | ||
injectHtml = async html => { | ||
this.injectedHtml.push(html); | ||
}; | ||
dehydrateData = (key, getData) => { | ||
if (typeof document === 'undefined') { | ||
const strKey = typeof key === 'string' ? key : JSON.stringify(key); | ||
this.injectHtml(async () => { | ||
const id = `__TSR_DEHYDRATED__${strKey}`; | ||
const data = typeof getData === 'function' ? await getData() : getData; | ||
return `<script id='${id}' suppressHydrationWarning>window["__TSR_DEHYDRATED__${escapeJSON(strKey)}"] = ${JSON.stringify(data)} | ||
;(() => { | ||
var el = document.getElementById('${id}') | ||
el.parentElement.removeChild(el) | ||
})() | ||
</script>`; | ||
}); | ||
return this.store.state.loaders[id]; | ||
})(); | ||
return loader; | ||
return () => this.hydrateData(key); | ||
} | ||
return () => undefined; | ||
}; | ||
#buildRouteTree = rootRouteConfig => { | ||
const recurseRoutes = (routeConfigs, parent) => { | ||
return routeConfigs.map((routeConfig, i) => { | ||
const routeOptions = routeConfig.options; | ||
const route$1 = new route.Route(routeConfig, routeOptions, i, parent, this); | ||
const existingRoute = this.routesById[route$1.id]; | ||
if (existingRoute) { | ||
if (process.env.NODE_ENV !== 'production') { | ||
console.warn(`Duplicate routes found with id: ${String(route$1.id)}`, this.routesById, route$1); | ||
hydrateData = key => { | ||
if (typeof document !== 'undefined') { | ||
const strKey = typeof key === 'string' ? key : JSON.stringify(key); | ||
return window[`__TSR_DEHYDRATED__${strKey}`]; | ||
} | ||
return undefined; | ||
}; | ||
// resolveMatchPromise = (matchId: string, key: string, value: any) => { | ||
// this.state.matches | ||
// .find((d) => d.id === matchId) | ||
// ?.__promisesByKey[key]?.resolve(value) | ||
// } | ||
#buildRouteTree = routeTree => { | ||
this.routeTree = routeTree; | ||
this.routesById = {}; | ||
this.routesByPath = {}; | ||
this.flatRoutes = []; | ||
const recurseRoutes = routes => { | ||
routes.forEach((route, i) => { | ||
route.init({ | ||
originalIndex: i, | ||
router: this | ||
}); | ||
const existingRoute = this.routesById[route.id]; | ||
invariant__default["default"](!existingRoute, `Duplicate routes found with id: ${String(route.id)}`); | ||
this.routesById[route.id] = route; | ||
if (!route.isRoot && route.path) { | ||
const trimmedFullPath = path.trimPathRight(route.fullPath); | ||
if (!this.routesByPath[trimmedFullPath] || route.fullPath.endsWith('/')) { | ||
this.routesByPath[trimmedFullPath] = route; | ||
} | ||
throw new Error(); | ||
} | ||
this.routesById[route$1.id] = route$1; | ||
const children = routeConfig.children; | ||
route$1.childRoutes = children.length ? recurseRoutes(children, route$1) : undefined; | ||
return route$1; | ||
const children = route.children; | ||
if (children?.length) { | ||
recurseRoutes(children); | ||
} | ||
}); | ||
}; | ||
const routes = recurseRoutes([rootRouteConfig]); | ||
return routes[0]; | ||
recurseRoutes([routeTree]); | ||
this.flatRoutes = Object.values(this.routesByPath).map((d, i) => { | ||
const trimmed = path.trimPath(d.fullPath); | ||
const parsed = path.parsePathname(trimmed); | ||
while (parsed.length > 1 && parsed[0]?.value === '/') { | ||
parsed.shift(); | ||
} | ||
const score = parsed.map(d => { | ||
if (d.type === 'param') { | ||
return 0.5; | ||
} | ||
if (d.type === 'wildcard') { | ||
return 0.25; | ||
} | ||
return 1; | ||
}); | ||
return { | ||
child: d, | ||
trimmed, | ||
parsed, | ||
index: i, | ||
score | ||
}; | ||
}).sort((a, b) => { | ||
let isIndex = a.trimmed === '/' ? 1 : b.trimmed === '/' ? -1 : 0; | ||
if (isIndex !== 0) return isIndex; | ||
const length = Math.min(a.score.length, b.score.length); | ||
// Sort by length of score | ||
if (a.score.length !== b.score.length) { | ||
return b.score.length - a.score.length; | ||
} | ||
// Sort by min available score | ||
for (let i = 0; i < length; i++) { | ||
if (a.score[i] !== b.score[i]) { | ||
return b.score[i] - a.score[i]; | ||
} | ||
} | ||
// Sort by min available parsed value | ||
for (let i = 0; i < length; i++) { | ||
if (a.parsed[i].value !== b.parsed[i].value) { | ||
return a.parsed[i].value > b.parsed[i].value ? 1 : -1; | ||
} | ||
} | ||
// Sort by length of trimmed full path | ||
if (a.trimmed !== b.trimmed) { | ||
return a.trimmed > b.trimmed ? 1 : -1; | ||
} | ||
// Sort by original index | ||
return a.index - b.index; | ||
}).map((d, i) => { | ||
d.child.rank = i; | ||
return d.child; | ||
}); | ||
}; | ||
@@ -729,3 +920,3 @@ #parseLocation = previousLocation => { | ||
searchStr: search, | ||
search: interop.replaceEqualDeep(previousLocation?.search, parsedSearch), | ||
search: utils.replaceEqualDeep(previousLocation?.search, parsedSearch), | ||
hash: hash.split('#').reverse()[0] ?? '', | ||
@@ -737,12 +928,7 @@ href: `${pathname}${search}${hash}`, | ||
}; | ||
#onFocus = () => { | ||
this.load(); | ||
}; | ||
#buildLocation = (dest = {}) => { | ||
const fromPathname = dest.fromCurrent ? this.store.state.latestLocation.pathname : dest.from ?? this.store.state.latestLocation.pathname; | ||
let pathname = path.resolvePath(this.basepath ?? '/', fromPathname, `${dest.to ?? '.'}`); | ||
const fromMatches = this.matchRoutes(this.store.state.latestLocation.pathname, { | ||
strictParseParams: true | ||
}); | ||
const toMatches = this.matchRoutes(pathname); | ||
dest.fromCurrent = dest.fromCurrent ?? dest.to === ''; | ||
const fromPathname = dest.fromCurrent ? this.state.location.pathname : dest.from ?? this.state.location.pathname; | ||
let pathname = path.resolvePath(this.basepath ?? '/', fromPathname, `${dest.to ?? ''}`); | ||
const fromMatches = this.matchRoutes(this.state.location.pathname, this.state.location.search); | ||
const prevParams = { | ||
@@ -753,10 +939,15 @@ ...utils.last(fromMatches)?.params | ||
if (nextParams) { | ||
toMatches.map(d => d.route.options.stringifyParams).filter(Boolean).forEach(fn => { | ||
Object.assign({}, nextParams, fn(nextParams)); | ||
dest.__matches?.map(d => this.getRoute(d.routeId).options.stringifyParams).filter(Boolean).forEach(fn => { | ||
nextParams = { | ||
...nextParams, | ||
...fn(nextParams) | ||
}; | ||
}); | ||
} | ||
pathname = path.interpolatePath(pathname, nextParams ?? {}); | ||
const preSearchFilters = dest.__matches?.map(match => this.getRoute(match.routeId).options.preSearchFilters ?? []).flat().filter(Boolean) ?? []; | ||
const postSearchFilters = dest.__matches?.map(match => this.getRoute(match.routeId).options.postSearchFilters ?? []).flat().filter(Boolean) ?? []; | ||
// Pre filters first | ||
const preFilteredSearch = dest.__preSearchFilters?.length ? dest.__preSearchFilters?.reduce((prev, next) => next(prev), this.store.state.latestLocation.search) : this.store.state.latestLocation.search; | ||
const preFilteredSearch = preSearchFilters?.length ? preSearchFilters?.reduce((prev, next) => next(prev), this.state.location.search) : this.state.location.search; | ||
@@ -766,11 +957,12 @@ // Then the link/navigate function | ||
: dest.search ? utils.functionalUpdate(dest.search, preFilteredSearch) ?? {} // Updater | ||
: dest.__preSearchFilters?.length ? preFilteredSearch // Preserve resolvedFrom filters | ||
: preSearchFilters?.length ? preFilteredSearch // Preserve resolvedFrom filters | ||
: {}; | ||
// Then post filters | ||
const postFilteredSearch = dest.__postSearchFilters?.length ? dest.__postSearchFilters.reduce((prev, next) => next(prev), destSearch) : destSearch; | ||
const search = interop.replaceEqualDeep(this.store.state.latestLocation.search, postFilteredSearch); | ||
const postFilteredSearch = postSearchFilters?.length ? postSearchFilters.reduce((prev, next) => next(prev), destSearch) : destSearch; | ||
const search = utils.replaceEqualDeep(this.state.location.search, postFilteredSearch); | ||
const searchStr = this.options.stringifySearch(search); | ||
let hash = dest.hash === true ? this.store.state.latestLocation.hash : utils.functionalUpdate(dest.hash, this.store.state.latestLocation.hash); | ||
hash = hash ? `#${hash}` : ''; | ||
const hash = dest.hash === true ? this.state.location.hash : utils.functionalUpdate(dest.hash, this.state.location.hash); | ||
const hashStr = hash ? `#${hash}` : ''; | ||
const nextState = dest.state === true ? this.state.location.state : utils.functionalUpdate(dest.state, this.state.location.state); | ||
return { | ||
@@ -780,9 +972,9 @@ pathname, | ||
searchStr, | ||
state: this.store.state.latestLocation.state, | ||
state: nextState, | ||
hash, | ||
href: `${pathname}${searchStr}${hash}`, | ||
href: this.history.createHref(`${pathname}${searchStr}${hashStr}`), | ||
key: dest.key | ||
}; | ||
}; | ||
#commitLocation = location => { | ||
#commitLocation = async location => { | ||
const next = this.buildNext(location); | ||
@@ -795,3 +987,3 @@ const id = '' + Date.now() + Math.random(); | ||
} | ||
const isSameUrl = this.store.state.latestLocation.href === next.href; | ||
const isSameUrl = this.state.location.href === next.href; | ||
if (isSameUrl && !next.key) { | ||
@@ -805,13 +997,77 @@ nextAction = 'replace'; | ||
}); | ||
// this.load(this.#parseLocation(this.store.state.latestLocation)) | ||
return this.navigationPromise = new Promise(resolve => { | ||
const previousNavigationResolve = this.resolveNavigation; | ||
this.resolveNavigation = () => { | ||
previousNavigationResolve(); | ||
resolve(); | ||
}; | ||
}); | ||
return this.latestLoadPromise; | ||
}; | ||
getRouteMatch = id => { | ||
return this.state.matchesById[id]; | ||
}; | ||
setRouteMatch = (id, updater) => { | ||
this.__store.setState(prev => ({ | ||
...prev, | ||
matchesById: { | ||
...prev.matchesById, | ||
[id]: updater(prev.matchesById[id]) | ||
} | ||
})); | ||
}; | ||
setRouteMatchData = (id, updater, opts) => { | ||
const match = this.getRouteMatch(id); | ||
if (!match) return; | ||
const route = this.getRoute(match.routeId); | ||
const updatedAt = opts?.updatedAt ?? Date.now(); | ||
const preloadInvalidAt = updatedAt + (opts?.maxAge ?? route.options.preloadMaxAge ?? this.options.defaultPreloadMaxAge ?? 5000); | ||
const invalidAt = updatedAt + (opts?.maxAge ?? route.options.maxAge ?? this.options.defaultMaxAge ?? Infinity); | ||
this.setRouteMatch(id, s => ({ | ||
...s, | ||
error: undefined, | ||
status: 'success', | ||
isFetching: false, | ||
updatedAt: Date.now(), | ||
loaderData: utils.functionalUpdate(updater, s.loaderData), | ||
preloadInvalidAt, | ||
invalidAt | ||
})); | ||
if (this.state.matches.find(d => d.id === id)) ; | ||
}; | ||
invalidate = async opts => { | ||
if (opts?.matchId) { | ||
this.setRouteMatch(opts.matchId, s => ({ | ||
...s, | ||
invalid: true | ||
})); | ||
const matchIndex = this.state.matches.findIndex(d => d.id === opts.matchId); | ||
const childMatch = this.state.matches[matchIndex + 1]; | ||
if (childMatch) { | ||
return this.invalidate({ | ||
matchId: childMatch.id, | ||
reload: false | ||
}); | ||
} | ||
} else { | ||
this.__store.batch(() => { | ||
Object.values(this.state.matchesById).forEach(match => { | ||
this.setRouteMatch(match.id, s => ({ | ||
...s, | ||
invalid: true | ||
})); | ||
}); | ||
}); | ||
} | ||
if (opts?.reload ?? true) { | ||
return this.reload(); | ||
} | ||
}; | ||
getIsInvalid = opts => { | ||
if (!opts?.matchId) { | ||
return !!this.state.matches.find(d => this.getIsInvalid({ | ||
matchId: d.id, | ||
preload: opts?.preload | ||
})); | ||
} | ||
const match = this.getRouteMatch(opts?.matchId); | ||
if (!match) { | ||
return false; | ||
} | ||
const now = Date.now(); | ||
return match.invalid || (opts?.preload ? match.preloadInvalidAt : match.invalidAt) < now; | ||
}; | ||
} | ||
@@ -824,14 +1080,11 @@ | ||
status: 'idle', | ||
latestLocation: null, | ||
currentLocation: null, | ||
currentMatches: [], | ||
loaders: {}, | ||
lastUpdated: Date.now(), | ||
matchCache: {}, | ||
get isFetching() { | ||
return this.status === 'loading' || this.currentMatches.some(d => d.store.state.isFetching); | ||
}, | ||
get isPreloading() { | ||
return Object.values(this.matchCache).some(d => d.match.store.state.isFetching && !this.currentMatches.find(dd => dd.id === d.match.id)); | ||
} | ||
isFetching: false, | ||
resolvedLocation: null, | ||
location: null, | ||
matchesById: {}, | ||
matchIds: [], | ||
pendingMatchIds: [], | ||
matches: [], | ||
pendingMatches: [], | ||
lastUpdated: Date.now() | ||
}; | ||
@@ -842,13 +1095,34 @@ } | ||
} | ||
function linkMatches(matches) { | ||
matches.forEach((match, index) => { | ||
const parent = matches[index - 1]; | ||
if (parent) { | ||
match.__setParentMatch(parent); | ||
} | ||
}); | ||
function redirect(opts) { | ||
opts.isRedirect = true; | ||
return opts; | ||
} | ||
function isRedirect(obj) { | ||
return !!obj?.isRedirect; | ||
} | ||
class SearchParamError extends Error {} | ||
class PathParamError extends Error {} | ||
function escapeJSON(jsonString) { | ||
return jsonString.replace(/\\/g, '\\\\') // Escape backslashes | ||
.replace(/'/g, "\\'") // Escape single quotes | ||
.replace(/"/g, '\\"'); // Escape double quotes | ||
} | ||
// A function that takes an import() argument which is a function and returns a new function that will | ||
// proxy arguments from the caller to the imported function, retaining all type | ||
// information along the way | ||
function lazyFn(fn, key) { | ||
return async (...args) => { | ||
const imported = await fn(); | ||
return imported[key || 'default'](...args); | ||
}; | ||
} | ||
exports.PathParamError = PathParamError; | ||
exports.Router = Router; | ||
exports.defaultFetchServerDataFn = defaultFetchServerDataFn; | ||
exports.SearchParamError = SearchParamError; | ||
exports.componentTypes = componentTypes; | ||
exports.isRedirect = isRedirect; | ||
exports.lazyFn = lazyFn; | ||
exports.redirect = redirect; | ||
//# sourceMappingURL=router.js.map |
/** | ||
* router-core | ||
* @tanstack/router-core/src/index.ts | ||
* | ||
@@ -4,0 +4,0 @@ * Copyright (c) TanStack |
/** | ||
* router-core | ||
* @tanstack/router-core/src/index.ts | ||
* | ||
@@ -18,11 +18,2 @@ * Copyright (c) TanStack | ||
} | ||
function warning(cond, message) { | ||
if (cond) { | ||
if (typeof console !== 'undefined') console.warn(message); | ||
try { | ||
throw new Error(message); | ||
} catch {} | ||
} | ||
return true; | ||
} | ||
function isFunction(d) { | ||
@@ -44,6 +35,83 @@ return typeof d === 'function'; | ||
/** | ||
* This function returns `a` if `b` is deeply equal. | ||
* If not, it will replace any deeply equal children of `b` with those of `a`. | ||
* This can be used for structural sharing between immutable JSON values for example. | ||
* Do not use this with signals | ||
*/ | ||
function replaceEqualDeep(prev, _next) { | ||
if (prev === _next) { | ||
return prev; | ||
} | ||
const next = _next; | ||
const array = Array.isArray(prev) && Array.isArray(next); | ||
if (array || isPlainObject(prev) && isPlainObject(next)) { | ||
const prevSize = array ? prev.length : Object.keys(prev).length; | ||
const nextItems = array ? next : Object.keys(next); | ||
const nextSize = nextItems.length; | ||
const copy = array ? [] : {}; | ||
let equalItems = 0; | ||
for (let i = 0; i < nextSize; i++) { | ||
const key = array ? i : nextItems[i]; | ||
copy[key] = replaceEqualDeep(prev[key], next[key]); | ||
if (copy[key] === prev[key]) { | ||
equalItems++; | ||
} | ||
} | ||
return prevSize === nextSize && equalItems === prevSize ? prev : copy; | ||
} | ||
return next; | ||
} | ||
// Copied from: https://github.com/jonschlinkert/is-plain-object | ||
function isPlainObject(o) { | ||
if (!hasObjectPrototype(o)) { | ||
return false; | ||
} | ||
// If has modified constructor | ||
const ctor = o.constructor; | ||
if (typeof ctor === 'undefined') { | ||
return true; | ||
} | ||
// If has modified prototype | ||
const prot = ctor.prototype; | ||
if (!hasObjectPrototype(prot)) { | ||
return false; | ||
} | ||
// If constructor does not have an Object-specific method | ||
if (!prot.hasOwnProperty('isPrototypeOf')) { | ||
return false; | ||
} | ||
// Most likely a plain Object | ||
return true; | ||
} | ||
function hasObjectPrototype(o) { | ||
return Object.prototype.toString.call(o) === '[object Object]'; | ||
} | ||
function partialDeepEqual(a, b) { | ||
if (a === b) { | ||
return true; | ||
} | ||
if (typeof a !== typeof b) { | ||
return false; | ||
} | ||
if (isPlainObject(a) && isPlainObject(b)) { | ||
return !Object.keys(b).some(key => !partialDeepEqual(a[key], b[key])); | ||
} | ||
if (Array.isArray(a) && Array.isArray(b)) { | ||
return a.length === b.length && a.every((item, index) => partialDeepEqual(item, b[index])); | ||
} | ||
return false; | ||
} | ||
exports.functionalUpdate = functionalUpdate; | ||
exports.isPlainObject = isPlainObject; | ||
exports.last = last; | ||
exports.partialDeepEqual = partialDeepEqual; | ||
exports.pick = pick; | ||
exports.warning = warning; | ||
exports.replaceEqualDeep = replaceEqualDeep; | ||
//# sourceMappingURL=utils.js.map |
/** | ||
* router-core | ||
* @tanstack/router-core/src/index.ts | ||
* | ||
@@ -13,3 +13,4 @@ * Copyright (c) TanStack | ||
export { default as invariant } from 'tiny-invariant'; | ||
import { setAutoFreeze, produce } from 'immer'; | ||
export { default as warning } from 'tiny-warning'; | ||
import { Store } from '@tanstack/react-store'; | ||
@@ -20,9 +21,42 @@ // While the public API was clearly inspired by the "history" npm package, | ||
const pushStateEvent = 'pushstate'; | ||
const popStateEvent = 'popstate'; | ||
const beforeUnloadEvent = 'beforeunload'; | ||
const beforeUnloadListener = event => { | ||
event.preventDefault(); | ||
// @ts-ignore | ||
return event.returnValue = ''; | ||
}; | ||
const stopBlocking = () => { | ||
removeEventListener(beforeUnloadEvent, beforeUnloadListener, { | ||
capture: true | ||
}); | ||
}; | ||
function createHistory(opts) { | ||
let currentLocation = opts.getLocation(); | ||
let location = opts.getLocation(); | ||
let unsub = () => {}; | ||
let listeners = new Set(); | ||
let blockers = []; | ||
let queue = []; | ||
const tryFlush = () => { | ||
if (blockers.length) { | ||
blockers[0]?.(tryFlush, () => { | ||
blockers = []; | ||
stopBlocking(); | ||
}); | ||
return; | ||
} | ||
while (queue.length) { | ||
queue.shift()?.(); | ||
} | ||
if (!opts.listener) { | ||
onUpdate(); | ||
} | ||
}; | ||
const queueTask = task => { | ||
queue.push(task); | ||
tryFlush(); | ||
}; | ||
const onUpdate = () => { | ||
currentLocation = opts.getLocation(); | ||
location = opts.getLocation(); | ||
listeners.forEach(listener => listener()); | ||
@@ -32,7 +66,7 @@ }; | ||
get location() { | ||
return currentLocation; | ||
return location; | ||
}, | ||
listen: cb => { | ||
if (listeners.size === 0) { | ||
unsub = opts.listener(onUpdate); | ||
unsub = typeof opts.listener === 'function' ? opts.listener(onUpdate) : () => {}; | ||
} | ||
@@ -48,20 +82,40 @@ listeners.add(cb); | ||
push: (path, state) => { | ||
opts.pushState(path, state); | ||
onUpdate(); | ||
queueTask(() => { | ||
opts.pushState(path, state); | ||
}); | ||
}, | ||
replace: (path, state) => { | ||
opts.replaceState(path, state); | ||
onUpdate(); | ||
queueTask(() => { | ||
opts.replaceState(path, state); | ||
}); | ||
}, | ||
go: index => { | ||
opts.go(index); | ||
onUpdate(); | ||
queueTask(() => { | ||
opts.go(index); | ||
}); | ||
}, | ||
back: () => { | ||
opts.back(); | ||
onUpdate(); | ||
queueTask(() => { | ||
opts.back(); | ||
}); | ||
}, | ||
forward: () => { | ||
opts.forward(); | ||
onUpdate(); | ||
queueTask(() => { | ||
opts.forward(); | ||
}); | ||
}, | ||
createHref: str => opts.createHref(str), | ||
block: cb => { | ||
blockers.push(cb); | ||
if (blockers.length === 1) { | ||
addEventListener(beforeUnloadEvent, beforeUnloadListener, { | ||
capture: true | ||
}); | ||
} | ||
return () => { | ||
blockers = blockers.filter(b => b !== cb); | ||
if (!blockers.length) { | ||
stopBlocking(); | ||
} | ||
}; | ||
} | ||
@@ -71,3 +125,3 @@ }; | ||
function createBrowserHistory(opts) { | ||
const getHref = opts?.getHref ?? (() => `${window.location.pathname}${window.location.hash}${window.location.search}`); | ||
const getHref = opts?.getHref ?? (() => `${window.location.pathname}${window.location.search}${window.location.hash}`); | ||
const createHref = opts?.createHref ?? (path => path); | ||
@@ -78,4 +132,20 @@ const getLocation = () => parseLocation(getHref(), history.state); | ||
listener: onUpdate => { | ||
window.addEventListener(pushStateEvent, onUpdate); | ||
window.addEventListener(popStateEvent, onUpdate); | ||
var pushState = window.history.pushState; | ||
window.history.pushState = function () { | ||
let res = pushState.apply(history, arguments); | ||
onUpdate(); | ||
return res; | ||
}; | ||
var replaceState = window.history.replaceState; | ||
window.history.replaceState = function () { | ||
let res = replaceState.apply(history, arguments); | ||
onUpdate(); | ||
return res; | ||
}; | ||
return () => { | ||
window.history.pushState = pushState; | ||
window.history.replaceState = replaceState; | ||
window.removeEventListener(pushStateEvent, onUpdate); | ||
window.removeEventListener(popStateEvent, onUpdate); | ||
@@ -98,3 +168,4 @@ }; | ||
forward: () => window.history.forward(), | ||
go: n => window.history.go(n) | ||
go: n => window.history.go(n), | ||
createHref: path => createHref(path) | ||
}); | ||
@@ -117,5 +188,3 @@ } | ||
getLocation, | ||
listener: () => { | ||
return () => {}; | ||
}, | ||
listener: false, | ||
pushState: (path, state) => { | ||
@@ -142,3 +211,4 @@ currentState = { | ||
}, | ||
go: n => window.history.go(n) | ||
go: n => window.history.go(n), | ||
createHref: path => path | ||
}); | ||
@@ -152,4 +222,4 @@ } | ||
pathname: href.substring(0, hashIndex > 0 ? searchIndex > 0 ? Math.min(hashIndex, searchIndex) : hashIndex : searchIndex > 0 ? searchIndex : href.length), | ||
hash: hashIndex > -1 ? href.substring(hashIndex, searchIndex) : '', | ||
search: searchIndex > -1 ? href.substring(searchIndex) : '', | ||
hash: hashIndex > -1 ? href.substring(hashIndex) : '', | ||
search: searchIndex > -1 ? href.slice(searchIndex, hashIndex === -1 ? undefined : hashIndex) : '', | ||
state | ||
@@ -167,11 +237,2 @@ }; | ||
} | ||
function warning(cond, message) { | ||
if (cond) { | ||
if (typeof console !== 'undefined') console.warn(message); | ||
try { | ||
throw new Error(message); | ||
} catch {} | ||
} | ||
return true; | ||
} | ||
function isFunction(d) { | ||
@@ -193,2 +254,77 @@ return typeof d === 'function'; | ||
/** | ||
* This function returns `a` if `b` is deeply equal. | ||
* If not, it will replace any deeply equal children of `b` with those of `a`. | ||
* This can be used for structural sharing between immutable JSON values for example. | ||
* Do not use this with signals | ||
*/ | ||
function replaceEqualDeep(prev, _next) { | ||
if (prev === _next) { | ||
return prev; | ||
} | ||
const next = _next; | ||
const array = Array.isArray(prev) && Array.isArray(next); | ||
if (array || isPlainObject(prev) && isPlainObject(next)) { | ||
const prevSize = array ? prev.length : Object.keys(prev).length; | ||
const nextItems = array ? next : Object.keys(next); | ||
const nextSize = nextItems.length; | ||
const copy = array ? [] : {}; | ||
let equalItems = 0; | ||
for (let i = 0; i < nextSize; i++) { | ||
const key = array ? i : nextItems[i]; | ||
copy[key] = replaceEqualDeep(prev[key], next[key]); | ||
if (copy[key] === prev[key]) { | ||
equalItems++; | ||
} | ||
} | ||
return prevSize === nextSize && equalItems === prevSize ? prev : copy; | ||
} | ||
return next; | ||
} | ||
// Copied from: https://github.com/jonschlinkert/is-plain-object | ||
function isPlainObject(o) { | ||
if (!hasObjectPrototype(o)) { | ||
return false; | ||
} | ||
// If has modified constructor | ||
const ctor = o.constructor; | ||
if (typeof ctor === 'undefined') { | ||
return true; | ||
} | ||
// If has modified prototype | ||
const prot = ctor.prototype; | ||
if (!hasObjectPrototype(prot)) { | ||
return false; | ||
} | ||
// If constructor does not have an Object-specific method | ||
if (!prot.hasOwnProperty('isPrototypeOf')) { | ||
return false; | ||
} | ||
// Most likely a plain Object | ||
return true; | ||
} | ||
function hasObjectPrototype(o) { | ||
return Object.prototype.toString.call(o) === '[object Object]'; | ||
} | ||
function partialDeepEqual(a, b) { | ||
if (a === b) { | ||
return true; | ||
} | ||
if (typeof a !== typeof b) { | ||
return false; | ||
} | ||
if (isPlainObject(a) && isPlainObject(b)) { | ||
return !Object.keys(b).some(key => !partialDeepEqual(a[key], b[key])); | ||
} | ||
if (Array.isArray(a) && Array.isArray(b)) { | ||
return a.length === b.length && a.every((item, index) => partialDeepEqual(item, b[index])); | ||
} | ||
return false; | ||
} | ||
function joinPaths(paths) { | ||
@@ -259,3 +395,3 @@ return cleanPath(paths.filter(Boolean).join('/')); | ||
segments.push(...split.map(part => { | ||
if (part.startsWith('*')) { | ||
if (part === '$' || part === '*') { | ||
return { | ||
@@ -286,7 +422,9 @@ type: 'wildcard', | ||
} | ||
function interpolatePath(path, params, leaveWildcard) { | ||
function interpolatePath(path, params, leaveWildcards = false) { | ||
const interpolatedPathSegments = parsePathname(path); | ||
return joinPaths(interpolatedPathSegments.map(segment => { | ||
if (segment.value === '*' && !leaveWildcard) { | ||
return ''; | ||
if (segment.type === 'wildcard') { | ||
const value = params[segment.value]; | ||
if (leaveWildcards) return `${segment.value}${value ?? ''}`; | ||
return value; | ||
} | ||
@@ -301,3 +439,3 @@ if (segment.type === 'param') { | ||
const pathParams = matchByPath(basepath, currentPathname, matchLocation); | ||
// const searchMatched = matchBySearch(currentLocation.search, matchLocation) | ||
// const searchMatched = matchBySearch(location.search, matchLocation) | ||
@@ -310,9 +448,21 @@ if (matchLocation.to && !pathParams) { | ||
function matchByPath(basepath, from, matchLocation) { | ||
if (!from.startsWith(basepath)) { | ||
return undefined; | ||
} | ||
// Remove the base path from the pathname | ||
from = basepath != '/' ? from.substring(basepath.length) : from; | ||
// Default to to $ (wildcard) | ||
const to = `${matchLocation.to ?? '$'}`; | ||
// Parse the from and to | ||
const baseSegments = parsePathname(from); | ||
const to = `${matchLocation.to ?? '*'}`; | ||
const routeSegments = parsePathname(to); | ||
if (!from.startsWith('/')) { | ||
baseSegments.unshift({ | ||
type: 'pathname', | ||
value: '/' | ||
}); | ||
} | ||
if (!to.startsWith('/')) { | ||
routeSegments.unshift({ | ||
type: 'pathname', | ||
value: '/' | ||
}); | ||
} | ||
const params = {}; | ||
@@ -323,4 +473,4 @@ let isMatch = (() => { | ||
const routeSegment = routeSegments[i]; | ||
const isLastRouteSegment = i === routeSegments.length - 1; | ||
const isLastBaseSegment = i === baseSegments.length - 1; | ||
const isLastBaseSegment = i >= baseSegments.length - 1; | ||
const isLastRouteSegment = i >= routeSegments.length - 1; | ||
if (routeSegment) { | ||
@@ -360,3 +510,3 @@ if (routeSegment.type === 'wildcard') { | ||
} | ||
if (isLastRouteSegment && !isLastBaseSegment) { | ||
if (!isLastBaseSegment && isLastRouteSegment) { | ||
return !!matchLocation.fuzzy; | ||
@@ -399,4 +549,3 @@ } | ||
if (str === 'true') return true; | ||
if (str.charAt(0) === '0') return str; | ||
return +str * 0 === 0 ? +str : str; | ||
return +str * 0 === 0 && +str + '' === str ? +str : str; | ||
} | ||
@@ -420,470 +569,123 @@ function decode(str) { | ||
class Route { | ||
constructor(routeConfig, options, originalIndex, parent, router) { | ||
Object.assign(this, { | ||
...routeConfig, | ||
originalIndex, | ||
options, | ||
getRouter: () => router, | ||
childRoutes: undefined, | ||
getParentRoute: () => parent | ||
}); | ||
router.options.createRoute?.({ | ||
router, | ||
route: this | ||
}); | ||
} | ||
} | ||
const rootRouteId = '__root__'; | ||
const createRouteConfig = (options = {}, children = [], isRoot = true, parentId, parentPath) => { | ||
if (isRoot) { | ||
options.path = rootRouteId; | ||
} | ||
// Strip the root from parentIds | ||
if (parentId === rootRouteId) { | ||
parentId = ''; | ||
} | ||
let path = isRoot ? rootRouteId : options.path; | ||
// | ParseParamsObj<TPath, TParams> | ||
// If the path is anything other than an index path, trim it up | ||
if (path && path !== '/') { | ||
path = trimPath(path); | ||
} | ||
const routeId = path || options.id; | ||
let id = joinPaths([parentId, routeId]); | ||
if (path === rootRouteId) { | ||
path = '/'; | ||
} | ||
if (id !== rootRouteId) { | ||
id = joinPaths(['/', id]); | ||
} | ||
const fullPath = id === rootRouteId ? '/' : trimPathRight(joinPaths([parentPath, path])); | ||
return { | ||
id: id, | ||
routeId: routeId, | ||
path: path, | ||
fullPath: fullPath, | ||
options: options, | ||
children, | ||
addChildren: children => createRouteConfig(options, children, false, parentId, parentPath), | ||
createRoute: childOptions => createRouteConfig(childOptions, undefined, false, id, fullPath), | ||
generate: () => { | ||
invariant(false, `routeConfig.generate() is used by TanStack Router's file-based routing code generation and should not actually be called during runtime. `); | ||
} | ||
}; | ||
}; | ||
// The parse type here allows a zod schema to be passed directly to the validator | ||
setAutoFreeze(false); | ||
let queue = []; | ||
let batching = false; | ||
function flush() { | ||
if (batching) return; | ||
queue.forEach(cb => cb()); | ||
queue = []; | ||
} | ||
function createStore(initialState, debug) { | ||
const listeners = new Set(); | ||
const store = { | ||
state: initialState, | ||
subscribe: listener => { | ||
listeners.add(listener); | ||
return () => listeners.delete(listener); | ||
}, | ||
setState: updater => { | ||
const previous = store.state; | ||
store.state = produce(d => { | ||
updater(d); | ||
})(previous); | ||
if (debug) console.log(store.state); | ||
queue.push(() => listeners.forEach(listener => listener(store.state, previous))); | ||
flush(); | ||
} | ||
}; | ||
return store; | ||
} | ||
function batch(cb) { | ||
batching = true; | ||
cb(); | ||
batching = false; | ||
flush(); | ||
} | ||
class Route { | ||
// Set up in this.init() | ||
// /** | ||
// * This function converts a store to an immutable value, which is | ||
// * more complex than you think. On first read, (when prev is undefined) | ||
// * every value must be recursively touched so tracking is "deep". | ||
// * Every object/array structure must also be cloned to | ||
// * have a new reference, otherwise it will get mutated by subsequent | ||
// * store updates. | ||
// * | ||
// * In the case that prev is supplied, we have to do deep comparisons | ||
// * between prev and next objects/array references and if they are deeply | ||
// * equal, we can return the prev version for referential equality. | ||
// */ | ||
// export function storeToImmutable<T>(prev: any, next: T): T { | ||
// const cache = new Map() | ||
// customId!: TCustomId | ||
// // Visit all nodes | ||
// // clone all next structures | ||
// // from bottom up, if prev === next, return prev | ||
// Optional | ||
// function recurse(prev: any, next: any) { | ||
// if (cache.has(next)) { | ||
// return cache.get(next) | ||
// } | ||
// const prevIsArray = Array.isArray(prev) | ||
// const nextIsArray = Array.isArray(next) | ||
// const prevIsObj = isPlainObject(prev) | ||
// const nextIsObj = isPlainObject(next) | ||
// const nextIsComplex = nextIsArray || nextIsObj | ||
// const isArray = prevIsArray && nextIsArray | ||
// const isObj = prevIsObj && nextIsObj | ||
// const isSameStructure = isArray || isObj | ||
// if (nextIsComplex) { | ||
// const prevSize = isArray | ||
// ? prev.length | ||
// : isObj | ||
// ? Object.keys(prev).length | ||
// : -1 | ||
// const nextKeys = isArray ? next : Object.keys(next) | ||
// const nextSize = nextKeys.length | ||
// let changed = false | ||
// const copy: any = nextIsArray ? [] : {} | ||
// for (let i = 0; i < nextSize; i++) { | ||
// const key = isArray ? i : nextKeys[i] | ||
// const prevValue = isSameStructure ? prev[key] : undefined | ||
// const nextValue = next[key] | ||
// // Recurse the new value | ||
// try { | ||
// console.count(key) | ||
// copy[key] = recurse(prevValue, nextValue) | ||
// } catch {} | ||
// // If the new value has changed reference, | ||
// // mark the obj/array as changed | ||
// if (!changed && copy[key] !== prevValue) { | ||
// changed = true | ||
// } | ||
// } | ||
// // No items have changed! | ||
// // If something has changed, return a clone of the next obj/array | ||
// if (changed || prevSize !== nextSize) { | ||
// cache.set(next, copy) | ||
// return copy | ||
// } | ||
// // If they are exactly the same, return the prev obj/array | ||
// cache.set(next, prev) | ||
// return prev | ||
// } | ||
// cache.set(next, next) | ||
// return next | ||
// } | ||
// return recurse(prev, next) | ||
// } | ||
/** | ||
* This function returns `a` if `b` is deeply equal. | ||
* If not, it will replace any deeply equal children of `b` with those of `a`. | ||
* This can be used for structural sharing between immutable JSON values for example. | ||
* Do not use this with signals | ||
*/ | ||
function replaceEqualDeep(prev, _next) { | ||
if (prev === _next) { | ||
return prev; | ||
constructor(options) { | ||
this.options = options || {}; | ||
this.isRoot = !options?.getParentRoute; | ||
Route.__onInit(this); | ||
} | ||
const next = _next; | ||
const array = Array.isArray(prev) && Array.isArray(next); | ||
if (array || isPlainObject(prev) && isPlainObject(next)) { | ||
const prevSize = array ? prev.length : Object.keys(prev).length; | ||
const nextItems = array ? next : Object.keys(next); | ||
const nextSize = nextItems.length; | ||
const copy = array ? [] : {}; | ||
let equalItems = 0; | ||
for (let i = 0; i < nextSize; i++) { | ||
const key = array ? i : nextItems[i]; | ||
copy[key] = replaceEqualDeep(prev[key], next[key]); | ||
if (copy[key] === prev[key]) { | ||
equalItems++; | ||
} | ||
init = opts => { | ||
this.originalIndex = opts.originalIndex; | ||
this.router = opts.router; | ||
const options = this.options; | ||
const isRoot = !options?.path && !options?.id; | ||
this.parentRoute = this.options?.getParentRoute?.(); | ||
if (isRoot) { | ||
this.path = rootRouteId; | ||
} else { | ||
invariant(this.parentRoute, `Child Route instances must pass a 'getParentRoute: () => ParentRoute' option that returns a Route instance.`); | ||
} | ||
return prevSize === nextSize && equalItems === prevSize ? prev : copy; | ||
} | ||
return next; | ||
} | ||
let path = isRoot ? rootRouteId : options.path; | ||
// Copied from: https://github.com/jonschlinkert/is-plain-object | ||
function isPlainObject(o) { | ||
if (!hasObjectPrototype(o)) { | ||
return false; | ||
} | ||
// If the path is anything other than an index path, trim it up | ||
if (path && path !== '/') { | ||
path = trimPath(path); | ||
} | ||
const customId = options?.id || path; | ||
// If has modified constructor | ||
const ctor = o.constructor; | ||
if (typeof ctor === 'undefined') { | ||
return true; | ||
} | ||
// If has modified prototype | ||
const prot = ctor.prototype; | ||
if (!hasObjectPrototype(prot)) { | ||
return false; | ||
} | ||
// If constructor does not have an Object-specific method | ||
if (!prot.hasOwnProperty('isPrototypeOf')) { | ||
return false; | ||
} | ||
// Most likely a plain Object | ||
return true; | ||
} | ||
function hasObjectPrototype(o) { | ||
return Object.prototype.toString.call(o) === '[object Object]'; | ||
} | ||
function trackDeep(obj) { | ||
const seen = new Set(); | ||
JSON.stringify(obj, (_, value) => { | ||
if (typeof value === 'function') { | ||
return undefined; | ||
// Strip the parentId prefix from the first level of children | ||
let id = isRoot ? rootRouteId : joinPaths([this.parentRoute.id === rootRouteId ? '' : this.parentRoute.id, customId]); | ||
if (path === rootRouteId) { | ||
path = '/'; | ||
} | ||
if (typeof value === 'object' && value !== null) { | ||
if (seen.has(value)) return; | ||
seen.add(value); | ||
if (id !== rootRouteId) { | ||
id = joinPaths(['/', id]); | ||
} | ||
return value; | ||
}); | ||
return obj; | ||
const fullPath = id === rootRouteId ? '/' : joinPaths([this.parentRoute.fullPath, path]); | ||
this.path = path; | ||
this.id = id; | ||
// this.customId = customId as TCustomId | ||
this.fullPath = fullPath; | ||
this.to = fullPath; | ||
}; | ||
addChildren = children => { | ||
this.children = children; | ||
return this; | ||
}; | ||
update = options => { | ||
Object.assign(this.options, options); | ||
return this; | ||
}; | ||
static __onInit = route => { | ||
// This is a dummy static method that should get | ||
// replaced by a framework specific implementation if necessary | ||
}; | ||
} | ||
const componentTypes = ['component', 'errorComponent', 'pendingComponent']; | ||
class RouteMatch { | ||
abortController = new AbortController(); | ||
#latestId = ''; | ||
#resolve = () => {}; | ||
onLoaderDataListeners = new Set(); | ||
constructor(router, route, opts) { | ||
Object.assign(this, { | ||
route, | ||
router, | ||
id: opts.id, | ||
pathname: opts.pathname, | ||
params: opts.params, | ||
store: createStore({ | ||
routeSearch: {}, | ||
search: {}, | ||
status: 'idle', | ||
routeLoaderData: {}, | ||
loaderData: {}, | ||
isFetching: false, | ||
invalid: false, | ||
invalidAt: Infinity | ||
}) | ||
}); | ||
if (!this.__hasLoaders()) { | ||
this.store.setState(s => s.status = 'success'); | ||
} | ||
class RouterContext { | ||
constructor() {} | ||
createRootRoute = options => { | ||
return new RootRoute(options); | ||
}; | ||
} | ||
class RootRoute extends Route { | ||
constructor(options) { | ||
super(options); | ||
} | ||
setLoaderData = loaderData => { | ||
batch(() => { | ||
this.store.setState(s => { | ||
s.routeLoaderData = loaderData; | ||
}); | ||
this.#updateLoaderData(); | ||
}); | ||
}; | ||
cancel = () => { | ||
this.abortController?.abort(); | ||
}; | ||
load = async loaderOpts => { | ||
const now = Date.now(); | ||
const minMaxAge = loaderOpts?.preload ? Math.max(loaderOpts?.maxAge, loaderOpts?.gcMaxAge) : 0; | ||
} | ||
// If this is a preload, add it to the preload cache | ||
if (loaderOpts?.preload && minMaxAge > 0) { | ||
// If the match is currently active, don't preload it | ||
if (this.router.store.state.currentMatches.find(d => d.id === this.id)) { | ||
return; | ||
} | ||
this.router.store.setState(s => { | ||
s.matchCache[this.id] = { | ||
gc: now + loaderOpts.gcMaxAge, | ||
match: this | ||
}; | ||
}); | ||
} | ||
// const rootRoute = new RootRoute({ | ||
// validateSearch: () => null as unknown as { root?: boolean }, | ||
// }) | ||
// If the match is invalid, errored or idle, trigger it to load | ||
if (this.store.state.status === 'success' && this.getIsInvalid() || this.store.state.status === 'error' || this.store.state.status === 'idle') { | ||
const maxAge = loaderOpts?.preload ? loaderOpts?.maxAge : undefined; | ||
await this.fetch({ | ||
maxAge | ||
}); | ||
} | ||
}; | ||
fetch = async opts => { | ||
this.__loadPromise = new Promise(async resolve => { | ||
const loadId = '' + Date.now() + Math.random(); | ||
this.#latestId = loadId; | ||
const checkLatest = () => loadId !== this.#latestId ? this.__loadPromise?.then(() => resolve()) : undefined; | ||
let latestPromise; | ||
batch(() => { | ||
// If the match was in an error state, set it | ||
// to a loading state again. Otherwise, keep it | ||
// as loading or resolved | ||
if (this.store.state.status === 'idle') { | ||
this.store.setState(s => s.status = 'loading'); | ||
} | ||
// const aRoute = new Route({ | ||
// getParentRoute: () => rootRoute, | ||
// path: 'a', | ||
// validateSearch: () => null as unknown as { a?: string }, | ||
// }) | ||
// We started loading the route, so it's no longer invalid | ||
this.store.setState(s => s.invalid = false); | ||
}); | ||
// const bRoute = new Route({ | ||
// getParentRoute: () => aRoute, | ||
// path: 'b', | ||
// }) | ||
// We are now fetching, even if it's in the background of a | ||
// resolved state | ||
this.store.setState(s => s.isFetching = true); | ||
this.#resolve = resolve; | ||
const componentsPromise = (async () => { | ||
// then run all component and data loaders in parallel | ||
// For each component type, potentially load it asynchronously | ||
// const rootIsRoot = rootRoute.isRoot | ||
// // ^? | ||
// const aIsRoot = aRoute.isRoot | ||
// // ^? | ||
await Promise.all(componentTypes.map(async type => { | ||
const component = this.route.options[type]; | ||
if (this[type]?.preload) { | ||
this[type] = await this.router.options.loadComponent(component); | ||
} | ||
})); | ||
})(); | ||
const dataPromise = Promise.resolve().then(async () => { | ||
try { | ||
if (this.route.options.loader) { | ||
const data = await this.router.loadMatchData(this); | ||
if (latestPromise = checkLatest()) return latestPromise; | ||
this.setLoaderData(data); | ||
} | ||
this.store.setState(s => { | ||
s.error = undefined; | ||
s.status = 'success'; | ||
s.updatedAt = Date.now(); | ||
s.invalidAt = s.updatedAt + (opts?.maxAge ?? this.route.options.loaderMaxAge ?? this.router.options.defaultLoaderMaxAge ?? 0); | ||
}); | ||
return this.store.state.routeLoaderData; | ||
} catch (err) { | ||
if (latestPromise = checkLatest()) return latestPromise; | ||
if (process.env.NODE_ENV !== 'production') { | ||
console.error(err); | ||
} | ||
this.store.setState(s => { | ||
s.error = err; | ||
s.status = 'error'; | ||
s.updatedAt = Date.now(); | ||
}); | ||
throw err; | ||
} | ||
}); | ||
const after = async () => { | ||
if (latestPromise = checkLatest()) return latestPromise; | ||
this.store.setState(s => s.isFetching = false); | ||
this.#resolve(); | ||
delete this.__loadPromise; | ||
}; | ||
try { | ||
await Promise.all([componentsPromise, dataPromise.catch(() => {})]); | ||
after(); | ||
} catch { | ||
after(); | ||
} | ||
}); | ||
return this.__loadPromise; | ||
}; | ||
invalidate = async () => { | ||
this.store.setState(s => s.invalid = true); | ||
if (this.router.store.state.currentMatches.find(d => d.id === this.id)) { | ||
await this.load(); | ||
} | ||
}; | ||
__hasLoaders = () => { | ||
return !!(this.route.options.loader || componentTypes.some(d => this.route.options[d]?.preload)); | ||
}; | ||
getIsInvalid = () => { | ||
const now = Date.now(); | ||
return this.store.state.invalid || this.store.state.invalidAt < now; | ||
}; | ||
#updateLoaderData = () => { | ||
this.store.setState(s => { | ||
s.loaderData = replaceEqualDeep(s.loaderData, { | ||
...this.parentMatch?.store.state.loaderData, | ||
...s.routeLoaderData | ||
}); | ||
}); | ||
this.onLoaderDataListeners.forEach(listener => listener()); | ||
}; | ||
__setParentMatch = parentMatch => { | ||
if (!this.parentMatch && parentMatch) { | ||
this.parentMatch = parentMatch; | ||
this.parentMatch.__onLoaderData(() => { | ||
this.#updateLoaderData(); | ||
}); | ||
} | ||
}; | ||
__onLoaderData = listener => { | ||
this.onLoaderDataListeners.add(listener); | ||
// return () => this.onLoaderDataListeners.delete(listener) | ||
}; | ||
// const rId = rootRoute.id | ||
// // ^? | ||
// const aId = aRoute.id | ||
// // ^? | ||
// const bId = bRoute.id | ||
// // ^? | ||
__validate = () => { | ||
// Validate the search params and stabilize them | ||
const parentSearch = this.parentMatch?.store.state.search ?? this.router.store.state.latestLocation.search; | ||
try { | ||
const prevSearch = this.store.state.routeSearch; | ||
const validator = typeof this.route.options.validateSearch === 'object' ? this.route.options.validateSearch.parse : this.route.options.validateSearch; | ||
let nextSearch = validator?.(parentSearch) ?? {}; | ||
batch(() => { | ||
// Invalidate route matches when search param stability changes | ||
if (prevSearch !== nextSearch) { | ||
this.store.setState(s => s.invalid = true); | ||
} | ||
this.store.setState(s => { | ||
s.routeSearch = nextSearch; | ||
s.search = { | ||
...parentSearch, | ||
...nextSearch | ||
}; | ||
}); | ||
}); | ||
componentTypes.map(async type => { | ||
const component = this.route.options[type]; | ||
if (typeof this[type] !== 'function') { | ||
this[type] = component; | ||
} | ||
}); | ||
} catch (err) { | ||
console.error(err); | ||
const error = new Error('Invalid search params found', { | ||
cause: err | ||
}); | ||
error.code = 'INVALID_SEARCH_PARAMS'; | ||
this.store.setState(s => { | ||
s.status = 'error'; | ||
s.error = error; | ||
}); | ||
// const rPath = rootRoute.fullPath | ||
// // ^? | ||
// const aPath = aRoute.fullPath | ||
// // ^? | ||
// const bPath = bRoute.fullPath | ||
// // ^? | ||
// Do not proceed with loading the route | ||
return; | ||
} | ||
}; | ||
} | ||
// const rSearch = rootRoute.__types.fullSearchSchema | ||
// // ^? | ||
// const aSearch = aRoute.__types.fullSearchSchema | ||
// // ^? | ||
// const bSearch = bRoute.__types.fullSearchSchema | ||
// // ^? | ||
// const config = rootRoute.addChildren([aRoute.addChildren([bRoute])]) | ||
// // ^? | ||
const defaultParseSearch = parseSearchWith(JSON.parse); | ||
@@ -936,33 +738,9 @@ const defaultStringifySearch = stringifySearchWith(JSON.stringify); | ||
const defaultFetchServerDataFn = async ({ | ||
router, | ||
routeMatch | ||
}) => { | ||
const next = router.buildNext({ | ||
to: '.', | ||
search: d => ({ | ||
...(d ?? {}), | ||
__data: { | ||
matchId: routeMatch.id | ||
} | ||
}) | ||
}); | ||
const res = await fetch(next.href, { | ||
method: 'GET', | ||
signal: routeMatch.abortController.signal | ||
}); | ||
if (res.ok) { | ||
return res.json(); | ||
} | ||
throw new Error('Failed to fetch match data'); | ||
}; | ||
// | ||
const componentTypes = ['component', 'errorComponent', 'pendingComponent']; | ||
class Router { | ||
#unsubHistory; | ||
startedLoadingAt = Date.now(); | ||
resolveNavigation = () => {}; | ||
constructor(options) { | ||
this.options = { | ||
defaultLoaderGcMaxAge: 5 * 60 * 1000, | ||
defaultLoaderMaxAge: 0, | ||
defaultPreloadMaxAge: 2000, | ||
defaultPreloadDelay: 50, | ||
@@ -972,46 +750,65 @@ context: undefined, | ||
stringifySearch: options?.stringifySearch ?? defaultStringifySearch, | ||
parseSearch: options?.parseSearch ?? defaultParseSearch, | ||
fetchServerDataFn: options?.fetchServerDataFn ?? defaultFetchServerDataFn | ||
parseSearch: options?.parseSearch ?? defaultParseSearch | ||
// fetchServerDataFn: options?.fetchServerDataFn ?? defaultFetchServerDataFn, | ||
}; | ||
this.store = createStore(getInitialRouterState()); | ||
this.basepath = ''; | ||
this.__store = new Store(getInitialRouterState(), { | ||
onUpdate: () => { | ||
const prev = this.state; | ||
this.state = this.__store.state; | ||
const matchesByIdChanged = prev.matchesById !== this.state.matchesById; | ||
let matchesChanged; | ||
let pendingMatchesChanged; | ||
if (!matchesByIdChanged) { | ||
matchesChanged = prev.matchIds.length !== this.state.matchIds.length || prev.matchIds.some((d, i) => d !== this.state.matchIds[i]); | ||
pendingMatchesChanged = prev.pendingMatchIds.length !== this.state.pendingMatchIds.length || prev.pendingMatchIds.some((d, i) => d !== this.state.pendingMatchIds[i]); | ||
} | ||
if (matchesByIdChanged || matchesChanged) { | ||
this.state.matches = this.state.matchIds.map(id => { | ||
return this.state.matchesById[id]; | ||
}); | ||
} | ||
if (matchesByIdChanged || pendingMatchesChanged) { | ||
this.state.pendingMatches = this.state.pendingMatchIds.map(id => { | ||
return this.state.matchesById[id]; | ||
}); | ||
} | ||
this.state.isFetching = [...this.state.matches, ...this.state.pendingMatches].some(d => d.isFetching); | ||
}, | ||
defaultPriority: 'low' | ||
}); | ||
this.state = this.__store.state; | ||
this.update(options); | ||
// Allow frameworks to hook into the router creation | ||
this.options.Router?.(this); | ||
const next = this.buildNext({ | ||
hash: true, | ||
fromCurrent: true, | ||
search: true, | ||
state: true | ||
}); | ||
if (this.state.location.href !== next.href) { | ||
this.#commitLocation({ | ||
...next, | ||
replace: true | ||
}); | ||
} | ||
} | ||
reset = () => { | ||
this.store.setState(s => Object.assign(s, getInitialRouterState())); | ||
this.__store.setState(s => Object.assign(s, getInitialRouterState())); | ||
}; | ||
mount = () => { | ||
// Mount only does anything on the client | ||
if (!isServer) { | ||
// If the router matches are empty, load the matches | ||
if (!this.store.state.currentMatches.length) { | ||
this.load(); | ||
} | ||
const visibilityChangeEvent = 'visibilitychange'; | ||
const focusEvent = 'focus'; | ||
// If the router matches are empty, start loading the matches | ||
// if (!this.state.matches.length) { | ||
this.safeLoad(); | ||
// } | ||
}; | ||
// addEventListener does not exist in React Native, but window does | ||
// In the future, we might need to invert control here for more adapters | ||
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition | ||
if (window.addEventListener) { | ||
// Listen to visibilitychange and focus | ||
window.addEventListener(visibilityChangeEvent, this.#onFocus, false); | ||
window.addEventListener(focusEvent, this.#onFocus, false); | ||
update = opts => { | ||
this.options = { | ||
...this.options, | ||
...opts, | ||
context: { | ||
...this.options.context, | ||
...opts?.context | ||
} | ||
return () => { | ||
if (window.removeEventListener) { | ||
// Be sure to unsubscribe if a new handler is set | ||
window.removeEventListener(visibilityChangeEvent, this.#onFocus); | ||
window.removeEventListener(focusEvent, this.#onFocus); | ||
} | ||
}; | ||
} | ||
return () => {}; | ||
}; | ||
update = opts => { | ||
Object.assign(this.options, opts); | ||
}; | ||
if (!this.history || this.options.history && this.options.history !== this.history) { | ||
@@ -1022,8 +819,12 @@ if (this.#unsubHistory) { | ||
this.history = this.options.history ?? (isServer ? createMemoryHistory() : createBrowserHistory()); | ||
this.store.setState(s => { | ||
s.latestLocation = this.#parseLocation(); | ||
s.currentLocation = s.latestLocation; | ||
}); | ||
const parsedLocation = this.#parseLocation(); | ||
this.__store.setState(s => ({ | ||
...s, | ||
resolvedLocation: parsedLocation, | ||
location: parsedLocation | ||
})); | ||
this.#unsubHistory = this.history.listen(() => { | ||
this.load(this.#parseLocation(this.store.state.latestLocation)); | ||
this.safeLoad({ | ||
next: this.#parseLocation(this.state.location) | ||
}); | ||
}); | ||
@@ -1033,8 +834,7 @@ } | ||
basepath, | ||
routeConfig | ||
routeTree | ||
} = this.options; | ||
this.basepath = `/${trimPath(basepath ?? '') ?? ''}`; | ||
if (routeConfig) { | ||
this.routesById = {}; | ||
this.routeTree = this.#buildRouteTree(routeConfig); | ||
if (routeTree && routeTree !== this.routeTree) { | ||
this.#buildRouteTree(routeTree); | ||
} | ||
@@ -1045,137 +845,102 @@ return this; | ||
const next = this.#buildLocation(opts); | ||
const matches = this.matchRoutes(next.pathname); | ||
const __preSearchFilters = matches.map(match => match.route.options.preSearchFilters ?? []).flat().filter(Boolean); | ||
const __postSearchFilters = matches.map(match => match.route.options.postSearchFilters ?? []).flat().filter(Boolean); | ||
const __matches = this.matchRoutes(next.pathname, next.search); | ||
return this.#buildLocation({ | ||
...opts, | ||
__preSearchFilters, | ||
__postSearchFilters | ||
__matches | ||
}); | ||
}; | ||
cancelMatches = () => { | ||
[...this.store.state.currentMatches, ...(this.store.state.pendingMatches || [])].forEach(match => { | ||
match.cancel(); | ||
this.state.matches.forEach(match => { | ||
this.cancelMatch(match.id); | ||
}); | ||
}; | ||
load = async next => { | ||
let now = Date.now(); | ||
const startedAt = now; | ||
this.startedLoadingAt = startedAt; | ||
cancelMatch = id => { | ||
this.getRouteMatch(id)?.abortController?.abort(); | ||
}; | ||
safeLoad = opts => { | ||
return this.load(opts).catch(err => { | ||
// console.warn(err) | ||
// invariant(false, 'Encountered an error during router.load()! ☝️.') | ||
}); | ||
}; | ||
latestLoadPromise = Promise.resolve(); | ||
load = async opts => { | ||
const promise = new Promise(async (resolve, reject) => { | ||
let latestPromise; | ||
const checkLatest = () => { | ||
return this.latestLoadPromise !== promise ? this.latestLoadPromise : undefined; | ||
}; | ||
// Cancel any pending matches | ||
this.cancelMatches(); | ||
let matches; | ||
batch(() => { | ||
if (next) { | ||
// Ingest the new location | ||
this.store.setState(s => { | ||
s.latestLocation = next; | ||
}); | ||
} | ||
// Cancel any pending matches | ||
// this.cancelMatches() | ||
// Match the routes | ||
matches = this.matchRoutes(this.store.state.latestLocation.pathname, { | ||
strictParseParams: true | ||
}); | ||
this.store.setState(s => { | ||
s.status = 'loading'; | ||
s.pendingMatches = matches; | ||
s.pendingLocation = this.store.state.latestLocation; | ||
}); | ||
}); | ||
let pendingMatches; | ||
this.__store.batch(() => { | ||
if (opts?.next) { | ||
// Ingest the new location | ||
this.__store.setState(s => ({ | ||
...s, | ||
location: opts.next | ||
})); | ||
} | ||
// Load the matches | ||
try { | ||
await this.loadMatches(matches); | ||
} catch (err) { | ||
console.warn(err); | ||
invariant(false, 'Matches failed to load due to error above ☝️. Navigation cancelled!'); | ||
} | ||
if (this.startedLoadingAt !== startedAt) { | ||
// Ignore side-effects of outdated side-effects | ||
return this.navigationPromise; | ||
} | ||
const previousMatches = this.store.state.currentMatches; | ||
const exiting = [], | ||
staying = []; | ||
previousMatches.forEach(d => { | ||
if (matches.find(dd => dd.id === d.id)) { | ||
staying.push(d); | ||
} else { | ||
exiting.push(d); | ||
} | ||
}); | ||
const entering = matches.filter(d => { | ||
return !previousMatches.find(dd => dd.id === d.id); | ||
}); | ||
now = Date.now(); | ||
exiting.forEach(d => { | ||
d.__onExit?.({ | ||
params: d.params, | ||
search: d.store.state.routeSearch | ||
}); | ||
// Clear non-loading error states when match leaves | ||
if (d.store.state.status === 'error' && !d.store.state.isFetching) { | ||
d.store.setState(s => { | ||
s.status = 'idle'; | ||
s.error = undefined; | ||
// Match the routes | ||
pendingMatches = this.matchRoutes(this.state.location.pathname, this.state.location.search, { | ||
throwOnError: opts?.throwOnError, | ||
debug: true | ||
}); | ||
} | ||
const gc = Math.max(d.route.options.loaderGcMaxAge ?? this.options.defaultLoaderGcMaxAge ?? 0, d.route.options.loaderMaxAge ?? this.options.defaultLoaderMaxAge ?? 0); | ||
if (gc > 0) { | ||
this.store.setState(s => { | ||
s.matchCache[d.id] = { | ||
gc: gc == Infinity ? Number.MAX_SAFE_INTEGER : now + gc, | ||
match: d | ||
}; | ||
}); | ||
} | ||
}); | ||
staying.forEach(d => { | ||
d.route.options.onTransition?.({ | ||
params: d.params, | ||
search: d.store.state.routeSearch | ||
this.__store.setState(s => ({ | ||
...s, | ||
status: 'pending', | ||
pendingMatchIds: pendingMatches.map(d => d.id), | ||
matchesById: this.#mergeMatches(s.matchesById, pendingMatches) | ||
})); | ||
}); | ||
}); | ||
entering.forEach(d => { | ||
d.__onExit = d.route.options.onLoaded?.({ | ||
params: d.params, | ||
search: d.store.state.search | ||
}); | ||
delete this.store.state.matchCache[d.id]; | ||
}); | ||
this.store.setState(s => { | ||
Object.assign(s, { | ||
status: 'idle', | ||
currentLocation: this.store.state.latestLocation, | ||
currentMatches: matches, | ||
pendingLocation: undefined, | ||
pendingMatches: undefined | ||
}); | ||
}); | ||
this.options.onRouteChange?.(); | ||
this.resolveNavigation(); | ||
}; | ||
cleanMatchCache = () => { | ||
const now = Date.now(); | ||
this.store.setState(s => { | ||
Object.keys(s.matchCache).forEach(matchId => { | ||
const entry = s.matchCache[matchId]; | ||
try { | ||
// Load the matches | ||
await this.loadMatches(pendingMatches); | ||
// Don't remove loading matches | ||
if (entry.match.store.state.status === 'loading') { | ||
return; | ||
// Only apply the latest transition | ||
if (latestPromise = checkLatest()) { | ||
return await latestPromise; | ||
} | ||
// Do not remove successful matches that are still valid | ||
if (entry.gc > 0 && entry.gc > now) { | ||
return; | ||
const prevLocation = this.state.resolvedLocation; | ||
this.__store.setState(s => ({ | ||
...s, | ||
status: 'idle', | ||
resolvedLocation: s.location, | ||
matchIds: s.pendingMatchIds, | ||
pendingMatchIds: [] | ||
})); | ||
if (prevLocation.href !== this.state.location.href) { | ||
this.options.onRouteChange?.(); | ||
} | ||
// Everything else gets removed | ||
delete s.matchCache[matchId]; | ||
}); | ||
resolve(); | ||
} catch (err) { | ||
// Only apply the latest transition | ||
if (latestPromise = checkLatest()) { | ||
return await latestPromise; | ||
} | ||
reject(err); | ||
} | ||
}); | ||
this.latestLoadPromise = promise; | ||
return this.latestLoadPromise; | ||
}; | ||
#mergeMatches = (prevMatchesById, nextMatches) => { | ||
const nextMatchesById = { | ||
...prevMatchesById | ||
}; | ||
let hadNew = false; | ||
nextMatches.forEach(match => { | ||
if (!nextMatchesById[match.id]) { | ||
hadNew = true; | ||
nextMatchesById[match.id] = match; | ||
} | ||
}); | ||
if (!hadNew) { | ||
return prevMatchesById; | ||
} | ||
return nextMatchesById; | ||
}; | ||
getRoute = id => { | ||
@@ -1186,163 +951,343 @@ const route = this.routesById[id]; | ||
}; | ||
loadRoute = async (navigateOpts = this.store.state.latestLocation) => { | ||
preloadRoute = async (navigateOpts = this.state.location) => { | ||
const next = this.buildNext(navigateOpts); | ||
const matches = this.matchRoutes(next.pathname, { | ||
strictParseParams: true | ||
const matches = this.matchRoutes(next.pathname, next.search, { | ||
throwOnError: true | ||
}); | ||
await this.loadMatches(matches); | ||
return matches; | ||
}; | ||
preloadRoute = async (navigateOpts = this.store.state.latestLocation, loaderOpts) => { | ||
const next = this.buildNext(navigateOpts); | ||
const matches = this.matchRoutes(next.pathname, { | ||
strictParseParams: true | ||
this.__store.setState(s => { | ||
return { | ||
...s, | ||
matchesById: this.#mergeMatches(s.matchesById, matches) | ||
}; | ||
}); | ||
await this.loadMatches(matches, { | ||
preload: true, | ||
maxAge: loaderOpts.maxAge ?? this.options.defaultPreloadMaxAge ?? this.options.defaultLoaderMaxAge ?? 0, | ||
gcMaxAge: loaderOpts.gcMaxAge ?? this.options.defaultPreloadGcMaxAge ?? this.options.defaultLoaderGcMaxAge ?? 0 | ||
maxAge: navigateOpts.maxAge | ||
}); | ||
return matches; | ||
}; | ||
matchRoutes = (pathname, opts) => { | ||
const matches = []; | ||
if (!this.routeTree) { | ||
return matches; | ||
} | ||
const existingMatches = [...this.store.state.currentMatches, ...(this.store.state.pendingMatches ?? [])]; | ||
const recurse = async routes => { | ||
const parentMatch = last(matches); | ||
let params = parentMatch?.params ?? {}; | ||
const filteredRoutes = this.options.filterRoutes?.(routes) ?? routes; | ||
let foundRoutes = []; | ||
const findMatchInRoutes = (parentRoutes, routes) => { | ||
routes.some(route => { | ||
if (!route.path && route.childRoutes?.length) { | ||
return findMatchInRoutes([...foundRoutes, route], route.childRoutes); | ||
} | ||
const fuzzy = !!(route.path !== '/' || route.childRoutes?.length); | ||
const matchParams = matchPathname(this.basepath, pathname, { | ||
to: route.fullPath, | ||
fuzzy, | ||
caseSensitive: route.options.caseSensitive ?? this.options.caseSensitive | ||
}); | ||
if (matchParams) { | ||
let parsedParams; | ||
try { | ||
parsedParams = route.options.parseParams?.(matchParams) ?? matchParams; | ||
} catch (err) { | ||
if (opts?.strictParseParams) { | ||
throw err; | ||
} | ||
} | ||
params = { | ||
...params, | ||
...parsedParams | ||
}; | ||
} | ||
if (!!matchParams) { | ||
foundRoutes = [...parentRoutes, route]; | ||
} | ||
return !!foundRoutes.length; | ||
cleanMatches = () => { | ||
const now = Date.now(); | ||
const outdatedMatchIds = Object.values(this.state.matchesById).filter(match => { | ||
const route = this.getRoute(match.routeId); | ||
return !this.state.matchIds.includes(match.id) && !this.state.pendingMatchIds.includes(match.id) && match.preloadInvalidAt < now && (route.options.gcMaxAge ? match.updatedAt + route.options.gcMaxAge < now : true); | ||
}).map(d => d.id); | ||
if (outdatedMatchIds.length) { | ||
this.__store.setState(s => { | ||
const matchesById = { | ||
...s.matchesById | ||
}; | ||
outdatedMatchIds.forEach(id => { | ||
delete matchesById[id]; | ||
}); | ||
return !!foundRoutes.length; | ||
}; | ||
findMatchInRoutes([], filteredRoutes); | ||
if (!foundRoutes.length) { | ||
return; | ||
} | ||
foundRoutes.forEach(foundRoute => { | ||
const interpolatedPath = interpolatePath(foundRoute.path, params); | ||
const matchId = interpolatePath(foundRoute.id, params, true); | ||
const match = existingMatches.find(d => d.id === matchId) || this.store.state.matchCache[matchId]?.match || new RouteMatch(this, foundRoute, { | ||
id: matchId, | ||
params, | ||
pathname: joinPaths([this.basepath, interpolatedPath]) | ||
}); | ||
matches.push(match); | ||
return { | ||
...s, | ||
matchesById | ||
}; | ||
}); | ||
const foundRoute = last(foundRoutes); | ||
if (foundRoute.childRoutes?.length) { | ||
recurse(foundRoute.childRoutes); | ||
} | ||
}; | ||
matchRoutes = (pathname, locationSearch, opts) => { | ||
let routeParams = {}; | ||
let foundRoute = this.flatRoutes.find(route => { | ||
const matchedParams = matchPathname(this.basepath, pathname, { | ||
to: route.fullPath, | ||
caseSensitive: route.options.caseSensitive ?? this.options.caseSensitive | ||
}); | ||
if (matchedParams) { | ||
routeParams = matchedParams; | ||
return true; | ||
} | ||
}; | ||
recurse([this.routeTree]); | ||
linkMatches(matches); | ||
return matches; | ||
}; | ||
loadMatches = async (resolvedMatches, loaderOpts) => { | ||
this.cleanMatchCache(); | ||
resolvedMatches.forEach(async match => { | ||
// Validate the match (loads search params etc) | ||
match.__validate(); | ||
return false; | ||
}); | ||
let routeCursor = foundRoute || this.routesById['__root__']; | ||
let matchedRoutes = [routeCursor]; | ||
while (routeCursor?.parentRoute) { | ||
routeCursor = routeCursor.parentRoute; | ||
if (routeCursor) matchedRoutes.unshift(routeCursor); | ||
} | ||
// Check each match middleware to see if the route can be accessed | ||
await Promise.all(resolvedMatches.map(async match => { | ||
// Alright, by now we should have all of our | ||
// matching routes and their param pairs, let's | ||
// Turn them into actual `Match` objects and | ||
// accumulate the params into a single params bag | ||
let allParams = {}; | ||
// Existing matches are matches that are already loaded along with | ||
// pending matches that are still loading | ||
const matches = matchedRoutes.map(route => { | ||
let parsedParams; | ||
let parsedParamsError; | ||
try { | ||
await match.route.options.beforeLoad?.({ | ||
router: this, | ||
match | ||
parsedParams = route.options.parseParams?.(routeParams) ?? routeParams; | ||
// (typeof route.options.parseParams === 'object' && | ||
// route.options.parseParams.parse | ||
// ? route.options.parseParams.parse(routeParams) | ||
// : (route.options.parseParams as any)?.(routeParams!)) ?? routeParams | ||
} catch (err) { | ||
parsedParamsError = new PathParamError(err.message, { | ||
cause: err | ||
}); | ||
} catch (err) { | ||
if (!loaderOpts?.preload) { | ||
match.route.options.onLoadError?.(err); | ||
if (opts?.throwOnError) { | ||
throw parsedParamsError; | ||
} | ||
throw err; | ||
} | ||
})); | ||
const matchPromises = resolvedMatches.map(async (match, index) => { | ||
const prevMatch = resolvedMatches[1]; | ||
const search = match.store.state.search; | ||
if (search.__data?.matchId && search.__data.matchId !== match.id) { | ||
return; | ||
// Add the parsed params to the accumulated params bag | ||
Object.assign(allParams, parsedParams); | ||
const interpolatedPath = interpolatePath(route.path, allParams); | ||
const key = route.options.key ? route.options.key({ | ||
params: allParams, | ||
search: locationSearch | ||
}) ?? '' : ''; | ||
const stringifiedKey = key ? JSON.stringify(key) : ''; | ||
const matchId = interpolatePath(route.id, allParams, true) + stringifiedKey; | ||
// Waste not, want not. If we already have a match for this route, | ||
// reuse it. This is important for layout routes, which might stick | ||
// around between navigation actions that only change leaf routes. | ||
const existingMatch = this.getRouteMatch(matchId); | ||
if (existingMatch) { | ||
return { | ||
...existingMatch | ||
}; | ||
} | ||
match.load(loaderOpts); | ||
if (match.store.state.status !== 'success' && match.__loadPromise) { | ||
// Wait for the first sign of activity from the match | ||
await match.__loadPromise; | ||
} | ||
if (prevMatch) { | ||
await prevMatch.__loadPromise; | ||
} | ||
// Create a fresh route match | ||
const hasLoaders = !!(route.options.loader || componentTypes.some(d => route.options[d]?.preload)); | ||
const routeMatch = { | ||
id: matchId, | ||
key: stringifiedKey, | ||
routeId: route.id, | ||
params: allParams, | ||
pathname: joinPaths([this.basepath, interpolatedPath]), | ||
updatedAt: Date.now(), | ||
invalidAt: Infinity, | ||
preloadInvalidAt: Infinity, | ||
routeSearch: {}, | ||
search: {}, | ||
status: hasLoaders ? 'idle' : 'success', | ||
isFetching: false, | ||
invalid: false, | ||
error: undefined, | ||
paramsError: parsedParamsError, | ||
searchError: undefined, | ||
loaderData: undefined, | ||
loadPromise: Promise.resolve(), | ||
routeContext: undefined, | ||
context: undefined, | ||
abortController: new AbortController(), | ||
fetchedAt: 0 | ||
}; | ||
return routeMatch; | ||
}); | ||
await Promise.all(matchPromises); | ||
}; | ||
loadMatchData = async routeMatch => { | ||
if (isServer || !this.options.useServerData) { | ||
return (await routeMatch.route.options.loader?.({ | ||
// parentLoaderPromise: routeMatch.parentMatch.dataPromise, | ||
params: routeMatch.params, | ||
search: routeMatch.store.state.routeSearch, | ||
signal: routeMatch.abortController.signal | ||
})) || {}; | ||
} else { | ||
// Refresh: | ||
// '/dashboard' | ||
// '/dashboard/invoices/' | ||
// '/dashboard/invoices/123' | ||
// New: | ||
// '/dashboard/invoices/456' | ||
// TODO: batch requests when possible | ||
const res = await this.options.fetchServerDataFn({ | ||
router: this, | ||
routeMatch | ||
// Take each match and resolve its search params and context | ||
// This has to happen after the matches are created or found | ||
// so that we can use the parent match's search params and context | ||
matches.forEach((match, i) => { | ||
const parentMatch = matches[i - 1]; | ||
const route = this.getRoute(match.routeId); | ||
const searchInfo = (() => { | ||
// Validate the search params and stabilize them | ||
const parentSearchInfo = { | ||
search: parentMatch?.search ?? locationSearch, | ||
routeSearch: parentMatch?.routeSearch ?? locationSearch | ||
}; | ||
try { | ||
const validator = typeof route.options.validateSearch === 'object' ? route.options.validateSearch.parse : route.options.validateSearch; | ||
const routeSearch = validator?.(parentSearchInfo.search) ?? {}; | ||
const search = { | ||
...parentSearchInfo.search, | ||
...routeSearch | ||
}; | ||
return { | ||
routeSearch: replaceEqualDeep(match.routeSearch, routeSearch), | ||
search: replaceEqualDeep(match.search, search) | ||
}; | ||
} catch (err) { | ||
match.searchError = new SearchParamError(err.message, { | ||
cause: err | ||
}); | ||
if (opts?.throwOnError) { | ||
throw match.searchError; | ||
} | ||
return parentSearchInfo; | ||
} | ||
})(); | ||
const contextInfo = (() => { | ||
try { | ||
const routeContext = route.options.getContext?.({ | ||
parentContext: parentMatch?.routeContext ?? {}, | ||
context: parentMatch?.context ?? this?.options.context ?? {}, | ||
params: match.params, | ||
search: match.search | ||
}) || {}; | ||
const context = { | ||
...(parentMatch?.context ?? this?.options.context), | ||
...routeContext | ||
}; | ||
return { | ||
context, | ||
routeContext | ||
}; | ||
} catch (err) { | ||
route.options.onError?.(err); | ||
throw err; | ||
} | ||
})(); | ||
Object.assign(match, { | ||
...searchInfo, | ||
...contextInfo | ||
}); | ||
return res; | ||
} | ||
}); | ||
return matches; | ||
}; | ||
invalidateRoute = async opts => { | ||
const next = this.buildNext(opts); | ||
const unloadedMatchIds = this.matchRoutes(next.pathname).map(d => d.id); | ||
await Promise.allSettled([...this.store.state.currentMatches, ...(this.store.state.pendingMatches ?? [])].map(async match => { | ||
if (unloadedMatchIds.includes(match.id)) { | ||
return match.invalidate(); | ||
loadMatches = async (resolvedMatches, opts) => { | ||
this.cleanMatches(); | ||
let firstBadMatchIndex; | ||
// Check each match middleware to see if the route can be accessed | ||
try { | ||
await Promise.all(resolvedMatches.map(async (match, index) => { | ||
const route = this.getRoute(match.routeId); | ||
if (!opts?.preload) { | ||
// Update each match with its latest url data | ||
this.setRouteMatch(match.id, s => ({ | ||
...s, | ||
routeSearch: match.routeSearch, | ||
search: match.search, | ||
routeContext: match.routeContext, | ||
context: match.context, | ||
error: match.error, | ||
paramsError: match.paramsError, | ||
searchError: match.searchError, | ||
params: match.params | ||
})); | ||
} | ||
const handleError = (err, handler) => { | ||
firstBadMatchIndex = firstBadMatchIndex ?? index; | ||
handler = handler || route.options.onError; | ||
if (isRedirect(err)) { | ||
throw err; | ||
} | ||
try { | ||
handler?.(err); | ||
} catch (errorHandlerErr) { | ||
err = errorHandlerErr; | ||
if (isRedirect(errorHandlerErr)) { | ||
throw errorHandlerErr; | ||
} | ||
} | ||
this.setRouteMatch(match.id, s => ({ | ||
...s, | ||
error: err, | ||
status: 'error', | ||
updatedAt: Date.now() | ||
})); | ||
}; | ||
if (match.paramsError) { | ||
handleError(match.paramsError, route.options.onParseParamsError); | ||
} | ||
if (match.searchError) { | ||
handleError(match.searchError, route.options.onValidateSearchError); | ||
} | ||
try { | ||
await route.options.beforeLoad?.({ | ||
...match, | ||
preload: !!opts?.preload | ||
}); | ||
} catch (err) { | ||
handleError(err, route.options.onBeforeLoadError); | ||
} | ||
})); | ||
} catch (err) { | ||
if (!opts?.preload) { | ||
this.navigate(err); | ||
} | ||
})); | ||
throw err; | ||
} | ||
const validResolvedMatches = resolvedMatches.slice(0, firstBadMatchIndex); | ||
const matchPromises = []; | ||
validResolvedMatches.forEach((match, index) => { | ||
matchPromises.push((async () => { | ||
const parentMatchPromise = matchPromises[index - 1]; | ||
const route = this.getRoute(match.routeId); | ||
if (match.isFetching || match.status === 'success' && !this.getIsInvalid({ | ||
matchId: match.id, | ||
preload: opts?.preload | ||
})) { | ||
return this.getRouteMatch(match.id)?.loadPromise; | ||
} | ||
const fetchedAt = Date.now(); | ||
const checkLatest = () => { | ||
const latest = this.getRouteMatch(match.id); | ||
return latest && latest.fetchedAt !== fetchedAt ? latest.loadPromise : undefined; | ||
}; | ||
const loadPromise = (async () => { | ||
let latestPromise; | ||
const componentsPromise = Promise.all(componentTypes.map(async type => { | ||
const component = route.options[type]; | ||
if (component?.preload) { | ||
await component.preload(); | ||
} | ||
})); | ||
const loaderPromise = route.options.loader?.({ | ||
...match, | ||
preload: !!opts?.preload, | ||
parentMatchPromise | ||
}); | ||
const handleError = err => { | ||
if (isRedirect(err)) { | ||
if (!opts?.preload) { | ||
this.navigate(err); | ||
} | ||
return true; | ||
} | ||
return false; | ||
}; | ||
try { | ||
const [_, loader] = await Promise.all([componentsPromise, loaderPromise]); | ||
if (latestPromise = checkLatest()) return await latestPromise; | ||
this.setRouteMatchData(match.id, () => loader, opts); | ||
} catch (err) { | ||
if (latestPromise = checkLatest()) return await latestPromise; | ||
if (handleError(err)) { | ||
return; | ||
} | ||
const errorHandler = route.options.onLoadError ?? route.options.onError; | ||
let caughtError = err; | ||
try { | ||
errorHandler?.(err); | ||
} catch (errorHandlerErr) { | ||
caughtError = errorHandlerErr; | ||
if (handleError(errorHandlerErr)) { | ||
return; | ||
} | ||
} | ||
this.setRouteMatch(match.id, s => ({ | ||
...s, | ||
error: caughtError, | ||
status: 'error', | ||
isFetching: false, | ||
updatedAt: Date.now() | ||
})); | ||
} | ||
})(); | ||
this.setRouteMatch(match.id, s => ({ | ||
...s, | ||
status: s.status !== 'success' ? 'pending' : s.status, | ||
isFetching: true, | ||
loadPromise, | ||
fetchedAt, | ||
invalid: false | ||
})); | ||
await loadPromise; | ||
})()); | ||
}); | ||
await Promise.all(matchPromises); | ||
}; | ||
reload = () => { | ||
this.navigate({ | ||
return this.navigate({ | ||
fromCurrent: true, | ||
@@ -1358,3 +1303,3 @@ replace: true, | ||
from, | ||
to = '.', | ||
to = '', | ||
search, | ||
@@ -1393,15 +1338,20 @@ hash, | ||
const next = this.buildNext(location); | ||
if (opts?.pending) { | ||
if (!this.store.state.pendingLocation) { | ||
return false; | ||
} | ||
return matchPathname(this.basepath, this.store.state.pendingLocation.pathname, { | ||
...opts, | ||
to: next.pathname | ||
}); | ||
if (opts?.pending && this.state.status !== 'pending') { | ||
return false; | ||
} | ||
return matchPathname(this.basepath, this.store.state.currentLocation.pathname, { | ||
const baseLocation = opts?.pending ? this.state.location : this.state.resolvedLocation; | ||
if (!baseLocation) { | ||
return false; | ||
} | ||
const match = matchPathname(this.basepath, baseLocation.pathname, { | ||
...opts, | ||
to: next.pathname | ||
}); | ||
if (!match) { | ||
return false; | ||
} | ||
if (opts?.includeSearch ?? true) { | ||
return partialDeepEqual(baseLocation.search, next.search) ? match : false; | ||
} | ||
return match; | ||
}; | ||
@@ -1418,4 +1368,2 @@ buildLink = ({ | ||
preload, | ||
preloadMaxAge: userPreloadMaxAge, | ||
preloadGcMaxAge: userPreloadGcMaxAge, | ||
preloadDelay: userPreloadDelay, | ||
@@ -1450,13 +1398,12 @@ disabled | ||
// Compare path/hash for matches | ||
const pathIsEqual = this.store.state.currentLocation.pathname === next.pathname; | ||
const currentPathSplit = this.store.state.currentLocation.pathname.split('/'); | ||
const currentPathSplit = this.state.location.pathname.split('/'); | ||
const nextPathSplit = next.pathname.split('/'); | ||
const pathIsFuzzyEqual = nextPathSplit.every((d, i) => d === currentPathSplit[i]); | ||
const hashIsEqual = this.store.state.currentLocation.hash === next.hash; | ||
// Combine the matches based on user options | ||
const pathTest = activeOptions?.exact ? pathIsEqual : pathIsFuzzyEqual; | ||
const hashTest = activeOptions?.includeHash ? hashIsEqual : true; | ||
const pathTest = activeOptions?.exact ? this.state.location.pathname === next.pathname : pathIsFuzzyEqual; | ||
const hashTest = activeOptions?.includeHash ? this.state.location.hash === next.hash : true; | ||
const searchTest = activeOptions?.includeSearch ?? true ? partialDeepEqual(this.state.location.search, next.search) : true; | ||
// The final "active" test | ||
const isActive = pathTest && hashTest; | ||
const isActive = pathTest && hashTest && searchTest; | ||
@@ -1467,5 +1414,2 @@ // The click handler | ||
e.preventDefault(); | ||
if (pathIsEqual && !search && !hash) { | ||
this.invalidateRoute(nextOpts); | ||
} | ||
@@ -1480,6 +1424,3 @@ // All is well? Navigate! | ||
if (preload) { | ||
this.preloadRoute(nextOpts, { | ||
maxAge: userPreloadMaxAge, | ||
gcMaxAge: userPreloadGcMaxAge | ||
}).catch(err => { | ||
this.preloadRoute(nextOpts).catch(err => { | ||
console.warn(err); | ||
@@ -1490,2 +1431,8 @@ console.warn('Error preloading route! ☝️'); | ||
}; | ||
const handleTouchStart = e => { | ||
this.preloadRoute(nextOpts).catch(err => { | ||
console.warn(err); | ||
console.warn('Error preloading route! ☝️'); | ||
}); | ||
}; | ||
const handleEnter = e => { | ||
@@ -1499,6 +1446,3 @@ const target = e.target || {}; | ||
target.preloadTimeout = null; | ||
this.preloadRoute(nextOpts, { | ||
maxAge: userPreloadMaxAge, | ||
gcMaxAge: userPreloadGcMaxAge | ||
}).catch(err => { | ||
this.preloadRoute(nextOpts).catch(err => { | ||
console.warn(err); | ||
@@ -1524,2 +1468,3 @@ console.warn('Error preloading route! ☝️'); | ||
handleLeave, | ||
handleTouchStart, | ||
isActive, | ||
@@ -1531,93 +1476,144 @@ disabled | ||
return { | ||
state: { | ||
...pick(this.store.state, ['latestLocation', 'currentLocation', 'status', 'lastUpdated']), | ||
currentMatches: this.store.state.currentMatches.map(match => ({ | ||
id: match.id, | ||
state: { | ||
...pick(match.store.state, ['status', 'routeLoaderData', 'invalidAt', 'invalid']) | ||
} | ||
})) | ||
}, | ||
context: this.options.context | ||
state: pick(this.state, ['location', 'status', 'lastUpdated']) | ||
}; | ||
}; | ||
hydrate = dehydratedRouter => { | ||
this.store.setState(s => { | ||
// Update the context TODO: make this part of state? | ||
this.options.context = dehydratedRouter.context; | ||
// Match the routes | ||
const currentMatches = this.matchRoutes(dehydratedRouter.state.latestLocation.pathname, { | ||
strictParseParams: true | ||
}); | ||
currentMatches.forEach((match, index) => { | ||
const dehydratedMatch = dehydratedRouter.state.currentMatches[index]; | ||
invariant(dehydratedMatch && dehydratedMatch.id === match.id, 'Oh no! There was a hydration mismatch when attempting to rethis.store the state of the router! 😬'); | ||
match.store.setState(s => { | ||
Object.assign(s, dehydratedMatch.state); | ||
}); | ||
match.setLoaderData(dehydratedMatch.state.routeLoaderData); | ||
}); | ||
currentMatches.forEach(match => match.__validate()); | ||
Object.assign(s, { | ||
...dehydratedRouter.state, | ||
currentMatches | ||
}); | ||
hydrate = async __do_not_use_server_ctx => { | ||
let _ctx = __do_not_use_server_ctx; | ||
// Client hydrates from window | ||
if (typeof document !== 'undefined') { | ||
_ctx = window.__TSR_DEHYDRATED__; | ||
} | ||
invariant(_ctx, 'Expected to find a __TSR_DEHYDRATED__ property on window... but we did not. Did you forget to render <DehydrateRouter /> in your app?'); | ||
const ctx = _ctx; | ||
this.dehydratedData = ctx.payload; | ||
this.options.hydrate?.(ctx.payload); | ||
this.__store.setState(s => { | ||
return { | ||
...s, | ||
...ctx.router.state, | ||
resolvedLocation: ctx.router.state.location | ||
}; | ||
}); | ||
await this.load(); | ||
return; | ||
}; | ||
getLoader = opts => { | ||
const id = opts.from || '/'; | ||
const route = this.getRoute(id); | ||
if (!route) return undefined; | ||
let loader = this.store.state.loaders[id] || (() => { | ||
this.store.setState(s => { | ||
s.loaders[id] = { | ||
pending: [], | ||
fetch: async loaderContext => { | ||
if (!route) { | ||
return; | ||
} | ||
const loaderState = { | ||
loadedAt: Date.now(), | ||
loaderContext | ||
}; | ||
this.store.setState(s => { | ||
s.loaders[id].current = loaderState; | ||
s.loaders[id].latest = loaderState; | ||
s.loaders[id].pending.push(loaderState); | ||
}); | ||
try { | ||
return await route.options.loader?.(loaderContext); | ||
} finally { | ||
this.store.setState(s => { | ||
s.loaders[id].pending = s.loaders[id].pending.filter(d => d !== loaderState); | ||
}); | ||
} | ||
} | ||
}; | ||
injectedHtml = []; | ||
injectHtml = async html => { | ||
this.injectedHtml.push(html); | ||
}; | ||
dehydrateData = (key, getData) => { | ||
if (typeof document === 'undefined') { | ||
const strKey = typeof key === 'string' ? key : JSON.stringify(key); | ||
this.injectHtml(async () => { | ||
const id = `__TSR_DEHYDRATED__${strKey}`; | ||
const data = typeof getData === 'function' ? await getData() : getData; | ||
return `<script id='${id}' suppressHydrationWarning>window["__TSR_DEHYDRATED__${escapeJSON(strKey)}"] = ${JSON.stringify(data)} | ||
;(() => { | ||
var el = document.getElementById('${id}') | ||
el.parentElement.removeChild(el) | ||
})() | ||
</script>`; | ||
}); | ||
return this.store.state.loaders[id]; | ||
})(); | ||
return loader; | ||
return () => this.hydrateData(key); | ||
} | ||
return () => undefined; | ||
}; | ||
#buildRouteTree = rootRouteConfig => { | ||
const recurseRoutes = (routeConfigs, parent) => { | ||
return routeConfigs.map((routeConfig, i) => { | ||
const routeOptions = routeConfig.options; | ||
const route = new Route(routeConfig, routeOptions, i, parent, this); | ||
hydrateData = key => { | ||
if (typeof document !== 'undefined') { | ||
const strKey = typeof key === 'string' ? key : JSON.stringify(key); | ||
return window[`__TSR_DEHYDRATED__${strKey}`]; | ||
} | ||
return undefined; | ||
}; | ||
// resolveMatchPromise = (matchId: string, key: string, value: any) => { | ||
// this.state.matches | ||
// .find((d) => d.id === matchId) | ||
// ?.__promisesByKey[key]?.resolve(value) | ||
// } | ||
#buildRouteTree = routeTree => { | ||
this.routeTree = routeTree; | ||
this.routesById = {}; | ||
this.routesByPath = {}; | ||
this.flatRoutes = []; | ||
const recurseRoutes = routes => { | ||
routes.forEach((route, i) => { | ||
route.init({ | ||
originalIndex: i, | ||
router: this | ||
}); | ||
const existingRoute = this.routesById[route.id]; | ||
if (existingRoute) { | ||
if (process.env.NODE_ENV !== 'production') { | ||
console.warn(`Duplicate routes found with id: ${String(route.id)}`, this.routesById, route); | ||
invariant(!existingRoute, `Duplicate routes found with id: ${String(route.id)}`); | ||
this.routesById[route.id] = route; | ||
if (!route.isRoot && route.path) { | ||
const trimmedFullPath = trimPathRight(route.fullPath); | ||
if (!this.routesByPath[trimmedFullPath] || route.fullPath.endsWith('/')) { | ||
this.routesByPath[trimmedFullPath] = route; | ||
} | ||
throw new Error(); | ||
} | ||
this.routesById[route.id] = route; | ||
const children = routeConfig.children; | ||
route.childRoutes = children.length ? recurseRoutes(children, route) : undefined; | ||
return route; | ||
const children = route.children; | ||
if (children?.length) { | ||
recurseRoutes(children); | ||
} | ||
}); | ||
}; | ||
const routes = recurseRoutes([rootRouteConfig]); | ||
return routes[0]; | ||
recurseRoutes([routeTree]); | ||
this.flatRoutes = Object.values(this.routesByPath).map((d, i) => { | ||
const trimmed = trimPath(d.fullPath); | ||
const parsed = parsePathname(trimmed); | ||
while (parsed.length > 1 && parsed[0]?.value === '/') { | ||
parsed.shift(); | ||
} | ||
const score = parsed.map(d => { | ||
if (d.type === 'param') { | ||
return 0.5; | ||
} | ||
if (d.type === 'wildcard') { | ||
return 0.25; | ||
} | ||
return 1; | ||
}); | ||
return { | ||
child: d, | ||
trimmed, | ||
parsed, | ||
index: i, | ||
score | ||
}; | ||
}).sort((a, b) => { | ||
let isIndex = a.trimmed === '/' ? 1 : b.trimmed === '/' ? -1 : 0; | ||
if (isIndex !== 0) return isIndex; | ||
const length = Math.min(a.score.length, b.score.length); | ||
// Sort by length of score | ||
if (a.score.length !== b.score.length) { | ||
return b.score.length - a.score.length; | ||
} | ||
// Sort by min available score | ||
for (let i = 0; i < length; i++) { | ||
if (a.score[i] !== b.score[i]) { | ||
return b.score[i] - a.score[i]; | ||
} | ||
} | ||
// Sort by min available parsed value | ||
for (let i = 0; i < length; i++) { | ||
if (a.parsed[i].value !== b.parsed[i].value) { | ||
return a.parsed[i].value > b.parsed[i].value ? 1 : -1; | ||
} | ||
} | ||
// Sort by length of trimmed full path | ||
if (a.trimmed !== b.trimmed) { | ||
return a.trimmed > b.trimmed ? 1 : -1; | ||
} | ||
// Sort by original index | ||
return a.index - b.index; | ||
}).map((d, i) => { | ||
d.child.rank = i; | ||
return d.child; | ||
}); | ||
}; | ||
@@ -1642,12 +1638,7 @@ #parseLocation = previousLocation => { | ||
}; | ||
#onFocus = () => { | ||
this.load(); | ||
}; | ||
#buildLocation = (dest = {}) => { | ||
const fromPathname = dest.fromCurrent ? this.store.state.latestLocation.pathname : dest.from ?? this.store.state.latestLocation.pathname; | ||
let pathname = resolvePath(this.basepath ?? '/', fromPathname, `${dest.to ?? '.'}`); | ||
const fromMatches = this.matchRoutes(this.store.state.latestLocation.pathname, { | ||
strictParseParams: true | ||
}); | ||
const toMatches = this.matchRoutes(pathname); | ||
dest.fromCurrent = dest.fromCurrent ?? dest.to === ''; | ||
const fromPathname = dest.fromCurrent ? this.state.location.pathname : dest.from ?? this.state.location.pathname; | ||
let pathname = resolvePath(this.basepath ?? '/', fromPathname, `${dest.to ?? ''}`); | ||
const fromMatches = this.matchRoutes(this.state.location.pathname, this.state.location.search); | ||
const prevParams = { | ||
@@ -1658,10 +1649,15 @@ ...last(fromMatches)?.params | ||
if (nextParams) { | ||
toMatches.map(d => d.route.options.stringifyParams).filter(Boolean).forEach(fn => { | ||
Object.assign({}, nextParams, fn(nextParams)); | ||
dest.__matches?.map(d => this.getRoute(d.routeId).options.stringifyParams).filter(Boolean).forEach(fn => { | ||
nextParams = { | ||
...nextParams, | ||
...fn(nextParams) | ||
}; | ||
}); | ||
} | ||
pathname = interpolatePath(pathname, nextParams ?? {}); | ||
const preSearchFilters = dest.__matches?.map(match => this.getRoute(match.routeId).options.preSearchFilters ?? []).flat().filter(Boolean) ?? []; | ||
const postSearchFilters = dest.__matches?.map(match => this.getRoute(match.routeId).options.postSearchFilters ?? []).flat().filter(Boolean) ?? []; | ||
// Pre filters first | ||
const preFilteredSearch = dest.__preSearchFilters?.length ? dest.__preSearchFilters?.reduce((prev, next) => next(prev), this.store.state.latestLocation.search) : this.store.state.latestLocation.search; | ||
const preFilteredSearch = preSearchFilters?.length ? preSearchFilters?.reduce((prev, next) => next(prev), this.state.location.search) : this.state.location.search; | ||
@@ -1671,11 +1667,12 @@ // Then the link/navigate function | ||
: dest.search ? functionalUpdate(dest.search, preFilteredSearch) ?? {} // Updater | ||
: dest.__preSearchFilters?.length ? preFilteredSearch // Preserve resolvedFrom filters | ||
: preSearchFilters?.length ? preFilteredSearch // Preserve resolvedFrom filters | ||
: {}; | ||
// Then post filters | ||
const postFilteredSearch = dest.__postSearchFilters?.length ? dest.__postSearchFilters.reduce((prev, next) => next(prev), destSearch) : destSearch; | ||
const search = replaceEqualDeep(this.store.state.latestLocation.search, postFilteredSearch); | ||
const postFilteredSearch = postSearchFilters?.length ? postSearchFilters.reduce((prev, next) => next(prev), destSearch) : destSearch; | ||
const search = replaceEqualDeep(this.state.location.search, postFilteredSearch); | ||
const searchStr = this.options.stringifySearch(search); | ||
let hash = dest.hash === true ? this.store.state.latestLocation.hash : functionalUpdate(dest.hash, this.store.state.latestLocation.hash); | ||
hash = hash ? `#${hash}` : ''; | ||
const hash = dest.hash === true ? this.state.location.hash : functionalUpdate(dest.hash, this.state.location.hash); | ||
const hashStr = hash ? `#${hash}` : ''; | ||
const nextState = dest.state === true ? this.state.location.state : functionalUpdate(dest.state, this.state.location.state); | ||
return { | ||
@@ -1685,9 +1682,9 @@ pathname, | ||
searchStr, | ||
state: this.store.state.latestLocation.state, | ||
state: nextState, | ||
hash, | ||
href: `${pathname}${searchStr}${hash}`, | ||
href: this.history.createHref(`${pathname}${searchStr}${hashStr}`), | ||
key: dest.key | ||
}; | ||
}; | ||
#commitLocation = location => { | ||
#commitLocation = async location => { | ||
const next = this.buildNext(location); | ||
@@ -1700,3 +1697,3 @@ const id = '' + Date.now() + Math.random(); | ||
} | ||
const isSameUrl = this.store.state.latestLocation.href === next.href; | ||
const isSameUrl = this.state.location.href === next.href; | ||
if (isSameUrl && !next.key) { | ||
@@ -1710,13 +1707,77 @@ nextAction = 'replace'; | ||
}); | ||
// this.load(this.#parseLocation(this.store.state.latestLocation)) | ||
return this.navigationPromise = new Promise(resolve => { | ||
const previousNavigationResolve = this.resolveNavigation; | ||
this.resolveNavigation = () => { | ||
previousNavigationResolve(); | ||
resolve(); | ||
}; | ||
}); | ||
return this.latestLoadPromise; | ||
}; | ||
getRouteMatch = id => { | ||
return this.state.matchesById[id]; | ||
}; | ||
setRouteMatch = (id, updater) => { | ||
this.__store.setState(prev => ({ | ||
...prev, | ||
matchesById: { | ||
...prev.matchesById, | ||
[id]: updater(prev.matchesById[id]) | ||
} | ||
})); | ||
}; | ||
setRouteMatchData = (id, updater, opts) => { | ||
const match = this.getRouteMatch(id); | ||
if (!match) return; | ||
const route = this.getRoute(match.routeId); | ||
const updatedAt = opts?.updatedAt ?? Date.now(); | ||
const preloadInvalidAt = updatedAt + (opts?.maxAge ?? route.options.preloadMaxAge ?? this.options.defaultPreloadMaxAge ?? 5000); | ||
const invalidAt = updatedAt + (opts?.maxAge ?? route.options.maxAge ?? this.options.defaultMaxAge ?? Infinity); | ||
this.setRouteMatch(id, s => ({ | ||
...s, | ||
error: undefined, | ||
status: 'success', | ||
isFetching: false, | ||
updatedAt: Date.now(), | ||
loaderData: functionalUpdate(updater, s.loaderData), | ||
preloadInvalidAt, | ||
invalidAt | ||
})); | ||
if (this.state.matches.find(d => d.id === id)) ; | ||
}; | ||
invalidate = async opts => { | ||
if (opts?.matchId) { | ||
this.setRouteMatch(opts.matchId, s => ({ | ||
...s, | ||
invalid: true | ||
})); | ||
const matchIndex = this.state.matches.findIndex(d => d.id === opts.matchId); | ||
const childMatch = this.state.matches[matchIndex + 1]; | ||
if (childMatch) { | ||
return this.invalidate({ | ||
matchId: childMatch.id, | ||
reload: false | ||
}); | ||
} | ||
} else { | ||
this.__store.batch(() => { | ||
Object.values(this.state.matchesById).forEach(match => { | ||
this.setRouteMatch(match.id, s => ({ | ||
...s, | ||
invalid: true | ||
})); | ||
}); | ||
}); | ||
} | ||
if (opts?.reload ?? true) { | ||
return this.reload(); | ||
} | ||
}; | ||
getIsInvalid = opts => { | ||
if (!opts?.matchId) { | ||
return !!this.state.matches.find(d => this.getIsInvalid({ | ||
matchId: d.id, | ||
preload: opts?.preload | ||
})); | ||
} | ||
const match = this.getRouteMatch(opts?.matchId); | ||
if (!match) { | ||
return false; | ||
} | ||
const now = Date.now(); | ||
return match.invalid || (opts?.preload ? match.preloadInvalidAt : match.invalidAt) < now; | ||
}; | ||
} | ||
@@ -1729,14 +1790,11 @@ | ||
status: 'idle', | ||
latestLocation: null, | ||
currentLocation: null, | ||
currentMatches: [], | ||
loaders: {}, | ||
lastUpdated: Date.now(), | ||
matchCache: {}, | ||
get isFetching() { | ||
return this.status === 'loading' || this.currentMatches.some(d => d.store.state.isFetching); | ||
}, | ||
get isPreloading() { | ||
return Object.values(this.matchCache).some(d => d.match.store.state.isFetching && !this.currentMatches.find(dd => dd.id === d.match.id)); | ||
} | ||
isFetching: false, | ||
resolvedLocation: null, | ||
location: null, | ||
matchesById: {}, | ||
matchIds: [], | ||
pendingMatchIds: [], | ||
matches: [], | ||
pendingMatches: [], | ||
lastUpdated: Date.now() | ||
}; | ||
@@ -1747,83 +1805,28 @@ } | ||
} | ||
function linkMatches(matches) { | ||
matches.forEach((match, index) => { | ||
const parent = matches[index - 1]; | ||
if (parent) { | ||
match.__setParentMatch(parent); | ||
} | ||
}); | ||
function redirect(opts) { | ||
opts.isRedirect = true; | ||
return opts; | ||
} | ||
function isRedirect(obj) { | ||
return !!obj?.isRedirect; | ||
} | ||
class SearchParamError extends Error {} | ||
class PathParamError extends Error {} | ||
function escapeJSON(jsonString) { | ||
return jsonString.replace(/\\/g, '\\\\') // Escape backslashes | ||
.replace(/'/g, "\\'") // Escape single quotes | ||
.replace(/"/g, '\\"'); // Escape double quotes | ||
} | ||
// RouterAction is a constrained identify function that takes options: key, action, onSuccess, onError, onSettled, etc | ||
function createAction(options) { | ||
const store = createStore({ | ||
submissions: [] | ||
}, options.debug); | ||
return { | ||
options, | ||
store, | ||
reset: () => { | ||
store.setState(s => { | ||
s.submissions = []; | ||
}); | ||
}, | ||
submit: async payload => { | ||
const submission = { | ||
submittedAt: Date.now(), | ||
status: 'pending', | ||
payload: payload, | ||
invalidate: () => { | ||
setSubmission(s => { | ||
s.isInvalid = true; | ||
}); | ||
}, | ||
getIsLatest: () => store.state.submissions[store.state.submissions.length - 1]?.submittedAt === submission.submittedAt | ||
}; | ||
const setSubmission = updater => { | ||
store.setState(s => { | ||
const a = s.submissions.find(d => d.submittedAt === submission.submittedAt); | ||
invariant(a, 'Could not find submission in store'); | ||
updater(a); | ||
}); | ||
}; | ||
store.setState(s => { | ||
s.submissions.push(submission); | ||
s.submissions.reverse(); | ||
s.submissions = s.submissions.slice(0, options.maxSubmissions ?? 10); | ||
s.submissions.reverse(); | ||
}); | ||
const after = async () => { | ||
options.onEachSettled?.(submission); | ||
if (submission.getIsLatest()) await options.onLatestSettled?.(submission); | ||
}; | ||
try { | ||
const res = await options.action?.(submission.payload); | ||
setSubmission(s => { | ||
s.response = res; | ||
}); | ||
await options.onEachSuccess?.(submission); | ||
if (submission.getIsLatest()) await options.onLatestSuccess?.(submission); | ||
await after(); | ||
setSubmission(s => { | ||
s.status = 'success'; | ||
}); | ||
return res; | ||
} catch (err) { | ||
console.error(err); | ||
setSubmission(s => { | ||
s.error = err; | ||
}); | ||
await options.onEachError?.(submission); | ||
if (submission.getIsLatest()) await options.onLatestError?.(submission); | ||
await after(); | ||
setSubmission(s => { | ||
s.status = 'error'; | ||
}); | ||
throw err; | ||
} | ||
} | ||
// A function that takes an import() argument which is a function and returns a new function that will | ||
// proxy arguments from the caller to the imported function, retaining all type | ||
// information along the way | ||
function lazyFn(fn, key) { | ||
return async (...args) => { | ||
const imported = await fn(); | ||
return imported[key || 'default'](...args); | ||
}; | ||
} | ||
export { Route, RouteMatch, Router, batch, cleanPath, createAction, createBrowserHistory, createHashHistory, createMemoryHistory, createRouteConfig, createStore, decode, defaultFetchServerDataFn, defaultParseSearch, defaultStringifySearch, encode, functionalUpdate, interpolatePath, joinPaths, last, matchByPath, matchPathname, parsePathname, parseSearchWith, pick, replaceEqualDeep, resolvePath, rootRouteId, stringifySearchWith, trackDeep, trimPath, trimPathLeft, trimPathRight, warning }; | ||
export { PathParamError, RootRoute, Route, Router, RouterContext, SearchParamError, cleanPath, componentTypes, createBrowserHistory, createHashHistory, createMemoryHistory, decode, defaultParseSearch, defaultStringifySearch, encode, functionalUpdate, interpolatePath, isPlainObject, isRedirect, joinPaths, last, lazyFn, matchByPath, matchPathname, parsePathname, parseSearchWith, partialDeepEqual, pick, redirect, replaceEqualDeep, resolvePath, rootRouteId, stringifySearchWith, trimPath, trimPathLeft, trimPathRight }; | ||
//# sourceMappingURL=index.js.map |
@@ -14,7 +14,7 @@ { | ||
"name": "tiny-invariant@1.3.1/node_modules/tiny-invariant/dist/esm/tiny-invariant.js", | ||
"uid": "1cd1-34" | ||
"uid": "1efb-41" | ||
}, | ||
{ | ||
"name": "immer@9.0.16/node_modules/immer/dist/immer.esm.mjs", | ||
"uid": "1cd1-48" | ||
"name": "tiny-warning@1.0.3/node_modules/tiny-warning/dist/tiny-warning.esm.js", | ||
"uid": "1efb-43" | ||
} | ||
@@ -24,55 +24,44 @@ ] | ||
{ | ||
"name": "packages/router-core/src", | ||
"name": "packages", | ||
"children": [ | ||
{ | ||
"uid": "1cd1-36", | ||
"name": "history.ts" | ||
"name": "router-core/src", | ||
"children": [ | ||
{ | ||
"uid": "1efb-45", | ||
"name": "history.ts" | ||
}, | ||
{ | ||
"uid": "1efb-47", | ||
"name": "utils.ts" | ||
}, | ||
{ | ||
"uid": "1efb-49", | ||
"name": "path.ts" | ||
}, | ||
{ | ||
"uid": "1efb-51", | ||
"name": "qss.ts" | ||
}, | ||
{ | ||
"uid": "1efb-53", | ||
"name": "route.ts" | ||
}, | ||
{ | ||
"uid": "1efb-57", | ||
"name": "searchParams.ts" | ||
}, | ||
{ | ||
"uid": "1efb-59", | ||
"name": "router.ts" | ||
}, | ||
{ | ||
"uid": "1efb-61", | ||
"name": "index.ts" | ||
} | ||
] | ||
}, | ||
{ | ||
"uid": "1cd1-38", | ||
"name": "utils.ts" | ||
}, | ||
{ | ||
"uid": "1cd1-40", | ||
"name": "path.ts" | ||
}, | ||
{ | ||
"uid": "1cd1-42", | ||
"name": "qss.ts" | ||
}, | ||
{ | ||
"uid": "1cd1-44", | ||
"name": "route.ts" | ||
}, | ||
{ | ||
"uid": "1cd1-46", | ||
"name": "routeConfig.ts" | ||
}, | ||
{ | ||
"uid": "1cd1-50", | ||
"name": "store.ts" | ||
}, | ||
{ | ||
"uid": "1cd1-52", | ||
"name": "interop.ts" | ||
}, | ||
{ | ||
"uid": "1cd1-54", | ||
"name": "routeMatch.ts" | ||
}, | ||
{ | ||
"uid": "1cd1-56", | ||
"name": "searchParams.ts" | ||
}, | ||
{ | ||
"uid": "1cd1-58", | ||
"name": "router.ts" | ||
}, | ||
{ | ||
"uid": "1cd1-60", | ||
"name": "actions.ts" | ||
}, | ||
{ | ||
"uid": "1cd1-62", | ||
"name": "index.ts" | ||
"name": "store/build/esm/index.js", | ||
"uid": "1efb-55" | ||
} | ||
@@ -87,98 +76,74 @@ ] | ||
"nodeParts": { | ||
"1cd1-34": { | ||
"1efb-41": { | ||
"renderedLength": 199, | ||
"gzipLength": 134, | ||
"brotliLength": 0, | ||
"mainUid": "1cd1-33" | ||
"mainUid": "1efb-40" | ||
}, | ||
"1cd1-36": { | ||
"renderedLength": 4236, | ||
"gzipLength": 1085, | ||
"1efb-43": { | ||
"renderedLength": 48, | ||
"gzipLength": 65, | ||
"brotliLength": 0, | ||
"mainUid": "1cd1-35" | ||
"mainUid": "1efb-42" | ||
}, | ||
"1cd1-38": { | ||
"renderedLength": 663, | ||
"gzipLength": 295, | ||
"1efb-45": { | ||
"renderedLength": 6426, | ||
"gzipLength": 1539, | ||
"brotliLength": 0, | ||
"mainUid": "1cd1-37" | ||
"mainUid": "1efb-44" | ||
}, | ||
"1cd1-40": { | ||
"renderedLength": 5601, | ||
"gzipLength": 1328, | ||
"1efb-47": { | ||
"renderedLength": 2821, | ||
"gzipLength": 990, | ||
"brotliLength": 0, | ||
"mainUid": "1cd1-39" | ||
"mainUid": "1efb-46" | ||
}, | ||
"1cd1-42": { | ||
"renderedLength": 1395, | ||
"gzipLength": 558, | ||
"1efb-49": { | ||
"renderedLength": 6028, | ||
"gzipLength": 1423, | ||
"brotliLength": 0, | ||
"mainUid": "1cd1-41" | ||
"mainUid": "1efb-48" | ||
}, | ||
"1cd1-44": { | ||
"renderedLength": 415, | ||
"gzipLength": 208, | ||
"1efb-51": { | ||
"renderedLength": 1371, | ||
"gzipLength": 552, | ||
"brotliLength": 0, | ||
"mainUid": "1cd1-43" | ||
"mainUid": "1efb-50" | ||
}, | ||
"1cd1-46": { | ||
"renderedLength": 1261, | ||
"gzipLength": 478, | ||
"1efb-53": { | ||
"renderedLength": 3416, | ||
"gzipLength": 1024, | ||
"brotliLength": 0, | ||
"mainUid": "1cd1-45" | ||
"mainUid": "1efb-52" | ||
}, | ||
"1cd1-48": { | ||
"renderedLength": 8205, | ||
"gzipLength": 3240, | ||
"1efb-55": { | ||
"renderedLength": 1969, | ||
"gzipLength": 653, | ||
"brotliLength": 0, | ||
"mainUid": "1cd1-47" | ||
"mainUid": "1efb-54" | ||
}, | ||
"1cd1-50": { | ||
"renderedLength": 893, | ||
"gzipLength": 351, | ||
"brotliLength": 0, | ||
"mainUid": "1cd1-49" | ||
}, | ||
"1cd1-52": { | ||
"renderedLength": 5233, | ||
"gzipLength": 1686, | ||
"brotliLength": 0, | ||
"mainUid": "1cd1-51" | ||
}, | ||
"1cd1-54": { | ||
"renderedLength": 7929, | ||
"gzipLength": 2039, | ||
"brotliLength": 0, | ||
"mainUid": "1cd1-53" | ||
}, | ||
"1cd1-56": { | ||
"1efb-57": { | ||
"renderedLength": 1387, | ||
"gzipLength": 483, | ||
"brotliLength": 0, | ||
"mainUid": "1cd1-55" | ||
"mainUid": "1efb-56" | ||
}, | ||
"1cd1-58": { | ||
"renderedLength": 28301, | ||
"gzipLength": 6498, | ||
"1efb-59": { | ||
"renderedLength": 38282, | ||
"gzipLength": 8519, | ||
"brotliLength": 0, | ||
"mainUid": "1cd1-57" | ||
"mainUid": "1efb-58" | ||
}, | ||
"1cd1-60": { | ||
"renderedLength": 2418, | ||
"gzipLength": 642, | ||
"brotliLength": 0, | ||
"mainUid": "1cd1-59" | ||
}, | ||
"1cd1-62": { | ||
"1efb-61": { | ||
"renderedLength": 0, | ||
"gzipLength": 0, | ||
"brotliLength": 0, | ||
"mainUid": "1cd1-61" | ||
"mainUid": "1efb-60" | ||
} | ||
}, | ||
"nodeMetas": { | ||
"1cd1-33": { | ||
"1efb-40": { | ||
"id": "/node_modules/.pnpm/tiny-invariant@1.3.1/node_modules/tiny-invariant/dist/esm/tiny-invariant.js", | ||
"moduleParts": { | ||
"index.production.js": "1cd1-34" | ||
"index.production.js": "1efb-41" | ||
}, | ||
@@ -188,19 +153,28 @@ "imported": [], | ||
{ | ||
"uid": "1cd1-61" | ||
"uid": "1efb-60" | ||
}, | ||
{ | ||
"uid": "1cd1-45" | ||
"uid": "1efb-52" | ||
}, | ||
{ | ||
"uid": "1cd1-57" | ||
}, | ||
"uid": "1efb-58" | ||
} | ||
] | ||
}, | ||
"1efb-42": { | ||
"id": "/node_modules/.pnpm/tiny-warning@1.0.3/node_modules/tiny-warning/dist/tiny-warning.esm.js", | ||
"moduleParts": { | ||
"index.production.js": "1efb-43" | ||
}, | ||
"imported": [], | ||
"importedBy": [ | ||
{ | ||
"uid": "1cd1-59" | ||
"uid": "1efb-60" | ||
} | ||
] | ||
}, | ||
"1cd1-35": { | ||
"1efb-44": { | ||
"id": "/packages/router-core/src/history.ts", | ||
"moduleParts": { | ||
"index.production.js": "1cd1-36" | ||
"index.production.js": "1efb-45" | ||
}, | ||
@@ -210,13 +184,13 @@ "imported": [], | ||
{ | ||
"uid": "1cd1-61" | ||
"uid": "1efb-60" | ||
}, | ||
{ | ||
"uid": "1cd1-57" | ||
"uid": "1efb-58" | ||
} | ||
] | ||
}, | ||
"1cd1-37": { | ||
"1efb-46": { | ||
"id": "/packages/router-core/src/utils.ts", | ||
"moduleParts": { | ||
"index.production.js": "1cd1-38" | ||
"index.production.js": "1efb-47" | ||
}, | ||
@@ -226,20 +200,20 @@ "imported": [], | ||
{ | ||
"uid": "1cd1-61" | ||
"uid": "1efb-60" | ||
}, | ||
{ | ||
"uid": "1cd1-39" | ||
"uid": "1efb-48" | ||
}, | ||
{ | ||
"uid": "1cd1-57" | ||
"uid": "1efb-58" | ||
} | ||
] | ||
}, | ||
"1cd1-39": { | ||
"1efb-48": { | ||
"id": "/packages/router-core/src/path.ts", | ||
"moduleParts": { | ||
"index.production.js": "1cd1-40" | ||
"index.production.js": "1efb-49" | ||
}, | ||
"imported": [ | ||
{ | ||
"uid": "1cd1-37" | ||
"uid": "1efb-46" | ||
} | ||
@@ -249,16 +223,16 @@ ], | ||
{ | ||
"uid": "1cd1-61" | ||
"uid": "1efb-60" | ||
}, | ||
{ | ||
"uid": "1cd1-45" | ||
"uid": "1efb-52" | ||
}, | ||
{ | ||
"uid": "1cd1-57" | ||
"uid": "1efb-58" | ||
} | ||
] | ||
}, | ||
"1cd1-41": { | ||
"1efb-50": { | ||
"id": "/packages/router-core/src/qss.ts", | ||
"moduleParts": { | ||
"index.production.js": "1cd1-42" | ||
"index.production.js": "1efb-51" | ||
}, | ||
@@ -268,35 +242,20 @@ "imported": [], | ||
{ | ||
"uid": "1cd1-61" | ||
"uid": "1efb-60" | ||
}, | ||
{ | ||
"uid": "1cd1-55" | ||
"uid": "1efb-56" | ||
} | ||
] | ||
}, | ||
"1cd1-43": { | ||
"1efb-52": { | ||
"id": "/packages/router-core/src/route.ts", | ||
"moduleParts": { | ||
"index.production.js": "1cd1-44" | ||
"index.production.js": "1efb-53" | ||
}, | ||
"imported": [], | ||
"importedBy": [ | ||
{ | ||
"uid": "1cd1-61" | ||
}, | ||
{ | ||
"uid": "1cd1-57" | ||
} | ||
] | ||
}, | ||
"1cd1-45": { | ||
"id": "/packages/router-core/src/routeConfig.ts", | ||
"moduleParts": { | ||
"index.production.js": "1cd1-46" | ||
}, | ||
"imported": [ | ||
{ | ||
"uid": "1cd1-33" | ||
"uid": "1efb-40" | ||
}, | ||
{ | ||
"uid": "1cd1-39" | ||
"uid": "1efb-48" | ||
} | ||
@@ -306,10 +265,10 @@ ], | ||
{ | ||
"uid": "1cd1-61" | ||
"uid": "1efb-60" | ||
} | ||
] | ||
}, | ||
"1cd1-47": { | ||
"id": "/node_modules/.pnpm/immer@9.0.16/node_modules/immer/dist/immer.esm.mjs", | ||
"1efb-54": { | ||
"id": "/packages/store/build/esm/index.js", | ||
"moduleParts": { | ||
"index.production.js": "1cd1-48" | ||
"index.production.js": "1efb-55" | ||
}, | ||
@@ -319,79 +278,14 @@ "imported": [], | ||
{ | ||
"uid": "1cd1-49" | ||
"uid": "1efb-64" | ||
} | ||
] | ||
}, | ||
"1cd1-49": { | ||
"id": "/packages/router-core/src/store.ts", | ||
"moduleParts": { | ||
"index.production.js": "1cd1-50" | ||
}, | ||
"imported": [ | ||
{ | ||
"uid": "1cd1-47" | ||
} | ||
], | ||
"importedBy": [ | ||
{ | ||
"uid": "1cd1-61" | ||
}, | ||
{ | ||
"uid": "1cd1-53" | ||
}, | ||
{ | ||
"uid": "1cd1-57" | ||
}, | ||
{ | ||
"uid": "1cd1-59" | ||
} | ||
] | ||
}, | ||
"1cd1-51": { | ||
"id": "/packages/router-core/src/interop.ts", | ||
"moduleParts": { | ||
"index.production.js": "1cd1-52" | ||
}, | ||
"imported": [], | ||
"importedBy": [ | ||
{ | ||
"uid": "1cd1-61" | ||
}, | ||
{ | ||
"uid": "1cd1-53" | ||
}, | ||
{ | ||
"uid": "1cd1-57" | ||
} | ||
] | ||
}, | ||
"1cd1-53": { | ||
"id": "/packages/router-core/src/routeMatch.ts", | ||
"moduleParts": { | ||
"index.production.js": "1cd1-54" | ||
}, | ||
"imported": [ | ||
{ | ||
"uid": "1cd1-49" | ||
}, | ||
{ | ||
"uid": "1cd1-51" | ||
} | ||
], | ||
"importedBy": [ | ||
{ | ||
"uid": "1cd1-61" | ||
}, | ||
{ | ||
"uid": "1cd1-57" | ||
} | ||
] | ||
}, | ||
"1cd1-55": { | ||
"1efb-56": { | ||
"id": "/packages/router-core/src/searchParams.ts", | ||
"moduleParts": { | ||
"index.production.js": "1cd1-56" | ||
"index.production.js": "1efb-57" | ||
}, | ||
"imported": [ | ||
{ | ||
"uid": "1cd1-41" | ||
"uid": "1efb-50" | ||
} | ||
@@ -401,41 +295,32 @@ ], | ||
{ | ||
"uid": "1cd1-61" | ||
"uid": "1efb-60" | ||
}, | ||
{ | ||
"uid": "1cd1-57" | ||
"uid": "1efb-58" | ||
} | ||
] | ||
}, | ||
"1cd1-57": { | ||
"1efb-58": { | ||
"id": "/packages/router-core/src/router.ts", | ||
"moduleParts": { | ||
"index.production.js": "1cd1-58" | ||
"index.production.js": "1efb-59" | ||
}, | ||
"imported": [ | ||
{ | ||
"uid": "1cd1-33" | ||
"uid": "1efb-64" | ||
}, | ||
{ | ||
"uid": "1cd1-39" | ||
"uid": "1efb-40" | ||
}, | ||
{ | ||
"uid": "1cd1-43" | ||
"uid": "1efb-48" | ||
}, | ||
{ | ||
"uid": "1cd1-53" | ||
"uid": "1efb-56" | ||
}, | ||
{ | ||
"uid": "1cd1-55" | ||
"uid": "1efb-46" | ||
}, | ||
{ | ||
"uid": "1cd1-49" | ||
}, | ||
{ | ||
"uid": "1cd1-37" | ||
}, | ||
{ | ||
"uid": "1cd1-51" | ||
}, | ||
{ | ||
"uid": "1cd1-35" | ||
"uid": "1efb-44" | ||
} | ||
@@ -445,78 +330,44 @@ ], | ||
{ | ||
"uid": "1cd1-61" | ||
"uid": "1efb-60" | ||
} | ||
] | ||
}, | ||
"1cd1-59": { | ||
"id": "/packages/router-core/src/actions.ts", | ||
"moduleParts": { | ||
"index.production.js": "1cd1-60" | ||
}, | ||
"imported": [ | ||
{ | ||
"uid": "1cd1-33" | ||
}, | ||
{ | ||
"uid": "1cd1-49" | ||
} | ||
], | ||
"importedBy": [ | ||
{ | ||
"uid": "1cd1-61" | ||
} | ||
] | ||
}, | ||
"1cd1-61": { | ||
"1efb-60": { | ||
"id": "/packages/router-core/src/index.ts", | ||
"moduleParts": { | ||
"index.production.js": "1cd1-62" | ||
"index.production.js": "1efb-61" | ||
}, | ||
"imported": [ | ||
{ | ||
"uid": "1cd1-33" | ||
"uid": "1efb-40" | ||
}, | ||
{ | ||
"uid": "1cd1-35" | ||
"uid": "1efb-42" | ||
}, | ||
{ | ||
"uid": "1cd1-63" | ||
"uid": "1efb-44" | ||
}, | ||
{ | ||
"uid": "1cd1-64" | ||
"uid": "1efb-62" | ||
}, | ||
{ | ||
"uid": "1cd1-39" | ||
"uid": "1efb-48" | ||
}, | ||
{ | ||
"uid": "1cd1-41" | ||
"uid": "1efb-50" | ||
}, | ||
{ | ||
"uid": "1cd1-43" | ||
"uid": "1efb-52" | ||
}, | ||
{ | ||
"uid": "1cd1-45" | ||
"uid": "1efb-63" | ||
}, | ||
{ | ||
"uid": "1cd1-65" | ||
"uid": "1efb-58" | ||
}, | ||
{ | ||
"uid": "1cd1-53" | ||
"uid": "1efb-56" | ||
}, | ||
{ | ||
"uid": "1cd1-57" | ||
}, | ||
{ | ||
"uid": "1cd1-55" | ||
}, | ||
{ | ||
"uid": "1cd1-37" | ||
}, | ||
{ | ||
"uid": "1cd1-51" | ||
}, | ||
{ | ||
"uid": "1cd1-59" | ||
}, | ||
{ | ||
"uid": "1cd1-49" | ||
"uid": "1efb-46" | ||
} | ||
@@ -527,4 +378,4 @@ ], | ||
}, | ||
"1cd1-63": { | ||
"id": "/packages/router-core/src/frameworks.ts", | ||
"1efb-62": { | ||
"id": "/packages/router-core/src/link.ts", | ||
"moduleParts": {}, | ||
@@ -534,8 +385,8 @@ "imported": [], | ||
{ | ||
"uid": "1cd1-61" | ||
"uid": "1efb-60" | ||
} | ||
] | ||
}, | ||
"1cd1-64": { | ||
"id": "/packages/router-core/src/link.ts", | ||
"1efb-63": { | ||
"id": "/packages/router-core/src/routeInfo.ts", | ||
"moduleParts": {}, | ||
@@ -545,15 +396,33 @@ "imported": [], | ||
{ | ||
"uid": "1cd1-61" | ||
"uid": "1efb-60" | ||
} | ||
] | ||
}, | ||
"1cd1-65": { | ||
"id": "/packages/router-core/src/routeInfo.ts", | ||
"1efb-64": { | ||
"id": "/packages/react-store/build/esm/index.js", | ||
"moduleParts": {}, | ||
"imported": [ | ||
{ | ||
"uid": "1efb-65" | ||
}, | ||
{ | ||
"uid": "1efb-54" | ||
} | ||
], | ||
"importedBy": [ | ||
{ | ||
"uid": "1efb-58" | ||
} | ||
] | ||
}, | ||
"1efb-65": { | ||
"id": "use-sync-external-store/shim/with-selector", | ||
"moduleParts": {}, | ||
"imported": [], | ||
"importedBy": [ | ||
{ | ||
"uid": "1cd1-61" | ||
"uid": "1efb-64" | ||
} | ||
] | ||
], | ||
"isExternal": true | ||
} | ||
@@ -560,0 +429,0 @@ }, |
/** | ||
* router-core | ||
* @tanstack/router-core/src/index.ts | ||
* | ||
@@ -12,2 +12,4 @@ * Copyright (c) TanStack | ||
export { default as invariant } from 'tiny-invariant'; | ||
export { default as warning } from 'tiny-warning'; | ||
import { Store } from '@tanstack/react-store'; | ||
@@ -17,7 +19,9 @@ interface RouterHistory { | ||
listen: (cb: () => void) => () => void; | ||
push: (path: string, state: any) => void; | ||
replace: (path: string, state: any) => void; | ||
push: (path: string, state?: any) => void; | ||
replace: (path: string, state?: any) => void; | ||
go: (index: number) => void; | ||
back: () => void; | ||
forward: () => void; | ||
createHref: (href: string) => string; | ||
block: (blockerFn: BlockerFn) => () => void; | ||
} | ||
@@ -33,2 +37,3 @@ interface ParsedPath { | ||
} | ||
type BlockerFn = (retry: () => void, cancel: () => void) => void; | ||
declare function createBrowserHistory(opts?: { | ||
@@ -44,14 +49,2 @@ getHref?: () => string; | ||
interface FrameworkGenerics { | ||
} | ||
type GetFrameworkGeneric<U> = U extends keyof FrameworkGenerics ? FrameworkGenerics[U] : any; | ||
type Store<TState> = { | ||
state: TState; | ||
subscribe: (listener: (next: TState, prev: TState) => void) => () => void; | ||
setState: (updater: (cb: TState) => void) => void; | ||
}; | ||
declare function createStore<TState>(initialState: TState, debug?: boolean): Store<TState>; | ||
declare function batch(cb: () => void): void; | ||
type NoInfer<T> = [T][T extends any ? 0 : never]; | ||
@@ -64,5 +57,5 @@ type IsAny<T, Y, N> = 1 extends 0 & T ? Y : N; | ||
type PickUnsafe<T, K> = K extends keyof T ? Pick<T, K> : never; | ||
type PickExtra<T, K> = Expand<{ | ||
type PickExtra<T, K> = { | ||
[TKey in keyof K as string extends TKey ? never : TKey extends keyof T ? never : TKey]: K[TKey]; | ||
}>; | ||
}; | ||
type PickRequired<T> = { | ||
@@ -75,2 +68,11 @@ [K in keyof T as undefined extends T[K] ? never : K]: T[K]; | ||
type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => any ? I : never; | ||
type Compute<T> = { | ||
[K in keyof T]: T[K]; | ||
} | never; | ||
type AllKeys<T> = T extends any ? keyof T : never; | ||
type MergeUnion<T, Keys extends keyof T = keyof T> = Compute<{ | ||
[K in Keys]: T[Keys]; | ||
} & { | ||
[K in AllKeys<T>]?: T extends any ? K extends keyof T ? T[K] : never : never; | ||
}>; | ||
type Values<O> = O[ValueKeys<O>]; | ||
@@ -91,15 +93,27 @@ type ValueKeys<O> = Extract<keyof O, PropertyKey>; | ||
declare function last<T>(arr: T[]): T | undefined; | ||
declare function warning(cond: any, message: string): cond is true; | ||
declare function functionalUpdate<TResult>(updater: Updater<TResult>, previous: TResult): TResult; | ||
declare function pick<T, K extends keyof T>(parent: T, keys: K[]): Pick<T, K>; | ||
/** | ||
* This function returns `a` if `b` is deeply equal. | ||
* If not, it will replace any deeply equal children of `b` with those of `a`. | ||
* This can be used for structural sharing between immutable JSON values for example. | ||
* Do not use this with signals | ||
*/ | ||
declare function replaceEqualDeep<T>(prev: any, _next: T): T; | ||
declare function isPlainObject(o: any): boolean; | ||
declare function partialDeepEqual(a: any, b: any): boolean; | ||
interface RegisterRouter { | ||
declare global { | ||
interface Window { | ||
__TSR_DEHYDRATED__?: HydrationCtx; | ||
} | ||
} | ||
interface Register { | ||
} | ||
type AnyRouter = Router<any, any, any>; | ||
type RegisteredRouter = RegisterRouter extends { | ||
router: Router<infer TRouteConfig, infer TAllRouteInfo, infer TRouterContext>; | ||
} ? Router<TRouteConfig, TAllRouteInfo, TRouterContext> : Router; | ||
type RegisteredAllRouteInfo = RegisterRouter extends { | ||
router: Router<infer TRouteConfig, infer TAllRouteInfo, infer TRouterContext>; | ||
} ? TAllRouteInfo : AnyAllRouteInfo; | ||
type RegisteredRouterPair = Register extends { | ||
router: infer TRouter extends AnyRouter; | ||
} ? [TRouter, TRouter['types']['RoutesInfo']] : [Router, AnyRoutesInfo]; | ||
type RegisteredRouter = RegisteredRouterPair[0]; | ||
type RegisteredRoutesInfo = RegisteredRouterPair[1]; | ||
interface LocationState { | ||
@@ -124,22 +138,52 @@ } | ||
type SearchParser = (searchStr: string) => Record<string, any>; | ||
type FilterRoutesFn = <TRoute extends Route<any, RouteInfo>>(routeConfigs: TRoute[]) => TRoute[]; | ||
interface RouterOptions<TRouteConfig extends AnyRouteConfig, TRouterContext> { | ||
type HydrationCtx = { | ||
router: DehydratedRouter; | ||
payload: Record<string, any>; | ||
}; | ||
interface RouteMatch<TRoutesInfo extends AnyRoutesInfo = DefaultRoutesInfo, TRoute extends AnyRoute = Route> { | ||
id: string; | ||
key?: string; | ||
routeId: string; | ||
pathname: string; | ||
params: TRoute['__types']['allParams']; | ||
status: 'idle' | 'pending' | 'success' | 'error'; | ||
isFetching: boolean; | ||
invalid: boolean; | ||
error: unknown; | ||
paramsError: unknown; | ||
searchError: unknown; | ||
updatedAt: number; | ||
invalidAt: number; | ||
preloadInvalidAt: number; | ||
loaderData: TRoute['__types']['loader']; | ||
loadPromise?: Promise<void>; | ||
__resolveLoadPromise?: () => void; | ||
routeContext: TRoute['__types']['routeContext']; | ||
context: TRoute['__types']['context']; | ||
routeSearch: TRoute['__types']['searchSchema']; | ||
search: TRoutesInfo['fullSearchSchema'] & TRoute['__types']['fullSearchSchema']; | ||
fetchedAt: number; | ||
abortController: AbortController; | ||
} | ||
type AnyRouteMatch = RouteMatch<AnyRoutesInfo, AnyRoute>; | ||
type RouterContextOptions<TRouteTree extends AnyRoute> = AnyContext extends TRouteTree['__types']['routerContext'] ? { | ||
context?: TRouteTree['__types']['routerContext']; | ||
} : { | ||
context: TRouteTree['__types']['routerContext']; | ||
}; | ||
interface RouterOptions<TRouteTree extends AnyRoute, TDehydrated extends Record<string, any>> { | ||
history?: RouterHistory; | ||
stringifySearch?: SearchSerializer; | ||
parseSearch?: SearchParser; | ||
filterRoutes?: FilterRoutesFn; | ||
defaultPreload?: false | 'intent'; | ||
defaultPreloadDelay?: number; | ||
defaultComponent?: RegisteredRouteComponent<RouteProps<unknown, AnySearchSchema, AnyPathParams, AnyContext, AnyContext>>; | ||
defaultErrorComponent?: RegisteredRouteErrorComponent<RouteProps<unknown, AnySearchSchema, AnyPathParams, AnyContext, AnyContext>>; | ||
defaultPendingComponent?: RegisteredRouteComponent<RouteProps<unknown, AnySearchSchema, AnyPathParams, AnyContext, AnyContext>>; | ||
defaultMaxAge?: number; | ||
defaultGcMaxAge?: number; | ||
defaultPreloadMaxAge?: number; | ||
defaultPreloadGcMaxAge?: number; | ||
defaultPreloadDelay?: number; | ||
defaultComponent?: GetFrameworkGeneric<'Component'>; | ||
defaultErrorComponent?: GetFrameworkGeneric<'ErrorComponent'>; | ||
defaultPendingComponent?: GetFrameworkGeneric<'Component'>; | ||
defaultLoaderMaxAge?: number; | ||
defaultLoaderGcMaxAge?: number; | ||
caseSensitive?: boolean; | ||
routeConfig?: TRouteConfig; | ||
routeTree?: TRouteTree; | ||
basepath?: string; | ||
useServerData?: boolean; | ||
Router?: (router: AnyRouter) => void; | ||
createRoute?: (opts: { | ||
@@ -149,46 +193,22 @@ route: AnyRoute; | ||
}) => void; | ||
context?: TRouterContext; | ||
loadComponent?: (component: GetFrameworkGeneric<'Component'>) => Promise<GetFrameworkGeneric<'Component'>>; | ||
onRouteChange?: () => void; | ||
fetchServerDataFn?: FetchServerDataFn; | ||
context?: TRouteTree['__types']['routerContext']; | ||
Wrap?: React.ComponentType<{ | ||
children: React.ReactNode; | ||
dehydratedState?: TDehydrated; | ||
}>; | ||
dehydrate?: () => TDehydrated; | ||
hydrate?: (dehydrated: TDehydrated) => void; | ||
} | ||
type FetchServerDataFn = (ctx: { | ||
router: AnyRouter; | ||
routeMatch: RouteMatch; | ||
}) => Promise<any>; | ||
interface Loader<TFullSearchSchema extends AnySearchSchema = {}, TAllParams extends AnyPathParams = {}, TRouteLoaderData = AnyLoaderData> { | ||
fetch: keyof PickRequired<TFullSearchSchema> extends never ? keyof TAllParams extends never ? (loaderContext: { | ||
signal?: AbortSignal; | ||
}) => Promise<TRouteLoaderData> : (loaderContext: { | ||
params: TAllParams; | ||
search?: TFullSearchSchema; | ||
signal?: AbortSignal; | ||
}) => Promise<TRouteLoaderData> : keyof TAllParams extends never ? (loaderContext: { | ||
search: TFullSearchSchema; | ||
params: TAllParams; | ||
signal?: AbortSignal; | ||
}) => Promise<TRouteLoaderData> : (loaderContext: { | ||
search: TFullSearchSchema; | ||
signal?: AbortSignal; | ||
}) => Promise<TRouteLoaderData>; | ||
current?: LoaderState<TFullSearchSchema, TAllParams>; | ||
latest?: LoaderState<TFullSearchSchema, TAllParams>; | ||
pending: LoaderState<TFullSearchSchema, TAllParams>[]; | ||
} | ||
interface LoaderState<TFullSearchSchema extends AnySearchSchema = {}, TAllParams extends AnyPathParams = {}> { | ||
loadedAt: number; | ||
loaderContext: LoaderContext<TFullSearchSchema, TAllParams>; | ||
} | ||
interface RouterStore<TSearchObj extends AnySearchSchema = {}, TState extends LocationState = LocationState> { | ||
status: 'idle' | 'loading'; | ||
latestLocation: ParsedLocation<TSearchObj, TState>; | ||
currentMatches: RouteMatch[]; | ||
currentLocation: ParsedLocation<TSearchObj, TState>; | ||
pendingMatches?: RouteMatch[]; | ||
pendingLocation?: ParsedLocation<TSearchObj, TState>; | ||
interface RouterState<TRoutesInfo extends AnyRoutesInfo = AnyRoutesInfo, TState extends LocationState = LocationState> { | ||
status: 'idle' | 'pending'; | ||
isFetching: boolean; | ||
matchesById: Record<string, RouteMatch<TRoutesInfo, TRoutesInfo['routeIntersection']>>; | ||
matchIds: string[]; | ||
pendingMatchIds: string[]; | ||
matches: RouteMatch<TRoutesInfo, TRoutesInfo['routeIntersection']>[]; | ||
pendingMatches: RouteMatch<TRoutesInfo, TRoutesInfo['routeIntersection']>[]; | ||
location: ParsedLocation<TRoutesInfo['fullSearchSchema'], TState>; | ||
resolvedLocation: ParsedLocation<TRoutesInfo['fullSearchSchema'], TState>; | ||
lastUpdated: number; | ||
loaders: Record<string, Loader>; | ||
isFetching: boolean; | ||
isPreloading: boolean; | ||
matchCache: Record<string, MatchCacheEntry>; | ||
} | ||
@@ -205,9 +225,4 @@ type ListenerFn = () => void; | ||
fromCurrent?: boolean; | ||
__preSearchFilters?: SearchFilter<any>[]; | ||
__postSearchFilters?: SearchFilter<any>[]; | ||
__matches?: AnyRouteMatch[]; | ||
} | ||
type MatchCacheEntry = { | ||
gc: number; | ||
match: RouteMatch; | ||
}; | ||
interface MatchLocation { | ||
@@ -223,180 +238,196 @@ to?: string | number | null; | ||
caseSensitive?: boolean; | ||
includeSearch?: boolean; | ||
fuzzy?: boolean; | ||
} | ||
interface DehydratedRouterState extends Pick<RouterStore, 'status' | 'latestLocation' | 'currentLocation' | 'lastUpdated'> { | ||
currentMatches: DehydratedRouteMatch[]; | ||
interface DehydratedRouterState extends Pick<RouterState, 'status' | 'location' | 'lastUpdated'> { | ||
} | ||
interface DehydratedRouter<TRouterContext = unknown> { | ||
interface DehydratedRouter { | ||
state: DehydratedRouterState; | ||
context: TRouterContext; | ||
} | ||
type MatchCache = Record<string, MatchCacheEntry>; | ||
interface DehydratedRouteMatch { | ||
id: string; | ||
state: Pick<RouteMatchStore<any, any>, 'status' | 'routeLoaderData' | 'invalid' | 'invalidAt'>; | ||
} | ||
interface RouterContext { | ||
} | ||
declare const defaultFetchServerDataFn: FetchServerDataFn; | ||
declare class Router<TRouteConfig extends AnyRouteConfig = RouteConfig, TAllRouteInfo extends AnyAllRouteInfo = AllRouteInfo<TRouteConfig>, TRouterContext = unknown> { | ||
type RouterConstructorOptions<TRouteTree extends AnyRoute, TDehydrated extends Record<string, any>> = Omit<RouterOptions<TRouteTree, TDehydrated>, 'context'> & RouterContextOptions<TRouteTree>; | ||
declare const componentTypes: readonly ["component", "errorComponent", "pendingComponent"]; | ||
declare class Router<TRouteTree extends AnyRoute = AnyRoute, TRoutesInfo extends AnyRoutesInfo = RoutesInfo<TRouteTree>, TDehydrated extends Record<string, any> = Record<string, any>> { | ||
#private; | ||
types: { | ||
RouteConfig: TRouteConfig; | ||
AllRouteInfo: TAllRouteInfo; | ||
RootRoute: TRouteTree; | ||
RoutesInfo: TRoutesInfo; | ||
}; | ||
options: PickAsRequired<RouterOptions<TRouteConfig, TRouterContext>, 'stringifySearch' | 'parseSearch' | 'context'>; | ||
options: PickAsRequired<RouterOptions<TRouteTree, TDehydrated>, 'stringifySearch' | 'parseSearch' | 'context'>; | ||
history: RouterHistory; | ||
basepath: string; | ||
routeTree: Route<TAllRouteInfo, RouteInfo>; | ||
routesById: RoutesById<TAllRouteInfo>; | ||
routeTree: RootRoute; | ||
routesById: RoutesById<TRoutesInfo>; | ||
routesByPath: RoutesByPath<TRoutesInfo>; | ||
flatRoutes: TRoutesInfo['routesByFullPath'][keyof TRoutesInfo['routesByFullPath']][]; | ||
navigateTimeout: undefined | Timeout; | ||
nextAction: undefined | 'push' | 'replace'; | ||
navigationPromise: undefined | Promise<void>; | ||
store: Store<RouterStore<TAllRouteInfo['fullSearchSchema']>>; | ||
startedLoadingAt: number; | ||
resolveNavigation: () => void; | ||
constructor(options?: RouterOptions<TRouteConfig, TRouterContext>); | ||
__store: Store<RouterState<TRoutesInfo>>; | ||
state: RouterState<TRoutesInfo>; | ||
dehydratedData?: TDehydrated; | ||
constructor(options: RouterConstructorOptions<TRouteTree, TDehydrated>); | ||
reset: () => void; | ||
mount: () => () => void; | ||
update: <TRouteConfig_1 extends RouteConfig<string, string, string, string, AnyLoaderData, AnyLoaderData, {}, AnyLoaderData, {}, {}, {}, {}, {}, {}, unknown> = RouteConfig<string, string, string, string, AnyLoaderData, AnyLoaderData, {}, AnyLoaderData, {}, {}, {}, {}, {}, {}, unknown>, TAllRouteInfo_1 extends AnyAllRouteInfo = AllRouteInfo<TRouteConfig_1>, TRouterContext_1 = unknown>(opts?: RouterOptions<TRouteConfig_1, TRouterContext_1> | undefined) => Router<TRouteConfig_1, TAllRouteInfo_1, TRouterContext_1>; | ||
buildNext: (opts: BuildNextOptions) => ParsedLocation<{}, LocationState>; | ||
mount: () => void; | ||
update: (opts?: RouterOptions<any, any>) => this; | ||
buildNext: (opts: BuildNextOptions) => ParsedLocation; | ||
cancelMatches: () => void; | ||
load: (next?: ParsedLocation) => Promise<void>; | ||
cleanMatchCache: () => void; | ||
getRoute: <TId extends keyof TAllRouteInfo["routeInfoById"]>(id: TId) => Route<TAllRouteInfo, TAllRouteInfo["routeInfoById"][TId], unknown>; | ||
loadRoute: (navigateOpts?: BuildNextOptions) => Promise<RouteMatch[]>; | ||
preloadRoute: (navigateOpts: BuildNextOptions | undefined, loaderOpts: { | ||
cancelMatch: (id: string) => void; | ||
safeLoad: (opts?: { | ||
next?: ParsedLocation; | ||
}) => Promise<void>; | ||
latestLoadPromise: Promise<void>; | ||
load: (opts?: { | ||
next?: ParsedLocation; | ||
throwOnError?: boolean; | ||
}) => Promise<void>; | ||
getRoute: <TId extends keyof TRoutesInfo["routesById"]>(id: TId) => TRoutesInfo["routesById"][TId]; | ||
preloadRoute: (navigateOpts?: BuildNextOptions & { | ||
maxAge?: number; | ||
gcMaxAge?: number; | ||
}) => Promise<RouteMatch<DefaultAllRouteInfo, RouteInfo<string, string, string, "/", {}, {}, {}, {}, {}, {}, {}, {}, {}, {}>>[]>; | ||
matchRoutes: (pathname: string, opts?: { | ||
strictParseParams?: boolean; | ||
}) => RouteMatch<DefaultAllRouteInfo, RouteInfo<string, string, string, "/", {}, {}, {}, {}, {}, {}, {}, {}, {}, {}>>[]; | ||
loadMatches: (resolvedMatches: RouteMatch[], loaderOpts?: { | ||
preload: true; | ||
maxAge: number; | ||
gcMaxAge: number; | ||
} | { | ||
preload?: false; | ||
maxAge?: never; | ||
gcMaxAge?: never; | ||
}) => Promise<RouteMatch<TRoutesInfo, TRoutesInfo["routeIntersection"]>[]>; | ||
cleanMatches: () => void; | ||
matchRoutes: (pathname: string, locationSearch: AnySearchSchema, opts?: { | ||
throwOnError?: boolean; | ||
debug?: boolean; | ||
}) => RouteMatch<TRoutesInfo, TRoutesInfo['routeIntersection']>[]; | ||
loadMatches: (resolvedMatches: AnyRouteMatch[], opts?: { | ||
preload?: boolean; | ||
maxAge?: number; | ||
}) => Promise<void>; | ||
loadMatchData: (routeMatch: RouteMatch<any, any>) => Promise<Record<string, unknown>>; | ||
invalidateRoute: <TFrom extends ValidFromPath<TAllRouteInfo> = "/", TTo extends string = ".">(opts: ToOptions<TAllRouteInfo, TFrom, TTo, ResolveRelativePath<TFrom, NoInfer<TTo>>>) => Promise<void>; | ||
reload: () => void; | ||
reload: () => Promise<void>; | ||
resolvePath: (from: string, path: string) => string; | ||
navigate: <TFrom extends ValidFromPath<TAllRouteInfo> = "/", TTo extends string = ".">({ from, to, search, hash, replace, params, }: NavigateOptions<TAllRouteInfo, TFrom, TTo>) => Promise<void>; | ||
matchRoute: <TFrom extends ValidFromPath<TAllRouteInfo> = "/", TTo extends string = ".">(location: ToOptions<TAllRouteInfo, TFrom, TTo, ResolveRelativePath<TFrom, NoInfer<TTo>>>, opts?: MatchRouteOptions) => false | TAllRouteInfo["routeInfoById"][ResolveRelativePath<TFrom, NoInfer<TTo>>]["allParams"]; | ||
buildLink: <TFrom extends ValidFromPath<TAllRouteInfo> = "/", TTo extends string = ".">({ from, to, search, params, hash, target, replace, activeOptions, preload, preloadMaxAge: userPreloadMaxAge, preloadGcMaxAge: userPreloadGcMaxAge, preloadDelay: userPreloadDelay, disabled, }: LinkOptions<TAllRouteInfo, TFrom, TTo>) => LinkInfo; | ||
dehydrate: () => DehydratedRouter<TRouterContext>; | ||
hydrate: (dehydratedRouter: DehydratedRouter<TRouterContext>) => void; | ||
getLoader: <TFrom extends keyof TAllRouteInfo["routeInfoById"] = "/">(opts: { | ||
from: TFrom; | ||
}) => unknown extends TAllRouteInfo["routeInfoById"][TFrom]["routeLoaderData"] ? Loader<LoaderContext<TAllRouteInfo["routeInfoById"][TFrom]["fullSearchSchema"], TAllRouteInfo["routeInfoById"][TFrom]["allParams"]>, TAllRouteInfo["routeInfoById"][TFrom]["routeLoaderData"], AnyLoaderData> | undefined : Loader<TAllRouteInfo["routeInfoById"][TFrom]["fullSearchSchema"], TAllRouteInfo["routeInfoById"][TFrom]["allParams"], TAllRouteInfo["routeInfoById"][TFrom]["routeLoaderData"]>; | ||
navigate: <TFrom extends string = "/", TTo extends string = "">({ from, to, search, hash, replace, params, }: NavigateOptions<TRoutesInfo, TFrom, TTo>) => Promise<void>; | ||
matchRoute: <TFrom extends string = "/", TTo extends string = "", TResolved extends string = ResolveRelativePath<TFrom, NoInfer<TTo>>>(location: ToOptions<TRoutesInfo, TFrom, TTo, ResolveRelativePath<TFrom, NoInfer<TTo>>>, opts?: MatchRouteOptions) => false | TRoutesInfo["routesById"][TResolved]["__types"]["allParams"]; | ||
buildLink: <TFrom extends string = "/", TTo extends string = "">({ from, to, search, params, hash, target, replace, activeOptions, preload, preloadDelay: userPreloadDelay, disabled, }: LinkOptions<TRoutesInfo, TFrom, TTo>) => LinkInfo; | ||
dehydrate: () => DehydratedRouter; | ||
hydrate: (__do_not_use_server_ctx?: HydrationCtx) => Promise<void>; | ||
injectedHtml: (string | (() => Promise<string> | string))[]; | ||
injectHtml: (html: string | (() => Promise<string> | string)) => Promise<void>; | ||
dehydrateData: <T>(key: any, getData: T | (() => T | Promise<T>)) => () => T | undefined; | ||
hydrateData: <T = unknown>(key: any) => T | undefined; | ||
getRouteMatch: (id: string) => undefined | RouteMatch<TRoutesInfo, AnyRoute>; | ||
setRouteMatch: (id: string, updater: (prev: RouteMatch<TRoutesInfo, AnyRoute>) => RouteMatch<TRoutesInfo, AnyRoute>) => void; | ||
setRouteMatchData: (id: string, updater: (prev: any) => any, opts?: { | ||
updatedAt?: number; | ||
maxAge?: number; | ||
}) => void; | ||
invalidate: (opts?: { | ||
matchId?: string; | ||
reload?: boolean; | ||
}) => Promise<void>; | ||
getIsInvalid: (opts?: { | ||
matchId: string; | ||
preload?: boolean; | ||
}) => boolean; | ||
} | ||
interface RouteMatchStore<TAllRouteInfo extends AnyAllRouteInfo = DefaultAllRouteInfo, TRouteInfo extends AnyRouteInfo = RouteInfo> { | ||
routeSearch: TRouteInfo['searchSchema']; | ||
search: Expand<TAllRouteInfo['fullSearchSchema'] & TRouteInfo['fullSearchSchema']>; | ||
status: 'idle' | 'loading' | 'success' | 'error'; | ||
updatedAt?: number; | ||
error?: unknown; | ||
invalid: boolean; | ||
loaderData: TRouteInfo['loaderData']; | ||
routeLoaderData: TRouteInfo['routeLoaderData']; | ||
isFetching: boolean; | ||
invalidAt: number; | ||
type AnyRedirect = Redirect<any, any, any>; | ||
type Redirect<TRoutesInfo extends AnyRoutesInfo = RegisteredRoutesInfo, TFrom extends TRoutesInfo['routePaths'] = '/', TTo extends string = ''> = NavigateOptions<TRoutesInfo, TFrom, TTo> & { | ||
code?: number; | ||
}; | ||
declare function redirect<TRoutesInfo extends AnyRoutesInfo = RegisteredRoutesInfo, TFrom extends TRoutesInfo['routePaths'] = '/', TTo extends string = ''>(opts: Redirect<TRoutesInfo, TFrom, TTo>): Redirect<TRoutesInfo, TFrom, TTo>; | ||
declare function isRedirect(obj: any): obj is AnyRedirect; | ||
declare class SearchParamError extends Error { | ||
} | ||
declare class RouteMatch<TAllRouteInfo extends AnyAllRouteInfo = DefaultAllRouteInfo, TRouteInfo extends AnyRouteInfo = RouteInfo> { | ||
#private; | ||
route: Route<TAllRouteInfo, TRouteInfo>; | ||
router: Router<TAllRouteInfo['routeConfig'], TAllRouteInfo>; | ||
store: Store<RouteMatchStore<TAllRouteInfo, TRouteInfo>>; | ||
id: string; | ||
pathname: string; | ||
params: TRouteInfo['allParams']; | ||
component: GetFrameworkGeneric<'Component'>; | ||
errorComponent: GetFrameworkGeneric<'ErrorComponent'>; | ||
pendingComponent: GetFrameworkGeneric<'Component'>; | ||
abortController: AbortController; | ||
onLoaderDataListeners: Set<() => void>; | ||
parentMatch?: RouteMatch; | ||
__loadPromise?: Promise<void>; | ||
__onExit?: void | ((matchContext: { | ||
params: TRouteInfo['allParams']; | ||
search: TRouteInfo['fullSearchSchema']; | ||
}) => void); | ||
constructor(router: AnyRouter, route: Route<TAllRouteInfo, TRouteInfo>, opts: { | ||
id: string; | ||
params: TRouteInfo['allParams']; | ||
pathname: string; | ||
}); | ||
setLoaderData: (loaderData: TRouteInfo['routeLoaderData']) => void; | ||
cancel: () => void; | ||
load: (loaderOpts?: { | ||
preload: true; | ||
maxAge: number; | ||
gcMaxAge: number; | ||
} | { | ||
preload?: false; | ||
maxAge?: never; | ||
gcMaxAge?: never; | ||
}) => Promise<void>; | ||
fetch: (opts?: { | ||
maxAge?: number; | ||
}) => Promise<TRouteInfo['routeLoaderData']>; | ||
invalidate: () => Promise<void>; | ||
__hasLoaders: () => boolean; | ||
getIsInvalid: () => boolean; | ||
__setParentMatch: (parentMatch?: RouteMatch) => void; | ||
__onLoaderData: (listener: () => void) => void; | ||
__validate: () => void; | ||
declare class PathParamError extends Error { | ||
} | ||
declare function lazyFn<T extends Record<string, (...args: any[]) => any>, TKey extends keyof T = 'default'>(fn: () => Promise<T>, key?: TKey): (...args: Parameters<T[TKey]>) => Promise<ReturnType<T[TKey]>>; | ||
declare const rootRouteId: "__root__"; | ||
type RootRouteId = typeof rootRouteId; | ||
type AnyLoaderData = {}; | ||
type AnyPathParams = {}; | ||
type AnySearchSchema = {}; | ||
type AnyContext = {}; | ||
interface RouteMeta { | ||
} | ||
type SearchSchemaValidator<TReturn, TParentSchema> = SearchSchemaValidatorObj<TReturn, TParentSchema> | SearchSchemaValidatorFn<TReturn, TParentSchema>; | ||
type SearchSchemaValidatorObj<TReturn, TParentSchema> = { | ||
parse?: SearchSchemaValidatorFn<TReturn, TParentSchema>; | ||
interface RouteContext { | ||
} | ||
interface RegisterRouteComponent<TProps> { | ||
} | ||
interface RegisterRouteErrorComponent<TProps> { | ||
} | ||
type RegisteredRouteComponent<TProps> = RegisterRouteComponent<TProps> extends { | ||
RouteComponent: infer T; | ||
} ? T : (props: TProps) => unknown; | ||
type RegisteredRouteErrorComponent<TProps> = RegisterRouteErrorComponent<TProps> extends { | ||
RouteErrorComponent: infer T; | ||
} ? T : (props: TProps) => unknown; | ||
type PreloadableObj = { | ||
preload?: () => Promise<void>; | ||
}; | ||
type SearchSchemaValidatorFn<TReturn, TParentSchema> = (searchObj: Record<string, unknown>) => {} extends TParentSchema ? TReturn : keyof TReturn extends keyof TParentSchema ? { | ||
error: 'Top level search params cannot be redefined by child routes!'; | ||
keys: keyof TReturn & keyof TParentSchema; | ||
} : TReturn; | ||
type DefinedPathParamWarning = 'Path params cannot be redefined by child routes!'; | ||
type ParentParams<TParentParams> = AnyPathParams extends TParentParams ? {} : { | ||
[Key in keyof TParentParams]?: DefinedPathParamWarning; | ||
type RoutePathOptions<TCustomId, TPath> = { | ||
path: TPath; | ||
} | { | ||
id: TCustomId; | ||
}; | ||
type LoaderFn<TRouteLoaderData extends AnyLoaderData = {}, TFullSearchSchema extends AnySearchSchema = {}, TAllParams extends AnyPathParams = {}> = (loaderContext: LoaderContext<TFullSearchSchema, TAllParams>) => TRouteLoaderData | Promise<TRouteLoaderData>; | ||
interface LoaderContext<TFullSearchSchema extends AnySearchSchema = {}, TAllParams extends AnyPathParams = {}> { | ||
type RoutePathOptionsIntersection<TCustomId, TPath> = UnionToIntersection<RoutePathOptions<TCustomId, TPath>>; | ||
type MetaOptions = keyof PickRequired<RouteMeta> extends never ? { | ||
meta?: RouteMeta; | ||
} : { | ||
meta: RouteMeta; | ||
}; | ||
type AnyRouteProps = RouteProps<any, any, any, any, any>; | ||
type ComponentPropsFromRoute<TRoute> = TRoute extends Route<infer TParentRoute, infer TPath, infer TFullPath, infer TCustomId, infer TId, infer TLoader, infer TSearchSchema, infer TFullSearchSchema, infer TParams, infer TAllParams, infer TParentContext, infer TAllParentContext, infer TRouteContext, infer TContext, infer TRouterContext, infer TChildren, infer TRoutesInfo> ? RouteProps<TLoader, TFullSearchSchema, TAllParams, TRouteContext, TContext> : never; | ||
type ComponentFromRoute<TRoute> = RegisteredRouteComponent<ComponentPropsFromRoute<TRoute>>; | ||
type RouteLoaderFromRoute<TRoute extends AnyRoute> = LoaderFn<TRoute['__types']['loader'], TRoute['__types']['searchSchema'], TRoute['__types']['fullSearchSchema'], TRoute['__types']['allParams'], TRoute['__types']['routeContext'], TRoute['__types']['context']>; | ||
type RouteProps<TLoader = unknown, TFullSearchSchema extends AnySearchSchema = AnySearchSchema, TAllParams = AnyPathParams, TRouteContext = AnyContext, TContext = AnyContext> = { | ||
useMatch: () => RouteMatch<AnyRoutesInfo, AnyRoute>; | ||
useLoader: () => UseLoaderResult<TLoader>; | ||
useSearch: <TStrict extends boolean = true, TSearch = TFullSearchSchema, TSelected = TSearch>(opts?: { | ||
strict?: TStrict; | ||
select?: (search: TSearch) => TSelected; | ||
}) => TStrict extends true ? TSelected : TSelected | undefined; | ||
useParams: <TDefaultSelected = TAllParams, TSelected = TDefaultSelected>(opts?: { | ||
select?: (params: TDefaultSelected) => TSelected; | ||
}) => TSelected; | ||
useContext: <TDefaultSelected = TContext, TSelected = TDefaultSelected>(opts?: { | ||
select?: (context: TDefaultSelected) => TSelected; | ||
}) => TSelected; | ||
useRouteContext: <TDefaultSelected = TRouteContext, TSelected = TDefaultSelected>(opts?: { | ||
select?: (context: TDefaultSelected) => TSelected; | ||
}) => TSelected; | ||
}; | ||
type RouteOptions<TParentRoute extends AnyRoute = AnyRoute, TCustomId extends string = string, TPath extends string = string, TLoader = unknown, TParentSearchSchema extends AnySearchSchema = {}, TSearchSchema extends AnySearchSchema = {}, TFullSearchSchema extends AnySearchSchema = TSearchSchema, TParentParams extends AnyPathParams = AnyPathParams, TParams extends AnyPathParams = Record<ParsePathParams<TPath>, string>, TAllParams extends AnyPathParams = TParams, TParentContext extends AnyContext = AnyContext, TAllParentContext extends IsAny<TParentRoute['__types']['allParams'], TParentContext, TParentRoute['__types']['allParams'] & TParentContext> = IsAny<TParentRoute['__types']['allParams'], TParentContext, TParentRoute['__types']['allParams'] & TParentContext>, TRouteContext extends RouteContext = RouteContext, TContext extends MergeParamsFromParent<TAllParentContext, TRouteContext> = MergeParamsFromParent<TAllParentContext, TRouteContext>> = BaseRouteOptions<TParentRoute, TCustomId, TPath, TLoader, TParentSearchSchema, TSearchSchema, TFullSearchSchema, TParentParams, TParams, TAllParams, TParentContext, TAllParentContext, TRouteContext, TContext> & UpdatableRouteOptions<TLoader, TSearchSchema, TFullSearchSchema, TAllParams, TRouteContext, TContext>; | ||
type ParamsFallback<TPath extends string, TParams> = unknown extends TParams ? Record<ParsePathParams<TPath>, string> : TParams; | ||
type BaseRouteOptions<TParentRoute extends AnyRoute = AnyRoute, TCustomId extends string = string, TPath extends string = string, TLoader = unknown, TParentSearchSchema extends AnySearchSchema = {}, TSearchSchema extends AnySearchSchema = {}, TFullSearchSchema extends AnySearchSchema = TSearchSchema, TParentParams extends AnyPathParams = AnyPathParams, TParams = unknown, TAllParams = ParamsFallback<TPath, TParams>, TParentContext extends AnyContext = AnyContext, TAllParentContext extends IsAny<TParentRoute['__types']['allParams'], TParentContext, TParentRoute['__types']['allParams'] & TParentContext> = IsAny<TParentRoute['__types']['allParams'], TParentContext, TParentRoute['__types']['allParams'] & TParentContext>, TRouteContext extends RouteContext = RouteContext, TContext extends MergeParamsFromParent<TAllParentContext, TRouteContext> = MergeParamsFromParent<TAllParentContext, TRouteContext>> = RoutePathOptions<TCustomId, TPath> & { | ||
getParentRoute: () => TParentRoute; | ||
validateSearch?: SearchSchemaValidator<TSearchSchema, TParentSearchSchema>; | ||
loader?: LoaderFn<TLoader, TSearchSchema, TFullSearchSchema, TAllParams, NoInfer<TRouteContext>, TContext>; | ||
} & ({ | ||
parseParams?: (rawParams: IsAny<TPath, any, Record<ParsePathParams<TPath>, string>>) => TParams extends Record<ParsePathParams<TPath>, any> ? TParams : 'parseParams must return an object'; | ||
stringifyParams?: (params: NoInfer<ParamsFallback<TPath, TParams>>) => Record<ParsePathParams<TPath>, string>; | ||
} | { | ||
stringifyParams?: never; | ||
parseParams?: never; | ||
}) & (keyof PickRequired<RouteContext> extends never ? { | ||
getContext?: GetContextFn<TParentRoute, TAllParams, TFullSearchSchema, TParentContext, TAllParentContext, TRouteContext>; | ||
} : { | ||
getContext: GetContextFn<TParentRoute, TAllParams, TFullSearchSchema, TParentContext, TAllParentContext, TRouteContext>; | ||
}); | ||
type GetContextFn<TParentRoute, TAllParams, TFullSearchSchema, TParentContext, TAllParentContext, TRouteContext> = (opts: { | ||
params: TAllParams; | ||
search: TFullSearchSchema; | ||
signal?: AbortSignal; | ||
} | ||
type UnloaderFn<TPath extends string> = (routeMatch: RouteMatch<any, RouteInfo<string, TPath>>) => void; | ||
type RouteOptions<TRouteId extends string = string, TPath extends string = string, TParentRouteLoaderData extends AnyLoaderData = {}, TRouteLoaderData extends AnyLoaderData = {}, TParentLoaderData extends AnyLoaderData = {}, TLoaderData extends AnyLoaderData = {}, TParentSearchSchema extends {} = {}, TSearchSchema extends AnySearchSchema = {}, TFullSearchSchema extends AnySearchSchema = TSearchSchema, TParentParams extends AnyPathParams = {}, TParams extends Record<ParsePathParams<TPath>, unknown> = Record<ParsePathParams<TPath>, string>, TAllParams extends AnyPathParams = {}> = ({ | ||
path: TPath; | ||
} | { | ||
id: TRouteId; | ||
}) & { | ||
} & (TParentRoute extends undefined ? { | ||
context?: TAllParentContext; | ||
parentContext?: TParentContext; | ||
} : { | ||
context: TAllParentContext; | ||
parentContext: TParentContext; | ||
})) => TRouteContext; | ||
type UpdatableRouteOptions<TLoader, TSearchSchema extends AnySearchSchema, TFullSearchSchema extends AnySearchSchema, TAllParams extends AnyPathParams, TRouteContext extends AnyContext, TContext extends AnyContext> = MetaOptions & { | ||
key?: null | false | GetKeyFn<TFullSearchSchema, TAllParams>; | ||
caseSensitive?: boolean; | ||
validateSearch?: SearchSchemaValidator<TSearchSchema, TParentSearchSchema>; | ||
wrapInSuspense?: boolean; | ||
component?: RegisteredRouteComponent<RouteProps<TLoader, TFullSearchSchema, TAllParams, TRouteContext, TContext>>; | ||
errorComponent?: RegisterRouteErrorComponent<RouteProps<TLoader, TFullSearchSchema, TAllParams, TRouteContext, TContext>>; | ||
pendingComponent?: RegisteredRouteComponent<RouteProps<TLoader, TFullSearchSchema, TAllParams, TRouteContext, TContext>>; | ||
preSearchFilters?: SearchFilter<TFullSearchSchema>[]; | ||
postSearchFilters?: SearchFilter<TFullSearchSchema>[]; | ||
component?: GetFrameworkGeneric<'Component'>; | ||
errorComponent?: GetFrameworkGeneric<'ErrorComponent'>; | ||
pendingComponent?: GetFrameworkGeneric<'Component'>; | ||
loader?: LoaderFn<TRouteLoaderData, TFullSearchSchema, TAllParams>; | ||
loaderMaxAge?: number; | ||
loaderGcMaxAge?: number; | ||
beforeLoad?: (opts: { | ||
router: AnyRouter; | ||
match: RouteMatch; | ||
}) => Promise<void> | void; | ||
preloadMaxAge?: number; | ||
maxAge?: number; | ||
gcMaxAge?: number; | ||
beforeLoad?: (opts: LoaderContext<TSearchSchema, TFullSearchSchema, TAllParams, NoInfer<TRouteContext>, TContext>) => Promise<void> | void; | ||
onBeforeLoadError?: (err: any) => void; | ||
onValidateSearchError?: (err: any) => void; | ||
onParseParamsError?: (err: any) => void; | ||
onLoadError?: (err: any) => void; | ||
onError?: (err: any) => void; | ||
onLoaded?: (matchContext: { | ||
@@ -413,127 +444,198 @@ params: TAllParams; | ||
}) => void; | ||
meta?: RouteMeta; | ||
} & ({ | ||
parseParams?: never; | ||
stringifyParams?: never; | ||
} | { | ||
parseParams: (rawParams: IsAny<TPath, any, Record<ParsePathParams<TPath>, string>>) => TParams; | ||
stringifyParams: (params: TParams) => Record<ParsePathParams<TPath>, string>; | ||
}) & (PickUnsafe<TParentParams, ParsePathParams<TPath>> extends never ? {} : 'Cannot redefined path params in child routes!'); | ||
}; | ||
type ParseParamsOption<TPath extends string, TParams> = ParseParamsFn<TPath, TParams>; | ||
type ParseParamsFn<TPath extends string, TParams> = (rawParams: IsAny<TPath, any, Record<ParsePathParams<TPath>, string>>) => TParams extends Record<ParsePathParams<TPath>, any> ? TParams : 'parseParams must return an object'; | ||
type ParseParamsObj<TPath extends string, TParams> = { | ||
parse?: ParseParamsFn<TPath, TParams>; | ||
}; | ||
type SearchSchemaValidator<TReturn, TParentSchema> = SearchSchemaValidatorObj<TReturn, TParentSchema> | SearchSchemaValidatorFn<TReturn, TParentSchema>; | ||
type SearchSchemaValidatorObj<TReturn, TParentSchema> = { | ||
parse?: SearchSchemaValidatorFn<TReturn, TParentSchema>; | ||
}; | ||
type SearchSchemaValidatorFn<TReturn, TParentSchema> = (searchObj: Record<string, unknown>) => {} extends TParentSchema ? TReturn : keyof TReturn extends keyof TParentSchema ? { | ||
error: 'Top level search params cannot be redefined by child routes!'; | ||
keys: keyof TReturn & keyof TParentSchema; | ||
} : TReturn; | ||
type DefinedPathParamWarning = 'Path params cannot be redefined by child routes!'; | ||
type ParentParams<TParentParams> = AnyPathParams extends TParentParams ? {} : { | ||
[Key in keyof TParentParams]?: DefinedPathParamWarning; | ||
}; | ||
type LoaderFn<TLoader = unknown, TSearchSchema extends AnySearchSchema = {}, TFullSearchSchema extends AnySearchSchema = {}, TAllParams = {}, TContext extends AnyContext = AnyContext, TAllContext extends AnyContext = AnyContext> = (match: LoaderContext<TSearchSchema, TFullSearchSchema, TAllParams, TContext, TAllContext> & { | ||
parentMatchPromise?: Promise<void>; | ||
}) => Promise<TLoader> | TLoader; | ||
type GetKeyFn<TFullSearchSchema extends AnySearchSchema = {}, TAllParams = {}> = (loaderContext: { | ||
params: TAllParams; | ||
search: TFullSearchSchema; | ||
}) => any; | ||
interface LoaderContext<TSearchSchema extends AnySearchSchema = {}, TFullSearchSchema extends AnySearchSchema = {}, TAllParams = {}, TContext extends AnyContext = AnyContext, TAllContext extends AnyContext = AnyContext> { | ||
params: TAllParams; | ||
routeSearch: TSearchSchema; | ||
search: TFullSearchSchema; | ||
abortController: AbortController; | ||
preload: boolean; | ||
routeContext: TContext; | ||
context: TAllContext; | ||
} | ||
type UnloaderFn<TPath extends string> = (routeMatch: RouteMatch<any, Route>) => void; | ||
type SearchFilter<T, U = T> = (prev: T) => U; | ||
interface RouteConfig<TId extends string = string, TRouteId extends string = string, TPath extends string = string, TFullPath extends string = string, TParentRouteLoaderData extends AnyLoaderData = AnyLoaderData, TRouteLoaderData extends AnyLoaderData = AnyLoaderData, TParentLoaderData extends AnyLoaderData = {}, TLoaderData extends AnyLoaderData = AnyLoaderData, TParentSearchSchema extends {} = {}, TSearchSchema extends AnySearchSchema = {}, TFullSearchSchema extends AnySearchSchema = {}, TParentParams extends AnyPathParams = {}, TParams extends AnyPathParams = {}, TAllParams extends AnyPathParams = {}, TKnownChildren = unknown> { | ||
type ResolveId<TParentRoute, TCustomId extends string, TPath extends string> = TParentRoute extends { | ||
id: infer TParentId extends string; | ||
} ? RoutePrefix<TParentId, string extends TCustomId ? TPath : TCustomId> : RootRouteId; | ||
type InferFullSearchSchema<TRoute> = TRoute extends { | ||
isRoot: true; | ||
__types: { | ||
searchSchema: infer TSearchSchema; | ||
}; | ||
} ? TSearchSchema : TRoute extends { | ||
__types: { | ||
fullSearchSchema: infer TFullSearchSchema; | ||
}; | ||
} ? TFullSearchSchema : {}; | ||
type ResolveFullSearchSchema<TParentRoute, TSearchSchema> = InferFullSearchSchema<TParentRoute> & TSearchSchema; | ||
interface AnyRoute extends Route<any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, any> { | ||
} | ||
type AnyRouteWithRouterContext<TRouterContext extends AnyContext> = Route<any, any, any, any, any, any, any, any, any, any, any, any, any, any, TRouterContext, any, any>; | ||
type MergeParamsFromParent<T, U> = IsAny<T, U, T & U>; | ||
type UseLoaderResult<T> = T extends Record<PropertyKey, infer U> ? { | ||
[K in keyof T]: UseLoaderResultPromise<T[K]>; | ||
} : UseLoaderResultPromise<T>; | ||
type UseLoaderResultPromise<T> = T extends Promise<infer U> ? StreamedPromise<U> : T; | ||
type StreamedPromise<T> = { | ||
promise: Promise<T>; | ||
status: 'resolved' | 'pending'; | ||
data: T; | ||
resolve: (value: T) => void; | ||
}; | ||
declare class Route<TParentRoute extends AnyRoute = AnyRoute, TPath extends string = '/', TFullPath extends ResolveFullPath<TParentRoute, TPath> = ResolveFullPath<TParentRoute, TPath>, TCustomId extends string = string, TId extends ResolveId<TParentRoute, TCustomId, TPath> = ResolveId<TParentRoute, TCustomId, TPath>, TLoader = unknown, TSearchSchema extends AnySearchSchema = {}, TFullSearchSchema extends AnySearchSchema = ResolveFullSearchSchema<TParentRoute, TSearchSchema>, TParams extends Record<ParsePathParams<TPath>, any> = Record<ParsePathParams<TPath>, string>, TAllParams extends MergeParamsFromParent<TParentRoute['__types']['allParams'], TParams> = MergeParamsFromParent<TParentRoute['__types']['allParams'], TParams>, TParentContext extends TParentRoute['__types']['routeContext'] = TParentRoute['__types']['routeContext'], TAllParentContext extends TParentRoute['__types']['context'] = TParentRoute['__types']['context'], TRouteContext extends RouteContext = RouteContext, TContext extends MergeParamsFromParent<TParentRoute['__types']['context'], TRouteContext> = MergeParamsFromParent<TParentRoute['__types']['context'], TRouteContext>, TRouterContext extends AnyContext = AnyContext, TChildren extends unknown = unknown, TRoutesInfo extends DefaultRoutesInfo = DefaultRoutesInfo> { | ||
__types: { | ||
parentRoute: TParentRoute; | ||
path: TPath; | ||
to: TrimPathRight<TFullPath>; | ||
fullPath: TFullPath; | ||
customId: TCustomId; | ||
id: TId; | ||
loader: TLoader; | ||
searchSchema: TSearchSchema; | ||
fullSearchSchema: TFullSearchSchema; | ||
params: TParams; | ||
allParams: TAllParams; | ||
parentContext: TParentContext; | ||
allParentContext: TAllParentContext; | ||
routeContext: TRouteContext; | ||
context: TContext; | ||
children: TChildren; | ||
routesInfo: TRoutesInfo; | ||
routerContext: TRouterContext; | ||
}; | ||
isRoot: TParentRoute extends Route<any> ? true : false; | ||
options: RouteOptions<TParentRoute, TCustomId, TPath, TLoader, InferFullSearchSchema<TParentRoute>, TSearchSchema, TFullSearchSchema, TParentRoute['__types']['allParams'], TParams, TAllParams, TParentContext, TAllParentContext, TRouteContext, TContext> & UpdatableRouteOptions<TLoader, TSearchSchema, TFullSearchSchema, TAllParams, TRouteContext, TContext>; | ||
parentRoute: TParentRoute; | ||
id: TId; | ||
routeId: TRouteId; | ||
path: NoInfer<TPath>; | ||
path: TPath; | ||
fullPath: TFullPath; | ||
options: RouteOptions<TRouteId, TPath, TParentRouteLoaderData, TRouteLoaderData, TParentLoaderData, TLoaderData, TParentSearchSchema, TSearchSchema, TFullSearchSchema, TParentParams, TParams, TAllParams>; | ||
children?: TKnownChildren; | ||
addChildren: IsAny<TId, any, <TNewChildren extends any>(children: TNewChildren extends AnyRouteConfig[] ? TNewChildren : { | ||
error: 'Invalid route detected'; | ||
route: TNewChildren; | ||
}) => RouteConfig<TId, TRouteId, TPath, TFullPath, TParentRouteLoaderData, TRouteLoaderData, TParentLoaderData, TLoaderData, TParentSearchSchema, TSearchSchema, TFullSearchSchema, TParentParams, TParams, TAllParams, TNewChildren>>; | ||
createRoute: CreateRouteConfigFn<false, TId, TFullPath, TRouteLoaderData, TLoaderData, TFullSearchSchema, TAllParams>; | ||
generate: GenerateFn<TRouteId, TPath, TParentRouteLoaderData, TParentLoaderData, TParentSearchSchema, TParentParams>; | ||
to: TrimPathRight<TFullPath>; | ||
children?: TChildren; | ||
originalIndex?: number; | ||
router?: Router<TRoutesInfo['routeTree'], TRoutesInfo>; | ||
rank: number; | ||
constructor(options: RouteOptions<TParentRoute, TCustomId, TPath, TLoader, InferFullSearchSchema<TParentRoute>, TSearchSchema, TFullSearchSchema, TParentRoute['__types']['allParams'], TParams, TAllParams, TParentContext, TAllParentContext, TRouteContext, TContext> & UpdatableRouteOptions<TLoader, TSearchSchema, TFullSearchSchema, TAllParams, TRouteContext, TContext>); | ||
init: (opts: { | ||
originalIndex: number; | ||
router: AnyRouter; | ||
}) => void; | ||
addChildren: <TNewChildren extends AnyRoute[]>(children: TNewChildren) => Route<TParentRoute, TPath, TFullPath, TCustomId, TId, TLoader, TSearchSchema, TFullSearchSchema, TParams, TAllParams, TParentContext, TAllParentContext, TRouteContext, TContext, TRouterContext, TNewChildren, TRoutesInfo>; | ||
update: (options: UpdatableRouteOptions<TLoader, TSearchSchema, TFullSearchSchema, TAllParams, TRouteContext, TContext>) => this; | ||
static __onInit: (route: typeof this$1) => void; | ||
} | ||
type GenerateFn<TRouteId extends string = string, TPath extends string = string, TParentRouteLoaderData extends AnyLoaderData = AnyLoaderData, TParentLoaderData extends AnyLoaderData = {}, TParentSearchSchema extends {} = {}, TParentParams extends AnyPathParams = {}> = <TRouteLoaderData extends AnyLoaderData = AnyLoaderData, TSearchSchema extends AnySearchSchema = {}, TParams extends Record<ParsePathParams<TPath>, unknown> = Record<ParsePathParams<TPath>, string>, TAllParams extends AnyPathParams extends TParams ? Record<ParsePathParams<TPath>, string> : NoInfer<TParams> = AnyPathParams extends TParams ? Record<ParsePathParams<TPath>, string> : NoInfer<TParams>>(options: Omit<RouteOptions<TRouteId, TPath, TParentRouteLoaderData, TRouteLoaderData, TParentLoaderData, Expand<TParentLoaderData & NoInfer<TRouteLoaderData>>, TParentSearchSchema, TSearchSchema, Expand<TParentSearchSchema & TSearchSchema>, TParentParams, TParams, Expand<TParentParams & TAllParams>>, 'path'>) => void; | ||
type CreateRouteConfigFn<TIsRoot extends boolean = false, TParentId extends string = string, TParentPath extends string = string, TParentRouteLoaderData extends AnyLoaderData = {}, TParentLoaderData extends AnyLoaderData = {}, TParentSearchSchema extends AnySearchSchema = {}, TParentParams extends AnyPathParams = {}> = <TRouteId extends string, TPath extends string, TRouteLoaderData extends AnyLoaderData, TSearchSchema extends AnySearchSchema = AnySearchSchema, TParams extends Record<ParsePathParams<TPath>, unknown> = Record<ParsePathParams<TPath>, string>, TAllParams extends AnyPathParams extends TParams ? Record<ParsePathParams<TPath>, string> : NoInfer<TParams> = AnyPathParams extends TParams ? Record<ParsePathParams<TPath>, string> : NoInfer<TParams>, TKnownChildren extends RouteConfig[] = RouteConfig[], TResolvedId extends string = string extends TRouteId ? string extends TPath ? string : TPath : TRouteId>(options?: TIsRoot extends true ? Omit<RouteOptions<TRouteId, TPath, TParentRouteLoaderData, TRouteLoaderData, TParentLoaderData, Expand<TParentLoaderData & NoInfer<TRouteLoaderData>>, TParentSearchSchema, TSearchSchema, Expand<TParentSearchSchema & TSearchSchema>, TParentParams, TParams, Expand<TParentParams & TAllParams>>, 'path'> & { | ||
path?: never; | ||
} : RouteOptions<TRouteId, TPath, TParentRouteLoaderData, TRouteLoaderData, TParentLoaderData, Expand<TParentLoaderData & NoInfer<TRouteLoaderData>>, TParentSearchSchema, TSearchSchema, Expand<TParentSearchSchema & TSearchSchema>, TParentParams, TParams, Expand<TParentParams & TAllParams>>, children?: TKnownChildren, isRoot?: boolean, parentId?: string, parentPath?: string) => RouteConfig<RoutePrefix<TParentId, TResolvedId>, TResolvedId, TPath, string extends TPath ? '' : RoutePath<RoutePrefix<TParentPath, TPath>>, TParentRouteLoaderData, TRouteLoaderData, TParentLoaderData, Expand<TParentLoaderData & NoInfer<TRouteLoaderData>>, TParentSearchSchema, TSearchSchema, Expand<TParentSearchSchema & TSearchSchema>, TParentParams, TParams, Expand<TParentParams & TAllParams>, TKnownChildren>; | ||
type RoutePath<T extends string> = T extends RootRouteId ? '/' : TrimPathRight<`${T}`>; | ||
type RoutePrefix<TPrefix extends string, TId extends string> = string extends TId ? RootRouteId : TId extends string ? `${TPrefix}/${TId}` extends '/' ? '/' : `/${TrimPathLeft<`${TrimPathRight<TPrefix>}/${TrimPath<TId>}`>}` : never; | ||
interface AnyRouteConfig extends RouteConfig<any, any, any, any, any, any, any, any, any, any, any, any, any, any, any> { | ||
type AnyRootRoute = RootRoute<any, any, any, any>; | ||
declare class RouterContext<TRouterContext extends {}> { | ||
constructor(); | ||
createRootRoute: <TLoader = unknown, TSearchSchema extends AnySearchSchema = {}, TContext extends RouteContext = RouteContext>(options?: Omit<RouteOptions<AnyRoute, "__root__", "", {}, TSearchSchema, {}, {}, AnyPathParams, Record<never, string>, Record<never, string>, AnyContext, AnyContext, RouteContext, RouteContext>, "caseSensitive" | "path" | "getParentRoute" | "stringifyParams" | "parseParams" | "id"> | undefined) => RootRoute<TLoader, TSearchSchema, TContext, TRouterContext>; | ||
} | ||
interface AnyRouteConfigWithChildren<TChildren> extends RouteConfig<any, any, any, any, any, any, any, any, any, any, any, any, any, any, TChildren> { | ||
declare class RootRoute<TLoader = unknown, TSearchSchema extends AnySearchSchema = {}, TContext extends RouteContext = RouteContext, TRouterContext extends {} = {}> extends Route<any, '/', '/', string, RootRouteId, TLoader, TSearchSchema, TSearchSchema, {}, {}, TRouterContext, TRouterContext, MergeParamsFromParent<TRouterContext, TContext>, MergeParamsFromParent<TRouterContext, TContext>, TRouterContext, any, any> { | ||
constructor(options?: Omit<RouteOptions<AnyRoute, RootRouteId, '', {}, TSearchSchema, {}, {}>, 'path' | 'id' | 'getParentRoute' | 'caseSensitive' | 'parseParams' | 'stringifyParams'>); | ||
} | ||
type ResolveFullPath<TParentRoute extends AnyRoute, TPath extends string, TPrefixed extends RoutePrefix<TParentRoute['fullPath'], TPath> = RoutePrefix<TParentRoute['fullPath'], TPath>> = TPrefixed extends RootRouteId ? '/' : TPrefixed; | ||
type RoutePrefix<TPrefix extends string, TPath extends string> = string extends TPath ? RootRouteId : TPath extends string ? TPrefix extends RootRouteId ? TPath extends '/' ? '/' : `/${TrimPath<TPath>}` : `${TPrefix}/${TPath}` extends '/' ? '/' : `/${TrimPathLeft<`${TrimPathRight<TPrefix>}/${TrimPath<TPath>}`>}` : never; | ||
type TrimPath<T extends string> = '' extends T ? '' : TrimPathRight<TrimPathLeft<T>>; | ||
type TrimPathLeft<T extends string> = T extends `${RootRouteId}/${infer U}` ? TrimPathLeft<U> : T extends `/${infer U}` ? TrimPathLeft<U> : T; | ||
type TrimPathRight<T extends string> = T extends '/' ? '/' : T extends `${infer U}/` ? TrimPathRight<U> : T; | ||
declare const createRouteConfig: CreateRouteConfigFn<true>; | ||
interface AnyRoute extends Route<any, any, any> { | ||
} | ||
declare class Route<TAllRouteInfo extends AnyAllRouteInfo = DefaultAllRouteInfo, TRouteInfo extends AnyRouteInfo = RouteInfo, TRouterContext = unknown> { | ||
routeInfo: TRouteInfo; | ||
id: TRouteInfo['id']; | ||
customId: TRouteInfo['customId']; | ||
path: TRouteInfo['path']; | ||
fullPath: TRouteInfo['fullPath']; | ||
getParentRoute: () => undefined | AnyRoute; | ||
childRoutes?: AnyRoute[]; | ||
options: RouteOptions; | ||
originalIndex: number; | ||
getRouter: () => Router<TAllRouteInfo['routeConfig'], TAllRouteInfo, TRouterContext>; | ||
constructor(routeConfig: RouteConfig, options: TRouteInfo['options'], originalIndex: number, parent: undefined | Route<TAllRouteInfo, any>, router: Router<TAllRouteInfo['routeConfig'], TAllRouteInfo, TRouterContext>); | ||
} | ||
interface AnyAllRouteInfo { | ||
routeConfig: AnyRouteConfig; | ||
routeInfo: AnyRouteInfo; | ||
routeInfoById: Record<string, AnyRouteInfo>; | ||
routeInfoByFullPath: Record<string, AnyRouteInfo>; | ||
interface AnyRoutesInfo { | ||
routeTree: AnyRoute; | ||
routeUnion: AnyRoute; | ||
routesById: Record<string, AnyRoute>; | ||
routesByFullPath: Record<string, AnyRoute>; | ||
routeIds: any; | ||
routePaths: any; | ||
routeIntersection: AnyRoute; | ||
fullSearchSchema: Record<string, any>; | ||
allParams: Record<string, any>; | ||
} | ||
interface DefaultAllRouteInfo { | ||
routeConfig: RouteConfig; | ||
routeInfo: RouteInfo; | ||
routeInfoById: Record<string, RouteInfo>; | ||
routeInfoByFullPath: Record<string, RouteInfo>; | ||
interface DefaultRoutesInfo { | ||
routeTree: AnyRoute; | ||
routeUnion: AnyRoute; | ||
routesById: Record<string, Route>; | ||
routesByFullPath: Record<string, Route>; | ||
routeIds: string; | ||
routePaths: string; | ||
routeIntersection: AnyRoute; | ||
fullSearchSchema: AnySearchSchema; | ||
allParams: AnyPathParams; | ||
} | ||
interface AllRouteInfo<TRouteConfig extends AnyRouteConfig = RouteConfig> extends RoutesInfoInner<TRouteConfig, ParseRouteConfig<TRouteConfig>> { | ||
interface RoutesInfo<TRouteTree extends AnyRoute = Route> extends RoutesInfoInner<TRouteTree, ParseRoute<TRouteTree>> { | ||
} | ||
type ParseRouteConfig<TRouteConfig = AnyRouteConfig> = TRouteConfig extends AnyRouteConfig ? RouteConfigRoute<TRouteConfig> | ParseRouteChildren<TRouteConfig> : never; | ||
type ParseRouteChildren<TRouteConfig> = TRouteConfig extends AnyRouteConfigWithChildren<infer TChildren> ? unknown extends TChildren ? never : TChildren extends AnyRouteConfig[] ? Values<{ | ||
[TId in TChildren[number]['id']]: ParseRouteChild<TChildren[number], TId>; | ||
}> : never : never; | ||
type ParseRouteChild<TRouteConfig, TId> = TRouteConfig & { | ||
id: TId; | ||
} extends AnyRouteConfig ? ParseRouteConfig<TRouteConfig> : never; | ||
type RouteConfigRoute<TRouteConfig> = TRouteConfig extends RouteConfig<infer TId, infer TCustomId, infer TPath, infer TFullPath, infer TParentRouteLoaderData, infer TRouteLoaderData, infer TParentLoaderData, infer TLoaderData, infer TParentSearchSchema, infer TSearchSchema, infer TFullSearchSchema, infer TParentParams, infer TParams, infer TAllParams, any> ? string extends TCustomId ? never : RouteInfo<TId, TCustomId, TPath, TFullPath, TParentRouteLoaderData, TRouteLoaderData, TParentLoaderData, TLoaderData, TParentSearchSchema, TSearchSchema, TFullSearchSchema, TParentParams, TParams, TAllParams> : never; | ||
interface RoutesInfoInner<TRouteConfig extends AnyRouteConfig, TRouteInfo extends RouteInfo<string, string, any, any, any, any, any, any, any, any, any, any, any, any> = RouteInfo, TRouteInfoById = { | ||
'/': TRouteInfo; | ||
interface RoutesInfoInner<TRouteTree extends AnyRoute, TRouteUnion extends AnyRoute = Route, TRoutesById = { | ||
'/': TRouteUnion; | ||
} & { | ||
[TInfo in TRouteInfo as TInfo['id']]: TInfo; | ||
}, TRouteInfoByFullPath = { | ||
'/': TRouteInfo; | ||
[TRoute in TRouteUnion as TRoute['id']]: TRoute; | ||
}, TRoutesByFullPath = { | ||
'/': TRouteUnion; | ||
} & { | ||
[TInfo in TRouteInfo as TInfo['fullPath'] extends RootRouteId ? never : string extends TInfo['fullPath'] ? never : TInfo['fullPath']]: TInfo; | ||
[TRoute in TRouteUnion as TRoute['fullPath'] extends RootRouteId ? never : string extends TRoute['fullPath'] ? never : `${TRoute['fullPath']}/` extends keyof TRoutesById ? never : TRoute['fullPath'] extends `${infer Trimmed}/` ? Trimmed : TRoute['fullPath']]: TRoute; | ||
}> { | ||
routeConfig: TRouteConfig; | ||
routeInfo: TRouteInfo; | ||
routeInfoById: TRouteInfoById; | ||
routeInfoByFullPath: TRouteInfoByFullPath; | ||
routeIds: keyof TRouteInfoById; | ||
routePaths: keyof TRouteInfoByFullPath; | ||
fullSearchSchema: Partial<UnionToIntersection<TRouteInfo['fullSearchSchema']>>; | ||
allParams: Partial<UnionToIntersection<TRouteInfo['allParams']>>; | ||
routeTree: TRouteTree; | ||
routeUnion: TRouteUnion; | ||
routesById: TRoutesById; | ||
routesByFullPath: TRoutesByFullPath; | ||
routeIds: keyof TRoutesById; | ||
routePaths: keyof TRoutesByFullPath; | ||
routeIntersection: Route<TRouteUnion['__types']['parentRoute'], // TParentRoute, | ||
TRouteUnion['__types']['path'], // TPath, | ||
TRouteUnion['__types']['fullPath'], // TFullPath, | ||
TRouteUnion['__types']['customId'], // TCustomId, | ||
TRouteUnion['__types']['id'], // TId, | ||
TRouteUnion['__types']['loader'], // TId, | ||
// TId, | ||
MergeUnion<TRouteUnion['__types']['searchSchema']> & {}, // TSearchSchema, | ||
// TSearchSchema, | ||
MergeUnion<TRouteUnion['__types']['fullSearchSchema']> & {}, // TFullSearchSchema, | ||
MergeUnion<TRouteUnion['__types']['params']>, // TParams, | ||
MergeUnion<TRouteUnion['__types']['allParams']>, // TAllParams, | ||
MergeUnion<TRouteUnion['__types']['parentContext']>, // TParentContext, | ||
MergeUnion<TRouteUnion['__types']['allParentContext']>, // TAllParentContext, | ||
// TAllParentContext, | ||
MergeUnion<TRouteUnion['__types']['routeContext']> & {}, // TRouteContext, | ||
// TRouteContext, | ||
MergeUnion<TRouteUnion['__types']['context']> & {}, // TContext, | ||
// TContext, | ||
MergeUnion<TRouteUnion['__types']['routerContext']> & {}, // TRouterContext, | ||
TRouteUnion['__types']['children'], // TChildren, | ||
TRouteUnion['__types']['routesInfo']>; | ||
fullSearchSchema: Partial<MergeUnion<TRouteUnion['__types']['fullSearchSchema']>>; | ||
allParams: Partial<MergeUnion<TRouteUnion['__types']['allParams']>>; | ||
} | ||
interface AnyRouteInfo extends RouteInfo<any, any, any, any, any, any, any, any, any, any, any, any, any, any> { | ||
} | ||
interface RouteInfo<TId extends string = string, TCustomId extends string = string, TPath extends string = string, TFullPath extends string = '/', TParentRouteLoaderData extends AnyLoaderData = {}, TRouteLoaderData extends AnyLoaderData = {}, TParentLoaderData extends AnyLoaderData = {}, TLoaderData extends AnyLoaderData = {}, TParentSearchSchema extends {} = {}, TSearchSchema extends AnySearchSchema = {}, TFullSearchSchema extends AnySearchSchema = {}, TParentParams extends AnyPathParams = {}, TParams extends AnyPathParams = {}, TAllParams extends AnyPathParams = {}> { | ||
id: TId; | ||
customId: TCustomId; | ||
path: TPath; | ||
fullPath: TFullPath; | ||
parentRouteLoaderData: TParentRouteLoaderData; | ||
routeLoaderData: TRouteLoaderData; | ||
parentLoaderData: TParentLoaderData; | ||
loaderData: TLoaderData; | ||
searchSchema: TSearchSchema; | ||
fullSearchSchema: TFullSearchSchema; | ||
parentParams: TParentParams; | ||
params: TParams; | ||
allParams: TAllParams; | ||
options: RouteOptions<TCustomId, TPath, TParentRouteLoaderData, TRouteLoaderData, TParentLoaderData, TLoaderData, TParentSearchSchema, TSearchSchema, TFullSearchSchema, TParentParams, TParams, TAllParams>; | ||
} | ||
type RoutesById<TAllRouteInfo extends AnyAllRouteInfo> = { | ||
[K in keyof TAllRouteInfo['routeInfoById']]: Route<TAllRouteInfo, TAllRouteInfo['routeInfoById'][K]>; | ||
type ParseRoute<TRouteTree> = TRouteTree extends AnyRoute ? TRouteTree | ParseRouteChildren<TRouteTree> : never; | ||
type ParseRouteChildren<TRouteTree> = TRouteTree extends Route<any, any, any, any, any, any, any, any, any, any, any, any, any, any, any, infer TChildren, any> ? unknown extends TChildren ? never : TChildren extends AnyRoute[] ? Values<{ | ||
[TId in TChildren[number]['id']]: ParseRouteChild<TChildren[number], TId>; | ||
}> : never : never; | ||
type ParseRouteChild<TRoute, TId> = TRoute extends AnyRoute ? ParseRoute<TRoute> : never; | ||
type RoutesById<TRoutesInfo extends AnyRoutesInfo> = { | ||
[K in keyof TRoutesInfo['routesById']]: TRoutesInfo['routesById'][K]; | ||
}; | ||
type RouteInfoById<TAllRouteInfo extends AnyAllRouteInfo, TId> = TId extends keyof TAllRouteInfo['routeInfoById'] ? IsAny<TAllRouteInfo['routeInfoById'][TId]['id'], RouteInfo, TAllRouteInfo['routeInfoById'][TId]> : never; | ||
type RouteInfoByPath<TAllRouteInfo extends AnyAllRouteInfo, TPath> = TPath extends keyof TAllRouteInfo['routeInfoByFullPath'] ? IsAny<TAllRouteInfo['routeInfoByFullPath'][TPath]['id'], RouteInfo, TAllRouteInfo['routeInfoByFullPath'][TPath]> : never; | ||
type RouteById<TRoutesInfo extends AnyRoutesInfo, TId> = TId extends keyof TRoutesInfo['routesById'] ? IsAny<TRoutesInfo['routesById'][TId]['id'], Route, TRoutesInfo['routesById'][TId]> : never; | ||
type RoutesByPath<TRoutesInfo extends AnyRoutesInfo> = { | ||
[K in keyof TRoutesInfo['routesByFullPath']]: TRoutesInfo['routesByFullPath'][K]; | ||
}; | ||
type RouteByPath<TRoutesInfo extends AnyRoutesInfo, TPath> = TPath extends keyof TRoutesInfo['routesByFullPath'] ? IsAny<TRoutesInfo['routesByFullPath'][TPath]['id'], Route, TRoutesInfo['routesByFullPath'][TPath]> : never; | ||
@@ -550,2 +652,3 @@ type LinkInfo = { | ||
handleLeave: (e: any) => void; | ||
handleTouchStart: (e: any) => void; | ||
isActive: boolean; | ||
@@ -557,3 +660,3 @@ disabled?: boolean; | ||
type ParsePathParams<T extends string> = Split<T>[number] extends infer U ? U extends `$${infer V}` ? V : never : never; | ||
type Join<T> = T extends [] ? '' : T extends [infer L extends string] ? L : T extends [infer L extends string, ...infer Tail extends [...string[]]] ? CleanPath<`${L}/${Join<Tail>}`> : never; | ||
type Join<T, Delimiter extends string = '/'> = T extends [] ? '' : T extends [infer L extends string] ? L : T extends [infer L extends string, ...infer Tail extends [...string[]]] ? CleanPath<`${L}${Delimiter}${Join<Tail>}`> : never; | ||
type RelativeToPathAutoComplete<AllPaths extends string, TFrom extends string, TTo extends string, SplitPaths extends string[] = Split<AllPaths, false>> = TTo extends `..${infer _}` ? SplitPaths extends [ | ||
@@ -572,12 +675,12 @@ ...Split<ResolveRelativePath<TFrom, TTo>, false>, | ||
} ? never : './' : never) | (TFrom extends `/` ? never : '../') | AllPaths; | ||
type NavigateOptions<TAllRouteInfo extends AnyAllRouteInfo = DefaultAllRouteInfo, TFrom extends TAllRouteInfo['routePaths'] = '/', TTo extends string = '.'> = ToOptions<TAllRouteInfo, TFrom, TTo> & { | ||
type NavigateOptions<TRoutesInfo extends AnyRoutesInfo = RegisteredRoutesInfo, TFrom extends TRoutesInfo['routePaths'] = '/', TTo extends string = ''> = ToOptions<TRoutesInfo, TFrom, TTo> & { | ||
replace?: boolean; | ||
}; | ||
type ToOptions<TAllRouteInfo extends AnyAllRouteInfo = DefaultAllRouteInfo, TFrom extends TAllRouteInfo['routePaths'] = '/', TTo extends string = '.', TResolvedTo = ResolveRelativePath<TFrom, NoInfer<TTo>>> = { | ||
to?: ToPathOption<TAllRouteInfo, TFrom, TTo>; | ||
type ToOptions<TRoutesInfo extends AnyRoutesInfo = RegisteredRoutesInfo, TFrom extends TRoutesInfo['routePaths'] = '/', TTo extends string = '', TResolvedTo = ResolveRelativePath<TFrom, NoInfer<TTo>>> = { | ||
to?: ToPathOption<TRoutesInfo, TFrom, TTo>; | ||
hash?: Updater<string>; | ||
state?: LocationState; | ||
from?: TFrom; | ||
} & CheckPath<TAllRouteInfo, NoInfer<TResolvedTo>, {}> & SearchParamOptions<TAllRouteInfo, TFrom, TResolvedTo> & PathParamOptions<TAllRouteInfo, TFrom, TResolvedTo>; | ||
type SearchParamOptions<TAllRouteInfo extends AnyAllRouteInfo, TFrom, TTo, TFromSchema = Expand<UnionToIntersection<TAllRouteInfo['fullSearchSchema'] & RouteInfoByPath<TAllRouteInfo, TFrom> extends never ? {} : RouteInfoByPath<TAllRouteInfo, TFrom>['fullSearchSchema']>>, TToSchema = Partial<RouteInfoByPath<TAllRouteInfo, TFrom>['fullSearchSchema']> & Omit<RouteInfoByPath<TAllRouteInfo, TTo>['fullSearchSchema'], keyof PickRequired<RouteInfoByPath<TAllRouteInfo, TFrom>['fullSearchSchema']>>, TFromFullSchema = Expand<UnionToIntersection<TAllRouteInfo['fullSearchSchema'] & TFromSchema>>, TToFullSchema = Expand<UnionToIntersection<TAllRouteInfo['fullSearchSchema'] & TToSchema>>> = keyof PickRequired<TToSchema> extends never ? { | ||
} & CheckPath<TRoutesInfo, NoInfer<TResolvedTo>, {}> & SearchParamOptions<TRoutesInfo, TFrom, TResolvedTo> & PathParamOptions<TRoutesInfo, TFrom, TResolvedTo>; | ||
type SearchParamOptions<TRoutesInfo extends AnyRoutesInfo, TFrom, TTo, TFromSchema = UnionToIntersection<TRoutesInfo['fullSearchSchema'] & RouteByPath<TRoutesInfo, TFrom> extends never ? {} : RouteByPath<TRoutesInfo, TFrom>['__types']['fullSearchSchema']>, TToSchema = Partial<RouteByPath<TRoutesInfo, TFrom>['__types']['fullSearchSchema']> & Omit<RouteByPath<TRoutesInfo, TTo>['__types']['fullSearchSchema'], keyof PickRequired<RouteByPath<TRoutesInfo, TFrom>['__types']['fullSearchSchema']>>, TFromFullSchema = UnionToIntersection<TRoutesInfo['fullSearchSchema'] & TFromSchema>, TToFullSchema = UnionToIntersection<TRoutesInfo['fullSearchSchema'] & TToSchema>> = keyof PickRequired<TToSchema> extends never ? { | ||
search?: true | SearchReducer<TFromFullSchema, TToFullSchema>; | ||
@@ -590,3 +693,3 @@ } : { | ||
} | ((current: TFrom) => TTo); | ||
type PathParamOptions<TAllRouteInfo extends AnyAllRouteInfo, TFrom, TTo, TFromSchema = Expand<UnionToIntersection<RouteInfoByPath<TAllRouteInfo, TFrom> extends never ? {} : RouteInfoByPath<TAllRouteInfo, TFrom>['allParams']>>, TToSchema = Partial<RouteInfoByPath<TAllRouteInfo, TFrom>['allParams']> & Omit<RouteInfoByPath<TAllRouteInfo, TTo>['allParams'], keyof PickRequired<RouteInfoByPath<TAllRouteInfo, TFrom>['allParams']>>, TFromFullParams = Expand<UnionToIntersection<TAllRouteInfo['allParams'] & TFromSchema>>, TToFullParams = Expand<UnionToIntersection<TAllRouteInfo['allParams'] & TToSchema>>> = keyof PickRequired<TToSchema> extends never ? { | ||
type PathParamOptions<TRoutesInfo extends AnyRoutesInfo, TFrom, TTo, TFromSchema = UnionToIntersection<RouteByPath<TRoutesInfo, TFrom> extends never ? {} : RouteByPath<TRoutesInfo, TFrom>['__types']['allParams']>, TToSchema = Partial<RouteByPath<TRoutesInfo, TFrom>['__types']['allParams']> & Omit<RouteByPath<TRoutesInfo, TTo>['__types']['allParams'], keyof PickRequired<RouteByPath<TRoutesInfo, TFrom>['__types']['allParams']>>, TFromFullParams = UnionToIntersection<TRoutesInfo['allParams'] & TFromSchema>, TToFullParams = UnionToIntersection<TRoutesInfo['allParams'] & TToSchema>> = keyof PickRequired<TToSchema> extends never ? { | ||
params?: ParamsReducer<TFromFullParams, TToFullParams>; | ||
@@ -597,33 +700,30 @@ } : { | ||
type ParamsReducer<TFrom, TTo> = TTo | ((current: TFrom) => TTo); | ||
type ToPathOption<TAllRouteInfo extends AnyAllRouteInfo = DefaultAllRouteInfo, TFrom extends TAllRouteInfo['routePaths'] = '/', TTo extends string = '.'> = TTo | RelativeToPathAutoComplete<TAllRouteInfo['routePaths'], NoInfer<TFrom> extends string ? NoInfer<TFrom> : '', NoInfer<TTo> & string>; | ||
type ToIdOption<TAllRouteInfo extends AnyAllRouteInfo = DefaultAllRouteInfo, TFrom extends TAllRouteInfo['routePaths'] = '/', TTo extends string = '.'> = TTo | RelativeToPathAutoComplete<TAllRouteInfo['routeIds'], NoInfer<TFrom> extends string ? NoInfer<TFrom> : '', NoInfer<TTo> & string>; | ||
type ToPathOption<TRoutesInfo extends AnyRoutesInfo = RegisteredRoutesInfo, TFrom extends TRoutesInfo['routePaths'] = '/', TTo extends string = ''> = TTo | RelativeToPathAutoComplete<TRoutesInfo['routePaths'], NoInfer<TFrom> extends string ? NoInfer<TFrom> : '', NoInfer<TTo> & string>; | ||
type ToIdOption<TRoutesInfo extends AnyRoutesInfo = RegisteredRoutesInfo, TFrom extends TRoutesInfo['routePaths'] = '/', TTo extends string = ''> = TTo | RelativeToPathAutoComplete<TRoutesInfo['routeIds'], NoInfer<TFrom> extends string ? NoInfer<TFrom> : '', NoInfer<TTo> & string>; | ||
interface ActiveOptions { | ||
exact?: boolean; | ||
includeHash?: boolean; | ||
includeSearch?: boolean; | ||
} | ||
type LinkOptions<TAllRouteInfo extends AnyAllRouteInfo = DefaultAllRouteInfo, TFrom extends TAllRouteInfo['routePaths'] = '/', TTo extends string = '.'> = NavigateOptions<TAllRouteInfo, TFrom, TTo> & { | ||
type LinkOptions<TRoutesInfo extends AnyRoutesInfo = RegisteredRoutesInfo, TFrom extends TRoutesInfo['routePaths'] = '/', TTo extends string = ''> = NavigateOptions<TRoutesInfo, TFrom, TTo> & { | ||
target?: HTMLAnchorElement['target']; | ||
activeOptions?: ActiveOptions; | ||
preload?: false | 'intent'; | ||
preloadMaxAge?: number; | ||
preloadGcMaxAge?: number; | ||
preloadDelay?: number; | ||
disabled?: boolean; | ||
}; | ||
type CheckRelativePath<TAllRouteInfo extends AnyAllRouteInfo, TFrom, TTo> = TTo extends string ? TFrom extends string ? ResolveRelativePath<TFrom, TTo> extends TAllRouteInfo['routePaths'] ? {} : { | ||
type CheckRelativePath<TRoutesInfo extends AnyRoutesInfo, TFrom, TTo> = TTo extends string ? TFrom extends string ? ResolveRelativePath<TFrom, TTo> extends TRoutesInfo['routePaths'] ? {} : { | ||
Error: `${TFrom} + ${TTo} resolves to ${ResolveRelativePath<TFrom, TTo>}, which is not a valid route path.`; | ||
'Valid Route Paths': TAllRouteInfo['routePaths']; | ||
'Valid Route Paths': TRoutesInfo['routePaths']; | ||
} : {} : {}; | ||
type CheckPath<TAllRouteInfo extends AnyAllRouteInfo, TPath, TPass> = Exclude<TPath, TAllRouteInfo['routePaths']> extends never ? TPass : CheckPathError<TAllRouteInfo, Exclude<TPath, TAllRouteInfo['routePaths']>>; | ||
type CheckPathError<TAllRouteInfo extends AnyAllRouteInfo, TInvalids> = Expand<{ | ||
Error: `${TInvalids extends string ? TInvalids : never} is not a valid route path.`; | ||
'Valid Route Paths': TAllRouteInfo['routePaths']; | ||
}>; | ||
type CheckId<TAllRouteInfo extends AnyAllRouteInfo, TPath, TPass> = Exclude<TPath, TAllRouteInfo['routeIds']> extends never ? TPass : CheckIdError<TAllRouteInfo, Exclude<TPath, TAllRouteInfo['routeIds']>>; | ||
type CheckIdError<TAllRouteInfo extends AnyAllRouteInfo, TInvalids> = Expand<{ | ||
type CheckPath<TRoutesInfo extends AnyRoutesInfo, TPath, TPass> = Exclude<TPath, TRoutesInfo['routePaths']> extends never ? TPass : CheckPathError<TRoutesInfo, Exclude<TPath, TRoutesInfo['routePaths']>>; | ||
type CheckPathError<TRoutesInfo extends AnyRoutesInfo, TInvalids> = { | ||
to: TRoutesInfo['routePaths']; | ||
}; | ||
type CheckId<TRoutesInfo extends AnyRoutesInfo, TPath, TPass> = Exclude<TPath, TRoutesInfo['routeIds']> extends never ? TPass : CheckIdError<TRoutesInfo, Exclude<TPath, TRoutesInfo['routeIds']>>; | ||
type CheckIdError<TRoutesInfo extends AnyRoutesInfo, TInvalids> = { | ||
Error: `${TInvalids extends string ? TInvalids : never} is not a valid route ID.`; | ||
'Valid Route IDs': TAllRouteInfo['routeIds']; | ||
}>; | ||
'Valid Route IDs': TRoutesInfo['routeIds']; | ||
}; | ||
type ResolveRelativePath<TFrom, TTo = '.'> = TFrom extends string ? TTo extends string ? TTo extends '.' ? TFrom : TTo extends `./` ? Join<[TFrom, '/']> : TTo extends `./${infer TRest}` ? ResolveRelativePath<TFrom, TRest> : TTo extends `/${infer TRest}` ? TTo : Split<TTo> extends ['..', ...infer ToRest] ? Split<TFrom> extends [...infer FromRest, infer FromTail] ? ToRest extends ['/'] ? Join<[...FromRest, '/']> : ResolveRelativePath<Join<FromRest>, Join<ToRest>> : never : Split<TTo> extends ['.', ...infer ToRest] ? ToRest extends ['/'] ? Join<[TFrom, '/']> : ResolveRelativePath<TFrom, Join<ToRest>> : CleanPath<Join<['/', ...Split<TFrom>, ...Split<TTo>]>> : never : never; | ||
type ValidFromPath<TAllRouteInfo extends AnyAllRouteInfo = DefaultAllRouteInfo> = undefined | (string extends TAllRouteInfo['routePaths'] ? string : TAllRouteInfo['routePaths']); | ||
@@ -641,3 +741,3 @@ interface Segment { | ||
declare function parsePathname(pathname?: string): Segment[]; | ||
declare function interpolatePath(path: string | undefined, params: any, leaveWildcard?: boolean): string; | ||
declare function interpolatePath(path: string | undefined, params: any, leaveWildcards?: boolean): string; | ||
declare function matchPathname(basepath: string, currentPathname: string, matchLocation: Pick<MatchLocation, 'to' | 'fuzzy' | 'caseSensitive'>): AnyPathParams | undefined; | ||
@@ -654,46 +754,2 @@ declare function matchByPath(basepath: string, from: string, matchLocation: Pick<MatchLocation, 'to' | 'caseSensitive' | 'fuzzy'>): Record<string, string> | undefined; | ||
/** | ||
* This function returns `a` if `b` is deeply equal. | ||
* If not, it will replace any deeply equal children of `b` with those of `a`. | ||
* This can be used for structural sharing between immutable JSON values for example. | ||
* Do not use this with signals | ||
*/ | ||
declare function replaceEqualDeep<T>(prev: any, _next: T): T; | ||
declare function trackDeep<T>(obj: T): T; | ||
interface ActionOptions<TKey extends string = string, TPayload = unknown, TResponse = unknown, TError = Error> { | ||
key?: TKey; | ||
action: (payload: TPayload) => TResponse | Promise<TResponse>; | ||
onLatestSuccess?: ActionCallback<TPayload, TResponse, TError>; | ||
onEachSuccess?: ActionCallback<TPayload, TResponse, TError>; | ||
onLatestError?: ActionCallback<TPayload, TResponse, TError>; | ||
onEachError?: ActionCallback<TPayload, TResponse, TError>; | ||
onLatestSettled?: ActionCallback<TPayload, TResponse, TError>; | ||
onEachSettled?: ActionCallback<TPayload, TResponse, TError>; | ||
maxSubmissions?: number; | ||
debug?: boolean; | ||
} | ||
type ActionCallback<TPayload, TResponse, TError> = (submission: ActionSubmission<TPayload, TResponse, TError>) => void | Promise<void>; | ||
interface Action<TKey extends string = string, TPayload = unknown, TResponse = unknown, TError = Error> { | ||
options: ActionOptions<TKey, TPayload, TResponse, TError>; | ||
submit: (payload?: TPayload) => Promise<TResponse>; | ||
reset: () => void; | ||
store: Store<ActionStore<TPayload, TResponse, TError>>; | ||
} | ||
interface ActionStore<TPayload = unknown, TResponse = unknown, TError = Error> { | ||
submissions: ActionSubmission<TPayload, TResponse, TError>[]; | ||
} | ||
type ActionFn<TActionPayload = unknown, TActionResponse = unknown> = (submission: TActionPayload) => TActionResponse | Promise<TActionResponse>; | ||
interface ActionSubmission<TPayload = unknown, TResponse = unknown, TError = Error> { | ||
submittedAt: number; | ||
status: 'idle' | 'pending' | 'success' | 'error'; | ||
payload: TPayload; | ||
response?: TResponse; | ||
error?: TError; | ||
isInvalid?: boolean; | ||
invalidate: () => void; | ||
getIsLatest: () => boolean; | ||
} | ||
declare function createAction<TKey extends string, TPayload, TResponse, TError>(options: ActionOptions<TKey, TPayload, TResponse, TError>): Action<TKey, TPayload, TResponse, TError>; | ||
export { Action, ActionFn, ActionOptions, ActionStore, ActionSubmission, ActiveOptions, AllRouteInfo, AnyAllRouteInfo, AnyLoaderData, AnyPathParams, AnyRoute, AnyRouteConfig, AnyRouteConfigWithChildren, AnyRouteInfo, AnyRouter, AnySearchSchema, BuildNextOptions, CheckId, CheckIdError, CheckPath, CheckPathError, CheckRelativePath, DeepAwaited, DefaultAllRouteInfo, DefinedPathParamWarning, DehydratedRouter, DehydratedRouterState, Expand, FilterRoutesFn, FrameworkGenerics, FromLocation, GetFrameworkGeneric, IsAny, IsAnyBoolean, IsKnown, LinkInfo, LinkOptions, ListenerFn, Loader, LoaderContext, LoaderFn, LoaderState, LocationState, MatchCache, MatchCacheEntry, MatchLocation, MatchRouteOptions, NavigateOptions, NoInfer, ParentParams, ParsePathParams, ParseRouteConfig, ParsedLocation, ParsedPath, PathParamMask, PathParamOptions, PickAsPartial, PickAsRequired, PickExclude, PickExtra, PickExtract, PickRequired, PickUnsafe, RegisterRouter, RegisteredAllRouteInfo, RegisteredRouter, RelativeToPathAutoComplete, ResolveRelativePath, RootRouteId, Route, RouteConfig, RouteConfigRoute, RouteInfo, RouteInfoById, RouteInfoByPath, RouteMatch, RouteMatchStore, RouteMeta, RouteOptions, Router, RouterContext, RouterHistory, RouterLocation, RouterOptions, RouterStore, RoutesById, RoutesInfoInner, SearchFilter, SearchParamOptions, SearchParser, SearchSchemaValidator, SearchSchemaValidatorFn, SearchSchemaValidatorObj, SearchSerializer, Segment, Split, Store, Timeout, ToIdOption, ToOptions, ToPathOption, UnionToIntersection, UnloaderFn, Updater, ValidFromPath, ValueKeys, Values, batch, cleanPath, createAction, createBrowserHistory, createHashHistory, createMemoryHistory, createRouteConfig, createStore, decode, defaultFetchServerDataFn, defaultParseSearch, defaultStringifySearch, encode, functionalUpdate, interpolatePath, joinPaths, last, matchByPath, matchPathname, parsePathname, parseSearchWith, pick, replaceEqualDeep, resolvePath, rootRouteId, stringifySearchWith, trackDeep, trimPath, trimPathLeft, trimPathRight, warning }; | ||
export { ActiveOptions, AnyContext, AnyPathParams, AnyRedirect, AnyRootRoute, AnyRoute, AnyRouteMatch, AnyRouteProps, AnyRouteWithRouterContext, AnyRouter, AnyRoutesInfo, AnySearchSchema, BaseRouteOptions, BuildNextOptions, CheckId, CheckIdError, CheckPath, CheckPathError, CheckRelativePath, ComponentFromRoute, ComponentPropsFromRoute, DeepAwaited, DefaultRoutesInfo, DefinedPathParamWarning, DehydratedRouter, DehydratedRouterState, Expand, FromLocation, GetKeyFn, HydrationCtx, InferFullSearchSchema, IsAny, IsAnyBoolean, IsKnown, LinkInfo, LinkOptions, ListenerFn, LoaderContext, LoaderFn, LocationState, MatchLocation, MatchRouteOptions, MergeParamsFromParent, MergeUnion, MetaOptions, NavigateOptions, NoInfer, ParamsFallback, ParentParams, ParseParamsFn, ParseParamsObj, ParseParamsOption, ParsePathParams, ParseRoute, ParseRouteChild, ParseRouteChildren, ParsedLocation, ParsedPath, PathParamError, PathParamMask, PathParamOptions, PickAsPartial, PickAsRequired, PickExclude, PickExtra, PickExtract, PickRequired, PickUnsafe, PreloadableObj, Redirect, Register, RegisterRouteComponent, RegisterRouteErrorComponent, RegisteredRouteComponent, RegisteredRouteErrorComponent, RegisteredRouter, RegisteredRouterPair, RegisteredRoutesInfo, RelativeToPathAutoComplete, ResolveFullPath, ResolveFullSearchSchema, ResolveId, ResolveRelativePath, RootRoute, RootRouteId, Route, RouteById, RouteByPath, RouteContext, RouteLoaderFromRoute, RouteMatch, RouteMeta, RouteOptions, RoutePathOptions, RoutePathOptionsIntersection, RouteProps, Router, RouterConstructorOptions, RouterContext, RouterContextOptions, RouterHistory, RouterLocation, RouterOptions, RouterState, RoutesById, RoutesByPath, RoutesInfo, RoutesInfoInner, SearchFilter, SearchParamError, SearchParamOptions, SearchParser, SearchSchemaValidator, SearchSchemaValidatorFn, SearchSchemaValidatorObj, SearchSerializer, Segment, Split, StreamedPromise, Timeout, ToIdOption, ToOptions, ToPathOption, TrimPath, TrimPathLeft, TrimPathRight, UnionToIntersection, UnloaderFn, UpdatableRouteOptions, Updater, UseLoaderResult, UseLoaderResultPromise, ValueKeys, Values, cleanPath, componentTypes, createBrowserHistory, createHashHistory, createMemoryHistory, decode, defaultParseSearch, defaultStringifySearch, encode, functionalUpdate, interpolatePath, isPlainObject, isRedirect, joinPaths, last, lazyFn, matchByPath, matchPathname, parsePathname, parseSearchWith, partialDeepEqual, pick, redirect, replaceEqualDeep, resolvePath, rootRouteId, stringifySearchWith, trimPath, trimPathLeft, trimPathRight }; |
/** | ||
* router-core | ||
* @tanstack/router-core/src/index.ts | ||
* | ||
@@ -11,3 +11,13 @@ * Copyright (c) TanStack | ||
*/ | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).RouterCore={})}(this,(function(t){"use strict";function e(t,e){if(!t)throw new Error("Invariant failed")}const r="popstate";function a(t){let e=t.getLocation(),r=()=>{},a=new Set;const o=()=>{e=t.getLocation(),a.forEach((t=>t()))};return{get location(){return e},listen:e=>(0===a.size&&(r=t.listener(o)),a.add(e),()=>{a.delete(e),0===a.size&&r()}),push:(e,r)=>{t.pushState(e,r),o()},replace:(e,r)=>{t.replaceState(e,r),o()},go:e=>{t.go(e),o()},back:()=>{t.back(),o()},forward:()=>{t.forward(),o()}}}function o(t){const e=t?.getHref??(()=>`${window.location.pathname}${window.location.hash}${window.location.search}`),o=t?.createHref??(t=>t);return a({getLocation:()=>n(e(),history.state),listener:t=>(window.addEventListener(r,t),()=>{window.removeEventListener(r,t)}),pushState:(t,e)=>{window.history.pushState({...e,key:i()},"",o(t))},replaceState:(t,e)=>{window.history.replaceState({...e,key:i()},"",o(t))},back:()=>window.history.back(),forward:()=>window.history.forward(),go:t=>window.history.go(t)})}function s(t={initialEntries:["/"]}){const e=t.initialEntries;let r=t.initialIndex??e.length-1,o={};return a({getLocation:()=>n(e[r],o),listener:()=>()=>{},pushState:(t,a)=>{o={...a,key:i()},e.push(t),r++},replaceState:(t,a)=>{o={...a,key:i()},e[r]=t},back:()=>{r--},forward:()=>{r=Math.min(r+1,e.length-1)},go:t=>window.history.go(t)})}function n(t,e){let r=t.indexOf("#"),a=t.indexOf("?");return{href:t,pathname:t.substring(0,r>0?a>0?Math.min(r,a):r:a>0?a:t.length),hash:r>-1?t.substring(r,a):"",search:a>-1?t.substring(a):"",state:e}}function i(){return(Math.random()+1).toString(36).substring(7)}function c(t){return t[t.length-1]}function h(t,e){return function(t){return"function"==typeof t}(t)?t(e):t}function u(t,e){return e.reduce(((e,r)=>(e[r]=t[r],e)),{})}function l(t){return d(t.filter(Boolean).join("/"))}function d(t){return t.replace(/\/{2,}/g,"/")}function f(t){return"/"===t?t:t.replace(/^\/{1,}/,"")}function p(t){return"/"===t?t:t.replace(/\/{1,}$/,"")}function m(t){return p(f(t))}function g(t,e,r){e=e.replace(new RegExp(`^${t}`),"/"),r=r.replace(new RegExp(`^${t}`),"/");let a=y(e);const o=y(r);o.forEach(((t,e)=>{if("/"===t.value)e?e===o.length-1&&a.push(t):a=[t];else if(".."===t.value)a.length>1&&"/"===c(a)?.value&&a.pop(),a.pop();else{if("."===t.value)return;a.push(t)}}));const s=l([t,...a.map((t=>t.value))]);return d(s)}function y(t){if(!t)return[];const e=[];if("/"===(t=d(t)).slice(0,1)&&(t=t.substring(1),e.push({type:"pathname",value:"/"})),!t)return e;const r=t.split("/").filter(Boolean);return e.push(...r.map((t=>t.startsWith("*")?{type:"wildcard",value:t}:"$"===t.charAt(0)?{type:"param",value:t}:{type:"pathname",value:t}))),"/"===t.slice(-1)&&(t=t.substring(1),e.push({type:"pathname",value:"/"})),e}function v(t,e,r){return l(y(t).map((t=>"*"!==t.value||r?"param"===t.type?e[t.value.substring(1)]??"":t.value:"")))}function b(t,e,r){const a=w(t,e,r);if(!r.to||a)return a??{}}function w(t,e,r){if(!e.startsWith(t))return;const a=y(e="/"!=t?e.substring(t.length):e),o=y(`${r.to??"*"}`),s={};let n=(()=>{for(let t=0;t<Math.max(a.length,o.length);t++){const e=a[t],n=o[t],i=t===o.length-1,c=t===a.length-1;if(n){if("wildcard"===n.type)return!!e?.value&&(s["*"]=l(a.slice(t).map((t=>t.value))),!0);if("pathname"===n.type){if("/"===n.value&&!e?.value)return!0;if(e)if(r.caseSensitive){if(n.value!==e.value)return!1}else if(n.value.toLowerCase()!==e.value.toLowerCase())return!1}if(!e)return!1;if("param"===n.type){if("/"===e?.value)return!1;"$"!==e.value.charAt(0)&&(s[n.value.substring(1)]=e.value)}}if(i&&!c)return!!r.fuzzy}return!0})();return n?s:void 0}function S(t,e){var r,a,o,s="";for(r in t)if(void 0!==(o=t[r]))if(Array.isArray(o))for(a=0;a<o.length;a++)s&&(s+="&"),s+=encodeURIComponent(r)+"="+encodeURIComponent(o[a]);else s&&(s+="&"),s+=encodeURIComponent(r)+"="+encodeURIComponent(o);return(e||"")+s}function P(t){if(!t)return"";var e=decodeURIComponent(t);return"false"!==e&&("true"===e||("0"===e.charAt(0)?e:0*+e==0?+e:e))}function L(t){for(var e,r,a={},o=t.split("&");e=o.shift();)void 0!==a[r=(e=e.split("=")).shift()]?a[r]=[].concat(a[r],P(e.shift())):a[r]=P(e.shift());return a}class A{constructor(t,e,r,a,o){Object.assign(this,{...t,originalIndex:r,options:e,getRouter:()=>o,childRoutes:void 0,getParentRoute:()=>a}),o.options.createRoute?.({router:o,route:this})}}const _="__root__",M=(t={},r=[],a=!0,o,s)=>{a&&(t.path=_),o===_&&(o="");let n=a?_:t.path;n&&"/"!==n&&(n=m(n));const i=n||t.id;let c=l([o,i]);n===_&&(n="/"),c!==_&&(c=l(["/",c]));const h=c===_?"/":p(l([s,n]));return{id:c,routeId:i,path:n,fullPath:h,options:t,children:r,addChildren:e=>M(t,e,!1,o,s),createRoute:t=>M(t,void 0,!1,c,h),generate:()=>{e(!1)}}};function x(t){for(var e=arguments.length,r=Array(e>1?e-1:0),a=1;a<e;a++)r[a-1]=arguments[a];throw Error("[Immer] minified error nr: "+t+(r.length?" "+r.map((function(t){return"'"+t+"'"})).join(","):"")+". Find the full error at: https://bit.ly/3cXEKWf")}function D(t){return!!t&&!!t[ft]}function O(t){var e;return!!t&&(function(t){if(!t||"object"!=typeof t)return!1;var e=Object.getPrototypeOf(t);if(null===e)return!0;var r=Object.hasOwnProperty.call(e,"constructor")&&e.constructor;return r===Object||"function"==typeof r&&Function.toString.call(r)===pt}(t)||Array.isArray(t)||!!t[dt]||!!(null===(e=t.constructor)||void 0===e?void 0:e[dt])||I(t)||C(t))}function E(t,e,r){void 0===r&&(r=!1),0===R(t)?(r?Object.keys:mt)(t).forEach((function(a){r&&"symbol"==typeof a||e(a,t[a],t)})):t.forEach((function(r,a){return e(a,r,t)}))}function R(t){var e=t[ft];return e?e.i>3?e.i-4:e.i:Array.isArray(t)?1:I(t)?2:C(t)?3:0}function j(t,e){return 2===R(t)?t.has(e):Object.prototype.hasOwnProperty.call(t,e)}function F(t,e,r){var a=R(t);2===a?t.set(e,r):3===a?(t.delete(e),t.add(r)):t[e]=r}function I(t){return ct&&t instanceof Map}function C(t){return ht&&t instanceof Set}function k(t){return t.o||t.t}function $(t){if(Array.isArray(t))return Array.prototype.slice.call(t);var e=gt(t);delete e[ft];for(var r=mt(e),a=0;a<r.length;a++){var o=r[a],s=e[o];!1===s.writable&&(s.writable=!0,s.configurable=!0),(s.get||s.set)&&(e[o]={configurable:!0,writable:!0,enumerable:s.enumerable,value:t[o]})}return Object.create(Object.getPrototypeOf(t),e)}function T(t,e){return void 0===e&&(e=!1),H(t)||D(t)||!O(t)||(R(t)>1&&(t.set=t.add=t.clear=t.delete=N),Object.freeze(t),e&&E(t,(function(t,e){return T(e,!0)}),!0)),t}function N(){x(2)}function H(t){return null==t||"object"!=typeof t||Object.isFrozen(t)}function z(t){var e=yt[t];return e||x(18,t),e}function U(){return nt}function B(t,e){e&&(z("Patches"),t.u=[],t.s=[],t.v=e)}function K(t){G(t),t.p.forEach(J),t.p=null}function G(t){t===nt&&(nt=t.l)}function W(t){return nt={p:[],l:nt,h:t,m:!0,_:0}}function J(t){var e=t[ft];0===e.i||1===e.i?e.j():e.O=!0}function X(t,e){e._=e.p.length;var r=e.p[0],a=void 0!==t&&t!==r;return e.h.g||z("ES5").S(e,t,a),a?(r[ft].P&&(K(e),x(4)),O(t)&&(t=q(e,t),e.l||Q(e,t)),e.u&&z("Patches").M(r[ft].t,t,e.u,e.s)):t=q(e,r,[]),K(e),e.u&&e.v(e.u,e.s),t!==lt?t:void 0}function q(t,e,r){if(H(e))return e;var a=e[ft];if(!a)return E(e,(function(o,s){return V(t,a,e,o,s,r)}),!0),e;if(a.A!==t)return e;if(!a.P)return Q(t,a.t,!0),a.t;if(!a.I){a.I=!0,a.A._--;var o=4===a.i||5===a.i?a.o=$(a.k):a.o;E(3===a.i?new Set(o):o,(function(e,s){return V(t,a,o,e,s,r)})),Q(t,o,!1),r&&t.u&&z("Patches").R(a,r,t.u,t.s)}return a.o}function V(t,e,r,a,o,s){if(D(o)){var n=q(t,o,s&&e&&3!==e.i&&!j(e.D,a)?s.concat(a):void 0);if(F(r,a,n),!D(n))return;t.m=!1}if(O(o)&&!H(o)){if(!t.h.F&&t._<1)return;q(t,o),e&&e.A.l||Q(t,o)}}function Q(t,e,r){void 0===r&&(r=!1),t.h.F&&t.m&&T(e,r)}function Y(t,e){var r=t[ft];return(r?k(r):t)[e]}function Z(t,e){if(e in t)for(var r=Object.getPrototypeOf(t);r;){var a=Object.getOwnPropertyDescriptor(r,e);if(a)return a;r=Object.getPrototypeOf(r)}}function tt(t){t.P||(t.P=!0,t.l&&tt(t.l))}function et(t){t.o||(t.o=$(t.t))}function rt(t,e,r){var a=I(e)?z("MapSet").N(e,r):C(e)?z("MapSet").T(e,r):t.g?function(t,e){var r=Array.isArray(t),a={i:r?1:0,A:e?e.A:U(),P:!1,I:!1,D:{},l:e,t:t,k:null,o:null,j:null,C:!1},o=a,s=vt;r&&(o=[a],s=bt);var n=Proxy.revocable(o,s),i=n.revoke,c=n.proxy;return a.k=c,a.j=i,c}(e,r):z("ES5").J(e,r);return(r?r.A:U()).p.push(a),a}function at(t){return D(t)||x(22,t),function t(e){if(!O(e))return e;var r,a=e[ft],o=R(e);if(a){if(!a.P&&(a.i<4||!z("ES5").K(a)))return a.t;a.I=!0,r=ot(e,o),a.I=!1}else r=ot(e,o);return E(r,(function(e,o){a&&function(t,e){return 2===R(t)?t.get(e):t[e]}(a.t,e)===o||F(r,e,t(o))})),3===o?new Set(r):r}(t)}function ot(t,e){switch(e){case 2:return new Map(t);case 3:return Array.from(t)}return $(t)}var st,nt,it="undefined"!=typeof Symbol&&"symbol"==typeof Symbol("x"),ct="undefined"!=typeof Map,ht="undefined"!=typeof Set,ut="undefined"!=typeof Proxy&&void 0!==Proxy.revocable&&"undefined"!=typeof Reflect,lt=it?Symbol.for("immer-nothing"):((st={})["immer-nothing"]=!0,st),dt=it?Symbol.for("immer-draftable"):"__$immer_draftable",ft=it?Symbol.for("immer-state"):"__$immer_state",pt=""+Object.prototype.constructor,mt="undefined"!=typeof Reflect&&Reflect.ownKeys?Reflect.ownKeys:void 0!==Object.getOwnPropertySymbols?function(t){return Object.getOwnPropertyNames(t).concat(Object.getOwnPropertySymbols(t))}:Object.getOwnPropertyNames,gt=Object.getOwnPropertyDescriptors||function(t){var e={};return mt(t).forEach((function(r){e[r]=Object.getOwnPropertyDescriptor(t,r)})),e},yt={},vt={get:function(t,e){if(e===ft)return t;var r=k(t);if(!j(r,e))return function(t,e,r){var a,o=Z(e,r);return o?"value"in o?o.value:null===(a=o.get)||void 0===a?void 0:a.call(t.k):void 0}(t,r,e);var a=r[e];return t.I||!O(a)?a:a===Y(t.t,e)?(et(t),t.o[e]=rt(t.A.h,a,t)):a},has:function(t,e){return e in k(t)},ownKeys:function(t){return Reflect.ownKeys(k(t))},set:function(t,e,r){var a=Z(k(t),e);if(null==a?void 0:a.set)return a.set.call(t.k,r),!0;if(!t.P){var o=Y(k(t),e),s=null==o?void 0:o[ft];if(s&&s.t===r)return t.o[e]=r,t.D[e]=!1,!0;if(function(t,e){return t===e?0!==t||1/t==1/e:t!=t&&e!=e}(r,o)&&(void 0!==r||j(t.t,e)))return!0;et(t),tt(t)}return t.o[e]===r&&"number"!=typeof r&&(void 0!==r||e in t.o)||(t.o[e]=r,t.D[e]=!0,!0)},deleteProperty:function(t,e){return void 0!==Y(t.t,e)||e in t.t?(t.D[e]=!1,et(t),tt(t)):delete t.D[e],t.o&&delete t.o[e],!0},getOwnPropertyDescriptor:function(t,e){var r=k(t),a=Reflect.getOwnPropertyDescriptor(r,e);return a?{writable:!0,configurable:1!==t.i||"length"!==e,enumerable:a.enumerable,value:r[e]}:a},defineProperty:function(){x(11)},getPrototypeOf:function(t){return Object.getPrototypeOf(t.t)},setPrototypeOf:function(){x(12)}},bt={};E(vt,(function(t,e){bt[t]=function(){return arguments[0]=arguments[0][0],e.apply(this,arguments)}})),bt.deleteProperty=function(t,e){return bt.set.call(this,t,e,void 0)},bt.set=function(t,e,r){return vt.set.call(this,t[0],e,r,t[0])};var wt=function(){function t(t){var e=this;this.g=ut,this.F=!0,this.produce=function(t,r,a){if("function"==typeof t&&"function"!=typeof r){var o=r;r=t;var s=e;return function(t){var e=this;void 0===t&&(t=o);for(var a=arguments.length,n=Array(a>1?a-1:0),i=1;i<a;i++)n[i-1]=arguments[i];return s.produce(t,(function(t){var a;return(a=r).call.apply(a,[e,t].concat(n))}))}}var n;if("function"!=typeof r&&x(6),void 0!==a&&"function"!=typeof a&&x(7),O(t)){var i=W(e),c=rt(e,t,void 0),h=!0;try{n=r(c),h=!1}finally{h?K(i):G(i)}return"undefined"!=typeof Promise&&n instanceof Promise?n.then((function(t){return B(i,a),X(t,i)}),(function(t){throw K(i),t})):(B(i,a),X(n,i))}if(!t||"object"!=typeof t){if(void 0===(n=r(t))&&(n=t),n===lt&&(n=void 0),e.F&&T(n,!0),a){var u=[],l=[];z("Patches").M(t,n,u,l),a(u,l)}return n}x(21,t)},this.produceWithPatches=function(t,r){if("function"==typeof t)return function(r){for(var a=arguments.length,o=Array(a>1?a-1:0),s=1;s<a;s++)o[s-1]=arguments[s];return e.produceWithPatches(r,(function(e){return t.apply(void 0,[e].concat(o))}))};var a,o,s=e.produce(t,r,(function(t,e){a=t,o=e}));return"undefined"!=typeof Promise&&s instanceof Promise?s.then((function(t){return[t,a,o]})):[s,a,o]},"boolean"==typeof(null==t?void 0:t.useProxies)&&this.setUseProxies(t.useProxies),"boolean"==typeof(null==t?void 0:t.autoFreeze)&&this.setAutoFreeze(t.autoFreeze)}var e=t.prototype;return e.createDraft=function(t){O(t)||x(8),D(t)&&(t=at(t));var e=W(this),r=rt(this,t,void 0);return r[ft].C=!0,G(e),r},e.finishDraft=function(t,e){var r=(t&&t[ft]).A;return B(r,e),X(void 0,r)},e.setAutoFreeze=function(t){this.F=t},e.setUseProxies=function(t){t&&!ut&&x(20),this.g=t},e.applyPatches=function(t,e){var r;for(r=e.length-1;r>=0;r--){var a=e[r];if(0===a.path.length&&"replace"===a.op){t=a.value;break}}r>-1&&(e=e.slice(r+1));var o=z("Patches").$;return D(t)?o(t,e):this.produce(t,(function(t){return o(t,e)}))},t}(),St=new wt,Pt=St.produce;St.produceWithPatches.bind(St);var Lt=St.setAutoFreeze.bind(St);St.setUseProxies.bind(St),St.applyPatches.bind(St),St.createDraft.bind(St),St.finishDraft.bind(St),Lt(!1);let At=[],_t=!1;function Mt(){_t||(At.forEach((t=>t())),At=[])}function xt(t,e){const r=new Set,a={state:t,subscribe:t=>(r.add(t),()=>r.delete(t)),setState:t=>{const o=a.state;a.state=Pt((e=>{t(e)}))(o),e&&console.log(a.state),At.push((()=>r.forEach((t=>t(a.state,o))))),Mt()}};return a}function Dt(t){_t=!0,t(),_t=!1,Mt()}function Ot(t,e){if(t===e)return t;const r=e,a=Array.isArray(t)&&Array.isArray(r);if(a||Et(t)&&Et(r)){const e=a?t.length:Object.keys(t).length,o=a?r:Object.keys(r),s=o.length,n=a?[]:{};let i=0;for(let e=0;e<s;e++){const s=a?e:o[e];n[s]=Ot(t[s],r[s]),n[s]===t[s]&&i++}return e===s&&i===e?t:n}return r}function Et(t){if(!Rt(t))return!1;const e=t.constructor;if(void 0===e)return!0;const r=e.prototype;return!!Rt(r)&&!!r.hasOwnProperty("isPrototypeOf")}function Rt(t){return"[object Object]"===Object.prototype.toString.call(t)}const jt=["component","errorComponent","pendingComponent"];class Ft{abortController=new AbortController;#t="";#e=()=>{};onLoaderDataListeners=new Set;constructor(t,e,r){Object.assign(this,{route:e,router:t,id:r.id,pathname:r.pathname,params:r.params,store:xt({routeSearch:{},search:{},status:"idle",routeLoaderData:{},loaderData:{},isFetching:!1,invalid:!1,invalidAt:1/0})}),this.__hasLoaders()||this.store.setState((t=>t.status="success"))}setLoaderData=t=>{Dt((()=>{this.store.setState((e=>{e.routeLoaderData=t})),this.#r()}))};cancel=()=>{this.abortController?.abort()};load=async t=>{const e=Date.now(),r=t?.preload?Math.max(t?.maxAge,t?.gcMaxAge):0;if(t?.preload&&r>0){if(this.router.store.state.currentMatches.find((t=>t.id===this.id)))return;this.router.store.setState((r=>{r.matchCache[this.id]={gc:e+t.gcMaxAge,match:this}}))}if("success"===this.store.state.status&&this.getIsInvalid()||"error"===this.store.state.status||"idle"===this.store.state.status){const e=t?.preload?t?.maxAge:void 0;await this.fetch({maxAge:e})}};fetch=async t=>(this.__loadPromise=new Promise((async e=>{const r=""+Date.now()+Math.random();this.#t=r;const a=()=>r!==this.#t?this.__loadPromise?.then((()=>e())):void 0;let o;Dt((()=>{"idle"===this.store.state.status&&this.store.setState((t=>t.status="loading")),this.store.setState((t=>t.invalid=!1))})),this.store.setState((t=>t.isFetching=!0)),this.#e=e;const s=(async()=>{await Promise.all(jt.map((async t=>{const e=this.route.options[t];this[t]?.preload&&(this[t]=await this.router.options.loadComponent(e))})))})(),n=Promise.resolve().then((async()=>{try{if(this.route.options.loader){const t=await this.router.loadMatchData(this);if(o=a())return o;this.setLoaderData(t)}return this.store.setState((e=>{e.error=void 0,e.status="success",e.updatedAt=Date.now(),e.invalidAt=e.updatedAt+(t?.maxAge??this.route.options.loaderMaxAge??this.router.options.defaultLoaderMaxAge??0)})),this.store.state.routeLoaderData}catch(t){if(o=a())return o;throw this.store.setState((e=>{e.error=t,e.status="error",e.updatedAt=Date.now()})),t}})),i=async()=>{if(o=a())return o;this.store.setState((t=>t.isFetching=!1)),this.#e(),delete this.__loadPromise};try{await Promise.all([s,n.catch((()=>{}))]),i()}catch{i()}})),this.__loadPromise);invalidate=async()=>{this.store.setState((t=>t.invalid=!0)),this.router.store.state.currentMatches.find((t=>t.id===this.id))&&await this.load()};__hasLoaders=()=>!(!this.route.options.loader&&!jt.some((t=>this.route.options[t]?.preload)));getIsInvalid=()=>{const t=Date.now();return this.store.state.invalid||this.store.state.invalidAt<t};#r=()=>{this.store.setState((t=>{t.loaderData=Ot(t.loaderData,{...this.parentMatch?.store.state.loaderData,...t.routeLoaderData})})),this.onLoaderDataListeners.forEach((t=>t()))};__setParentMatch=t=>{!this.parentMatch&&t&&(this.parentMatch=t,this.parentMatch.__onLoaderData((()=>{this.#r()})))};__onLoaderData=t=>{this.onLoaderDataListeners.add(t)};__validate=()=>{const t=this.parentMatch?.store.state.search??this.router.store.state.latestLocation.search;try{const e=this.store.state.routeSearch;let r=("object"==typeof this.route.options.validateSearch?this.route.options.validateSearch.parse:this.route.options.validateSearch)?.(t)??{};Dt((()=>{e!==r&&this.store.setState((t=>t.invalid=!0)),this.store.setState((e=>{e.routeSearch=r,e.search={...t,...r}}))})),jt.map((async t=>{const e=this.route.options[t];"function"!=typeof this[t]&&(this[t]=e)}))}catch(t){console.error(t);const e=new Error("Invalid search params found",{cause:t});return e.code="INVALID_SEARCH_PARAMS",void this.store.setState((t=>{t.status="error",t.error=e}))}}}const It=kt(JSON.parse),Ct=$t(JSON.stringify);function kt(t){return e=>{"?"===e.substring(0,1)&&(e=e.substring(1));let r=L(e);for(let e in r){const a=r[e];if("string"==typeof a)try{r[e]=t(a)}catch(t){}}return r}}function $t(t){return e=>{(e={...e})&&Object.keys(e).forEach((r=>{const a=e[r];if(void 0===a||void 0===a)delete e[r];else if(a&&"object"==typeof a&&null!==a)try{e[r]=t(a)}catch(t){}}));const r=S(e).toString();return r?`?${r}`:""}}const Tt=async({router:t,routeMatch:e})=>{const r=t.buildNext({to:".",search:t=>({...t??{},__data:{matchId:e.id}})}),a=await fetch(r.href,{method:"GET",signal:e.abortController.signal});if(a.ok)return a.json();throw new Error("Failed to fetch match data")};const Nt="undefined"==typeof window||!window.document.createElement;function Ht(){return{status:"idle",latestLocation:null,currentLocation:null,currentMatches:[],loaders:{},lastUpdated:Date.now(),matchCache:{},get isFetching(){return"loading"===this.status||this.currentMatches.some((t=>t.store.state.isFetching))},get isPreloading(){return Object.values(this.matchCache).some((t=>t.match.store.state.isFetching&&!this.currentMatches.find((e=>e.id===t.match.id))))}}}t.Route=A,t.RouteMatch=Ft,t.Router=class{#a;startedLoadingAt=Date.now();resolveNavigation=()=>{};constructor(t){this.options={defaultLoaderGcMaxAge:3e5,defaultLoaderMaxAge:0,defaultPreloadMaxAge:2e3,defaultPreloadDelay:50,context:void 0,...t,stringifySearch:t?.stringifySearch??Ct,parseSearch:t?.parseSearch??It,fetchServerDataFn:t?.fetchServerDataFn??Tt},this.store=xt(Ht()),this.basepath="",this.update(t),this.options.Router?.(this)}reset=()=>{this.store.setState((t=>Object.assign(t,Ht())))};mount=()=>{if(!Nt){this.store.state.currentMatches.length||this.load();const t="visibilitychange",e="focus";return window.addEventListener&&(window.addEventListener(t,this.#o,!1),window.addEventListener(e,this.#o,!1)),()=>{window.removeEventListener&&(window.removeEventListener(t,this.#o),window.removeEventListener(e,this.#o))}}return()=>{}};update=t=>{Object.assign(this.options,t),(!this.history||this.options.history&&this.options.history!==this.history)&&(this.#a&&this.#a(),this.history=this.options.history??(Nt?s():o()),this.store.setState((t=>{t.latestLocation=this.#s(),t.currentLocation=t.latestLocation})),this.#a=this.history.listen((()=>{this.load(this.#s(this.store.state.latestLocation))})));const{basepath:e,routeConfig:r}=this.options;return this.basepath=`/${m(e??"")??""}`,r&&(this.routesById={},this.routeTree=this.#n(r)),this};buildNext=t=>{const e=this.#i(t),r=this.matchRoutes(e.pathname),a=r.map((t=>t.route.options.preSearchFilters??[])).flat().filter(Boolean),o=r.map((t=>t.route.options.postSearchFilters??[])).flat().filter(Boolean);return this.#i({...t,__preSearchFilters:a,__postSearchFilters:o})};cancelMatches=()=>{[...this.store.state.currentMatches,...this.store.state.pendingMatches||[]].forEach((t=>{t.cancel()}))};load=async t=>{let r=Date.now();const a=r;let o;this.startedLoadingAt=a,this.cancelMatches(),Dt((()=>{t&&this.store.setState((e=>{e.latestLocation=t})),o=this.matchRoutes(this.store.state.latestLocation.pathname,{strictParseParams:!0}),this.store.setState((t=>{t.status="loading",t.pendingMatches=o,t.pendingLocation=this.store.state.latestLocation}))}));try{await this.loadMatches(o)}catch(t){console.warn(t),e(!1)}if(this.startedLoadingAt!==a)return this.navigationPromise;const s=this.store.state.currentMatches,n=[],i=[];s.forEach((t=>{o.find((e=>e.id===t.id))?i.push(t):n.push(t)}));const c=o.filter((t=>!s.find((e=>e.id===t.id))));r=Date.now(),n.forEach((t=>{t.__onExit?.({params:t.params,search:t.store.state.routeSearch}),"error"!==t.store.state.status||t.store.state.isFetching||t.store.setState((t=>{t.status="idle",t.error=void 0}));const e=Math.max(t.route.options.loaderGcMaxAge??this.options.defaultLoaderGcMaxAge??0,t.route.options.loaderMaxAge??this.options.defaultLoaderMaxAge??0);e>0&&this.store.setState((a=>{a.matchCache[t.id]={gc:e==1/0?Number.MAX_SAFE_INTEGER:r+e,match:t}}))})),i.forEach((t=>{t.route.options.onTransition?.({params:t.params,search:t.store.state.routeSearch})})),c.forEach((t=>{t.__onExit=t.route.options.onLoaded?.({params:t.params,search:t.store.state.search}),delete this.store.state.matchCache[t.id]})),this.store.setState((t=>{Object.assign(t,{status:"idle",currentLocation:this.store.state.latestLocation,currentMatches:o,pendingLocation:void 0,pendingMatches:void 0})})),this.options.onRouteChange?.(),this.resolveNavigation()};cleanMatchCache=()=>{const t=Date.now();this.store.setState((e=>{Object.keys(e.matchCache).forEach((r=>{const a=e.matchCache[r];"loading"!==a.match.store.state.status&&(a.gc>0&&a.gc>t||delete e.matchCache[r])}))}))};getRoute=t=>{const r=this.routesById[t];return e(r),r};loadRoute=async(t=this.store.state.latestLocation)=>{const e=this.buildNext(t),r=this.matchRoutes(e.pathname,{strictParseParams:!0});return await this.loadMatches(r),r};preloadRoute=async(t=this.store.state.latestLocation,e)=>{const r=this.buildNext(t),a=this.matchRoutes(r.pathname,{strictParseParams:!0});return await this.loadMatches(a,{preload:!0,maxAge:e.maxAge??this.options.defaultPreloadMaxAge??this.options.defaultLoaderMaxAge??0,gcMaxAge:e.gcMaxAge??this.options.defaultPreloadGcMaxAge??this.options.defaultLoaderGcMaxAge??0}),a};matchRoutes=(t,e)=>{const r=[];if(!this.routeTree)return r;const a=[...this.store.state.currentMatches,...this.store.state.pendingMatches??[]],o=async s=>{let n=c(r)?.params??{};const i=this.options.filterRoutes?.(s)??s;let h=[];const u=(r,a)=>(a.some((a=>{if(!a.path&&a.childRoutes?.length)return u([...h,a],a.childRoutes);const o=!("/"===a.path&&!a.childRoutes?.length),s=b(this.basepath,t,{to:a.fullPath,fuzzy:o,caseSensitive:a.options.caseSensitive??this.options.caseSensitive});if(s){let t;try{t=a.options.parseParams?.(s)??s}catch(t){if(e?.strictParseParams)throw t}n={...n,...t}}return s&&(h=[...r,a]),!!h.length})),!!h.length);if(u([],i),!h.length)return;h.forEach((t=>{const e=v(t.path,n),o=v(t.id,n,!0),s=a.find((t=>t.id===o))||this.store.state.matchCache[o]?.match||new Ft(this,t,{id:o,params:n,pathname:l([this.basepath,e])});r.push(s)}));const d=c(h);d.childRoutes?.length&&o(d.childRoutes)};return o([this.routeTree]),function(t){t.forEach(((e,r)=>{const a=t[r-1];a&&e.__setParentMatch(a)}))}(r),r};loadMatches=async(t,e)=>{this.cleanMatchCache(),t.forEach((async t=>{t.__validate()})),await Promise.all(t.map((async t=>{try{await(t.route.options.beforeLoad?.({router:this,match:t}))}catch(r){throw e?.preload||t.route.options.onLoadError?.(r),r}})));const r=t.map((async(r,a)=>{const o=t[1],s=r.store.state.search;s.__data?.matchId&&s.__data.matchId!==r.id||(r.load(e),"success"!==r.store.state.status&&r.__loadPromise&&await r.__loadPromise,o&&await o.__loadPromise)}));await Promise.all(r)};loadMatchData=async t=>{if(Nt||!this.options.useServerData)return await(t.route.options.loader?.({params:t.params,search:t.store.state.routeSearch,signal:t.abortController.signal}))||{};return await this.options.fetchServerDataFn({router:this,routeMatch:t})};invalidateRoute=async t=>{const e=this.buildNext(t),r=this.matchRoutes(e.pathname).map((t=>t.id));await Promise.allSettled([...this.store.state.currentMatches,...this.store.state.pendingMatches??[]].map((async t=>{if(r.includes(t.id))return t.invalidate()})))};reload=()=>{this.navigate({fromCurrent:!0,replace:!0,search:!0})};resolvePath=(t,e)=>g(this.basepath,t,d(e));navigate=async({from:t,to:r=".",search:a,hash:o,replace:s,params:n})=>{const i=String(r),c=void 0===t?t:String(t);let h;try{new URL(`${i}`),h=!0}catch(t){}return e(!h),this.#c({from:c,to:i,search:a,hash:o,replace:s,params:n})};matchRoute=(t,e)=>{t={...t,to:t.to?this.resolvePath(t.from??"",t.to):void 0};const r=this.buildNext(t);return e?.pending?!!this.store.state.pendingLocation&&b(this.basepath,this.store.state.pendingLocation.pathname,{...e,to:r.pathname}):b(this.basepath,this.store.state.currentLocation.pathname,{...e,to:r.pathname})};buildLink=({from:t,to:e=".",search:r,params:a,hash:o,target:s,replace:n,activeOptions:i,preload:c,preloadMaxAge:h,preloadGcMaxAge:u,preloadDelay:l,disabled:d})=>{try{return new URL(`${e}`),{type:"external",href:e}}catch(t){}const f={from:t,to:e,search:r,params:a,hash:o,replace:n},p=this.buildNext(f);c=c??this.options.defaultPreload;const m=l??this.options.defaultPreloadDelay??0,g=this.store.state.currentLocation.pathname===p.pathname,y=this.store.state.currentLocation.pathname.split("/"),v=p.pathname.split("/").every(((t,e)=>t===y[e])),b=this.store.state.currentLocation.hash===p.hash;return{type:"internal",next:p,handleFocus:t=>{c&&this.preloadRoute(f,{maxAge:h,gcMaxAge:u}).catch((t=>{console.warn(t),console.warn("Error preloading route! ☝️")}))},handleClick:t=>{d||function(t){return!!(t.metaKey||t.altKey||t.ctrlKey||t.shiftKey)}(t)||t.defaultPrevented||s&&"_self"!==s||0!==t.button||(t.preventDefault(),!g||r||o||this.invalidateRoute(f),this.#c(f))},handleEnter:t=>{const e=t.target||{};if(c){if(e.preloadTimeout)return;e.preloadTimeout=setTimeout((()=>{e.preloadTimeout=null,this.preloadRoute(f,{maxAge:h,gcMaxAge:u}).catch((t=>{console.warn(t),console.warn("Error preloading route! ☝️")}))}),m)}},handleLeave:t=>{const e=t.target||{};e.preloadTimeout&&(clearTimeout(e.preloadTimeout),e.preloadTimeout=null)},isActive:(i?.exact?g:v)&&(!i?.includeHash||b),disabled:d}};dehydrate=()=>({state:{...u(this.store.state,["latestLocation","currentLocation","status","lastUpdated"]),currentMatches:this.store.state.currentMatches.map((t=>({id:t.id,state:{...u(t.store.state,["status","routeLoaderData","invalidAt","invalid"])}})))},context:this.options.context});hydrate=t=>{this.store.setState((r=>{this.options.context=t.context;const a=this.matchRoutes(t.state.latestLocation.pathname,{strictParseParams:!0});a.forEach(((r,a)=>{const o=t.state.currentMatches[a];e(o&&o.id===r.id),r.store.setState((t=>{Object.assign(t,o.state)})),r.setLoaderData(o.state.routeLoaderData)})),a.forEach((t=>t.__validate())),Object.assign(r,{...t.state,currentMatches:a})}))};getLoader=t=>{const e=t.from||"/",r=this.getRoute(e);if(!r)return;let a=this.store.state.loaders[e]||(()=>(this.store.setState((t=>{t.loaders[e]={pending:[],fetch:async t=>{if(!r)return;const a={loadedAt:Date.now(),loaderContext:t};this.store.setState((t=>{t.loaders[e].current=a,t.loaders[e].latest=a,t.loaders[e].pending.push(a)}));try{return await(r.options.loader?.(t))}finally{this.store.setState((t=>{t.loaders[e].pending=t.loaders[e].pending.filter((t=>t!==a))}))}}}})),this.store.state.loaders[e]))();return a};#n=t=>{const e=(t,r)=>t.map(((t,a)=>{const o=t.options,s=new A(t,o,a,r,this);if(this.routesById[s.id])throw new Error;this.routesById[s.id]=s;const n=t.children;return s.childRoutes=n.length?e(n,s):void 0,s}));return e([t])[0]};#s=t=>{let{pathname:e,search:r,hash:a,state:o}=this.history.location;const s=this.options.parseSearch(r);return{pathname:e,searchStr:r,search:Ot(t?.search,s),hash:a.split("#").reverse()[0]??"",href:`${e}${r}${a}`,state:o,key:o?.key||"__init__"}};#o=()=>{this.load()};#i=(t={})=>{const e=t.fromCurrent?this.store.state.latestLocation.pathname:t.from??this.store.state.latestLocation.pathname;let r=g(this.basepath??"/",e,`${t.to??"."}`);const a=this.matchRoutes(this.store.state.latestLocation.pathname,{strictParseParams:!0}),o=this.matchRoutes(r),s={...c(a)?.params};let n=!0===(t.params??!0)?s:h(t.params,s);n&&o.map((t=>t.route.options.stringifyParams)).filter(Boolean).forEach((t=>{Object.assign({},n,t(n))})),r=v(r,n??{});const i=t.__preSearchFilters?.length?t.__preSearchFilters?.reduce(((t,e)=>e(t)),this.store.state.latestLocation.search):this.store.state.latestLocation.search,u=!0===t.search?i:t.search?h(t.search,i)??{}:t.__preSearchFilters?.length?i:{},l=t.__postSearchFilters?.length?t.__postSearchFilters.reduce(((t,e)=>e(t)),u):u,d=Ot(this.store.state.latestLocation.search,l),f=this.options.stringifySearch(d);let p=!0===t.hash?this.store.state.latestLocation.hash:h(t.hash,this.store.state.latestLocation.hash);return p=p?`#${p}`:"",{pathname:r,search:d,searchStr:f,state:this.store.state.latestLocation.state,hash:p,href:`${r}${f}${p}`,key:t.key}};#c=t=>{const e=this.buildNext(t),r=""+Date.now()+Math.random();this.navigateTimeout&&clearTimeout(this.navigateTimeout);let a="replace";t.replace||(a="push");this.store.state.latestLocation.href===e.href&&!e.key&&(a="replace");const o=`${e.pathname}${e.searchStr}${e.hash?`#${e.hash}`:""}`;return this.history["push"===a?"push":"replace"](o,{id:r,...e.state}),this.navigationPromise=new Promise((t=>{const e=this.resolveNavigation;this.resolveNavigation=()=>{e(),t()}}))}},t.batch=Dt,t.cleanPath=d,t.createAction=function(t){const r=xt({submissions:[]},t.debug);return{options:t,store:r,reset:()=>{r.setState((t=>{t.submissions=[]}))},submit:async a=>{const o={submittedAt:Date.now(),status:"pending",payload:a,invalidate:()=>{s((t=>{t.isInvalid=!0}))},getIsLatest:()=>r.state.submissions[r.state.submissions.length-1]?.submittedAt===o.submittedAt},s=t=>{r.setState((r=>{const a=r.submissions.find((t=>t.submittedAt===o.submittedAt));e(a),t(a)}))};r.setState((e=>{e.submissions.push(o),e.submissions.reverse(),e.submissions=e.submissions.slice(0,t.maxSubmissions??10),e.submissions.reverse()}));const n=async()=>{t.onEachSettled?.(o),o.getIsLatest()&&await(t.onLatestSettled?.(o))};try{const e=await(t.action?.(o.payload));return s((t=>{t.response=e})),await(t.onEachSuccess?.(o)),o.getIsLatest()&&await(t.onLatestSuccess?.(o)),await n(),s((t=>{t.status="success"})),e}catch(e){throw console.error(e),s((t=>{t.error=e})),await(t.onEachError?.(o)),o.getIsLatest()&&await(t.onLatestError?.(o)),await n(),s((t=>{t.status="error"})),e}}}},t.createBrowserHistory=o,t.createHashHistory=function(){return o({getHref:()=>window.location.hash.substring(1),createHref:t=>`#${t}`})},t.createMemoryHistory=s,t.createRouteConfig=M,t.createStore=xt,t.decode=L,t.defaultFetchServerDataFn=Tt,t.defaultParseSearch=It,t.defaultStringifySearch=Ct,t.encode=S,t.functionalUpdate=h,t.interpolatePath=v,t.invariant=e,t.joinPaths=l,t.last=c,t.matchByPath=w,t.matchPathname=b,t.parsePathname=y,t.parseSearchWith=kt,t.pick=u,t.replaceEqualDeep=Ot,t.resolvePath=g,t.rootRouteId=_,t.stringifySearchWith=$t,t.trackDeep=function(t){const e=new Set;return JSON.stringify(t,((t,r)=>{if("function"!=typeof r){if("object"==typeof r&&null!==r){if(e.has(r))return;e.add(r)}return r}})),t},t.trimPath=m,t.trimPathLeft=f,t.trimPathRight=p,t.warning=function(t,e){if(t){"undefined"!=typeof console&&console.warn(e);try{throw new Error(e)}catch{}}return!0},Object.defineProperty(t,"__esModule",{value:!0})})); | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("use-sync-external-store/shim/with-selector")):"function"==typeof define&&define.amd?define(["exports","use-sync-external-store/shim/with-selector"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).RouterCore={})}(this,(function(t){"use strict";function e(t,e){if(!t)throw new Error("Invariant failed")}const s="pushstate",a="popstate",r="beforeunload",o=t=>(t.preventDefault(),t.returnValue=""),i=()=>{removeEventListener(r,o,{capture:!0})};function n(t){let e=t.getLocation(),s=()=>{},a=new Set,n=[],h=[];const c=()=>{if(n.length)n[0]?.(c,(()=>{n=[],i()}));else{for(;h.length;)h.shift()?.();t.listener||u()}},l=t=>{h.push(t),c()},u=()=>{e=t.getLocation(),a.forEach((t=>t()))};return{get location(){return e},listen:e=>(0===a.size&&(s="function"==typeof t.listener?t.listener(u):()=>{}),a.add(e),()=>{a.delete(e),0===a.size&&s()}),push:(e,s)=>{l((()=>{t.pushState(e,s)}))},replace:(e,s)=>{l((()=>{t.replaceState(e,s)}))},go:e=>{l((()=>{t.go(e)}))},back:()=>{l((()=>{t.back()}))},forward:()=>{l((()=>{t.forward()}))},createHref:e=>t.createHref(e),block:t=>(n.push(t),1===n.length&&addEventListener(r,o,{capture:!0}),()=>{n=n.filter((e=>e!==t)),n.length||i()})}}function h(t){const e=t?.getHref??(()=>`${window.location.pathname}${window.location.search}${window.location.hash}`),r=t?.createHref??(t=>t);return n({getLocation:()=>l(e(),history.state),listener:t=>{window.addEventListener(s,t),window.addEventListener(a,t);var e=window.history.pushState;window.history.pushState=function(){let s=e.apply(history,arguments);return t(),s};var r=window.history.replaceState;return window.history.replaceState=function(){let e=r.apply(history,arguments);return t(),e},()=>{window.history.pushState=e,window.history.replaceState=r,window.removeEventListener(s,t),window.removeEventListener(a,t)}},pushState:(t,e)=>{window.history.pushState({...e,key:u()},"",r(t))},replaceState:(t,e)=>{window.history.replaceState({...e,key:u()},"",r(t))},back:()=>window.history.back(),forward:()=>window.history.forward(),go:t=>window.history.go(t),createHref:t=>r(t)})}function c(t={initialEntries:["/"]}){const e=t.initialEntries;let s=t.initialIndex??e.length-1,a={};return n({getLocation:()=>l(e[s],a),listener:!1,pushState:(t,r)=>{a={...r,key:u()},e.push(t),s++},replaceState:(t,r)=>{a={...r,key:u()},e[s]=t},back:()=>{s--},forward:()=>{s=Math.min(s+1,e.length-1)},go:t=>window.history.go(t),createHref:t=>t})}function l(t,e){let s=t.indexOf("#"),a=t.indexOf("?");return{href:t,pathname:t.substring(0,s>0?a>0?Math.min(s,a):s:a>0?a:t.length),hash:s>-1?t.substring(s):"",search:a>-1?t.slice(a,-1===s?void 0:s):"",state:e}}function u(){return(Math.random()+1).toString(36).substring(7)}function d(t){return t[t.length-1]}function p(t,e){return"function"==typeof t?t(e):t}function f(t,e){return e.reduce(((e,s)=>(e[s]=t[s],e)),{})}function m(t,e){if(t===e)return t;const s=e,a=Array.isArray(t)&&Array.isArray(s);if(a||y(t)&&y(s)){const e=a?t.length:Object.keys(t).length,r=a?s:Object.keys(s),o=r.length,i=a?[]:{};let n=0;for(let e=0;e<o;e++){const o=a?e:r[e];i[o]=m(t[o],s[o]),i[o]===t[o]&&n++}return e===o&&n===e?t:i}return s}function y(t){if(!g(t))return!1;const e=t.constructor;if(void 0===e)return!0;const s=e.prototype;return!!g(s)&&!!s.hasOwnProperty("isPrototypeOf")}function g(t){return"[object Object]"===Object.prototype.toString.call(t)}function v(t,e){return t===e||typeof t==typeof e&&(y(t)&&y(e)?!Object.keys(e).some((s=>!v(t[s],e[s]))):!(!Array.isArray(t)||!Array.isArray(e))&&(t.length===e.length&&t.every(((t,s)=>v(t,e[s])))))}function w(t){return _(t.filter(Boolean).join("/"))}function _(t){return t.replace(/\/{2,}/g,"/")}function b(t){return"/"===t?t:t.replace(/^\/{1,}/,"")}function R(t){return"/"===t?t:t.replace(/\/{1,}$/,"")}function I(t){return R(b(t))}function S(t,e,s){e=e.replace(new RegExp(`^${t}`),"/"),s=s.replace(new RegExp(`^${t}`),"/");let a=x(e);const r=x(s);r.forEach(((t,e)=>{if("/"===t.value)e?e===r.length-1&&a.push(t):a=[t];else if(".."===t.value)a.length>1&&"/"===d(a)?.value&&a.pop(),a.pop();else{if("."===t.value)return;a.push(t)}}));return _(w([t,...a.map((t=>t.value))]))}function x(t){if(!t)return[];const e=[];if("/"===(t=_(t)).slice(0,1)&&(t=t.substring(1),e.push({type:"pathname",value:"/"})),!t)return e;const s=t.split("/").filter(Boolean);return e.push(...s.map((t=>"$"===t||"*"===t?{type:"wildcard",value:t}:"$"===t.charAt(0)?{type:"param",value:t}:{type:"pathname",value:t}))),"/"===t.slice(-1)&&(t=t.substring(1),e.push({type:"pathname",value:"/"})),e}function P(t,e,s=!1){return w(x(t).map((t=>{if("wildcard"===t.type){const a=e[t.value];return s?`${t.value}${a??""}`:a}return"param"===t.type?e[t.value.substring(1)]??"":t.value})))}function E(t,e,s){const a=M(t,e,s);if(!s.to||a)return a??{}}function M(t,e,s){e="/"!=t?e.substring(t.length):e;const a=`${s.to??"$"}`,r=x(e),o=x(a);e.startsWith("/")||r.unshift({type:"pathname",value:"/"}),a.startsWith("/")||o.unshift({type:"pathname",value:"/"});const i={};return(()=>{for(let t=0;t<Math.max(r.length,o.length);t++){const e=r[t],a=o[t],n=t>=r.length-1,h=t>=o.length-1;if(a){if("wildcard"===a.type)return!!e?.value&&(i["*"]=w(r.slice(t).map((t=>t.value))),!0);if("pathname"===a.type){if("/"===a.value&&!e?.value)return!0;if(e)if(s.caseSensitive){if(a.value!==e.value)return!1}else if(a.value.toLowerCase()!==e.value.toLowerCase())return!1}if(!e)return!1;if("param"===a.type){if("/"===e?.value)return!1;"$"!==e.value.charAt(0)&&(i[a.value.substring(1)]=e.value)}}if(!n&&h)return!!s.fuzzy}return!0})()?i:void 0}function L(t,e){var s,a,r,o="";for(s in t)if(void 0!==(r=t[s]))if(Array.isArray(r))for(a=0;a<r.length;a++)o&&(o+="&"),o+=encodeURIComponent(s)+"="+encodeURIComponent(r[a]);else o&&(o+="&"),o+=encodeURIComponent(s)+"="+encodeURIComponent(r);return(e||"")+o}function A(t){if(!t)return"";var e=decodeURIComponent(t);return"false"!==e&&("true"===e||(0*+e==0&&+e+""===e?+e:e))}function D(t){for(var e,s,a={},r=t.split("&");e=r.shift();)void 0!==a[s=(e=e.split("=")).shift()]?a[s]=[].concat(a[s],A(e.shift())):a[s]=A(e.shift());return a}const $="__root__";class B{constructor(t){this.options=t||{},this.isRoot=!t?.getParentRoute,B.__onInit(this)}init=t=>{this.originalIndex=t.originalIndex,this.router=t.router;const s=this.options,a=!s?.path&&!s?.id;this.parentRoute=this.options?.getParentRoute?.(),a?this.path=$:e(this.parentRoute);let r=a?$:s.path;r&&"/"!==r&&(r=I(r));const o=s?.id||r;let i=a?$:w([this.parentRoute.id===$?"":this.parentRoute.id,o]);r===$&&(r="/"),i!==$&&(i=w(["/",i]));const n=i===$?"/":w([this.parentRoute.fullPath,r]);this.path=r,this.id=i,this.fullPath=n,this.to=n};addChildren=t=>(this.children=t,this);update=t=>(Object.assign(this.options,t),this);static __onInit=t=>{}}class O extends B{constructor(t){super(t)}} | ||
/** | ||
* @tanstack/store/src/index.ts | ||
* | ||
* Copyright (c) TanStack | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE.md file in the root directory of this source tree. | ||
* | ||
* @license MIT | ||
*/class C{listeners=new Set;_batching=!1;_flushing=0;_nextPriority=null;constructor(t,e){this.state=t,this.options=e}subscribe=t=>{this.listeners.add(t);const e=this.options?.onSubscribe?.(t,this);return()=>{this.listeners.delete(t),e?.()}};setState=(t,e)=>{const s=this.state;this.state=this.options?.updateFn?this.options.updateFn(s)(t):t(s);const a=e?.priority??this.options?.defaultPriority??"high";null===this._nextPriority||"high"===this._nextPriority?this._nextPriority=a:this._nextPriority=this.options?.defaultPriority??"high",this.options?.onUpdate?.({priority:this._nextPriority}),this._flush()};_flush=()=>{if(this._batching)return;const t=++this._flushing;this.listeners.forEach((e=>{this._flushing===t&&e({priority:this._nextPriority??"high"})}))};batch=t=>{if(this._batching)return t();this._batching=!0,t(),this._batching=!1,this._flush()}}const T=k(JSON.parse),H=j(JSON.stringify);function k(t){return e=>{"?"===e.substring(0,1)&&(e=e.substring(1));let s=D(e);for(let e in s){const a=s[e];if("string"==typeof a)try{s[e]=t(a)}catch(t){}}return s}}function j(t){return e=>{(e={...e})&&Object.keys(e).forEach((s=>{const a=e[s];if(void 0===a||void 0===a)delete e[s];else if(a&&"object"==typeof a&&null!==a)try{e[s]=t(a)}catch(t){}}));const s=L(e).toString();return s?`?${s}`:""}}const F=["component","errorComponent","pendingComponent"];const N="undefined"==typeof window||!window.document.createElement;function U(){return{status:"idle",isFetching:!1,resolvedLocation:null,location:null,matchesById:{},matchIds:[],pendingMatchIds:[],matches:[],pendingMatches:[],lastUpdated:Date.now()}}function J(t){return!!t?.isRedirect}class W extends Error{}class z extends Error{}t.PathParamError=z,t.RootRoute=O,t.Route=B,t.Router=class{#t;constructor(t){this.options={defaultPreloadDelay:50,context:void 0,...t,stringifySearch:t?.stringifySearch??H,parseSearch:t?.parseSearch??T},this.__store=new C(U(),{onUpdate:()=>{const t=this.state;this.state=this.__store.state;const e=t.matchesById!==this.state.matchesById;let s,a;e||(s=t.matchIds.length!==this.state.matchIds.length||t.matchIds.some(((t,e)=>t!==this.state.matchIds[e])),a=t.pendingMatchIds.length!==this.state.pendingMatchIds.length||t.pendingMatchIds.some(((t,e)=>t!==this.state.pendingMatchIds[e]))),(e||s)&&(this.state.matches=this.state.matchIds.map((t=>this.state.matchesById[t]))),(e||a)&&(this.state.pendingMatches=this.state.pendingMatchIds.map((t=>this.state.matchesById[t]))),this.state.isFetching=[...this.state.matches,...this.state.pendingMatches].some((t=>t.isFetching))},defaultPriority:"low"}),this.state=this.__store.state,this.update(t);const e=this.buildNext({hash:!0,fromCurrent:!0,search:!0,state:!0});this.state.location.href!==e.href&&this.#e({...e,replace:!0})}reset=()=>{this.__store.setState((t=>Object.assign(t,U())))};mount=()=>{this.safeLoad()};update=t=>{if(this.options={...this.options,...t,context:{...this.options.context,...t?.context}},!this.history||this.options.history&&this.options.history!==this.history){this.#t&&this.#t(),this.history=this.options.history??(N?c():h());const t=this.#s();this.__store.setState((e=>({...e,resolvedLocation:t,location:t}))),this.#t=this.history.listen((()=>{this.safeLoad({next:this.#s(this.state.location)})}))}const{basepath:e,routeTree:s}=this.options;return this.basepath=`/${I(e??"")??""}`,s&&s!==this.routeTree&&this.#a(s),this};buildNext=t=>{const e=this.#r(t),s=this.matchRoutes(e.pathname,e.search);return this.#r({...t,__matches:s})};cancelMatches=()=>{this.state.matches.forEach((t=>{this.cancelMatch(t.id)}))};cancelMatch=t=>{this.getRouteMatch(t)?.abortController?.abort()};safeLoad=t=>this.load(t).catch((t=>{}));latestLoadPromise=Promise.resolve();load=async t=>{const e=new Promise((async(s,a)=>{let r;const o=()=>this.latestLoadPromise!==e?this.latestLoadPromise:void 0;let i;this.__store.batch((()=>{t?.next&&this.__store.setState((e=>({...e,location:t.next}))),i=this.matchRoutes(this.state.location.pathname,this.state.location.search,{throwOnError:t?.throwOnError,debug:!0}),this.__store.setState((t=>({...t,status:"pending",pendingMatchIds:i.map((t=>t.id)),matchesById:this.#o(t.matchesById,i)})))}));try{if(await this.loadMatches(i),r=o())return await r;const t=this.state.resolvedLocation;this.__store.setState((t=>({...t,status:"idle",resolvedLocation:t.location,matchIds:t.pendingMatchIds,pendingMatchIds:[]}))),t.href!==this.state.location.href&&this.options.onRouteChange?.(),s()}catch(t){if(r=o())return await r;a(t)}}));return this.latestLoadPromise=e,this.latestLoadPromise};#o=(t,e)=>{const s={...t};let a=!1;return e.forEach((t=>{s[t.id]||(a=!0,s[t.id]=t)})),a?s:t};getRoute=t=>{const s=this.routesById[t];return e(s),s};preloadRoute=async(t=this.state.location)=>{const e=this.buildNext(t),s=this.matchRoutes(e.pathname,e.search,{throwOnError:!0});return this.__store.setState((t=>({...t,matchesById:this.#o(t.matchesById,s)}))),await this.loadMatches(s,{preload:!0,maxAge:t.maxAge}),s};cleanMatches=()=>{const t=Date.now(),e=Object.values(this.state.matchesById).filter((e=>{const s=this.getRoute(e.routeId);return!this.state.matchIds.includes(e.id)&&!this.state.pendingMatchIds.includes(e.id)&&e.preloadInvalidAt<t&&(!s.options.gcMaxAge||e.updatedAt+s.options.gcMaxAge<t)})).map((t=>t.id));e.length&&this.__store.setState((t=>{const s={...t.matchesById};return e.forEach((t=>{delete s[t]})),{...t,matchesById:s}}))};matchRoutes=(t,e,s)=>{let a={},r=this.flatRoutes.find((e=>{const s=E(this.basepath,t,{to:e.fullPath,caseSensitive:e.options.caseSensitive??this.options.caseSensitive});return!!s&&(a=s,!0)}))||this.routesById.__root__,o=[r];for(;r?.parentRoute;)r=r.parentRoute,r&&o.unshift(r);let i={};const n=o.map((t=>{let r,o;try{r=t.options.parseParams?.(a)??a}catch(t){if(o=new z(t.message,{cause:t}),s?.throwOnError)throw o}Object.assign(i,r);const n=P(t.path,i),h=t.options.key?t.options.key({params:i,search:e})??"":"",c=h?JSON.stringify(h):"",l=P(t.id,i,!0)+c,u=this.getRouteMatch(l);if(u)return{...u};const d=!(!t.options.loader&&!F.some((e=>t.options[e]?.preload)));return{id:l,key:c,routeId:t.id,params:i,pathname:w([this.basepath,n]),updatedAt:Date.now(),invalidAt:1/0,preloadInvalidAt:1/0,routeSearch:{},search:{},status:d?"idle":"success",isFetching:!1,invalid:!1,error:void 0,paramsError:o,searchError:void 0,loaderData:void 0,loadPromise:Promise.resolve(),routeContext:void 0,context:void 0,abortController:new AbortController,fetchedAt:0}}));return n.forEach(((t,a)=>{const r=n[a-1],o=this.getRoute(t.routeId),i=(()=>{const a={search:r?.search??e,routeSearch:r?.routeSearch??e};try{const e=("object"==typeof o.options.validateSearch?o.options.validateSearch.parse:o.options.validateSearch)?.(a.search)??{},s={...a.search,...e};return{routeSearch:m(t.routeSearch,e),search:m(t.search,s)}}catch(e){if(t.searchError=new W(e.message,{cause:e}),s?.throwOnError)throw t.searchError;return a}})(),h=(()=>{try{const e=o.options.getContext?.({parentContext:r?.routeContext??{},context:r?.context??this?.options.context??{},params:t.params,search:t.search})||{};return{context:{...r?.context??this?.options.context,...e},routeContext:e}}catch(t){throw o.options.onError?.(t),t}})();Object.assign(t,{...i,...h})})),n};loadMatches=async(t,e)=>{let s;this.cleanMatches();try{await Promise.all(t.map((async(t,a)=>{const r=this.getRoute(t.routeId);e?.preload||this.setRouteMatch(t.id,(e=>({...e,routeSearch:t.routeSearch,search:t.search,routeContext:t.routeContext,context:t.context,error:t.error,paramsError:t.paramsError,searchError:t.searchError,params:t.params})));const o=(e,o)=>{if(s=s??a,o=o||r.options.onError,J(e))throw e;try{o?.(e)}catch(t){if(e=t,J(t))throw t}this.setRouteMatch(t.id,(t=>({...t,error:e,status:"error",updatedAt:Date.now()})))};t.paramsError&&o(t.paramsError,r.options.onParseParamsError),t.searchError&&o(t.searchError,r.options.onValidateSearchError);try{await(r.options.beforeLoad?.({...t,preload:!!e?.preload}))}catch(t){o(t,r.options.onBeforeLoadError)}})))}catch(t){throw e?.preload||this.navigate(t),t}const a=t.slice(0,s),r=[];a.forEach(((t,s)=>{r.push((async()=>{const a=r[s-1],o=this.getRoute(t.routeId);if(t.isFetching||"success"===t.status&&!this.getIsInvalid({matchId:t.id,preload:e?.preload}))return this.getRouteMatch(t.id)?.loadPromise;const i=Date.now(),n=()=>{const e=this.getRouteMatch(t.id);return e&&e.fetchedAt!==i?e.loadPromise:void 0},h=(async()=>{let s;const r=Promise.all(F.map((async t=>{const e=o.options[t];e?.preload&&await e.preload()}))),i=o.options.loader?.({...t,preload:!!e?.preload,parentMatchPromise:a}),h=t=>!!J(t)&&(e?.preload||this.navigate(t),!0);try{const[a,o]=await Promise.all([r,i]);if(s=n())return await s;this.setRouteMatchData(t.id,(()=>o),e)}catch(e){if(s=n())return await s;if(h(e))return;const a=o.options.onLoadError??o.options.onError;let r=e;try{a?.(e)}catch(t){if(r=t,h(t))return}this.setRouteMatch(t.id,(t=>({...t,error:r,status:"error",isFetching:!1,updatedAt:Date.now()})))}})();this.setRouteMatch(t.id,(t=>({...t,status:"success"!==t.status?"pending":t.status,isFetching:!0,loadPromise:h,fetchedAt:i,invalid:!1}))),await h})())})),await Promise.all(r)};reload=()=>this.navigate({fromCurrent:!0,replace:!0,search:!0});resolvePath=(t,e)=>S(this.basepath,t,_(e));navigate=async({from:t,to:s="",search:a,hash:r,replace:o,params:i})=>{const n=String(s),h=void 0===t?t:String(t);let c;try{new URL(`${n}`),c=!0}catch(t){}return e(!c),this.#e({from:h,to:n,search:a,hash:r,replace:o,params:i})};matchRoute=(t,e)=>{t={...t,to:t.to?this.resolvePath(t.from??"",t.to):void 0};const s=this.buildNext(t);if(e?.pending&&"pending"!==this.state.status)return!1;const a=e?.pending?this.state.location:this.state.resolvedLocation;if(!a)return!1;const r=E(this.basepath,a.pathname,{...e,to:s.pathname});return!!r&&(e?.includeSearch??1?!!v(a.search,s.search)&&r:r)};buildLink=({from:t,to:e=".",search:s,params:a,hash:r,target:o,replace:i,activeOptions:n,preload:h,preloadDelay:c,disabled:l})=>{try{return new URL(`${e}`),{type:"external",href:e}}catch(t){}const u={from:t,to:e,search:s,params:a,hash:r,replace:i},d=this.buildNext(u);h=h??this.options.defaultPreload;const p=c??this.options.defaultPreloadDelay??0,f=this.state.location.pathname.split("/"),m=d.pathname.split("/").every(((t,e)=>t===f[e])),y=n?.exact?this.state.location.pathname===d.pathname:m,g=!n?.includeHash||this.state.location.hash===d.hash,w=!(n?.includeSearch??1)||v(this.state.location.search,d.search);return{type:"internal",next:d,handleFocus:t=>{h&&this.preloadRoute(u).catch((t=>{console.warn(t),console.warn("Error preloading route! ☝️")}))},handleClick:t=>{l||function(t){return!!(t.metaKey||t.altKey||t.ctrlKey||t.shiftKey)}(t)||t.defaultPrevented||o&&"_self"!==o||0!==t.button||(t.preventDefault(),this.#e(u))},handleEnter:t=>{const e=t.target||{};if(h){if(e.preloadTimeout)return;e.preloadTimeout=setTimeout((()=>{e.preloadTimeout=null,this.preloadRoute(u).catch((t=>{console.warn(t),console.warn("Error preloading route! ☝️")}))}),p)}},handleLeave:t=>{const e=t.target||{};e.preloadTimeout&&(clearTimeout(e.preloadTimeout),e.preloadTimeout=null)},handleTouchStart:t=>{this.preloadRoute(u).catch((t=>{console.warn(t),console.warn("Error preloading route! ☝️")}))},isActive:y&&g&&w,disabled:l}};dehydrate=()=>({state:f(this.state,["location","status","lastUpdated"])});hydrate=async t=>{let s=t;"undefined"!=typeof document&&(s=window.__TSR_DEHYDRATED__),e(s);const a=s;this.dehydratedData=a.payload,this.options.hydrate?.(a.payload),this.__store.setState((t=>({...t,...a.router.state,resolvedLocation:a.router.state.location}))),await this.load()};injectedHtml=[];injectHtml=async t=>{this.injectedHtml.push(t)};dehydrateData=(t,e)=>{if("undefined"==typeof document){const s="string"==typeof t?t:JSON.stringify(t);return this.injectHtml((async()=>{const t=`__TSR_DEHYDRATED__${s}`,a="function"==typeof e?await e():e;return`<script id='${t}' suppressHydrationWarning>window["__TSR_DEHYDRATED__${r=s,r.replace(/\\/g,"\\\\").replace(/'/g,"\\'").replace(/"/g,'\\"')}"] = ${JSON.stringify(a)}\n ;(() => {\n var el = document.getElementById('${t}')\n el.parentElement.removeChild(el)\n })()\n <\/script>`;var r})),()=>this.hydrateData(t)}return()=>{}};hydrateData=t=>{if("undefined"!=typeof document){const e="string"==typeof t?t:JSON.stringify(t);return window[`__TSR_DEHYDRATED__${e}`]}};#a=t=>{this.routeTree=t,this.routesById={},this.routesByPath={},this.flatRoutes=[];const s=t=>{t.forEach(((t,a)=>{t.init({originalIndex:a,router:this});if(e(!this.routesById[t.id],String(t.id)),this.routesById[t.id]=t,!t.isRoot&&t.path){const e=R(t.fullPath);this.routesByPath[e]&&!t.fullPath.endsWith("/")||(this.routesByPath[e]=t)}const r=t.children;r?.length&&s(r)}))};s([t]),this.flatRoutes=Object.values(this.routesByPath).map(((t,e)=>{const s=I(t.fullPath),a=x(s);for(;a.length>1&&"/"===a[0]?.value;)a.shift();const r=a.map((t=>"param"===t.type?.5:"wildcard"===t.type?.25:1));return{child:t,trimmed:s,parsed:a,index:e,score:r}})).sort(((t,e)=>{let s="/"===t.trimmed?1:"/"===e.trimmed?-1:0;if(0!==s)return s;const a=Math.min(t.score.length,e.score.length);if(t.score.length!==e.score.length)return e.score.length-t.score.length;for(let s=0;s<a;s++)if(t.score[s]!==e.score[s])return e.score[s]-t.score[s];for(let s=0;s<a;s++)if(t.parsed[s].value!==e.parsed[s].value)return t.parsed[s].value>e.parsed[s].value?1:-1;return t.trimmed!==e.trimmed?t.trimmed>e.trimmed?1:-1:t.index-e.index})).map(((t,e)=>(t.child.rank=e,t.child)))};#s=t=>{let{pathname:e,search:s,hash:a,state:r}=this.history.location;const o=this.options.parseSearch(s);return{pathname:e,searchStr:s,search:m(t?.search,o),hash:a.split("#").reverse()[0]??"",href:`${e}${s}${a}`,state:r,key:r?.key||"__init__"}};#r=(t={})=>{t.fromCurrent=t.fromCurrent??""===t.to;const e=t.fromCurrent?this.state.location.pathname:t.from??this.state.location.pathname;let s=S(this.basepath??"/",e,`${t.to??""}`);const a={...d(this.matchRoutes(this.state.location.pathname,this.state.location.search))?.params};let r=!0===(t.params??!0)?a:p(t.params,a);r&&t.__matches?.map((t=>this.getRoute(t.routeId).options.stringifyParams)).filter(Boolean).forEach((t=>{r={...r,...t(r)}})),s=P(s,r??{});const o=t.__matches?.map((t=>this.getRoute(t.routeId).options.preSearchFilters??[])).flat().filter(Boolean)??[],i=t.__matches?.map((t=>this.getRoute(t.routeId).options.postSearchFilters??[])).flat().filter(Boolean)??[],n=o?.length?o?.reduce(((t,e)=>e(t)),this.state.location.search):this.state.location.search,h=!0===t.search?n:t.search?p(t.search,n)??{}:o?.length?n:{},c=i?.length?i.reduce(((t,e)=>e(t)),h):h,l=m(this.state.location.search,c),u=this.options.stringifySearch(l),f=!0===t.hash?this.state.location.hash:p(t.hash,this.state.location.hash),y=f?`#${f}`:"";return{pathname:s,search:l,searchStr:u,state:!0===t.state?this.state.location.state:p(t.state,this.state.location.state),hash:f,href:this.history.createHref(`${s}${u}${y}`),key:t.key}};#e=async t=>{const e=this.buildNext(t),s=""+Date.now()+Math.random();this.navigateTimeout&&clearTimeout(this.navigateTimeout);let a="replace";t.replace||(a="push");this.state.location.href===e.href&&!e.key&&(a="replace");const r=`${e.pathname}${e.searchStr}${e.hash?`#${e.hash}`:""}`;return this.history["push"===a?"push":"replace"](r,{id:s,...e.state}),this.latestLoadPromise};getRouteMatch=t=>this.state.matchesById[t];setRouteMatch=(t,e)=>{this.__store.setState((s=>({...s,matchesById:{...s.matchesById,[t]:e(s.matchesById[t])}})))};setRouteMatchData=(t,e,s)=>{const a=this.getRouteMatch(t);if(!a)return;const r=this.getRoute(a.routeId),o=s?.updatedAt??Date.now(),i=o+(s?.maxAge??r.options.preloadMaxAge??this.options.defaultPreloadMaxAge??5e3),n=o+(s?.maxAge??r.options.maxAge??this.options.defaultMaxAge??1/0);this.setRouteMatch(t,(t=>({...t,error:void 0,status:"success",isFetching:!1,updatedAt:Date.now(),loaderData:p(e,t.loaderData),preloadInvalidAt:i,invalidAt:n}))),this.state.matches.find((e=>e.id===t))};invalidate=async t=>{if(t?.matchId){this.setRouteMatch(t.matchId,(t=>({...t,invalid:!0})));const e=this.state.matches.findIndex((e=>e.id===t.matchId)),s=this.state.matches[e+1];if(s)return this.invalidate({matchId:s.id,reload:!1})}else this.__store.batch((()=>{Object.values(this.state.matchesById).forEach((t=>{this.setRouteMatch(t.id,(t=>({...t,invalid:!0})))}))}));if(t?.reload??1)return this.reload()};getIsInvalid=t=>{if(!t?.matchId)return!!this.state.matches.find((e=>this.getIsInvalid({matchId:e.id,preload:t?.preload})));const e=this.getRouteMatch(t?.matchId);if(!e)return!1;const s=Date.now();return e.invalid||(t?.preload?e.preloadInvalidAt:e.invalidAt)<s}},t.RouterContext=class{constructor(){}createRootRoute=t=>new O(t)},t.SearchParamError=W,t.cleanPath=_,t.componentTypes=F,t.createBrowserHistory=h,t.createHashHistory=function(){return h({getHref:()=>window.location.hash.substring(1),createHref:t=>`#${t}`})},t.createMemoryHistory=c,t.decode=D,t.defaultParseSearch=T,t.defaultStringifySearch=H,t.encode=L,t.functionalUpdate=p,t.interpolatePath=P,t.invariant=e,t.isPlainObject=y,t.isRedirect=J,t.joinPaths=w,t.last=d,t.lazyFn=function(t,e){return async(...s)=>(await t())[e||"default"](...s)},t.matchByPath=M,t.matchPathname=E,t.parsePathname=x,t.parseSearchWith=k,t.partialDeepEqual=v,t.pick=f,t.redirect=function(t){return t.isRedirect=!0,t},t.replaceEqualDeep=m,t.resolvePath=S,t.rootRouteId=$,t.stringifySearchWith=j,t.trimPath=I,t.trimPathLeft=b,t.trimPathRight=R,t.warning=function(t,e){},Object.defineProperty(t,"__esModule",{value:!0})})); | ||
//# sourceMappingURL=index.production.js.map |
{ | ||
"name": "@tanstack/router-core", | ||
"author": "Tanner Linsley", | ||
"version": "0.0.1-beta.54", | ||
"version": "0.0.1-beta.145", | ||
"license": "MIT", | ||
@@ -43,9 +43,12 @@ "repository": "tanstack/router", | ||
"@babel/runtime": "^7.16.7", | ||
"@solidjs/reactivity": "^0.0.7", | ||
"immer": "^9.0.15", | ||
"tiny-invariant": "^1.3.1" | ||
"tiny-invariant": "^1.3.1", | ||
"tiny-warning": "^1.0.3", | ||
"@gisatcz/cross-package-react-context": "^0.2.0", | ||
"@tanstack/react-store": "0.0.1-beta.134" | ||
}, | ||
"devDependencies": { | ||
"babel-plugin-transform-async-to-promises": "^0.8.18" | ||
"scripts": { | ||
"build": "rollup --config rollup.config.js", | ||
"test": "vitest", | ||
"test:dev": "vitest --watch" | ||
} | ||
} |
@@ -5,12 +5,12 @@ // While the public API was clearly inspired by the "history" npm package, | ||
import { match } from 'assert' | ||
export interface RouterHistory { | ||
location: RouterLocation | ||
listen: (cb: () => void) => () => void | ||
push: (path: string, state: any) => void | ||
replace: (path: string, state: any) => void | ||
push: (path: string, state?: any) => void | ||
replace: (path: string, state?: any) => void | ||
go: (index: number) => void | ||
back: () => void | ||
forward: () => void | ||
createHref: (href: string) => string | ||
block: (blockerFn: BlockerFn) => () => void | ||
} | ||
@@ -29,7 +29,23 @@ | ||
type BlockerFn = (retry: () => void, cancel: () => void) => void | ||
const pushStateEvent = 'pushstate' | ||
const popStateEvent = 'popstate' | ||
const beforeUnloadEvent = 'beforeunload' | ||
const beforeUnloadListener = (event: Event) => { | ||
event.preventDefault() | ||
// @ts-ignore | ||
return (event.returnValue = '') | ||
} | ||
const stopBlocking = () => { | ||
removeEventListener(beforeUnloadEvent, beforeUnloadListener, { | ||
capture: true, | ||
}) | ||
} | ||
function createHistory(opts: { | ||
getLocation: () => RouterLocation | ||
listener: (onUpdate: () => void) => () => void | ||
listener: false | ((onUpdate: () => void) => () => void) | ||
pushState: (path: string, state: any) => void | ||
@@ -40,10 +56,35 @@ replaceState: (path: string, state: any) => void | ||
forward: () => void | ||
createHref: (path: string) => string | ||
}): RouterHistory { | ||
let currentLocation = opts.getLocation() | ||
let location = opts.getLocation() | ||
let unsub = () => {} | ||
let listeners = new Set<() => void>() | ||
let blockers: BlockerFn[] = [] | ||
let queue: (() => void)[] = [] | ||
const tryFlush = () => { | ||
if (blockers.length) { | ||
blockers[0]?.(tryFlush, () => { | ||
blockers = [] | ||
stopBlocking() | ||
}) | ||
return | ||
} | ||
while (queue.length) { | ||
queue.shift()?.() | ||
} | ||
if (!opts.listener) { | ||
onUpdate() | ||
} | ||
} | ||
const queueTask = (task: () => void) => { | ||
queue.push(task) | ||
tryFlush() | ||
} | ||
const onUpdate = () => { | ||
currentLocation = opts.getLocation() | ||
location = opts.getLocation() | ||
listeners.forEach((listener) => listener()) | ||
@@ -54,7 +95,10 @@ } | ||
get location() { | ||
return currentLocation | ||
return location | ||
}, | ||
listen: (cb: () => void) => { | ||
if (listeners.size === 0) { | ||
unsub = opts.listener(onUpdate) | ||
unsub = | ||
typeof opts.listener === 'function' | ||
? opts.listener(onUpdate) | ||
: () => {} | ||
} | ||
@@ -71,21 +115,44 @@ listeners.add(cb) | ||
push: (path: string, state: any) => { | ||
opts.pushState(path, state) | ||
onUpdate() | ||
queueTask(() => { | ||
opts.pushState(path, state) | ||
}) | ||
}, | ||
replace: (path: string, state: any) => { | ||
opts.replaceState(path, state) | ||
onUpdate() | ||
queueTask(() => { | ||
opts.replaceState(path, state) | ||
}) | ||
}, | ||
go: (index) => { | ||
opts.go(index) | ||
onUpdate() | ||
queueTask(() => { | ||
opts.go(index) | ||
}) | ||
}, | ||
back: () => { | ||
opts.back() | ||
onUpdate() | ||
queueTask(() => { | ||
opts.back() | ||
}) | ||
}, | ||
forward: () => { | ||
opts.forward() | ||
onUpdate() | ||
queueTask(() => { | ||
opts.forward() | ||
}) | ||
}, | ||
createHref: (str) => opts.createHref(str), | ||
block: (cb) => { | ||
blockers.push(cb) | ||
if (blockers.length === 1) { | ||
addEventListener(beforeUnloadEvent, beforeUnloadListener, { | ||
capture: true, | ||
}) | ||
} | ||
return () => { | ||
blockers = blockers.filter((b) => b !== cb) | ||
if (!blockers.length) { | ||
stopBlocking() | ||
} | ||
} | ||
}, | ||
} | ||
@@ -101,3 +168,3 @@ } | ||
(() => | ||
`${window.location.pathname}${window.location.hash}${window.location.search}`) | ||
`${window.location.pathname}${window.location.search}${window.location.hash}`) | ||
const createHref = opts?.createHref ?? ((path) => path) | ||
@@ -109,4 +176,22 @@ const getLocation = () => parseLocation(getHref(), history.state) | ||
listener: (onUpdate) => { | ||
window.addEventListener(pushStateEvent, onUpdate) | ||
window.addEventListener(popStateEvent, onUpdate) | ||
var pushState = window.history.pushState | ||
window.history.pushState = function () { | ||
let res = pushState.apply(history, arguments as any) | ||
onUpdate() | ||
return res | ||
} | ||
var replaceState = window.history.replaceState | ||
window.history.replaceState = function () { | ||
let res = replaceState.apply(history, arguments as any) | ||
onUpdate() | ||
return res | ||
} | ||
return () => { | ||
window.history.pushState = pushState | ||
window.history.replaceState = replaceState | ||
window.removeEventListener(pushStateEvent, onUpdate) | ||
window.removeEventListener(popStateEvent, onUpdate) | ||
@@ -132,2 +217,3 @@ } | ||
go: (n) => window.history.go(n), | ||
createHref: (path) => createHref(path), | ||
}) | ||
@@ -159,5 +245,3 @@ } | ||
getLocation, | ||
listener: () => { | ||
return () => {} | ||
}, | ||
listener: false, | ||
pushState: (path, state) => { | ||
@@ -185,2 +269,3 @@ currentState = { | ||
go: (n) => window.history.go(n), | ||
createHref: (path) => path, | ||
}) | ||
@@ -205,4 +290,7 @@ } | ||
), | ||
hash: hashIndex > -1 ? href.substring(hashIndex, searchIndex) : '', | ||
search: searchIndex > -1 ? href.substring(searchIndex) : '', | ||
hash: hashIndex > -1 ? href.substring(hashIndex) : '', | ||
search: | ||
searchIndex > -1 | ||
? href.slice(searchIndex, hashIndex === -1 ? undefined : hashIndex) | ||
: '', | ||
state, | ||
@@ -209,0 +297,0 @@ } |
export { default as invariant } from 'tiny-invariant' | ||
export { default as warning } from 'tiny-warning' | ||
export * from './history' | ||
export * from './frameworks' | ||
export * from './link' | ||
@@ -9,10 +8,5 @@ export * from './path' | ||
export * from './route' | ||
export * from './routeConfig' | ||
export * from './routeInfo' | ||
export * from './routeMatch' | ||
export * from './router' | ||
export * from './searchParams' | ||
export * from './utils' | ||
export * from './interop' | ||
export * from './actions' | ||
export * from './store' |
184
src/link.ts
@@ -1,14 +0,4 @@ | ||
import { | ||
AnyAllRouteInfo, | ||
DefaultAllRouteInfo, | ||
RouteInfoByPath, | ||
} from './routeInfo' | ||
import { ParsedLocation, LocationState } from './router' | ||
import { | ||
Expand, | ||
NoInfer, | ||
PickRequired, | ||
UnionToIntersection, | ||
Updater, | ||
} from './utils' | ||
import { AnyRoutesInfo, RouteByPath } from './routeInfo' | ||
import { ParsedLocation, LocationState, RegisteredRoutesInfo } from './router' | ||
import { NoInfer, PickRequired, UnionToIntersection, Updater } from './utils' | ||
@@ -27,2 +17,3 @@ export type LinkInfo = | ||
handleLeave: (e: any) => void | ||
handleTouchStart: (e: any) => void | ||
isActive: boolean | ||
@@ -32,6 +23,2 @@ disabled?: boolean | ||
type StartsWith<A, B> = A extends `${B extends string ? B : never}${infer _}` | ||
? true | ||
: false | ||
type CleanPath<T extends string> = T extends `${infer L}//${infer R}` | ||
@@ -73,3 +60,3 @@ ? CleanPath<`${CleanPath<L>}/${CleanPath<R>}`> | ||
type Join<T> = T extends [] | ||
type Join<T, Delimiter extends string = '/'> = T extends [] | ||
? '' | ||
@@ -79,3 +66,3 @@ : T extends [infer L extends string] | ||
: T extends [infer L extends string, ...infer Tail extends [...string[]]] | ||
? CleanPath<`${L}/${Join<Tail>}`> | ||
? CleanPath<`${L}${Delimiter}${Join<Tail>}`> | ||
: never | ||
@@ -130,7 +117,7 @@ | ||
export type NavigateOptions< | ||
TAllRouteInfo extends AnyAllRouteInfo = DefaultAllRouteInfo, | ||
TFrom extends TAllRouteInfo['routePaths'] = '/', | ||
TTo extends string = '.', | ||
> = ToOptions<TAllRouteInfo, TFrom, TTo> & { | ||
// Whether to replace the current history stack instead of pushing a new one | ||
TRoutesInfo extends AnyRoutesInfo = RegisteredRoutesInfo, | ||
TFrom extends TRoutesInfo['routePaths'] = '/', | ||
TTo extends string = '', | ||
> = ToOptions<TRoutesInfo, TFrom, TTo> & { | ||
// `replace` is a boolean that determines whether the navigation should replace the current history entry or push a new one. | ||
replace?: boolean | ||
@@ -140,8 +127,8 @@ } | ||
export type ToOptions< | ||
TAllRouteInfo extends AnyAllRouteInfo = DefaultAllRouteInfo, | ||
TFrom extends TAllRouteInfo['routePaths'] = '/', | ||
TTo extends string = '.', | ||
TRoutesInfo extends AnyRoutesInfo = RegisteredRoutesInfo, | ||
TFrom extends TRoutesInfo['routePaths'] = '/', | ||
TTo extends string = '', | ||
TResolvedTo = ResolveRelativePath<TFrom, NoInfer<TTo>>, | ||
> = { | ||
to?: ToPathOption<TAllRouteInfo, TFrom, TTo> | ||
to?: ToPathOption<TRoutesInfo, TFrom, TTo> | ||
// The new has string or a function to update it | ||
@@ -155,17 +142,15 @@ hash?: Updater<string> | ||
// fromCurrent?: boolean | ||
} & CheckPath<TAllRouteInfo, NoInfer<TResolvedTo>, {}> & | ||
SearchParamOptions<TAllRouteInfo, TFrom, TResolvedTo> & | ||
PathParamOptions<TAllRouteInfo, TFrom, TResolvedTo> | ||
} & CheckPath<TRoutesInfo, NoInfer<TResolvedTo>, {}> & | ||
SearchParamOptions<TRoutesInfo, TFrom, TResolvedTo> & | ||
PathParamOptions<TRoutesInfo, TFrom, TResolvedTo> | ||
export type SearchParamOptions< | ||
TAllRouteInfo extends AnyAllRouteInfo, | ||
TRoutesInfo extends AnyRoutesInfo, | ||
TFrom, | ||
TTo, | ||
TFromSchema = Expand< | ||
UnionToIntersection< | ||
TAllRouteInfo['fullSearchSchema'] & | ||
RouteInfoByPath<TAllRouteInfo, TFrom> extends never | ||
? {} | ||
: RouteInfoByPath<TAllRouteInfo, TFrom>['fullSearchSchema'] | ||
> | ||
TFromSchema = UnionToIntersection< | ||
TRoutesInfo['fullSearchSchema'] & | ||
RouteByPath<TRoutesInfo, TFrom> extends never | ||
? {} | ||
: RouteByPath<TRoutesInfo, TFrom>['__types']['fullSearchSchema'] | ||
>, | ||
@@ -175,15 +160,15 @@ // Find the schema for the new path, and make optional any keys | ||
TToSchema = Partial< | ||
RouteInfoByPath<TAllRouteInfo, TFrom>['fullSearchSchema'] | ||
RouteByPath<TRoutesInfo, TFrom>['__types']['fullSearchSchema'] | ||
> & | ||
Omit< | ||
RouteInfoByPath<TAllRouteInfo, TTo>['fullSearchSchema'], | ||
RouteByPath<TRoutesInfo, TTo>['__types']['fullSearchSchema'], | ||
keyof PickRequired< | ||
RouteInfoByPath<TAllRouteInfo, TFrom>['fullSearchSchema'] | ||
RouteByPath<TRoutesInfo, TFrom>['__types']['fullSearchSchema'] | ||
> | ||
>, | ||
TFromFullSchema = Expand< | ||
UnionToIntersection<TAllRouteInfo['fullSearchSchema'] & TFromSchema> | ||
TFromFullSchema = UnionToIntersection< | ||
TRoutesInfo['fullSearchSchema'] & TFromSchema | ||
>, | ||
TToFullSchema = Expand< | ||
UnionToIntersection<TAllRouteInfo['fullSearchSchema'] & TToSchema> | ||
TToFullSchema = UnionToIntersection< | ||
TRoutesInfo['fullSearchSchema'] & TToSchema | ||
>, | ||
@@ -203,25 +188,21 @@ > = keyof PickRequired<TToSchema> extends never | ||
export type PathParamOptions< | ||
TAllRouteInfo extends AnyAllRouteInfo, | ||
TRoutesInfo extends AnyRoutesInfo, | ||
TFrom, | ||
TTo, | ||
TFromSchema = Expand< | ||
UnionToIntersection< | ||
RouteInfoByPath<TAllRouteInfo, TFrom> extends never | ||
? {} | ||
: RouteInfoByPath<TAllRouteInfo, TFrom>['allParams'] | ||
> | ||
TFromSchema = UnionToIntersection< | ||
RouteByPath<TRoutesInfo, TFrom> extends never | ||
? {} | ||
: RouteByPath<TRoutesInfo, TFrom>['__types']['allParams'] | ||
>, | ||
// Find the schema for the new path, and make optional any keys | ||
// that are already defined in the current schema | ||
TToSchema = Partial<RouteInfoByPath<TAllRouteInfo, TFrom>['allParams']> & | ||
TToSchema = Partial<RouteByPath<TRoutesInfo, TFrom>['__types']['allParams']> & | ||
Omit< | ||
RouteInfoByPath<TAllRouteInfo, TTo>['allParams'], | ||
keyof PickRequired<RouteInfoByPath<TAllRouteInfo, TFrom>['allParams']> | ||
RouteByPath<TRoutesInfo, TTo>['__types']['allParams'], | ||
keyof PickRequired< | ||
RouteByPath<TRoutesInfo, TFrom>['__types']['allParams'] | ||
> | ||
>, | ||
TFromFullParams = Expand< | ||
UnionToIntersection<TAllRouteInfo['allParams'] & TFromSchema> | ||
>, | ||
TToFullParams = Expand< | ||
UnionToIntersection<TAllRouteInfo['allParams'] & TToSchema> | ||
>, | ||
TFromFullParams = UnionToIntersection<TRoutesInfo['allParams'] & TFromSchema>, | ||
TToFullParams = UnionToIntersection<TRoutesInfo['allParams'] & TToSchema>, | ||
> = keyof PickRequired<TToSchema> extends never | ||
@@ -238,9 +219,9 @@ ? { | ||
export type ToPathOption< | ||
TAllRouteInfo extends AnyAllRouteInfo = DefaultAllRouteInfo, | ||
TFrom extends TAllRouteInfo['routePaths'] = '/', | ||
TTo extends string = '.', | ||
TRoutesInfo extends AnyRoutesInfo = RegisteredRoutesInfo, | ||
TFrom extends TRoutesInfo['routePaths'] = '/', | ||
TTo extends string = '', | ||
> = | ||
| TTo | ||
| RelativeToPathAutoComplete< | ||
TAllRouteInfo['routePaths'], | ||
TRoutesInfo['routePaths'], | ||
NoInfer<TFrom> extends string ? NoInfer<TFrom> : '', | ||
@@ -251,9 +232,9 @@ NoInfer<TTo> & string | ||
export type ToIdOption< | ||
TAllRouteInfo extends AnyAllRouteInfo = DefaultAllRouteInfo, | ||
TFrom extends TAllRouteInfo['routePaths'] = '/', | ||
TTo extends string = '.', | ||
TRoutesInfo extends AnyRoutesInfo = RegisteredRoutesInfo, | ||
TFrom extends TRoutesInfo['routePaths'] = '/', | ||
TTo extends string = '', | ||
> = | ||
| TTo | ||
| RelativeToPathAutoComplete< | ||
TAllRouteInfo['routeIds'], | ||
TRoutesInfo['routeIds'], | ||
NoInfer<TFrom> extends string ? NoInfer<TFrom> : '', | ||
@@ -266,9 +247,10 @@ NoInfer<TTo> & string | ||
includeHash?: boolean | ||
includeSearch?: boolean | ||
} | ||
export type LinkOptions< | ||
TAllRouteInfo extends AnyAllRouteInfo = DefaultAllRouteInfo, | ||
TFrom extends TAllRouteInfo['routePaths'] = '/', | ||
TTo extends string = '.', | ||
> = NavigateOptions<TAllRouteInfo, TFrom, TTo> & { | ||
TRoutesInfo extends AnyRoutesInfo = RegisteredRoutesInfo, | ||
TFrom extends TRoutesInfo['routePaths'] = '/', | ||
TTo extends string = '', | ||
> = NavigateOptions<TRoutesInfo, TFrom, TTo> & { | ||
// The standard anchor tag target attribute | ||
@@ -280,6 +262,2 @@ target?: HTMLAnchorElement['target'] | ||
preload?: false | 'intent' | ||
// When preloaded, the preloaded result will be considered "fresh" for this duration in milliseconds | ||
preloadMaxAge?: number | ||
// When preloaded and subsequently inactive, the preloaded result will remain in memory for this duration in milliseconds | ||
preloadGcMaxAge?: number | ||
// Delay intent preloading by this many milliseconds. If the intent exits before this delay, the preload will be cancelled. | ||
@@ -292,3 +270,3 @@ preloadDelay?: number | ||
export type CheckRelativePath< | ||
TAllRouteInfo extends AnyAllRouteInfo, | ||
TRoutesInfo extends AnyRoutesInfo, | ||
TFrom, | ||
@@ -298,3 +276,3 @@ TTo, | ||
? TFrom extends string | ||
? ResolveRelativePath<TFrom, TTo> extends TAllRouteInfo['routePaths'] | ||
? ResolveRelativePath<TFrom, TTo> extends TRoutesInfo['routePaths'] | ||
? {} | ||
@@ -306,3 +284,3 @@ : { | ||
>}, which is not a valid route path.` | ||
'Valid Route Paths': TAllRouteInfo['routePaths'] | ||
'Valid Route Paths': TRoutesInfo['routePaths'] | ||
} | ||
@@ -313,36 +291,26 @@ : {} | ||
export type CheckPath< | ||
TAllRouteInfo extends AnyAllRouteInfo, | ||
TRoutesInfo extends AnyRoutesInfo, | ||
TPath, | ||
TPass, | ||
> = Exclude<TPath, TAllRouteInfo['routePaths']> extends never | ||
> = Exclude<TPath, TRoutesInfo['routePaths']> extends never | ||
? TPass | ||
: CheckPathError<TAllRouteInfo, Exclude<TPath, TAllRouteInfo['routePaths']>> | ||
: CheckPathError<TRoutesInfo, Exclude<TPath, TRoutesInfo['routePaths']>> | ||
export type CheckPathError< | ||
TAllRouteInfo extends AnyAllRouteInfo, | ||
TInvalids, | ||
> = Expand<{ | ||
Error: `${TInvalids extends string | ||
? TInvalids | ||
: never} is not a valid route path.` | ||
'Valid Route Paths': TAllRouteInfo['routePaths'] | ||
}> | ||
export type CheckPathError<TRoutesInfo extends AnyRoutesInfo, TInvalids> = { | ||
to: TRoutesInfo['routePaths'] | ||
} | ||
export type CheckId< | ||
TAllRouteInfo extends AnyAllRouteInfo, | ||
export type CheckId<TRoutesInfo extends AnyRoutesInfo, TPath, TPass> = Exclude< | ||
TPath, | ||
TPass, | ||
> = Exclude<TPath, TAllRouteInfo['routeIds']> extends never | ||
TRoutesInfo['routeIds'] | ||
> extends never | ||
? TPass | ||
: CheckIdError<TAllRouteInfo, Exclude<TPath, TAllRouteInfo['routeIds']>> | ||
: CheckIdError<TRoutesInfo, Exclude<TPath, TRoutesInfo['routeIds']>> | ||
export type CheckIdError< | ||
TAllRouteInfo extends AnyAllRouteInfo, | ||
TInvalids, | ||
> = Expand<{ | ||
export type CheckIdError<TRoutesInfo extends AnyRoutesInfo, TInvalids> = { | ||
Error: `${TInvalids extends string | ||
? TInvalids | ||
: never} is not a valid route ID.` | ||
'Valid Route IDs': TAllRouteInfo['routeIds'] | ||
}> | ||
'Valid Route IDs': TRoutesInfo['routeIds'] | ||
} | ||
@@ -372,9 +340,1 @@ export type ResolveRelativePath<TFrom, TTo = '.'> = TFrom extends string | ||
: never | ||
export type ValidFromPath< | ||
TAllRouteInfo extends AnyAllRouteInfo = DefaultAllRouteInfo, | ||
> = | ||
| undefined | ||
| (string extends TAllRouteInfo['routePaths'] | ||
? string | ||
: TAllRouteInfo['routePaths']) |
@@ -1,3 +0,2 @@ | ||
import invariant from 'tiny-invariant' | ||
import { AnyPathParams } from './routeConfig' | ||
import { AnyPathParams } from './route' | ||
import { MatchLocation } from './router' | ||
@@ -94,3 +93,3 @@ import { last } from './utils' | ||
...split.map((part): Segment => { | ||
if (part.startsWith('*')) { | ||
if (part === '$' || part === '*') { | ||
return { | ||
@@ -130,3 +129,3 @@ type: 'wildcard', | ||
params: any, | ||
leaveWildcard?: boolean, | ||
leaveWildcards: boolean = false, | ||
) { | ||
@@ -137,4 +136,6 @@ const interpolatedPathSegments = parsePathname(path) | ||
interpolatedPathSegments.map((segment) => { | ||
if (segment.value === '*' && !leaveWildcard) { | ||
return '' | ||
if (segment.type === 'wildcard') { | ||
const value = params[segment.value] | ||
if (leaveWildcards) return `${segment.value}${value ?? ''}` | ||
return value | ||
} | ||
@@ -157,3 +158,3 @@ | ||
const pathParams = matchByPath(basepath, currentPathname, matchLocation) | ||
// const searchMatched = matchBySearch(currentLocation.search, matchLocation) | ||
// const searchMatched = matchBySearch(location.search, matchLocation) | ||
@@ -172,10 +173,24 @@ if (matchLocation.to && !pathParams) { | ||
): Record<string, string> | undefined { | ||
if (!from.startsWith(basepath)) { | ||
return undefined | ||
} | ||
// Remove the base path from the pathname | ||
from = basepath != '/' ? from.substring(basepath.length) : from | ||
// Default to to $ (wildcard) | ||
const to = `${matchLocation.to ?? '$'}` | ||
// Parse the from and to | ||
const baseSegments = parsePathname(from) | ||
const to = `${matchLocation.to ?? '*'}` | ||
const routeSegments = parsePathname(to) | ||
if (!from.startsWith('/')) { | ||
baseSegments.unshift({ | ||
type: 'pathname', | ||
value: '/', | ||
}) | ||
} | ||
if (!to.startsWith('/')) { | ||
routeSegments.unshift({ | ||
type: 'pathname', | ||
value: '/', | ||
}) | ||
} | ||
const params: Record<string, string> = {} | ||
@@ -192,4 +207,4 @@ | ||
const isLastRouteSegment = i === routeSegments.length - 1 | ||
const isLastBaseSegment = i === baseSegments.length - 1 | ||
const isLastBaseSegment = i >= baseSegments.length - 1 | ||
const isLastRouteSegment = i >= routeSegments.length - 1 | ||
@@ -238,6 +253,7 @@ if (routeSegment) { | ||
if (isLastRouteSegment && !isLastBaseSegment) { | ||
if (!isLastBaseSegment && isLastRouteSegment) { | ||
return !!matchLocation.fuzzy | ||
} | ||
} | ||
return true | ||
@@ -244,0 +260,0 @@ })() |
@@ -33,4 +33,3 @@ // @ts-nocheck | ||
if (str === 'true') return true | ||
if (str.charAt(0) === '0') return str | ||
return +str * 0 === 0 ? +str : str | ||
return +str * 0 === 0 && +str + '' === str ? +str : str | ||
} | ||
@@ -37,0 +36,0 @@ |
966
src/route.ts
@@ -1,50 +0,936 @@ | ||
import { Action, ActionOptions } from './actions' | ||
import { RouteConfig, RouteOptions } from './routeConfig' | ||
import { | ||
AnyAllRouteInfo, | ||
AnyRouteInfo, | ||
DefaultAllRouteInfo, | ||
RouteInfo, | ||
} from './routeInfo' | ||
import { Router } from './router' | ||
import { ParsePathParams } from './link' | ||
import { AnyRouter, Router, RouteMatch } from './router' | ||
import { IsAny, NoInfer, PickRequired, UnionToIntersection } from './utils' | ||
import invariant from 'tiny-invariant' | ||
import { joinPaths, trimPath } from './path' | ||
import { AnyRoutesInfo, DefaultRoutesInfo } from './routeInfo' | ||
export interface AnyRoute extends Route<any, any, any> {} | ||
export const rootRouteId = '__root__' as const | ||
export type RootRouteId = typeof rootRouteId | ||
export type AnyPathParams = {} | ||
export type AnySearchSchema = {} | ||
export type AnyContext = {} | ||
export interface RouteMeta {} | ||
export interface RouteContext {} | ||
export interface RegisterRouteComponent<TProps> { | ||
// RouteComponent: unknown // This is registered by the framework | ||
} | ||
export interface RegisterRouteErrorComponent<TProps> { | ||
// RouteErrorComponent: unknown // This is registered by the framework | ||
} | ||
export type RegisteredRouteComponent<TProps> = | ||
RegisterRouteComponent<TProps> extends { | ||
RouteComponent: infer T | ||
} | ||
? T | ||
: (props: TProps) => unknown | ||
export type RegisteredRouteErrorComponent<TProps> = | ||
RegisterRouteErrorComponent<TProps> extends { | ||
RouteErrorComponent: infer T | ||
} | ||
? T | ||
: (props: TProps) => unknown | ||
export type PreloadableObj = { preload?: () => Promise<void> } | ||
export type RoutePathOptions<TCustomId, TPath> = | ||
| { | ||
path: TPath | ||
} | ||
| { | ||
id: TCustomId | ||
} | ||
export type RoutePathOptionsIntersection<TCustomId, TPath> = | ||
UnionToIntersection<RoutePathOptions<TCustomId, TPath>> | ||
export type MetaOptions = keyof PickRequired<RouteMeta> extends never | ||
? { | ||
meta?: RouteMeta | ||
} | ||
: { | ||
meta: RouteMeta | ||
} | ||
export type AnyRouteProps = RouteProps<any, any, any, any, any> | ||
export type ComponentPropsFromRoute<TRoute> = TRoute extends Route< | ||
infer TParentRoute, | ||
infer TPath, | ||
infer TFullPath, | ||
infer TCustomId, | ||
infer TId, | ||
infer TLoader, | ||
infer TSearchSchema, | ||
infer TFullSearchSchema, | ||
infer TParams, | ||
infer TAllParams, | ||
infer TParentContext, | ||
infer TAllParentContext, | ||
infer TRouteContext, | ||
infer TContext, | ||
infer TRouterContext, | ||
infer TChildren, | ||
infer TRoutesInfo | ||
> | ||
? RouteProps<TLoader, TFullSearchSchema, TAllParams, TRouteContext, TContext> | ||
: never | ||
export type ComponentFromRoute<TRoute> = RegisteredRouteComponent< | ||
ComponentPropsFromRoute<TRoute> | ||
> | ||
export type RouteLoaderFromRoute<TRoute extends AnyRoute> = LoaderFn< | ||
TRoute['__types']['loader'], | ||
TRoute['__types']['searchSchema'], | ||
TRoute['__types']['fullSearchSchema'], | ||
TRoute['__types']['allParams'], | ||
TRoute['__types']['routeContext'], | ||
TRoute['__types']['context'] | ||
> | ||
export type RouteProps< | ||
TLoader = unknown, | ||
TFullSearchSchema extends AnySearchSchema = AnySearchSchema, | ||
TAllParams = AnyPathParams, | ||
TRouteContext = AnyContext, | ||
TContext = AnyContext, | ||
> = { | ||
useMatch: () => RouteMatch<AnyRoutesInfo, AnyRoute> | ||
useLoader: () => UseLoaderResult<TLoader> | ||
useSearch: < | ||
TStrict extends boolean = true, | ||
TSearch = TFullSearchSchema, | ||
TSelected = TSearch, | ||
>(opts?: { | ||
strict?: TStrict | ||
select?: (search: TSearch) => TSelected | ||
}) => TStrict extends true ? TSelected : TSelected | undefined | ||
useParams: < | ||
TDefaultSelected = TAllParams, | ||
TSelected = TDefaultSelected, | ||
>(opts?: { | ||
select?: (params: TDefaultSelected) => TSelected | ||
}) => TSelected | ||
useContext: < | ||
TDefaultSelected = TContext, | ||
TSelected = TDefaultSelected, | ||
>(opts?: { | ||
select?: (context: TDefaultSelected) => TSelected | ||
}) => TSelected | ||
useRouteContext: < | ||
TDefaultSelected = TRouteContext, | ||
TSelected = TDefaultSelected, | ||
>(opts?: { | ||
select?: (context: TDefaultSelected) => TSelected | ||
}) => TSelected | ||
} | ||
export type RouteOptions< | ||
TParentRoute extends AnyRoute = AnyRoute, | ||
TCustomId extends string = string, | ||
TPath extends string = string, | ||
TLoader = unknown, | ||
TParentSearchSchema extends AnySearchSchema = {}, | ||
TSearchSchema extends AnySearchSchema = {}, | ||
TFullSearchSchema extends AnySearchSchema = TSearchSchema, | ||
TParentParams extends AnyPathParams = AnyPathParams, | ||
TParams extends AnyPathParams = Record<ParsePathParams<TPath>, string>, | ||
TAllParams extends AnyPathParams = TParams, | ||
TParentContext extends AnyContext = AnyContext, | ||
TAllParentContext extends IsAny< | ||
TParentRoute['__types']['allParams'], | ||
TParentContext, | ||
TParentRoute['__types']['allParams'] & TParentContext | ||
> = IsAny< | ||
TParentRoute['__types']['allParams'], | ||
TParentContext, | ||
TParentRoute['__types']['allParams'] & TParentContext | ||
>, | ||
TRouteContext extends RouteContext = RouteContext, | ||
TContext extends MergeParamsFromParent< | ||
TAllParentContext, | ||
TRouteContext | ||
> = MergeParamsFromParent<TAllParentContext, TRouteContext>, | ||
> = BaseRouteOptions< | ||
TParentRoute, | ||
TCustomId, | ||
TPath, | ||
TLoader, | ||
TParentSearchSchema, | ||
TSearchSchema, | ||
TFullSearchSchema, | ||
TParentParams, | ||
TParams, | ||
TAllParams, | ||
TParentContext, | ||
TAllParentContext, | ||
TRouteContext, | ||
TContext | ||
> & | ||
UpdatableRouteOptions< | ||
TLoader, | ||
TSearchSchema, | ||
TFullSearchSchema, | ||
TAllParams, | ||
TRouteContext, | ||
TContext | ||
> | ||
export type ParamsFallback< | ||
TPath extends string, | ||
TParams, | ||
> = unknown extends TParams ? Record<ParsePathParams<TPath>, string> : TParams | ||
export type BaseRouteOptions< | ||
TParentRoute extends AnyRoute = AnyRoute, | ||
TCustomId extends string = string, | ||
TPath extends string = string, | ||
TLoader = unknown, | ||
TParentSearchSchema extends AnySearchSchema = {}, | ||
TSearchSchema extends AnySearchSchema = {}, | ||
TFullSearchSchema extends AnySearchSchema = TSearchSchema, | ||
TParentParams extends AnyPathParams = AnyPathParams, | ||
TParams = unknown, | ||
TAllParams = ParamsFallback<TPath, TParams>, | ||
TParentContext extends AnyContext = AnyContext, | ||
TAllParentContext extends IsAny< | ||
TParentRoute['__types']['allParams'], | ||
TParentContext, | ||
TParentRoute['__types']['allParams'] & TParentContext | ||
> = IsAny< | ||
TParentRoute['__types']['allParams'], | ||
TParentContext, | ||
TParentRoute['__types']['allParams'] & TParentContext | ||
>, | ||
TRouteContext extends RouteContext = RouteContext, | ||
TContext extends MergeParamsFromParent< | ||
TAllParentContext, | ||
TRouteContext | ||
> = MergeParamsFromParent<TAllParentContext, TRouteContext>, | ||
> = RoutePathOptions<TCustomId, TPath> & { | ||
getParentRoute: () => TParentRoute | ||
validateSearch?: SearchSchemaValidator<TSearchSchema, TParentSearchSchema> | ||
loader?: LoaderFn< | ||
TLoader, | ||
TSearchSchema, | ||
TFullSearchSchema, | ||
TAllParams, | ||
NoInfer<TRouteContext>, | ||
TContext | ||
> | ||
} & ( | ||
| { | ||
// Both or none | ||
parseParams?: ( | ||
rawParams: IsAny<TPath, any, Record<ParsePathParams<TPath>, string>>, | ||
) => TParams extends Record<ParsePathParams<TPath>, any> | ||
? TParams | ||
: 'parseParams must return an object' | ||
// | { | ||
// parse: ( | ||
// rawParams: IsAny< | ||
// TPath, | ||
// any, | ||
// Record<ParsePathParams<TPath>, string> | ||
// >, | ||
// ) => TParams extends Record<ParsePathParams<TPath>, any> | ||
// ? TParams | ||
// : 'parseParams must return an object' | ||
// } | ||
stringifyParams?: ( | ||
params: NoInfer<ParamsFallback<TPath, TParams>>, | ||
) => Record<ParsePathParams<TPath>, string> | ||
} | ||
| { | ||
stringifyParams?: never | ||
parseParams?: never | ||
} | ||
) & | ||
(keyof PickRequired<RouteContext> extends never | ||
? { | ||
getContext?: GetContextFn< | ||
TParentRoute, | ||
TAllParams, | ||
TFullSearchSchema, | ||
TParentContext, | ||
TAllParentContext, | ||
TRouteContext | ||
> | ||
} | ||
: { | ||
getContext: GetContextFn< | ||
TParentRoute, | ||
TAllParams, | ||
TFullSearchSchema, | ||
TParentContext, | ||
TAllParentContext, | ||
TRouteContext | ||
> | ||
}) | ||
type GetContextFn< | ||
TParentRoute, | ||
TAllParams, | ||
TFullSearchSchema, | ||
TParentContext, | ||
TAllParentContext, | ||
TRouteContext, | ||
> = ( | ||
opts: { | ||
params: TAllParams | ||
search: TFullSearchSchema | ||
} & (TParentRoute extends undefined | ||
? { | ||
context?: TAllParentContext | ||
parentContext?: TParentContext | ||
} | ||
: { | ||
context: TAllParentContext | ||
parentContext: TParentContext | ||
}), | ||
) => TRouteContext | ||
export type UpdatableRouteOptions< | ||
TLoader, | ||
TSearchSchema extends AnySearchSchema, | ||
TFullSearchSchema extends AnySearchSchema, | ||
TAllParams extends AnyPathParams, | ||
TRouteContext extends AnyContext, | ||
TContext extends AnyContext, | ||
> = MetaOptions & { | ||
key?: null | false | GetKeyFn<TFullSearchSchema, TAllParams> | ||
// If true, this route will be matched as case-sensitive | ||
caseSensitive?: boolean | ||
// If true, this route will be forcefully wrapped in a suspense boundary | ||
wrapInSuspense?: boolean | ||
// The content to be rendered when the route is matched. If no component is provided, defaults to `<Outlet />` | ||
component?: RegisteredRouteComponent< | ||
RouteProps<TLoader, TFullSearchSchema, TAllParams, TRouteContext, TContext> | ||
> | ||
// The content to be rendered when the route encounters an error | ||
errorComponent?: RegisterRouteErrorComponent< | ||
RouteProps<TLoader, TFullSearchSchema, TAllParams, TRouteContext, TContext> | ||
> // | ||
// If supported by your framework, the content to be rendered as the fallback content until the route is ready to render | ||
pendingComponent?: RegisteredRouteComponent< | ||
RouteProps<TLoader, TFullSearchSchema, TAllParams, TRouteContext, TContext> | ||
> | ||
// Filter functions that can manipulate search params *before* they are passed to links and navigate | ||
// calls that match this route. | ||
preSearchFilters?: SearchFilter<TFullSearchSchema>[] | ||
// Filter functions that can manipulate search params *after* they are passed to links and navigate | ||
// calls that match this route. | ||
postSearchFilters?: SearchFilter<TFullSearchSchema>[] | ||
// If set, preload matches of this route will be considered fresh for this many milliseconds. | ||
preloadMaxAge?: number | ||
// If set, a match of this route will be considered fresh for this many milliseconds. | ||
maxAge?: number | ||
// If set, a match of this route that becomes inactive (or unused) will be garbage collected after this many milliseconds | ||
gcMaxAge?: number | ||
// This async function is called before a route is loaded. | ||
// If an error is thrown here, the route's loader will not be called. | ||
// If thrown during a navigation, the navigation will be cancelled and the error will be passed to the `onLoadError` function. | ||
// If thrown during a preload event, the error will be logged to the console. | ||
beforeLoad?: ( | ||
opts: LoaderContext< | ||
TSearchSchema, | ||
TFullSearchSchema, | ||
TAllParams, | ||
NoInfer<TRouteContext>, | ||
TContext | ||
>, | ||
) => Promise<void> | void | ||
// This function will be called if the route's loader throws an error **during an attempted navigation**. | ||
// If you want to redirect due to an error, call `router.navigate()` from within this function. | ||
onBeforeLoadError?: (err: any) => void | ||
// This function will be called if the route's validateSearch option throws an error **during an attempted validation**. | ||
// If you want to redirect due to an error, call `router.navigate()` from within this function. | ||
// If you want to display the errorComponent, rethrow the error | ||
onValidateSearchError?: (err: any) => void | ||
onParseParamsError?: (err: any) => void | ||
onLoadError?: (err: any) => void | ||
onError?: (err: any) => void | ||
// This function is called | ||
// when moving from an inactive state to an active one. Likewise, when moving from | ||
// an active to an inactive state, the return function (if provided) is called. | ||
onLoaded?: (matchContext: { | ||
params: TAllParams | ||
search: TFullSearchSchema | ||
}) => | ||
| void | ||
| undefined | ||
| ((match: { params: TAllParams; search: TFullSearchSchema }) => void) | ||
// This function is called when the route remains active from one transition to the next. | ||
onTransition?: (match: { | ||
params: TAllParams | ||
search: TFullSearchSchema | ||
}) => void | ||
} | ||
export type ParseParamsOption<TPath extends string, TParams> = ParseParamsFn< | ||
TPath, | ||
TParams | ||
> | ||
// | ParseParamsObj<TPath, TParams> | ||
export type ParseParamsFn<TPath extends string, TParams> = ( | ||
rawParams: IsAny<TPath, any, Record<ParsePathParams<TPath>, string>>, | ||
) => TParams extends Record<ParsePathParams<TPath>, any> | ||
? TParams | ||
: 'parseParams must return an object' | ||
export type ParseParamsObj<TPath extends string, TParams> = { | ||
parse?: ParseParamsFn<TPath, TParams> | ||
} | ||
// The parse type here allows a zod schema to be passed directly to the validator | ||
export type SearchSchemaValidator<TReturn, TParentSchema> = | ||
| SearchSchemaValidatorObj<TReturn, TParentSchema> | ||
| SearchSchemaValidatorFn<TReturn, TParentSchema> | ||
export type SearchSchemaValidatorObj<TReturn, TParentSchema> = { | ||
parse?: SearchSchemaValidatorFn<TReturn, TParentSchema> | ||
} | ||
export type SearchSchemaValidatorFn<TReturn, TParentSchema> = ( | ||
searchObj: Record<string, unknown>, | ||
) => {} extends TParentSchema | ||
? TReturn | ||
: keyof TReturn extends keyof TParentSchema | ||
? { | ||
error: 'Top level search params cannot be redefined by child routes!' | ||
keys: keyof TReturn & keyof TParentSchema | ||
} | ||
: TReturn | ||
export type DefinedPathParamWarning = | ||
'Path params cannot be redefined by child routes!' | ||
export type ParentParams<TParentParams> = AnyPathParams extends TParentParams | ||
? {} | ||
: { | ||
[Key in keyof TParentParams]?: DefinedPathParamWarning | ||
} | ||
export type LoaderFn< | ||
TLoader = unknown, | ||
TSearchSchema extends AnySearchSchema = {}, | ||
TFullSearchSchema extends AnySearchSchema = {}, | ||
TAllParams = {}, | ||
TContext extends AnyContext = AnyContext, | ||
TAllContext extends AnyContext = AnyContext, | ||
> = ( | ||
match: LoaderContext< | ||
TSearchSchema, | ||
TFullSearchSchema, | ||
TAllParams, | ||
TContext, | ||
TAllContext | ||
> & { | ||
parentMatchPromise?: Promise<void> | ||
}, | ||
) => Promise<TLoader> | TLoader | ||
export type GetKeyFn< | ||
TFullSearchSchema extends AnySearchSchema = {}, | ||
TAllParams = {}, | ||
> = (loaderContext: { params: TAllParams; search: TFullSearchSchema }) => any | ||
export interface LoaderContext< | ||
TSearchSchema extends AnySearchSchema = {}, | ||
TFullSearchSchema extends AnySearchSchema = {}, | ||
TAllParams = {}, | ||
TContext extends AnyContext = AnyContext, | ||
TAllContext extends AnyContext = AnyContext, | ||
> { | ||
params: TAllParams | ||
routeSearch: TSearchSchema | ||
search: TFullSearchSchema | ||
abortController: AbortController | ||
preload: boolean | ||
routeContext: TContext | ||
context: TAllContext | ||
} | ||
export type UnloaderFn<TPath extends string> = ( | ||
routeMatch: RouteMatch<any, Route>, | ||
) => void | ||
export type SearchFilter<T, U = T> = (prev: T) => U | ||
export type ResolveId< | ||
TParentRoute, | ||
TCustomId extends string, | ||
TPath extends string, | ||
> = TParentRoute extends { id: infer TParentId extends string } | ||
? RoutePrefix<TParentId, string extends TCustomId ? TPath : TCustomId> | ||
: RootRouteId | ||
export type InferFullSearchSchema<TRoute> = TRoute extends { | ||
isRoot: true | ||
__types: { | ||
searchSchema: infer TSearchSchema | ||
} | ||
} | ||
? TSearchSchema | ||
: TRoute extends { | ||
__types: { | ||
fullSearchSchema: infer TFullSearchSchema | ||
} | ||
} | ||
? TFullSearchSchema | ||
: {} | ||
export type ResolveFullSearchSchema<TParentRoute, TSearchSchema> = | ||
InferFullSearchSchema<TParentRoute> & TSearchSchema | ||
export interface AnyRoute | ||
extends Route< | ||
any, | ||
any, | ||
any, | ||
any, | ||
any, | ||
any, | ||
any, | ||
any, | ||
any, | ||
any, | ||
any, | ||
any, | ||
any, | ||
any, | ||
any, | ||
any, | ||
any | ||
> {} | ||
export type AnyRouteWithRouterContext<TRouterContext extends AnyContext> = | ||
Route< | ||
any, | ||
any, | ||
any, | ||
any, | ||
any, | ||
any, | ||
any, | ||
any, | ||
any, | ||
any, | ||
any, | ||
any, | ||
any, | ||
any, | ||
TRouterContext, | ||
any, | ||
any | ||
> | ||
export type MergeParamsFromParent<T, U> = IsAny<T, U, T & U> | ||
export type UseLoaderResult<T> = T extends Record<PropertyKey, infer U> | ||
? { | ||
[K in keyof T]: UseLoaderResultPromise<T[K]> | ||
} | ||
: UseLoaderResultPromise<T> | ||
export type UseLoaderResultPromise<T> = T extends Promise<infer U> | ||
? StreamedPromise<U> | ||
: T | ||
export type StreamedPromise<T> = { | ||
promise: Promise<T> | ||
status: 'resolved' | 'pending' | ||
data: T | ||
resolve: (value: T) => void | ||
} | ||
export class Route< | ||
TAllRouteInfo extends AnyAllRouteInfo = DefaultAllRouteInfo, | ||
TRouteInfo extends AnyRouteInfo = RouteInfo, | ||
TRouterContext = unknown, | ||
TParentRoute extends AnyRoute = AnyRoute, | ||
TPath extends string = '/', | ||
TFullPath extends ResolveFullPath<TParentRoute, TPath> = ResolveFullPath< | ||
TParentRoute, | ||
TPath | ||
>, | ||
TCustomId extends string = string, | ||
TId extends ResolveId<TParentRoute, TCustomId, TPath> = ResolveId< | ||
TParentRoute, | ||
TCustomId, | ||
TPath | ||
>, | ||
TLoader = unknown, | ||
TSearchSchema extends AnySearchSchema = {}, | ||
TFullSearchSchema extends AnySearchSchema = ResolveFullSearchSchema< | ||
TParentRoute, | ||
TSearchSchema | ||
>, | ||
TParams extends Record<ParsePathParams<TPath>, any> = Record< | ||
ParsePathParams<TPath>, | ||
string | ||
>, | ||
TAllParams extends MergeParamsFromParent< | ||
TParentRoute['__types']['allParams'], | ||
TParams | ||
> = MergeParamsFromParent<TParentRoute['__types']['allParams'], TParams>, | ||
TParentContext extends TParentRoute['__types']['routeContext'] = TParentRoute['__types']['routeContext'], | ||
TAllParentContext extends TParentRoute['__types']['context'] = TParentRoute['__types']['context'], | ||
TRouteContext extends RouteContext = RouteContext, | ||
TContext extends MergeParamsFromParent< | ||
TParentRoute['__types']['context'], | ||
TRouteContext | ||
> = MergeParamsFromParent<TParentRoute['__types']['context'], TRouteContext>, | ||
TRouterContext extends AnyContext = AnyContext, | ||
TChildren extends unknown = unknown, | ||
TRoutesInfo extends DefaultRoutesInfo = DefaultRoutesInfo, | ||
> { | ||
routeInfo!: TRouteInfo | ||
id!: TRouteInfo['id'] | ||
customId!: TRouteInfo['customId'] | ||
path!: TRouteInfo['path'] | ||
fullPath!: TRouteInfo['fullPath'] | ||
getParentRoute!: () => undefined | AnyRoute | ||
childRoutes?: AnyRoute[] | ||
options!: RouteOptions | ||
originalIndex!: number | ||
getRouter!: () => Router< | ||
TAllRouteInfo['routeConfig'], | ||
TAllRouteInfo, | ||
TRouterContext | ||
> | ||
__types!: { | ||
parentRoute: TParentRoute | ||
path: TPath | ||
to: TrimPathRight<TFullPath> | ||
fullPath: TFullPath | ||
customId: TCustomId | ||
id: TId | ||
loader: TLoader | ||
searchSchema: TSearchSchema | ||
fullSearchSchema: TFullSearchSchema | ||
params: TParams | ||
allParams: TAllParams | ||
parentContext: TParentContext | ||
allParentContext: TAllParentContext | ||
routeContext: TRouteContext | ||
context: TContext | ||
children: TChildren | ||
routesInfo: TRoutesInfo | ||
routerContext: TRouterContext | ||
} | ||
isRoot: TParentRoute extends Route<any> ? true : false | ||
options: RouteOptions< | ||
TParentRoute, | ||
TCustomId, | ||
TPath, | ||
TLoader, | ||
InferFullSearchSchema<TParentRoute>, | ||
TSearchSchema, | ||
TFullSearchSchema, | ||
TParentRoute['__types']['allParams'], | ||
TParams, | ||
TAllParams, | ||
TParentContext, | ||
TAllParentContext, | ||
TRouteContext, | ||
TContext | ||
> & | ||
UpdatableRouteOptions< | ||
TLoader, | ||
TSearchSchema, | ||
TFullSearchSchema, | ||
TAllParams, | ||
TRouteContext, | ||
TContext | ||
> | ||
// Set up in this.init() | ||
parentRoute!: TParentRoute | ||
id!: TId | ||
// customId!: TCustomId | ||
path!: TPath | ||
fullPath!: TFullPath | ||
to!: TrimPathRight<TFullPath> | ||
// Optional | ||
children?: TChildren | ||
originalIndex?: number | ||
router?: Router<TRoutesInfo['routeTree'], TRoutesInfo> | ||
rank!: number | ||
constructor( | ||
routeConfig: RouteConfig, | ||
options: TRouteInfo['options'], | ||
originalIndex: number, | ||
parent: undefined | Route<TAllRouteInfo, any>, | ||
router: Router<TAllRouteInfo['routeConfig'], TAllRouteInfo, TRouterContext>, | ||
options: RouteOptions< | ||
TParentRoute, | ||
TCustomId, | ||
TPath, | ||
TLoader, | ||
InferFullSearchSchema<TParentRoute>, | ||
TSearchSchema, | ||
TFullSearchSchema, | ||
TParentRoute['__types']['allParams'], | ||
TParams, | ||
TAllParams, | ||
TParentContext, | ||
TAllParentContext, | ||
TRouteContext, | ||
TContext | ||
> & | ||
UpdatableRouteOptions< | ||
TLoader, | ||
TSearchSchema, | ||
TFullSearchSchema, | ||
TAllParams, | ||
TRouteContext, | ||
TContext | ||
>, | ||
) { | ||
Object.assign(this, { | ||
...routeConfig, | ||
originalIndex, | ||
this.options = (options as any) || {} | ||
this.isRoot = !options?.getParentRoute as any | ||
Route.__onInit(this as any) | ||
} | ||
init = (opts: { originalIndex: number; router: AnyRouter }) => { | ||
this.originalIndex = opts.originalIndex | ||
this.router = opts.router | ||
const options = this.options as RouteOptions< | ||
TParentRoute, | ||
TCustomId, | ||
TPath, | ||
InferFullSearchSchema<TParentRoute>, | ||
TSearchSchema, | ||
TParentRoute['__types']['allParams'], | ||
TParams | ||
> & | ||
RoutePathOptionsIntersection<TCustomId, TPath> | ||
const isRoot = !options?.path && !options?.id | ||
this.parentRoute = this.options?.getParentRoute?.() | ||
if (isRoot) { | ||
this.path = rootRouteId as TPath | ||
} else { | ||
invariant( | ||
this.parentRoute, | ||
`Child Route instances must pass a 'getParentRoute: () => ParentRoute' option that returns a Route instance.`, | ||
) | ||
} | ||
let path: undefined | string = isRoot ? rootRouteId : options.path | ||
// If the path is anything other than an index path, trim it up | ||
if (path && path !== '/') { | ||
path = trimPath(path) | ||
} | ||
const customId = options?.id || path | ||
// Strip the parentId prefix from the first level of children | ||
let id = isRoot | ||
? rootRouteId | ||
: joinPaths([ | ||
(this.parentRoute.id as any) === rootRouteId | ||
? '' | ||
: this.parentRoute.id, | ||
customId, | ||
]) | ||
if (path === rootRouteId) { | ||
path = '/' | ||
} | ||
if (id !== rootRouteId) { | ||
id = joinPaths(['/', id]) | ||
} | ||
const fullPath = | ||
id === rootRouteId ? '/' : joinPaths([this.parentRoute.fullPath, path]) | ||
this.path = path as TPath | ||
this.id = id as TId | ||
// this.customId = customId as TCustomId | ||
this.fullPath = fullPath as TFullPath | ||
this.to = fullPath as TrimPathRight<TFullPath> | ||
} | ||
addChildren = <TNewChildren extends AnyRoute[]>( | ||
children: TNewChildren, | ||
): Route< | ||
TParentRoute, | ||
TPath, | ||
TFullPath, | ||
TCustomId, | ||
TId, | ||
TLoader, | ||
TSearchSchema, | ||
TFullSearchSchema, | ||
TParams, | ||
TAllParams, | ||
TParentContext, | ||
TAllParentContext, | ||
TRouteContext, | ||
TContext, | ||
TRouterContext, | ||
TNewChildren, | ||
TRoutesInfo | ||
> => { | ||
this.children = children as any | ||
return this as any | ||
} | ||
update = ( | ||
options: UpdatableRouteOptions< | ||
TLoader, | ||
TSearchSchema, | ||
TFullSearchSchema, | ||
TAllParams, | ||
TRouteContext, | ||
TContext | ||
>, | ||
) => { | ||
Object.assign(this.options, options) | ||
return this | ||
} | ||
static __onInit = (route: typeof this) => { | ||
// This is a dummy static method that should get | ||
// replaced by a framework specific implementation if necessary | ||
} | ||
} | ||
export type AnyRootRoute = RootRoute<any, any, any, any> | ||
export class RouterContext<TRouterContext extends {}> { | ||
constructor() {} | ||
createRootRoute = < | ||
TLoader = unknown, | ||
TSearchSchema extends AnySearchSchema = {}, | ||
TContext extends RouteContext = RouteContext, | ||
>( | ||
options?: Omit< | ||
RouteOptions<AnyRoute, RootRouteId, '', {}, TSearchSchema, {}, {}>, | ||
| 'path' | ||
| 'id' | ||
| 'getParentRoute' | ||
| 'caseSensitive' | ||
| 'parseParams' | ||
| 'stringifyParams' | ||
>, | ||
) => { | ||
return new RootRoute<TLoader, TSearchSchema, TContext, TRouterContext>( | ||
options, | ||
getRouter: () => router, | ||
childRoutes: undefined!, | ||
getParentRoute: () => parent, | ||
}) | ||
) | ||
} | ||
} | ||
router.options.createRoute?.({ router, route: this }) | ||
export class RootRoute< | ||
TLoader = unknown, | ||
TSearchSchema extends AnySearchSchema = {}, | ||
TContext extends RouteContext = RouteContext, | ||
TRouterContext extends {} = {}, | ||
> extends Route< | ||
any, | ||
'/', | ||
'/', | ||
string, | ||
RootRouteId, | ||
TLoader, | ||
TSearchSchema, | ||
TSearchSchema, | ||
{}, | ||
{}, | ||
TRouterContext, | ||
TRouterContext, | ||
MergeParamsFromParent<TRouterContext, TContext>, | ||
MergeParamsFromParent<TRouterContext, TContext>, | ||
TRouterContext, | ||
any, | ||
any | ||
> { | ||
constructor( | ||
options?: Omit< | ||
RouteOptions<AnyRoute, RootRouteId, '', {}, TSearchSchema, {}, {}>, | ||
| 'path' | ||
| 'id' | ||
| 'getParentRoute' | ||
| 'caseSensitive' | ||
| 'parseParams' | ||
| 'stringifyParams' | ||
>, | ||
) { | ||
super(options as any) | ||
} | ||
} | ||
export type ResolveFullPath< | ||
TParentRoute extends AnyRoute, | ||
TPath extends string, | ||
TPrefixed extends RoutePrefix<TParentRoute['fullPath'], TPath> = RoutePrefix< | ||
TParentRoute['fullPath'], | ||
TPath | ||
>, | ||
> = TPrefixed extends RootRouteId ? '/' : TPrefixed | ||
type RoutePrefix< | ||
TPrefix extends string, | ||
TPath extends string, | ||
> = string extends TPath | ||
? RootRouteId | ||
: TPath extends string | ||
? TPrefix extends RootRouteId | ||
? TPath extends '/' | ||
? '/' | ||
: `/${TrimPath<TPath>}` | ||
: `${TPrefix}/${TPath}` extends '/' | ||
? '/' | ||
: `/${TrimPathLeft<`${TrimPathRight<TPrefix>}/${TrimPath<TPath>}`>}` | ||
: never | ||
export type TrimPath<T extends string> = '' extends T | ||
? '' | ||
: TrimPathRight<TrimPathLeft<T>> | ||
export type TrimPathLeft<T extends string> = | ||
T extends `${RootRouteId}/${infer U}` | ||
? TrimPathLeft<U> | ||
: T extends `/${infer U}` | ||
? TrimPathLeft<U> | ||
: T | ||
export type TrimPathRight<T extends string> = T extends '/' | ||
? '/' | ||
: T extends `${infer U}/` | ||
? TrimPathRight<U> | ||
: T | ||
// const rootRoute = new RootRoute({ | ||
// validateSearch: () => null as unknown as { root?: boolean }, | ||
// }) | ||
// const aRoute = new Route({ | ||
// getParentRoute: () => rootRoute, | ||
// path: 'a', | ||
// validateSearch: () => null as unknown as { a?: string }, | ||
// }) | ||
// const bRoute = new Route({ | ||
// getParentRoute: () => aRoute, | ||
// path: 'b', | ||
// }) | ||
// const rootIsRoot = rootRoute.isRoot | ||
// // ^? | ||
// const aIsRoot = aRoute.isRoot | ||
// // ^? | ||
// const rId = rootRoute.id | ||
// // ^? | ||
// const aId = aRoute.id | ||
// // ^? | ||
// const bId = bRoute.id | ||
// // ^? | ||
// const rPath = rootRoute.fullPath | ||
// // ^? | ||
// const aPath = aRoute.fullPath | ||
// // ^? | ||
// const bPath = bRoute.fullPath | ||
// // ^? | ||
// const rSearch = rootRoute.__types.fullSearchSchema | ||
// // ^? | ||
// const aSearch = aRoute.__types.fullSearchSchema | ||
// // ^? | ||
// const bSearch = bRoute.__types.fullSearchSchema | ||
// // ^? | ||
// const config = rootRoute.addChildren([aRoute.addChildren([bRoute])]) | ||
// // ^? |
@@ -1,21 +0,13 @@ | ||
import { Route } from './route' | ||
import { | ||
AnyLoaderData, | ||
AnyPathParams, | ||
AnyRouteConfig, | ||
AnyRouteConfigWithChildren, | ||
AnySearchSchema, | ||
RootRouteId, | ||
RouteConfig, | ||
RouteOptions, | ||
} from './routeConfig' | ||
import { IsAny, UnionToIntersection, Values } from './utils' | ||
import { AnyRoute, Route } from './route' | ||
import { AnyPathParams, AnySearchSchema, RootRouteId } from './route' | ||
import { IsAny, MergeUnion, Values } from './utils' | ||
export interface AnyAllRouteInfo { | ||
routeConfig: AnyRouteConfig | ||
routeInfo: AnyRouteInfo | ||
routeInfoById: Record<string, AnyRouteInfo> | ||
routeInfoByFullPath: Record<string, AnyRouteInfo> | ||
export interface AnyRoutesInfo { | ||
routeTree: AnyRoute | ||
routeUnion: AnyRoute | ||
routesById: Record<string, AnyRoute> | ||
routesByFullPath: Record<string, AnyRoute> | ||
routeIds: any | ||
routePaths: any | ||
routeIntersection: AnyRoute | ||
fullSearchSchema: Record<string, any> | ||
@@ -25,9 +17,10 @@ allParams: Record<string, any> | ||
export interface DefaultAllRouteInfo { | ||
routeConfig: RouteConfig | ||
routeInfo: RouteInfo | ||
routeInfoById: Record<string, RouteInfo> | ||
routeInfoByFullPath: Record<string, RouteInfo> | ||
export interface DefaultRoutesInfo { | ||
routeTree: AnyRoute | ||
routeUnion: AnyRoute | ||
routesById: Record<string, Route> | ||
routesByFullPath: Record<string, Route> | ||
routeIds: string | ||
routePaths: string | ||
routeIntersection: AnyRoute | ||
fullSearchSchema: AnySearchSchema | ||
@@ -37,197 +30,125 @@ allParams: AnyPathParams | ||
export interface AllRouteInfo<TRouteConfig extends AnyRouteConfig = RouteConfig> | ||
extends RoutesInfoInner<TRouteConfig, ParseRouteConfig<TRouteConfig>> {} | ||
export interface RoutesInfo<TRouteTree extends AnyRoute = Route> | ||
extends RoutesInfoInner<TRouteTree, ParseRoute<TRouteTree>> {} | ||
export type ParseRouteConfig<TRouteConfig = AnyRouteConfig> = | ||
TRouteConfig extends AnyRouteConfig | ||
? RouteConfigRoute<TRouteConfig> | ParseRouteChildren<TRouteConfig> | ||
: never | ||
type ParseRouteChildren<TRouteConfig> = | ||
TRouteConfig extends AnyRouteConfigWithChildren<infer TChildren> | ||
? unknown extends TChildren | ||
export interface RoutesInfoInner< | ||
TRouteTree extends AnyRoute, | ||
TRouteUnion extends AnyRoute = Route, | ||
TRoutesById = { '/': TRouteUnion } & { | ||
[TRoute in TRouteUnion as TRoute['id']]: TRoute | ||
}, | ||
// RoutePaths should always use index routes if possible, but not | ||
// force trailing slashes. To do this, we check if each route | ||
// has an index route registered and if it does, we omit the layout | ||
// route. Then for any index routes, we remove the trailing slash | ||
TRoutesByFullPath = { '/': TRouteUnion } & { | ||
[TRoute in TRouteUnion as TRoute['fullPath'] extends RootRouteId | ||
? never | ||
: TChildren extends AnyRouteConfig[] | ||
? Values<{ | ||
[TId in TChildren[number]['id']]: ParseRouteChild< | ||
TChildren[number], | ||
TId | ||
> | ||
}> | ||
: never // Children are not routes | ||
: never // No children | ||
: string extends TRoute['fullPath'] | ||
? never | ||
: `${TRoute['fullPath']}/` extends keyof TRoutesById | ||
? never | ||
: TRoute['fullPath'] extends `${infer Trimmed}/` | ||
? Trimmed | ||
: TRoute['fullPath']]: TRoute | ||
}, | ||
> { | ||
routeTree: TRouteTree | ||
routeUnion: TRouteUnion | ||
routesById: TRoutesById | ||
routesByFullPath: TRoutesByFullPath | ||
routeIds: keyof TRoutesById | ||
routePaths: keyof TRoutesByFullPath | ||
routeIntersection: Route< | ||
TRouteUnion['__types']['parentRoute'], // TParentRoute, | ||
TRouteUnion['__types']['path'], // TPath, | ||
TRouteUnion['__types']['fullPath'], // TFullPath, | ||
TRouteUnion['__types']['customId'], // TCustomId, | ||
TRouteUnion['__types']['id'], // TId, | ||
TRouteUnion['__types']['loader'], // TId, | ||
MergeUnion<TRouteUnion['__types']['searchSchema']> & {}, // TSearchSchema, | ||
MergeUnion<TRouteUnion['__types']['fullSearchSchema']> & {}, // TFullSearchSchema, | ||
MergeUnion<TRouteUnion['__types']['params']>, // TParams, | ||
MergeUnion<TRouteUnion['__types']['allParams']>, // TAllParams, | ||
MergeUnion<TRouteUnion['__types']['parentContext']>, // TParentContext, | ||
MergeUnion<TRouteUnion['__types']['allParentContext']>, // TAllParentContext, | ||
MergeUnion<TRouteUnion['__types']['routeContext']> & {}, // TRouteContext, | ||
MergeUnion<TRouteUnion['__types']['context']> & {}, // TContext, | ||
MergeUnion<TRouteUnion['__types']['routerContext']> & {}, // TRouterContext, | ||
TRouteUnion['__types']['children'], // TChildren, | ||
TRouteUnion['__types']['routesInfo'] // TRoutesInfo, | ||
> | ||
fullSearchSchema: Partial< | ||
MergeUnion<TRouteUnion['__types']['fullSearchSchema']> | ||
> | ||
allParams: Partial<MergeUnion<TRouteUnion['__types']['allParams']>> | ||
} | ||
type ParseRouteChild<TRouteConfig, TId> = TRouteConfig & { | ||
id: TId | ||
} extends AnyRouteConfig | ||
? ParseRouteConfig<TRouteConfig> | ||
export type ParseRoute<TRouteTree> = TRouteTree extends AnyRoute | ||
? TRouteTree | ParseRouteChildren<TRouteTree> | ||
: never | ||
// Generics! | ||
export type RouteConfigRoute<TRouteConfig> = TRouteConfig extends RouteConfig< | ||
infer TId, | ||
infer TCustomId, | ||
infer TPath, | ||
infer TFullPath, | ||
infer TParentRouteLoaderData, | ||
infer TRouteLoaderData, | ||
infer TParentLoaderData, | ||
infer TLoaderData, | ||
infer TParentSearchSchema, | ||
infer TSearchSchema, | ||
infer TFullSearchSchema, | ||
infer TParentParams, | ||
infer TParams, | ||
infer TAllParams, | ||
export type ParseRouteChildren<TRouteTree> = TRouteTree extends Route< | ||
any, | ||
any, | ||
any, | ||
any, | ||
any, | ||
any, | ||
any, | ||
any, | ||
any, | ||
any, | ||
any, | ||
any, | ||
any, | ||
any, | ||
any, | ||
infer TChildren, | ||
any | ||
> | ||
? string extends TCustomId | ||
? unknown extends TChildren | ||
? never | ||
: RouteInfo< | ||
TId, | ||
TCustomId, | ||
TPath, | ||
TFullPath, | ||
TParentRouteLoaderData, | ||
TRouteLoaderData, | ||
TParentLoaderData, | ||
TLoaderData, | ||
TParentSearchSchema, | ||
TSearchSchema, | ||
TFullSearchSchema, | ||
TParentParams, | ||
TParams, | ||
TAllParams | ||
> | ||
: TChildren extends AnyRoute[] | ||
? Values<{ | ||
[TId in TChildren[number]['id']]: ParseRouteChild< | ||
TChildren[number], | ||
TId | ||
> | ||
}> | ||
: never | ||
: never | ||
export interface RoutesInfoInner< | ||
TRouteConfig extends AnyRouteConfig, | ||
TRouteInfo extends RouteInfo< | ||
string, | ||
string, | ||
any, | ||
any, | ||
any, | ||
any, | ||
any, | ||
any, | ||
any, | ||
any, | ||
any, | ||
any, | ||
any, | ||
any | ||
> = RouteInfo, | ||
TRouteInfoById = { '/': TRouteInfo } & { | ||
[TInfo in TRouteInfo as TInfo['id']]: TInfo | ||
}, | ||
TRouteInfoByFullPath = { '/': TRouteInfo } & { | ||
[TInfo in TRouteInfo as TInfo['fullPath'] extends RootRouteId | ||
? never | ||
: string extends TInfo['fullPath'] | ||
? never | ||
: TInfo['fullPath']]: TInfo | ||
}, | ||
> { | ||
routeConfig: TRouteConfig | ||
routeInfo: TRouteInfo | ||
routeInfoById: TRouteInfoById | ||
routeInfoByFullPath: TRouteInfoByFullPath | ||
routeIds: keyof TRouteInfoById | ||
routePaths: keyof TRouteInfoByFullPath | ||
fullSearchSchema: Partial<UnionToIntersection<TRouteInfo['fullSearchSchema']>> | ||
allParams: Partial<UnionToIntersection<TRouteInfo['allParams']>> | ||
} | ||
export type ParseRouteChild<TRoute, TId> = TRoute extends AnyRoute | ||
? ParseRoute<TRoute> | ||
: never | ||
export interface AnyRouteInfo | ||
extends RouteInfo< | ||
any, | ||
any, | ||
any, | ||
any, | ||
any, | ||
any, | ||
any, | ||
any, | ||
any, | ||
any, | ||
any, | ||
any, | ||
any, | ||
any | ||
> {} | ||
export interface RouteInfo< | ||
TId extends string = string, | ||
TCustomId extends string = string, | ||
TPath extends string = string, | ||
TFullPath extends string = '/', | ||
TParentRouteLoaderData extends AnyLoaderData = {}, | ||
TRouteLoaderData extends AnyLoaderData = {}, | ||
TParentLoaderData extends AnyLoaderData = {}, | ||
TLoaderData extends AnyLoaderData = {}, | ||
TParentSearchSchema extends {} = {}, | ||
TSearchSchema extends AnySearchSchema = {}, | ||
TFullSearchSchema extends AnySearchSchema = {}, | ||
TParentParams extends AnyPathParams = {}, | ||
TParams extends AnyPathParams = {}, | ||
TAllParams extends AnyPathParams = {}, | ||
> { | ||
id: TId | ||
customId: TCustomId | ||
path: TPath | ||
fullPath: TFullPath | ||
parentRouteLoaderData: TParentRouteLoaderData | ||
routeLoaderData: TRouteLoaderData | ||
parentLoaderData: TParentLoaderData | ||
loaderData: TLoaderData | ||
searchSchema: TSearchSchema | ||
fullSearchSchema: TFullSearchSchema | ||
parentParams: TParentParams | ||
params: TParams | ||
allParams: TAllParams | ||
options: RouteOptions< | ||
TCustomId, | ||
TPath, | ||
TParentRouteLoaderData, | ||
TRouteLoaderData, | ||
TParentLoaderData, | ||
TLoaderData, | ||
TParentSearchSchema, | ||
TSearchSchema, | ||
TFullSearchSchema, | ||
TParentParams, | ||
TParams, | ||
TAllParams | ||
> | ||
export type RoutesById<TRoutesInfo extends AnyRoutesInfo> = { | ||
[K in keyof TRoutesInfo['routesById']]: TRoutesInfo['routesById'][K] | ||
} | ||
export type RoutesById<TAllRouteInfo extends AnyAllRouteInfo> = { | ||
[K in keyof TAllRouteInfo['routeInfoById']]: Route< | ||
TAllRouteInfo, | ||
TAllRouteInfo['routeInfoById'][K] | ||
> | ||
} | ||
export type RouteInfoById< | ||
TAllRouteInfo extends AnyAllRouteInfo, | ||
export type RouteById< | ||
TRoutesInfo extends AnyRoutesInfo, | ||
TId, | ||
> = TId extends keyof TAllRouteInfo['routeInfoById'] | ||
> = TId extends keyof TRoutesInfo['routesById'] | ||
? IsAny< | ||
TAllRouteInfo['routeInfoById'][TId]['id'], | ||
RouteInfo, | ||
TAllRouteInfo['routeInfoById'][TId] | ||
TRoutesInfo['routesById'][TId]['id'], | ||
Route, | ||
TRoutesInfo['routesById'][TId] | ||
> | ||
: never | ||
export type RouteInfoByPath< | ||
TAllRouteInfo extends AnyAllRouteInfo, | ||
export type RoutesByPath<TRoutesInfo extends AnyRoutesInfo> = { | ||
[K in keyof TRoutesInfo['routesByFullPath']]: TRoutesInfo['routesByFullPath'][K] | ||
} | ||
export type RouteByPath< | ||
TRoutesInfo extends AnyRoutesInfo, | ||
TPath, | ||
> = TPath extends keyof TAllRouteInfo['routeInfoByFullPath'] | ||
> = TPath extends keyof TRoutesInfo['routesByFullPath'] | ||
? IsAny< | ||
TAllRouteInfo['routeInfoByFullPath'][TPath]['id'], | ||
RouteInfo, | ||
TAllRouteInfo['routeInfoByFullPath'][TPath] | ||
TRoutesInfo['routesByFullPath'][TPath]['id'], | ||
Route, | ||
TRoutesInfo['routesByFullPath'][TPath] | ||
> | ||
: never |
1953
src/router.ts
@@ -0,4 +1,6 @@ | ||
import { Store } from '@tanstack/react-store' | ||
import invariant from 'tiny-invariant' | ||
import { GetFrameworkGeneric } from './frameworks' | ||
// | ||
import { | ||
@@ -9,3 +11,2 @@ LinkInfo, | ||
ToOptions, | ||
ValidFromPath, | ||
ResolveRelativePath, | ||
@@ -18,24 +19,26 @@ } from './link' | ||
matchPathname, | ||
parsePathname, | ||
resolvePath, | ||
trimPath, | ||
trimPathRight, | ||
} from './path' | ||
import { AnyRoute, Route } from './route' | ||
import { | ||
AnyLoaderData, | ||
Route, | ||
AnySearchSchema, | ||
AnyRoute, | ||
RootRoute, | ||
AnyContext, | ||
AnyPathParams, | ||
AnyRouteConfig, | ||
AnySearchSchema, | ||
LoaderContext, | ||
RouteConfig, | ||
SearchFilter, | ||
} from './routeConfig' | ||
RouteProps, | ||
RegisteredRouteComponent, | ||
RegisteredRouteErrorComponent, | ||
} from './route' | ||
import { | ||
AllRouteInfo, | ||
AnyAllRouteInfo, | ||
RouteInfo, | ||
RoutesInfo, | ||
AnyRoutesInfo, | ||
RoutesById, | ||
RoutesByPath, | ||
DefaultRoutesInfo, | ||
} from './routeInfo' | ||
import { RouteMatch, RouteMatchStore } from './routeMatch' | ||
import { defaultParseSearch, defaultStringifySearch } from './searchParams' | ||
import { createStore, batch, Store } from './store' | ||
import { | ||
@@ -47,7 +50,7 @@ functionalUpdate, | ||
PickAsRequired, | ||
PickRequired, | ||
Timeout, | ||
Updater, | ||
replaceEqualDeep, | ||
partialDeepEqual, | ||
} from './utils' | ||
import { replaceEqualDeep } from './interop' | ||
import { | ||
@@ -58,5 +61,12 @@ createBrowserHistory, | ||
} from './history' | ||
import { createMemo } from '@solidjs/reactivity' | ||
export interface RegisterRouter { | ||
// | ||
declare global { | ||
interface Window { | ||
__TSR_DEHYDRATED__?: HydrationCtx | ||
} | ||
} | ||
export interface Register { | ||
// router: Router | ||
@@ -67,13 +77,10 @@ } | ||
export type RegisteredRouter = RegisterRouter extends { | ||
router: Router<infer TRouteConfig, infer TAllRouteInfo, infer TRouterContext> | ||
export type RegisteredRouterPair = Register extends { | ||
router: infer TRouter extends AnyRouter | ||
} | ||
? Router<TRouteConfig, TAllRouteInfo, TRouterContext> | ||
: Router | ||
? [TRouter, TRouter['types']['RoutesInfo']] | ||
: [Router, AnyRoutesInfo] | ||
export type RegisteredAllRouteInfo = RegisterRouter extends { | ||
router: Router<infer TRouteConfig, infer TAllRouteInfo, infer TRouterContext> | ||
} | ||
? TAllRouteInfo | ||
: AnyAllRouteInfo | ||
export type RegisteredRouter = RegisteredRouterPair[0] | ||
export type RegisteredRoutesInfo = RegisteredRouterPair[1] | ||
@@ -104,9 +111,52 @@ export interface LocationState {} | ||
export type SearchParser = (searchStr: string) => Record<string, any> | ||
export type FilterRoutesFn = <TRoute extends Route<any, RouteInfo>>( | ||
routeConfigs: TRoute[], | ||
) => TRoute[] | ||
export type HydrationCtx = { | ||
router: DehydratedRouter | ||
payload: Record<string, any> | ||
} | ||
export interface RouteMatch< | ||
TRoutesInfo extends AnyRoutesInfo = DefaultRoutesInfo, | ||
TRoute extends AnyRoute = Route, | ||
> { | ||
id: string | ||
key?: string | ||
routeId: string | ||
pathname: string | ||
params: TRoute['__types']['allParams'] | ||
status: 'idle' | 'pending' | 'success' | 'error' | ||
isFetching: boolean | ||
invalid: boolean | ||
error: unknown | ||
paramsError: unknown | ||
searchError: unknown | ||
updatedAt: number | ||
invalidAt: number | ||
preloadInvalidAt: number | ||
loaderData: TRoute['__types']['loader'] | ||
loadPromise?: Promise<void> | ||
__resolveLoadPromise?: () => void | ||
routeContext: TRoute['__types']['routeContext'] | ||
context: TRoute['__types']['context'] | ||
routeSearch: TRoute['__types']['searchSchema'] | ||
search: TRoutesInfo['fullSearchSchema'] & | ||
TRoute['__types']['fullSearchSchema'] | ||
fetchedAt: number | ||
abortController: AbortController | ||
} | ||
export type AnyRouteMatch = RouteMatch<AnyRoutesInfo, AnyRoute> | ||
export type RouterContextOptions<TRouteTree extends AnyRoute> = | ||
AnyContext extends TRouteTree['__types']['routerContext'] | ||
? { | ||
context?: TRouteTree['__types']['routerContext'] | ||
} | ||
: { | ||
context: TRouteTree['__types']['routerContext'] | ||
} | ||
export interface RouterOptions< | ||
TRouteConfig extends AnyRouteConfig, | ||
TRouterContext, | ||
TRouteTree extends AnyRoute, | ||
TDehydrated extends Record<string, any>, | ||
> { | ||
@@ -116,82 +166,47 @@ history?: RouterHistory | ||
parseSearch?: SearchParser | ||
filterRoutes?: FilterRoutesFn | ||
defaultPreload?: false | 'intent' | ||
defaultPreloadDelay?: number | ||
defaultComponent?: RegisteredRouteComponent< | ||
RouteProps<unknown, AnySearchSchema, AnyPathParams, AnyContext, AnyContext> | ||
> | ||
defaultErrorComponent?: RegisteredRouteErrorComponent< | ||
RouteProps<unknown, AnySearchSchema, AnyPathParams, AnyContext, AnyContext> | ||
> | ||
defaultPendingComponent?: RegisteredRouteComponent< | ||
RouteProps<unknown, AnySearchSchema, AnyPathParams, AnyContext, AnyContext> | ||
> | ||
defaultMaxAge?: number | ||
defaultGcMaxAge?: number | ||
defaultPreloadMaxAge?: number | ||
defaultPreloadGcMaxAge?: number | ||
defaultPreloadDelay?: number | ||
defaultComponent?: GetFrameworkGeneric<'Component'> | ||
defaultErrorComponent?: GetFrameworkGeneric<'ErrorComponent'> | ||
defaultPendingComponent?: GetFrameworkGeneric<'Component'> | ||
defaultLoaderMaxAge?: number | ||
defaultLoaderGcMaxAge?: number | ||
caseSensitive?: boolean | ||
routeConfig?: TRouteConfig | ||
routeTree?: TRouteTree | ||
basepath?: string | ||
useServerData?: boolean | ||
Router?: (router: AnyRouter) => void | ||
createRoute?: (opts: { route: AnyRoute; router: AnyRouter }) => void | ||
context?: TRouterContext | ||
loadComponent?: ( | ||
component: GetFrameworkGeneric<'Component'>, | ||
) => Promise<GetFrameworkGeneric<'Component'>> | ||
onRouteChange?: () => void | ||
fetchServerDataFn?: FetchServerDataFn | ||
context?: TRouteTree['__types']['routerContext'] | ||
Wrap?: React.ComponentType<{ | ||
children: React.ReactNode | ||
dehydratedState?: TDehydrated | ||
}> | ||
dehydrate?: () => TDehydrated | ||
hydrate?: (dehydrated: TDehydrated) => void | ||
} | ||
type FetchServerDataFn = (ctx: { | ||
router: AnyRouter | ||
routeMatch: RouteMatch | ||
}) => Promise<any> | ||
export interface Loader< | ||
TFullSearchSchema extends AnySearchSchema = {}, | ||
TAllParams extends AnyPathParams = {}, | ||
TRouteLoaderData = AnyLoaderData, | ||
> { | ||
fetch: keyof PickRequired<TFullSearchSchema> extends never | ||
? keyof TAllParams extends never | ||
? (loaderContext: { signal?: AbortSignal }) => Promise<TRouteLoaderData> | ||
: (loaderContext: { | ||
params: TAllParams | ||
search?: TFullSearchSchema | ||
signal?: AbortSignal | ||
}) => Promise<TRouteLoaderData> | ||
: keyof TAllParams extends never | ||
? (loaderContext: { | ||
search: TFullSearchSchema | ||
params: TAllParams | ||
signal?: AbortSignal | ||
}) => Promise<TRouteLoaderData> | ||
: (loaderContext: { | ||
search: TFullSearchSchema | ||
signal?: AbortSignal | ||
}) => Promise<TRouteLoaderData> | ||
current?: LoaderState<TFullSearchSchema, TAllParams> | ||
latest?: LoaderState<TFullSearchSchema, TAllParams> | ||
pending: LoaderState<TFullSearchSchema, TAllParams>[] | ||
} | ||
export interface LoaderState< | ||
TFullSearchSchema extends AnySearchSchema = {}, | ||
TAllParams extends AnyPathParams = {}, | ||
> { | ||
loadedAt: number | ||
loaderContext: LoaderContext<TFullSearchSchema, TAllParams> | ||
} | ||
export interface RouterStore< | ||
TSearchObj extends AnySearchSchema = {}, | ||
export interface RouterState< | ||
TRoutesInfo extends AnyRoutesInfo = AnyRoutesInfo, | ||
TState extends LocationState = LocationState, | ||
> { | ||
status: 'idle' | 'loading' | ||
latestLocation: ParsedLocation<TSearchObj, TState> | ||
currentMatches: RouteMatch[] | ||
currentLocation: ParsedLocation<TSearchObj, TState> | ||
pendingMatches?: RouteMatch[] | ||
pendingLocation?: ParsedLocation<TSearchObj, TState> | ||
status: 'idle' | 'pending' | ||
isFetching: boolean | ||
matchesById: Record< | ||
string, | ||
RouteMatch<TRoutesInfo, TRoutesInfo['routeIntersection']> | ||
> | ||
matchIds: string[] | ||
pendingMatchIds: string[] | ||
matches: RouteMatch<TRoutesInfo, TRoutesInfo['routeIntersection']>[] | ||
pendingMatches: RouteMatch<TRoutesInfo, TRoutesInfo['routeIntersection']>[] | ||
location: ParsedLocation<TRoutesInfo['fullSearchSchema'], TState> | ||
resolvedLocation: ParsedLocation<TRoutesInfo['fullSearchSchema'], TState> | ||
lastUpdated: number | ||
loaders: Record<string, Loader> | ||
isFetching: boolean | ||
isPreloading: boolean | ||
matchCache: Record<string, MatchCacheEntry> | ||
} | ||
@@ -210,11 +225,5 @@ | ||
fromCurrent?: boolean | ||
__preSearchFilters?: SearchFilter<any>[] | ||
__postSearchFilters?: SearchFilter<any>[] | ||
__matches?: AnyRouteMatch[] | ||
} | ||
export type MatchCacheEntry = { | ||
gc: number | ||
match: RouteMatch | ||
} | ||
export interface MatchLocation { | ||
@@ -231,2 +240,3 @@ to?: string | number | null | ||
caseSensitive?: boolean | ||
includeSearch?: boolean | ||
fuzzy?: boolean | ||
@@ -240,66 +250,32 @@ } | ||
export interface DehydratedRouterState | ||
extends Pick< | ||
RouterStore, | ||
'status' | 'latestLocation' | 'currentLocation' | 'lastUpdated' | ||
> { | ||
currentMatches: DehydratedRouteMatch[] | ||
} | ||
extends Pick<RouterState, 'status' | 'location' | 'lastUpdated'> {} | ||
export interface DehydratedRouter<TRouterContext = unknown> { | ||
// location: Router['__location'] | ||
export interface DehydratedRouter { | ||
state: DehydratedRouterState | ||
context: TRouterContext | ||
} | ||
export type MatchCache = Record<string, MatchCacheEntry> | ||
export type RouterConstructorOptions< | ||
TRouteTree extends AnyRoute, | ||
TDehydrated extends Record<string, any>, | ||
> = Omit<RouterOptions<TRouteTree, TDehydrated>, 'context'> & | ||
RouterContextOptions<TRouteTree> | ||
interface DehydratedRouteMatch { | ||
id: string | ||
state: Pick< | ||
RouteMatchStore<any, any>, | ||
'status' | 'routeLoaderData' | 'invalid' | 'invalidAt' | ||
> | ||
} | ||
export const componentTypes = [ | ||
'component', | ||
'errorComponent', | ||
'pendingComponent', | ||
] as const | ||
export interface RouterContext {} | ||
export const defaultFetchServerDataFn: FetchServerDataFn = async ({ | ||
router, | ||
routeMatch, | ||
}) => { | ||
const next = router.buildNext({ | ||
to: '.', | ||
search: (d: any) => ({ | ||
...(d ?? {}), | ||
__data: { | ||
matchId: routeMatch.id, | ||
}, | ||
}), | ||
}) | ||
const res = await fetch(next.href, { | ||
method: 'GET', | ||
signal: routeMatch.abortController.signal, | ||
}) | ||
if (res.ok) { | ||
return res.json() | ||
} | ||
throw new Error('Failed to fetch match data') | ||
} | ||
export class Router< | ||
TRouteConfig extends AnyRouteConfig = RouteConfig, | ||
TAllRouteInfo extends AnyAllRouteInfo = AllRouteInfo<TRouteConfig>, | ||
TRouterContext = unknown, | ||
TRouteTree extends AnyRoute = AnyRoute, | ||
TRoutesInfo extends AnyRoutesInfo = RoutesInfo<TRouteTree>, | ||
TDehydrated extends Record<string, any> = Record<string, any>, | ||
> { | ||
types!: { | ||
// Super secret internal stuff | ||
RouteConfig: TRouteConfig | ||
AllRouteInfo: TAllRouteInfo | ||
RootRoute: TRouteTree | ||
RoutesInfo: TRoutesInfo | ||
} | ||
options: PickAsRequired< | ||
RouterOptions<TRouteConfig, TRouterContext>, | ||
RouterOptions<TRouteTree, TDehydrated>, | ||
'stringifySearch' | 'parseSearch' | 'context' | ||
@@ -309,6 +285,7 @@ > | ||
#unsubHistory?: () => void | ||
basepath: string | ||
// __location: Location<TAllRouteInfo['fullSearchSchema']> | ||
routeTree!: Route<TAllRouteInfo, RouteInfo> | ||
routesById!: RoutesById<TAllRouteInfo> | ||
basepath!: string | ||
routeTree!: RootRoute | ||
routesById!: RoutesById<TRoutesInfo> | ||
routesByPath!: RoutesByPath<TRoutesInfo> | ||
flatRoutes!: TRoutesInfo['routesByFullPath'][keyof TRoutesInfo['routesByFullPath']][] | ||
navigateTimeout: undefined | Timeout | ||
@@ -318,11 +295,8 @@ nextAction: undefined | 'push' | 'replace' | ||
store: Store<RouterStore<TAllRouteInfo['fullSearchSchema']>> | ||
startedLoadingAt = Date.now() | ||
resolveNavigation = () => {} | ||
__store: Store<RouterState<TRoutesInfo>> | ||
state: RouterState<TRoutesInfo> | ||
dehydratedData?: TDehydrated | ||
constructor(options?: RouterOptions<TRouteConfig, TRouterContext>) { | ||
constructor(options: RouterConstructorOptions<TRouteTree, TDehydrated>) { | ||
this.options = { | ||
defaultLoaderGcMaxAge: 5 * 60 * 1000, | ||
defaultLoaderMaxAge: 0, | ||
defaultPreloadMaxAge: 2000, | ||
defaultPreloadDelay: 50, | ||
@@ -333,60 +307,88 @@ context: undefined!, | ||
parseSearch: options?.parseSearch ?? defaultParseSearch, | ||
fetchServerDataFn: options?.fetchServerDataFn ?? defaultFetchServerDataFn, | ||
// fetchServerDataFn: options?.fetchServerDataFn ?? defaultFetchServerDataFn, | ||
} | ||
this.store = createStore(getInitialRouterState()) | ||
this.basepath = '' | ||
this.__store = new Store<RouterState<TRoutesInfo>>( | ||
getInitialRouterState(), | ||
{ | ||
onUpdate: () => { | ||
const prev = this.state | ||
this.update(options) | ||
this.state = this.__store.state | ||
// Allow frameworks to hook into the router creation | ||
this.options.Router?.(this) | ||
} | ||
const matchesByIdChanged = prev.matchesById !== this.state.matchesById | ||
let matchesChanged | ||
let pendingMatchesChanged | ||
reset = () => { | ||
this.store.setState((s) => Object.assign(s, getInitialRouterState())) | ||
} | ||
if (!matchesByIdChanged) { | ||
matchesChanged = | ||
prev.matchIds.length !== this.state.matchIds.length || | ||
prev.matchIds.some((d, i) => d !== this.state.matchIds[i]) | ||
mount = () => { | ||
// Mount only does anything on the client | ||
if (!isServer) { | ||
// If the router matches are empty, load the matches | ||
if (!this.store.state.currentMatches.length) { | ||
this.load() | ||
} | ||
pendingMatchesChanged = | ||
prev.pendingMatchIds.length !== | ||
this.state.pendingMatchIds.length || | ||
prev.pendingMatchIds.some( | ||
(d, i) => d !== this.state.pendingMatchIds[i], | ||
) | ||
} | ||
const visibilityChangeEvent = 'visibilitychange' | ||
const focusEvent = 'focus' | ||
if (matchesByIdChanged || matchesChanged) { | ||
this.state.matches = this.state.matchIds.map((id) => { | ||
return this.state.matchesById[id] as any | ||
}) | ||
} | ||
// addEventListener does not exist in React Native, but window does | ||
// In the future, we might need to invert control here for more adapters | ||
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition | ||
if (window.addEventListener) { | ||
// Listen to visibilitychange and focus | ||
window.addEventListener(visibilityChangeEvent, this.#onFocus, false) | ||
window.addEventListener(focusEvent, this.#onFocus, false) | ||
} | ||
if (matchesByIdChanged || pendingMatchesChanged) { | ||
this.state.pendingMatches = this.state.pendingMatchIds.map((id) => { | ||
return this.state.matchesById[id] as any | ||
}) | ||
} | ||
return () => { | ||
if (window.removeEventListener) { | ||
// Be sure to unsubscribe if a new handler is set | ||
this.state.isFetching = [ | ||
...this.state.matches, | ||
...this.state.pendingMatches, | ||
].some((d) => d.isFetching) | ||
}, | ||
defaultPriority: 'low', | ||
}, | ||
) | ||
window.removeEventListener(visibilityChangeEvent, this.#onFocus) | ||
window.removeEventListener(focusEvent, this.#onFocus) | ||
} | ||
} | ||
this.state = this.__store.state | ||
this.update(options) | ||
const next = this.buildNext({ | ||
hash: true, | ||
fromCurrent: true, | ||
search: true, | ||
state: true, | ||
}) | ||
if (this.state.location.href !== next.href) { | ||
this.#commitLocation({ ...next, replace: true }) | ||
} | ||
} | ||
return () => {} | ||
reset = () => { | ||
this.__store.setState((s) => Object.assign(s, getInitialRouterState())) | ||
} | ||
update = < | ||
TRouteConfig extends RouteConfig = RouteConfig, | ||
TAllRouteInfo extends AnyAllRouteInfo = AllRouteInfo<TRouteConfig>, | ||
TRouterContext = unknown, | ||
>( | ||
opts?: RouterOptions<TRouteConfig, TRouterContext>, | ||
): Router<TRouteConfig, TAllRouteInfo, TRouterContext> => { | ||
Object.assign(this.options, opts) | ||
mount = () => { | ||
// If the router matches are empty, start loading the matches | ||
// if (!this.state.matches.length) { | ||
this.safeLoad() | ||
// } | ||
} | ||
update = (opts?: RouterOptions<any, any>): this => { | ||
this.options = { | ||
...this.options, | ||
...opts, | ||
context: { | ||
...this.options.context, | ||
...opts?.context, | ||
}, | ||
} | ||
if ( | ||
@@ -404,43 +406,36 @@ !this.history || | ||
this.store.setState((s) => { | ||
s.latestLocation = this.#parseLocation() | ||
s.currentLocation = s.latestLocation | ||
}) | ||
const parsedLocation = this.#parseLocation() | ||
this.__store.setState((s) => ({ | ||
...s, | ||
resolvedLocation: parsedLocation, | ||
location: parsedLocation, | ||
})) | ||
this.#unsubHistory = this.history.listen(() => { | ||
this.load(this.#parseLocation(this.store.state.latestLocation)) | ||
this.safeLoad({ | ||
next: this.#parseLocation(this.state.location), | ||
}) | ||
}) | ||
} | ||
const { basepath, routeConfig } = this.options | ||
const { basepath, routeTree } = this.options | ||
this.basepath = `/${trimPath(basepath ?? '') ?? ''}` | ||
if (routeConfig) { | ||
this.routesById = {} as any | ||
this.routeTree = this.#buildRouteTree(routeConfig) | ||
if (routeTree && routeTree !== this.routeTree) { | ||
this.#buildRouteTree(routeTree) | ||
} | ||
return this as any | ||
return this | ||
} | ||
buildNext = (opts: BuildNextOptions) => { | ||
buildNext = (opts: BuildNextOptions): ParsedLocation => { | ||
const next = this.#buildLocation(opts) | ||
const matches = this.matchRoutes(next.pathname) | ||
const __matches = this.matchRoutes(next.pathname, next.search) | ||
const __preSearchFilters = matches | ||
.map((match) => match.route.options.preSearchFilters ?? []) | ||
.flat() | ||
.filter(Boolean) | ||
const __postSearchFilters = matches | ||
.map((match) => match.route.options.postSearchFilters ?? []) | ||
.flat() | ||
.filter(Boolean) | ||
return this.#buildLocation({ | ||
...opts, | ||
__preSearchFilters, | ||
__postSearchFilters, | ||
__matches, | ||
}) | ||
@@ -450,162 +445,134 @@ } | ||
cancelMatches = () => { | ||
;[ | ||
...this.store.state.currentMatches, | ||
...(this.store.state.pendingMatches || []), | ||
].forEach((match) => { | ||
match.cancel() | ||
this.state.matches.forEach((match) => { | ||
this.cancelMatch(match.id) | ||
}) | ||
} | ||
load = async (next?: ParsedLocation) => { | ||
let now = Date.now() | ||
const startedAt = now | ||
this.startedLoadingAt = startedAt | ||
cancelMatch = (id: string) => { | ||
this.getRouteMatch(id)?.abortController?.abort() | ||
} | ||
// Cancel any pending matches | ||
this.cancelMatches() | ||
safeLoad = (opts?: { next?: ParsedLocation }) => { | ||
return this.load(opts).catch((err) => { | ||
// console.warn(err) | ||
// invariant(false, 'Encountered an error during router.load()! ☝️.') | ||
}) | ||
} | ||
let matches!: RouteMatch<any, any>[] | ||
latestLoadPromise: Promise<void> = Promise.resolve() | ||
batch(() => { | ||
if (next) { | ||
// Ingest the new location | ||
this.store.setState((s) => { | ||
s.latestLocation = next | ||
}) | ||
load = async (opts?: { next?: ParsedLocation; throwOnError?: boolean }) => { | ||
const promise = new Promise<void>(async (resolve, reject) => { | ||
let latestPromise: Promise<void> | undefined | null | ||
const checkLatest = (): undefined | Promise<void> | null => { | ||
return this.latestLoadPromise !== promise | ||
? this.latestLoadPromise | ||
: undefined | ||
} | ||
// Match the routes | ||
matches = this.matchRoutes(this.store.state.latestLocation.pathname, { | ||
strictParseParams: true, | ||
}) | ||
// Cancel any pending matches | ||
// this.cancelMatches() | ||
this.store.setState((s) => { | ||
s.status = 'loading' | ||
s.pendingMatches = matches | ||
s.pendingLocation = this.store.state.latestLocation | ||
}) | ||
}) | ||
let pendingMatches!: RouteMatch<any, any>[] | ||
// Load the matches | ||
try { | ||
await this.loadMatches(matches) | ||
} catch (err: any) { | ||
console.warn(err) | ||
invariant( | ||
false, | ||
'Matches failed to load due to error above ☝️. Navigation cancelled!', | ||
) | ||
} | ||
this.__store.batch(() => { | ||
if (opts?.next) { | ||
// Ingest the new location | ||
this.__store.setState((s) => ({ | ||
...s, | ||
location: opts.next!, | ||
})) | ||
} | ||
if (this.startedLoadingAt !== startedAt) { | ||
// Ignore side-effects of outdated side-effects | ||
return this.navigationPromise | ||
} | ||
// Match the routes | ||
pendingMatches = this.matchRoutes( | ||
this.state.location.pathname, | ||
this.state.location.search, | ||
{ | ||
throwOnError: opts?.throwOnError, | ||
debug: true, | ||
}, | ||
) | ||
const previousMatches = this.store.state.currentMatches | ||
this.__store.setState((s) => ({ | ||
...s, | ||
status: 'pending', | ||
pendingMatchIds: pendingMatches.map((d) => d.id), | ||
matchesById: this.#mergeMatches(s.matchesById, pendingMatches), | ||
})) | ||
}) | ||
const exiting: RouteMatch[] = [], | ||
staying: RouteMatch[] = [] | ||
try { | ||
// Load the matches | ||
await this.loadMatches(pendingMatches) | ||
previousMatches.forEach((d) => { | ||
if (matches.find((dd) => dd.id === d.id)) { | ||
staying.push(d) | ||
} else { | ||
exiting.push(d) | ||
} | ||
}) | ||
// Only apply the latest transition | ||
if ((latestPromise = checkLatest())) { | ||
return await latestPromise | ||
} | ||
const entering = matches.filter((d) => { | ||
return !previousMatches.find((dd) => dd.id === d.id) | ||
}) | ||
const prevLocation = this.state.resolvedLocation | ||
now = Date.now() | ||
this.__store.setState((s) => ({ | ||
...s, | ||
status: 'idle', | ||
resolvedLocation: s.location, | ||
matchIds: s.pendingMatchIds, | ||
pendingMatchIds: [], | ||
})) | ||
exiting.forEach((d) => { | ||
d.__onExit?.({ | ||
params: d.params, | ||
search: d.store.state.routeSearch, | ||
}) | ||
if (prevLocation!.href !== this.state.location.href) { | ||
this.options.onRouteChange?.() | ||
} | ||
// Clear non-loading error states when match leaves | ||
if (d.store.state.status === 'error' && !d.store.state.isFetching) { | ||
d.store.setState((s) => { | ||
s.status = 'idle' | ||
s.error = undefined | ||
}) | ||
} | ||
resolve() | ||
} catch (err) { | ||
// Only apply the latest transition | ||
if ((latestPromise = checkLatest())) { | ||
return await latestPromise | ||
} | ||
const gc = Math.max( | ||
d.route.options.loaderGcMaxAge ?? | ||
this.options.defaultLoaderGcMaxAge ?? | ||
0, | ||
d.route.options.loaderMaxAge ?? this.options.defaultLoaderMaxAge ?? 0, | ||
) | ||
if (gc > 0) { | ||
this.store.setState((s) => { | ||
s.matchCache[d.id] = { | ||
gc: gc == Infinity ? Number.MAX_SAFE_INTEGER : now + gc, | ||
match: d, | ||
} | ||
}) | ||
reject(err) | ||
} | ||
}) | ||
staying.forEach((d) => { | ||
d.route.options.onTransition?.({ | ||
params: d.params, | ||
search: d.store.state.routeSearch, | ||
}) | ||
}) | ||
this.latestLoadPromise = promise | ||
entering.forEach((d) => { | ||
d.__onExit = d.route.options.onLoaded?.({ | ||
params: d.params, | ||
search: d.store.state.search, | ||
}) | ||
delete this.store.state.matchCache[d.id] | ||
}) | ||
this.store.setState((s) => { | ||
Object.assign(s, { | ||
status: 'idle', | ||
currentLocation: this.store.state.latestLocation, | ||
currentMatches: matches, | ||
pendingLocation: undefined, | ||
pendingMatches: undefined, | ||
}) | ||
}) | ||
this.options.onRouteChange?.() | ||
this.resolveNavigation() | ||
return this.latestLoadPromise | ||
} | ||
cleanMatchCache = () => { | ||
const now = Date.now() | ||
#mergeMatches = ( | ||
prevMatchesById: Record< | ||
string, | ||
RouteMatch<TRoutesInfo, TRoutesInfo['routeIntersection']> | ||
>, | ||
nextMatches: RouteMatch[], | ||
): Record< | ||
string, | ||
RouteMatch<TRoutesInfo, TRoutesInfo['routeIntersection']> | ||
> => { | ||
const nextMatchesById: any = { | ||
...prevMatchesById, | ||
} | ||
this.store.setState((s) => { | ||
Object.keys(s.matchCache).forEach((matchId) => { | ||
const entry = s.matchCache[matchId]! | ||
let hadNew = false | ||
// Don't remove loading matches | ||
if (entry.match.store.state.status === 'loading') { | ||
return | ||
} | ||
nextMatches.forEach((match) => { | ||
if (!nextMatchesById[match.id]) { | ||
hadNew = true | ||
nextMatchesById[match.id] = match | ||
} | ||
}) | ||
// Do not remove successful matches that are still valid | ||
if (entry.gc > 0 && entry.gc > now) { | ||
return | ||
} | ||
if (!hadNew) { | ||
return prevMatchesById | ||
} | ||
// Everything else gets removed | ||
delete s.matchCache[matchId] | ||
}) | ||
}) | ||
return nextMatchesById | ||
} | ||
getRoute = <TId extends keyof TAllRouteInfo['routeInfoById']>( | ||
getRoute = <TId extends keyof TRoutesInfo['routesById']>( | ||
id: TId, | ||
): Route<TAllRouteInfo, TAllRouteInfo['routeInfoById'][TId]> => { | ||
): TRoutesInfo['routesById'][TId] => { | ||
const route = this.routesById[id] | ||
@@ -618,245 +585,457 @@ | ||
loadRoute = async ( | ||
navigateOpts: BuildNextOptions = this.store.state.latestLocation, | ||
): Promise<RouteMatch[]> => { | ||
const next = this.buildNext(navigateOpts) | ||
const matches = this.matchRoutes(next.pathname, { | ||
strictParseParams: true, | ||
}) | ||
await this.loadMatches(matches) | ||
return matches | ||
} | ||
preloadRoute = async ( | ||
navigateOpts: BuildNextOptions = this.store.state.latestLocation, | ||
loaderOpts: { maxAge?: number; gcMaxAge?: number }, | ||
navigateOpts: BuildNextOptions & { | ||
maxAge?: number | ||
} = this.state.location, | ||
) => { | ||
const next = this.buildNext(navigateOpts) | ||
const matches = this.matchRoutes(next.pathname, { | ||
strictParseParams: true, | ||
const matches = this.matchRoutes(next.pathname, next.search, { | ||
throwOnError: true, | ||
}) | ||
this.__store.setState((s) => { | ||
return { | ||
...s, | ||
matchesById: this.#mergeMatches(s.matchesById, matches), | ||
} | ||
}) | ||
await this.loadMatches(matches, { | ||
preload: true, | ||
maxAge: | ||
loaderOpts.maxAge ?? | ||
this.options.defaultPreloadMaxAge ?? | ||
this.options.defaultLoaderMaxAge ?? | ||
0, | ||
gcMaxAge: | ||
loaderOpts.gcMaxAge ?? | ||
this.options.defaultPreloadGcMaxAge ?? | ||
this.options.defaultLoaderGcMaxAge ?? | ||
0, | ||
maxAge: navigateOpts.maxAge, | ||
}) | ||
return matches | ||
} | ||
matchRoutes = (pathname: string, opts?: { strictParseParams?: boolean }) => { | ||
const matches: RouteMatch[] = [] | ||
cleanMatches = () => { | ||
const now = Date.now() | ||
if (!this.routeTree) { | ||
return matches | ||
const outdatedMatchIds = Object.values(this.state.matchesById) | ||
.filter((match) => { | ||
const route = this.getRoute(match.routeId) | ||
return ( | ||
!this.state.matchIds.includes(match.id) && | ||
!this.state.pendingMatchIds.includes(match.id) && | ||
match.preloadInvalidAt < now && | ||
(route.options.gcMaxAge | ||
? match.updatedAt + route.options.gcMaxAge < now | ||
: true) | ||
) | ||
}) | ||
.map((d) => d.id) | ||
if (outdatedMatchIds.length) { | ||
this.__store.setState((s) => { | ||
const matchesById = { ...s.matchesById } | ||
outdatedMatchIds.forEach((id) => { | ||
delete matchesById[id] | ||
}) | ||
return { | ||
...s, | ||
matchesById, | ||
} | ||
}) | ||
} | ||
} | ||
const existingMatches = [ | ||
...this.store.state.currentMatches, | ||
...(this.store.state.pendingMatches ?? []), | ||
] | ||
matchRoutes = ( | ||
pathname: string, | ||
locationSearch: AnySearchSchema, | ||
opts?: { throwOnError?: boolean; debug?: boolean }, | ||
): RouteMatch<TRoutesInfo, TRoutesInfo['routeIntersection']>[] => { | ||
let routeParams: AnyPathParams = {} | ||
const recurse = async (routes: Route<any, any>[]): Promise<void> => { | ||
const parentMatch = last(matches) | ||
let params = parentMatch?.params ?? {} | ||
let foundRoute = this.flatRoutes.find((route) => { | ||
const matchedParams = matchPathname(this.basepath, pathname, { | ||
to: route.fullPath, | ||
caseSensitive: | ||
route.options.caseSensitive ?? this.options.caseSensitive, | ||
}) | ||
const filteredRoutes = this.options.filterRoutes?.(routes) ?? routes | ||
if (matchedParams) { | ||
routeParams = matchedParams | ||
return true | ||
} | ||
let foundRoutes: Route[] = [] | ||
return false | ||
}) | ||
const findMatchInRoutes = (parentRoutes: Route[], routes: Route[]) => { | ||
routes.some((route) => { | ||
if (!route.path && route.childRoutes?.length) { | ||
return findMatchInRoutes([...foundRoutes, route], route.childRoutes) | ||
} | ||
let routeCursor = foundRoute || (this.routesById['__root__'] as any) | ||
const fuzzy = !!(route.path !== '/' || route.childRoutes?.length) | ||
let matchedRoutes: AnyRoute[] = [routeCursor] | ||
const matchParams = matchPathname(this.basepath, pathname, { | ||
to: route.fullPath, | ||
fuzzy, | ||
caseSensitive: | ||
route.options.caseSensitive ?? this.options.caseSensitive, | ||
}) | ||
while (routeCursor?.parentRoute) { | ||
routeCursor = routeCursor.parentRoute | ||
if (routeCursor) matchedRoutes.unshift(routeCursor) | ||
} | ||
if (matchParams) { | ||
let parsedParams | ||
// Alright, by now we should have all of our | ||
// matching routes and their param pairs, let's | ||
// Turn them into actual `Match` objects and | ||
// accumulate the params into a single params bag | ||
let allParams = {} | ||
try { | ||
parsedParams = | ||
route.options.parseParams?.(matchParams!) ?? matchParams | ||
} catch (err) { | ||
if (opts?.strictParseParams) { | ||
throw err | ||
} | ||
} | ||
// Existing matches are matches that are already loaded along with | ||
// pending matches that are still loading | ||
params = { | ||
...params, | ||
...parsedParams, | ||
} | ||
} | ||
const matches = matchedRoutes.map((route) => { | ||
let parsedParams | ||
let parsedParamsError | ||
if (!!matchParams) { | ||
foundRoutes = [...parentRoutes, route] | ||
} | ||
return !!foundRoutes.length | ||
try { | ||
parsedParams = | ||
(route.options.parseParams as any)?.(routeParams!) ?? routeParams | ||
// (typeof route.options.parseParams === 'object' && | ||
// route.options.parseParams.parse | ||
// ? route.options.parseParams.parse(routeParams) | ||
// : (route.options.parseParams as any)?.(routeParams!)) ?? routeParams | ||
} catch (err: any) { | ||
parsedParamsError = new PathParamError(err.message, { | ||
cause: err, | ||
}) | ||
return !!foundRoutes.length | ||
if (opts?.throwOnError) { | ||
throw parsedParamsError | ||
} | ||
} | ||
findMatchInRoutes([], filteredRoutes) | ||
// Add the parsed params to the accumulated params bag | ||
Object.assign(allParams, parsedParams) | ||
if (!foundRoutes.length) { | ||
return | ||
const interpolatedPath = interpolatePath(route.path, allParams) | ||
const key = route.options.key | ||
? route.options.key({ | ||
params: allParams, | ||
search: locationSearch, | ||
}) ?? '' | ||
: '' | ||
const stringifiedKey = key ? JSON.stringify(key) : '' | ||
const matchId = | ||
interpolatePath(route.id, allParams, true) + stringifiedKey | ||
// Waste not, want not. If we already have a match for this route, | ||
// reuse it. This is important for layout routes, which might stick | ||
// around between navigation actions that only change leaf routes. | ||
const existingMatch = this.getRouteMatch(matchId) | ||
if (existingMatch) { | ||
return { ...existingMatch } | ||
} | ||
foundRoutes.forEach((foundRoute) => { | ||
const interpolatedPath = interpolatePath(foundRoute.path, params) | ||
const matchId = interpolatePath(foundRoute.id, params, true) | ||
// Create a fresh route match | ||
const hasLoaders = !!( | ||
route.options.loader || | ||
componentTypes.some((d) => (route.options[d] as any)?.preload) | ||
) | ||
const match = | ||
existingMatches.find((d) => d.id === matchId) || | ||
this.store.state.matchCache[matchId]?.match || | ||
new RouteMatch(this, foundRoute, { | ||
id: matchId, | ||
params, | ||
pathname: joinPaths([this.basepath, interpolatedPath]), | ||
const routeMatch: RouteMatch = { | ||
id: matchId, | ||
key: stringifiedKey, | ||
routeId: route.id, | ||
params: allParams, | ||
pathname: joinPaths([this.basepath, interpolatedPath]), | ||
updatedAt: Date.now(), | ||
invalidAt: Infinity, | ||
preloadInvalidAt: Infinity, | ||
routeSearch: {}, | ||
search: {} as any, | ||
status: hasLoaders ? 'idle' : 'success', | ||
isFetching: false, | ||
invalid: false, | ||
error: undefined, | ||
paramsError: parsedParamsError, | ||
searchError: undefined, | ||
loaderData: undefined, | ||
loadPromise: Promise.resolve(), | ||
routeContext: undefined!, | ||
context: undefined!, | ||
abortController: new AbortController(), | ||
fetchedAt: 0, | ||
} | ||
return routeMatch | ||
}) | ||
// Take each match and resolve its search params and context | ||
// This has to happen after the matches are created or found | ||
// so that we can use the parent match's search params and context | ||
matches.forEach((match, i): any => { | ||
const parentMatch = matches[i - 1] | ||
const route = this.getRoute(match.routeId) | ||
const searchInfo = (() => { | ||
// Validate the search params and stabilize them | ||
const parentSearchInfo = { | ||
search: parentMatch?.search ?? locationSearch, | ||
routeSearch: parentMatch?.routeSearch ?? locationSearch, | ||
} | ||
try { | ||
const validator = | ||
typeof route.options.validateSearch === 'object' | ||
? route.options.validateSearch.parse | ||
: route.options.validateSearch | ||
const routeSearch = validator?.(parentSearchInfo.search) ?? {} | ||
const search = { | ||
...parentSearchInfo.search, | ||
...routeSearch, | ||
} | ||
return { | ||
routeSearch: replaceEqualDeep(match.routeSearch, routeSearch), | ||
search: replaceEqualDeep(match.search, search), | ||
} | ||
} catch (err: any) { | ||
match.searchError = new SearchParamError(err.message, { | ||
cause: err, | ||
}) | ||
matches.push(match) | ||
}) | ||
if (opts?.throwOnError) { | ||
throw match.searchError | ||
} | ||
const foundRoute = last(foundRoutes)! | ||
return parentSearchInfo | ||
} | ||
})() | ||
if (foundRoute.childRoutes?.length) { | ||
recurse(foundRoute.childRoutes) | ||
} | ||
} | ||
const contextInfo = (() => { | ||
try { | ||
const routeContext = | ||
route.options.getContext?.({ | ||
parentContext: parentMatch?.routeContext ?? {}, | ||
context: parentMatch?.context ?? this?.options.context ?? {}, | ||
params: match.params, | ||
search: match.search, | ||
}) || ({} as any) | ||
recurse([this.routeTree]) | ||
const context = { | ||
...(parentMatch?.context ?? this?.options.context), | ||
...routeContext, | ||
} as any | ||
linkMatches(matches) | ||
return { | ||
context, | ||
routeContext, | ||
} | ||
} catch (err) { | ||
route.options.onError?.(err) | ||
throw err | ||
} | ||
})() | ||
return matches | ||
Object.assign(match, { | ||
...searchInfo, | ||
...contextInfo, | ||
}) | ||
}) | ||
return matches as any | ||
} | ||
loadMatches = async ( | ||
resolvedMatches: RouteMatch[], | ||
loaderOpts?: | ||
| { preload: true; maxAge: number; gcMaxAge: number } | ||
| { preload?: false; maxAge?: never; gcMaxAge?: never }, | ||
resolvedMatches: AnyRouteMatch[], | ||
opts?: { | ||
preload?: boolean | ||
maxAge?: number | ||
}, | ||
) => { | ||
this.cleanMatchCache() | ||
resolvedMatches.forEach(async (match) => { | ||
// Validate the match (loads search params etc) | ||
match.__validate() | ||
}) | ||
this.cleanMatches() | ||
let firstBadMatchIndex: number | undefined | ||
// Check each match middleware to see if the route can be accessed | ||
await Promise.all( | ||
resolvedMatches.map(async (match) => { | ||
try { | ||
await match.route.options.beforeLoad?.({ | ||
router: this as any, | ||
match, | ||
}) | ||
} catch (err) { | ||
if (!loaderOpts?.preload) { | ||
match.route.options.onLoadError?.(err) | ||
try { | ||
await Promise.all( | ||
resolvedMatches.map(async (match, index) => { | ||
const route = this.getRoute(match.routeId) | ||
if (!opts?.preload) { | ||
// Update each match with its latest url data | ||
this.setRouteMatch(match.id, (s) => ({ | ||
...s, | ||
routeSearch: match.routeSearch, | ||
search: match.search, | ||
routeContext: match.routeContext, | ||
context: match.context, | ||
error: match.error, | ||
paramsError: match.paramsError, | ||
searchError: match.searchError, | ||
params: match.params, | ||
})) | ||
} | ||
throw err | ||
} | ||
}), | ||
) | ||
const handleError = ( | ||
err: any, | ||
handler: undefined | ((err: any) => void), | ||
) => { | ||
firstBadMatchIndex = firstBadMatchIndex ?? index | ||
handler = handler || route.options.onError | ||
const matchPromises = resolvedMatches.map(async (match, index) => { | ||
const prevMatch = resolvedMatches[(index = 1)] | ||
const search = match.store.state.search as { __data?: any } | ||
if (isRedirect(err)) { | ||
throw err | ||
} | ||
if (search.__data?.matchId && search.__data.matchId !== match.id) { | ||
return | ||
} | ||
try { | ||
handler?.(err) | ||
} catch (errorHandlerErr) { | ||
err = errorHandlerErr | ||
match.load(loaderOpts) | ||
if (isRedirect(errorHandlerErr)) { | ||
throw errorHandlerErr | ||
} | ||
} | ||
if (match.store.state.status !== 'success' && match.__loadPromise) { | ||
// Wait for the first sign of activity from the match | ||
await match.__loadPromise | ||
} | ||
this.setRouteMatch(match.id, (s) => ({ | ||
...s, | ||
error: err, | ||
status: 'error', | ||
updatedAt: Date.now(), | ||
})) | ||
} | ||
if (prevMatch) { | ||
await prevMatch.__loadPromise | ||
} | ||
}) | ||
if (match.paramsError) { | ||
handleError(match.paramsError, route.options.onParseParamsError) | ||
} | ||
await Promise.all(matchPromises) | ||
} | ||
if (match.searchError) { | ||
handleError(match.searchError, route.options.onValidateSearchError) | ||
} | ||
loadMatchData = async ( | ||
routeMatch: RouteMatch<any, any>, | ||
): Promise<Record<string, unknown>> => { | ||
if (isServer || !this.options.useServerData) { | ||
return ( | ||
(await routeMatch.route.options.loader?.({ | ||
// parentLoaderPromise: routeMatch.parentMatch.dataPromise, | ||
params: routeMatch.params, | ||
search: routeMatch.store.state.routeSearch, | ||
signal: routeMatch.abortController.signal, | ||
})) || {} | ||
try { | ||
await route.options.beforeLoad?.({ | ||
...match, | ||
preload: !!opts?.preload, | ||
}) | ||
} catch (err) { | ||
handleError(err, route.options.onBeforeLoadError) | ||
} | ||
}), | ||
) | ||
} else { | ||
// Refresh: | ||
// '/dashboard' | ||
// '/dashboard/invoices/' | ||
// '/dashboard/invoices/123' | ||
} catch (err) { | ||
if (!opts?.preload) { | ||
this.navigate(err as any) | ||
} | ||
// New: | ||
// '/dashboard/invoices/456' | ||
throw err | ||
} | ||
// TODO: batch requests when possible | ||
const validResolvedMatches = resolvedMatches.slice(0, firstBadMatchIndex) | ||
const matchPromises: Promise<any>[] = [] | ||
const res = await this.options.fetchServerDataFn!({ | ||
router: this, | ||
routeMatch, | ||
}) | ||
validResolvedMatches.forEach((match, index) => { | ||
matchPromises.push( | ||
(async () => { | ||
const parentMatchPromise = matchPromises[index - 1] | ||
const route = this.getRoute(match.routeId) | ||
return res | ||
} | ||
} | ||
if ( | ||
match.isFetching || | ||
(match.status === 'success' && | ||
!this.getIsInvalid({ matchId: match.id, preload: opts?.preload })) | ||
) { | ||
return this.getRouteMatch(match.id)?.loadPromise | ||
} | ||
invalidateRoute = async < | ||
TFrom extends ValidFromPath<TAllRouteInfo> = '/', | ||
TTo extends string = '.', | ||
>( | ||
opts: ToOptions<TAllRouteInfo, TFrom, TTo>, | ||
) => { | ||
const next = this.buildNext(opts) | ||
const unloadedMatchIds = this.matchRoutes(next.pathname).map((d) => d.id) | ||
const fetchedAt = Date.now() | ||
const checkLatest = () => { | ||
const latest = this.getRouteMatch(match.id) | ||
return latest && latest.fetchedAt !== fetchedAt | ||
? latest.loadPromise | ||
: undefined | ||
} | ||
await Promise.allSettled( | ||
[ | ||
...this.store.state.currentMatches, | ||
...(this.store.state.pendingMatches ?? []), | ||
].map(async (match) => { | ||
if (unloadedMatchIds.includes(match.id)) { | ||
return match.invalidate() | ||
} | ||
}), | ||
) | ||
const loadPromise = (async () => { | ||
let latestPromise | ||
const componentsPromise = Promise.all( | ||
componentTypes.map(async (type) => { | ||
const component = route.options[type] | ||
if ((component as any)?.preload) { | ||
await (component as any).preload() | ||
} | ||
}), | ||
) | ||
const loaderPromise = route.options.loader?.({ | ||
...match, | ||
preload: !!opts?.preload, | ||
parentMatchPromise, | ||
}) | ||
const handleError = (err: any) => { | ||
if (isRedirect(err)) { | ||
if (!opts?.preload) { | ||
this.navigate(err as any) | ||
} | ||
return true | ||
} | ||
return false | ||
} | ||
try { | ||
const [_, loader] = await Promise.all([ | ||
componentsPromise, | ||
loaderPromise, | ||
]) | ||
if ((latestPromise = checkLatest())) return await latestPromise | ||
this.setRouteMatchData(match.id, () => loader, opts) | ||
} catch (err) { | ||
if ((latestPromise = checkLatest())) return await latestPromise | ||
if (handleError(err)) { | ||
return | ||
} | ||
const errorHandler = | ||
route.options.onLoadError ?? route.options.onError | ||
let caughtError = err | ||
try { | ||
errorHandler?.(err) | ||
} catch (errorHandlerErr) { | ||
caughtError = errorHandlerErr | ||
if (handleError(errorHandlerErr)) { | ||
return | ||
} | ||
} | ||
this.setRouteMatch(match.id, (s) => ({ | ||
...s, | ||
error: caughtError, | ||
status: 'error', | ||
isFetching: false, | ||
updatedAt: Date.now(), | ||
})) | ||
} | ||
})() | ||
this.setRouteMatch(match.id, (s) => ({ | ||
...s, | ||
status: s.status !== 'success' ? 'pending' : s.status, | ||
isFetching: true, | ||
loadPromise, | ||
fetchedAt, | ||
invalid: false, | ||
})) | ||
await loadPromise | ||
})(), | ||
) | ||
}) | ||
await Promise.all(matchPromises) | ||
} | ||
reload = () => { | ||
this.navigate({ | ||
return this.navigate({ | ||
fromCurrent: true, | ||
@@ -872,8 +1051,5 @@ replace: true, | ||
navigate = async < | ||
TFrom extends ValidFromPath<TAllRouteInfo> = '/', | ||
TTo extends string = '.', | ||
>({ | ||
navigate = async <TFrom extends string = '/', TTo extends string = ''>({ | ||
from, | ||
to = '.' as any, | ||
to = '' as any, | ||
search, | ||
@@ -883,3 +1059,3 @@ hash, | ||
params, | ||
}: NavigateOptions<TAllRouteInfo, TFrom, TTo>) => { | ||
}: NavigateOptions<TRoutesInfo, TFrom, TTo>) => { | ||
// If this link simply reloads the current route, | ||
@@ -915,13 +1091,9 @@ // make sure it has a new key so it will trigger a data refresh | ||
matchRoute = < | ||
TFrom extends ValidFromPath<TAllRouteInfo> = '/', | ||
TTo extends string = '.', | ||
TFrom extends string = '/', | ||
TTo extends string = '', | ||
TResolved extends string = ResolveRelativePath<TFrom, NoInfer<TTo>>, | ||
>( | ||
location: ToOptions<TAllRouteInfo, TFrom, TTo>, | ||
location: ToOptions<TRoutesInfo, TFrom, TTo>, | ||
opts?: MatchRouteOptions, | ||
): | ||
| false | ||
| TAllRouteInfo['routeInfoById'][ResolveRelativePath< | ||
TFrom, | ||
NoInfer<TTo> | ||
>]['allParams'] => { | ||
): false | TRoutesInfo['routesById'][TResolved]['__types']['allParams'] => { | ||
location = { | ||
@@ -932,35 +1104,34 @@ ...location, | ||
: undefined, | ||
} | ||
} as any | ||
const next = this.buildNext(location) | ||
if (opts?.pending && this.state.status !== 'pending') { | ||
return false | ||
} | ||
if (opts?.pending) { | ||
if (!this.store.state.pendingLocation) { | ||
return false | ||
} | ||
const baseLocation = opts?.pending | ||
? this.state.location | ||
: this.state.resolvedLocation | ||
return matchPathname( | ||
this.basepath, | ||
this.store.state.pendingLocation!.pathname, | ||
{ | ||
...opts, | ||
to: next.pathname, | ||
}, | ||
) as any | ||
if (!baseLocation) { | ||
return false | ||
} | ||
return matchPathname( | ||
this.basepath, | ||
this.store.state.currentLocation.pathname, | ||
{ | ||
...opts, | ||
to: next.pathname, | ||
}, | ||
) as any | ||
const match = matchPathname(this.basepath, baseLocation.pathname, { | ||
...opts, | ||
to: next.pathname, | ||
}) as any | ||
if (!match) { | ||
return false | ||
} | ||
if (opts?.includeSearch ?? true) { | ||
return partialDeepEqual(baseLocation.search, next.search) ? match : false | ||
} | ||
return match | ||
} | ||
buildLink = < | ||
TFrom extends ValidFromPath<TAllRouteInfo> = '/', | ||
TTo extends string = '.', | ||
>({ | ||
buildLink = <TFrom extends string = '/', TTo extends string = ''>({ | ||
from, | ||
@@ -975,7 +1146,5 @@ to = '.' as any, | ||
preload, | ||
preloadMaxAge: userPreloadMaxAge, | ||
preloadGcMaxAge: userPreloadGcMaxAge, | ||
preloadDelay: userPreloadDelay, | ||
disabled, | ||
}: LinkOptions<TAllRouteInfo, TFrom, TTo>): LinkInfo => { | ||
}: LinkOptions<TRoutesInfo, TFrom, TTo>): LinkInfo => { | ||
// If this link simply reloads the current route, | ||
@@ -1011,6 +1180,3 @@ // make sure it has a new key so it will trigger a data refresh | ||
// Compare path/hash for matches | ||
const pathIsEqual = | ||
this.store.state.currentLocation.pathname === next.pathname | ||
const currentPathSplit = | ||
this.store.state.currentLocation.pathname.split('/') | ||
const currentPathSplit = this.state.location.pathname.split('/') | ||
const nextPathSplit = next.pathname.split('/') | ||
@@ -1020,9 +1186,16 @@ const pathIsFuzzyEqual = nextPathSplit.every( | ||
) | ||
const hashIsEqual = this.store.state.currentLocation.hash === next.hash | ||
// Combine the matches based on user options | ||
const pathTest = activeOptions?.exact ? pathIsEqual : pathIsFuzzyEqual | ||
const hashTest = activeOptions?.includeHash ? hashIsEqual : true | ||
const pathTest = activeOptions?.exact | ||
? this.state.location.pathname === next.pathname | ||
: pathIsFuzzyEqual | ||
const hashTest = activeOptions?.includeHash | ||
? this.state.location.hash === next.hash | ||
: true | ||
const searchTest = | ||
activeOptions?.includeSearch ?? true | ||
? partialDeepEqual(this.state.location.search, next.search) | ||
: true | ||
// The final "active" test | ||
const isActive = pathTest && hashTest | ||
const isActive = pathTest && hashTest && searchTest | ||
@@ -1039,5 +1212,2 @@ // The click handler | ||
e.preventDefault() | ||
if (pathIsEqual && !search && !hash) { | ||
this.invalidateRoute(nextOpts as any) | ||
} | ||
@@ -1052,6 +1222,3 @@ // All is well? Navigate! | ||
if (preload) { | ||
this.preloadRoute(nextOpts, { | ||
maxAge: userPreloadMaxAge, | ||
gcMaxAge: userPreloadGcMaxAge, | ||
}).catch((err) => { | ||
this.preloadRoute(nextOpts).catch((err) => { | ||
console.warn(err) | ||
@@ -1063,2 +1230,9 @@ console.warn('Error preloading route! ☝️') | ||
const handleTouchStart = (e: TouchEvent) => { | ||
this.preloadRoute(nextOpts).catch((err) => { | ||
console.warn(err) | ||
console.warn('Error preloading route! ☝️') | ||
}) | ||
} | ||
const handleEnter = (e: MouseEvent) => { | ||
@@ -1074,6 +1248,3 @@ const target = (e.target || {}) as LinkCurrentTargetElement | ||
target.preloadTimeout = null | ||
this.preloadRoute(nextOpts, { | ||
maxAge: userPreloadMaxAge, | ||
gcMaxAge: userPreloadGcMaxAge, | ||
}).catch((err) => { | ||
this.preloadRoute(nextOpts).catch((err) => { | ||
console.warn(err) | ||
@@ -1102,2 +1273,3 @@ console.warn('Error preloading route! ☝️') | ||
handleLeave, | ||
handleTouchStart, | ||
isActive, | ||
@@ -1108,154 +1280,182 @@ disabled, | ||
dehydrate = (): DehydratedRouter<TRouterContext> => { | ||
dehydrate = (): DehydratedRouter => { | ||
return { | ||
state: { | ||
...pick(this.store.state, [ | ||
'latestLocation', | ||
'currentLocation', | ||
'status', | ||
'lastUpdated', | ||
]), | ||
currentMatches: this.store.state.currentMatches.map((match) => ({ | ||
id: match.id, | ||
state: { | ||
...pick(match.store.state, [ | ||
'status', | ||
'routeLoaderData', | ||
'invalidAt', | ||
'invalid', | ||
]), | ||
}, | ||
})), | ||
}, | ||
context: this.options.context as TRouterContext, | ||
state: pick(this.state, ['location', 'status', 'lastUpdated']), | ||
} | ||
} | ||
hydrate = (dehydratedRouter: DehydratedRouter<TRouterContext>) => { | ||
this.store.setState((s) => { | ||
// Update the context TODO: make this part of state? | ||
this.options.context = dehydratedRouter.context | ||
hydrate = async (__do_not_use_server_ctx?: HydrationCtx) => { | ||
let _ctx = __do_not_use_server_ctx | ||
// Client hydrates from window | ||
if (typeof document !== 'undefined') { | ||
_ctx = window.__TSR_DEHYDRATED__ | ||
} | ||
// Match the routes | ||
const currentMatches = this.matchRoutes( | ||
dehydratedRouter.state.latestLocation.pathname, | ||
{ | ||
strictParseParams: true, | ||
}, | ||
) | ||
invariant( | ||
_ctx, | ||
'Expected to find a __TSR_DEHYDRATED__ property on window... but we did not. Did you forget to render <DehydrateRouter /> in your app?', | ||
) | ||
currentMatches.forEach((match, index) => { | ||
const dehydratedMatch = dehydratedRouter.state.currentMatches[index] | ||
invariant( | ||
dehydratedMatch && dehydratedMatch.id === match.id, | ||
'Oh no! There was a hydration mismatch when attempting to rethis.store the state of the router! 😬', | ||
) | ||
match.store.setState((s) => { | ||
Object.assign(s, dehydratedMatch.state) | ||
}) | ||
match.setLoaderData(dehydratedMatch.state.routeLoaderData) | ||
}) | ||
const ctx = _ctx | ||
this.dehydratedData = ctx.payload as any | ||
this.options.hydrate?.(ctx.payload as any) | ||
currentMatches.forEach((match) => match.__validate()) | ||
this.__store.setState((s) => { | ||
return { | ||
...s, | ||
...ctx.router.state, | ||
resolvedLocation: ctx.router.state.location, | ||
} | ||
}) | ||
Object.assign(s, { ...dehydratedRouter.state, currentMatches }) | ||
}) | ||
await this.load() | ||
return | ||
} | ||
getLoader = <TFrom extends keyof TAllRouteInfo['routeInfoById'] = '/'>(opts: { | ||
from: TFrom | ||
}): unknown extends TAllRouteInfo['routeInfoById'][TFrom]['routeLoaderData'] | ||
? | ||
| Loader< | ||
LoaderContext< | ||
TAllRouteInfo['routeInfoById'][TFrom]['fullSearchSchema'], | ||
TAllRouteInfo['routeInfoById'][TFrom]['allParams'] | ||
>, | ||
TAllRouteInfo['routeInfoById'][TFrom]['routeLoaderData'] | ||
> | ||
| undefined | ||
: Loader< | ||
TAllRouteInfo['routeInfoById'][TFrom]['fullSearchSchema'], | ||
TAllRouteInfo['routeInfoById'][TFrom]['allParams'], | ||
TAllRouteInfo['routeInfoById'][TFrom]['routeLoaderData'] | ||
> => { | ||
const id = opts.from || ('/' as any) | ||
injectedHtml: (string | (() => Promise<string> | string))[] = [] | ||
const route = this.getRoute(id) | ||
injectHtml = async (html: string | (() => Promise<string> | string)) => { | ||
this.injectedHtml.push(html) | ||
} | ||
if (!route) return undefined as any | ||
dehydrateData = <T>(key: any, getData: T | (() => Promise<T> | T)) => { | ||
if (typeof document === 'undefined') { | ||
const strKey = typeof key === 'string' ? key : JSON.stringify(key) | ||
let loader = | ||
this.store.state.loaders[id] || | ||
(() => { | ||
this.store.setState((s) => { | ||
s.loaders[id] = { | ||
pending: [], | ||
fetch: (async (loaderContext: LoaderContext<any, any>) => { | ||
if (!route) { | ||
return | ||
} | ||
const loaderState: LoaderState<any, any> = { | ||
loadedAt: Date.now(), | ||
loaderContext, | ||
} | ||
this.store.setState((s) => { | ||
s.loaders[id]!.current = loaderState | ||
s.loaders[id]!.latest = loaderState | ||
s.loaders[id]!.pending.push(loaderState) | ||
}) | ||
try { | ||
return await route.options.loader?.(loaderContext) | ||
} finally { | ||
this.store.setState((s) => { | ||
s.loaders[id]!.pending = s.loaders[id]!.pending.filter( | ||
(d) => d !== loaderState, | ||
) | ||
}) | ||
} | ||
}) as any, | ||
} | ||
}) | ||
return this.store.state.loaders[id]! | ||
})() | ||
this.injectHtml(async () => { | ||
const id = `__TSR_DEHYDRATED__${strKey}` | ||
const data = | ||
typeof getData === 'function' ? await (getData as any)() : getData | ||
return `<script id='${id}' suppressHydrationWarning>window["__TSR_DEHYDRATED__${escapeJSON( | ||
strKey, | ||
)}"] = ${JSON.stringify(data)} | ||
;(() => { | ||
var el = document.getElementById('${id}') | ||
el.parentElement.removeChild(el) | ||
})() | ||
</script>` | ||
}) | ||
return loader as any | ||
return () => this.hydrateData<T>(key) | ||
} | ||
return () => undefined | ||
} | ||
#buildRouteTree = (rootRouteConfig: RouteConfig) => { | ||
const recurseRoutes = ( | ||
routeConfigs: RouteConfig[], | ||
parent?: Route<TAllRouteInfo, any, any>, | ||
): Route<TAllRouteInfo, any, any>[] => { | ||
return routeConfigs.map((routeConfig, i) => { | ||
const routeOptions = routeConfig.options | ||
const route = new Route(routeConfig, routeOptions, i, parent, this) | ||
hydrateData = <T = unknown>(key: any) => { | ||
if (typeof document !== 'undefined') { | ||
const strKey = typeof key === 'string' ? key : JSON.stringify(key) | ||
return window[`__TSR_DEHYDRATED__${strKey}` as any] as T | ||
} | ||
return undefined | ||
} | ||
// resolveMatchPromise = (matchId: string, key: string, value: any) => { | ||
// this.state.matches | ||
// .find((d) => d.id === matchId) | ||
// ?.__promisesByKey[key]?.resolve(value) | ||
// } | ||
#buildRouteTree = (routeTree: TRouteTree) => { | ||
this.routeTree = routeTree as any | ||
this.routesById = {} as any | ||
this.routesByPath = {} as any | ||
this.flatRoutes = [] as any | ||
const recurseRoutes = (routes: AnyRoute[]) => { | ||
routes.forEach((route, i) => { | ||
route.init({ originalIndex: i, router: this }) | ||
const existingRoute = (this.routesById as any)[route.id] | ||
if (existingRoute) { | ||
if (process.env.NODE_ENV !== 'production') { | ||
console.warn( | ||
`Duplicate routes found with id: ${String(route.id)}`, | ||
this.routesById, | ||
route, | ||
) | ||
invariant( | ||
!existingRoute, | ||
`Duplicate routes found with id: ${String(route.id)}`, | ||
) | ||
;(this.routesById as any)[route.id] = route | ||
if (!route.isRoot && route.path) { | ||
const trimmedFullPath = trimPathRight(route.fullPath) | ||
if ( | ||
!this.routesByPath[trimmedFullPath] || | ||
route.fullPath.endsWith('/') | ||
) { | ||
;(this.routesByPath as any)[trimmedFullPath] = route | ||
} | ||
throw new Error() | ||
} | ||
;(this.routesById as any)[route.id] = route | ||
const children = route.children as Route[] | ||
const children = routeConfig.children as RouteConfig[] | ||
if (children?.length) { | ||
recurseRoutes(children) | ||
} | ||
}) | ||
} | ||
route.childRoutes = children.length | ||
? recurseRoutes(children, route) | ||
: undefined | ||
recurseRoutes([routeTree]) | ||
return route | ||
this.flatRoutes = (Object.values(this.routesByPath) as AnyRoute[]) | ||
.map((d, i) => { | ||
const trimmed = trimPath(d.fullPath) | ||
const parsed = parsePathname(trimmed) | ||
while (parsed.length > 1 && parsed[0]?.value === '/') { | ||
parsed.shift() | ||
} | ||
const score = parsed.map((d) => { | ||
if (d.type === 'param') { | ||
return 0.5 | ||
} | ||
if (d.type === 'wildcard') { | ||
return 0.25 | ||
} | ||
return 1 | ||
}) | ||
return { child: d, trimmed, parsed, index: i, score } | ||
}) | ||
} | ||
.sort((a, b) => { | ||
let isIndex = a.trimmed === '/' ? 1 : b.trimmed === '/' ? -1 : 0 | ||
const routes = recurseRoutes([rootRouteConfig]) | ||
if (isIndex !== 0) return isIndex | ||
return routes[0]! | ||
const length = Math.min(a.score.length, b.score.length) | ||
// Sort by length of score | ||
if (a.score.length !== b.score.length) { | ||
return b.score.length - a.score.length | ||
} | ||
// Sort by min available score | ||
for (let i = 0; i < length; i++) { | ||
if (a.score[i] !== b.score[i]) { | ||
return b.score[i]! - a.score[i]! | ||
} | ||
} | ||
// Sort by min available parsed value | ||
for (let i = 0; i < length; i++) { | ||
if (a.parsed[i]!.value !== b.parsed[i]!.value) { | ||
return a.parsed[i]!.value! > b.parsed[i]!.value! ? 1 : -1 | ||
} | ||
} | ||
// Sort by length of trimmed full path | ||
if (a.trimmed !== b.trimmed) { | ||
return a.trimmed > b.trimmed ? 1 : -1 | ||
} | ||
// Sort by original index | ||
return a.index - b.index | ||
}) | ||
.map((d, i) => { | ||
d.child.rank = i | ||
return d.child | ||
}) as any | ||
} | ||
@@ -1279,10 +1479,8 @@ | ||
#onFocus = () => { | ||
this.load() | ||
} | ||
#buildLocation = (dest: BuildNextOptions = {}): ParsedLocation => { | ||
dest.fromCurrent = dest.fromCurrent ?? dest.to === '' | ||
#buildLocation = (dest: BuildNextOptions = {}): ParsedLocation => { | ||
const fromPathname = dest.fromCurrent | ||
? this.store.state.latestLocation.pathname | ||
: dest.from ?? this.store.state.latestLocation.pathname | ||
? this.state.location.pathname | ||
: dest.from ?? this.state.location.pathname | ||
@@ -1292,14 +1490,10 @@ let pathname = resolvePath( | ||
fromPathname, | ||
`${dest.to ?? '.'}`, | ||
`${dest.to ?? ''}`, | ||
) | ||
const fromMatches = this.matchRoutes( | ||
this.store.state.latestLocation.pathname, | ||
{ | ||
strictParseParams: true, | ||
}, | ||
this.state.location.pathname, | ||
this.state.location.search, | ||
) | ||
const toMatches = this.matchRoutes(pathname) | ||
const prevParams = { ...last(fromMatches)?.params } | ||
@@ -1313,7 +1507,7 @@ | ||
if (nextParams) { | ||
toMatches | ||
.map((d) => d.route.options.stringifyParams) | ||
dest.__matches | ||
?.map((d) => this.getRoute(d.routeId).options.stringifyParams) | ||
.filter(Boolean) | ||
.forEach((fn) => { | ||
Object.assign({}, nextParams!, fn!(nextParams!)) | ||
nextParams = { ...nextParams!, ...fn!(nextParams!) } | ||
}) | ||
@@ -1324,9 +1518,27 @@ } | ||
const preSearchFilters = | ||
dest.__matches | ||
?.map( | ||
(match) => | ||
this.getRoute(match.routeId).options.preSearchFilters ?? [], | ||
) | ||
.flat() | ||
.filter(Boolean) ?? [] | ||
const postSearchFilters = | ||
dest.__matches | ||
?.map( | ||
(match) => | ||
this.getRoute(match.routeId).options.postSearchFilters ?? [], | ||
) | ||
.flat() | ||
.filter(Boolean) ?? [] | ||
// Pre filters first | ||
const preFilteredSearch = dest.__preSearchFilters?.length | ||
? dest.__preSearchFilters?.reduce( | ||
const preFilteredSearch = preSearchFilters?.length | ||
? preSearchFilters?.reduce( | ||
(prev, next) => next(prev), | ||
this.store.state.latestLocation.search, | ||
this.state.location.search, | ||
) | ||
: this.store.state.latestLocation.search | ||
: this.state.location.search | ||
@@ -1339,3 +1551,3 @@ // Then the link/navigate function | ||
? functionalUpdate(dest.search, preFilteredSearch) ?? {} // Updater | ||
: dest.__preSearchFilters?.length | ||
: preSearchFilters?.length | ||
? preFilteredSearch // Preserve resolvedFrom filters | ||
@@ -1345,8 +1557,8 @@ : {} | ||
// Then post filters | ||
const postFilteredSearch = dest.__postSearchFilters?.length | ||
? dest.__postSearchFilters.reduce((prev, next) => next(prev), destSearch) | ||
const postFilteredSearch = postSearchFilters?.length | ||
? postSearchFilters.reduce((prev, next) => next(prev), destSearch) | ||
: destSearch | ||
const search = replaceEqualDeep( | ||
this.store.state.latestLocation.search, | ||
this.state.location.search, | ||
postFilteredSearch, | ||
@@ -1356,8 +1568,15 @@ ) | ||
const searchStr = this.options.stringifySearch(search) | ||
let hash = | ||
const hash = | ||
dest.hash === true | ||
? this.store.state.latestLocation.hash | ||
: functionalUpdate(dest.hash!, this.store.state.latestLocation.hash) | ||
hash = hash ? `#${hash}` : '' | ||
? this.state.location.hash | ||
: functionalUpdate(dest.hash!, this.state.location.hash) | ||
const hashStr = hash ? `#${hash}` : '' | ||
const nextState = | ||
dest.state === true | ||
? this.state.location.state | ||
: functionalUpdate(dest.state, this.state.location.state)! | ||
return { | ||
@@ -1367,5 +1586,5 @@ pathname, | ||
searchStr, | ||
state: this.store.state.latestLocation.state, | ||
state: nextState, | ||
hash, | ||
href: `${pathname}${searchStr}${hash}`, | ||
href: this.history.createHref(`${pathname}${searchStr}${hashStr}`), | ||
key: dest.key, | ||
@@ -1375,3 +1594,5 @@ } | ||
#commitLocation = (location: BuildNextOptions & { replace?: boolean }) => { | ||
#commitLocation = async ( | ||
location: BuildNextOptions & { replace?: boolean }, | ||
) => { | ||
const next = this.buildNext(location) | ||
@@ -1388,3 +1609,3 @@ const id = '' + Date.now() + Math.random() | ||
const isSameUrl = this.store.state.latestLocation.href === next.href | ||
const isSameUrl = this.state.location.href === next.href | ||
@@ -1404,13 +1625,123 @@ if (isSameUrl && !next.key) { | ||
// this.load(this.#parseLocation(this.store.state.latestLocation)) | ||
return this.latestLoadPromise | ||
} | ||
return (this.navigationPromise = new Promise((resolve) => { | ||
const previousNavigationResolve = this.resolveNavigation | ||
getRouteMatch = ( | ||
id: string, | ||
): undefined | RouteMatch<TRoutesInfo, AnyRoute> => { | ||
return this.state.matchesById[id] | ||
} | ||
this.resolveNavigation = () => { | ||
previousNavigationResolve() | ||
resolve() | ||
} | ||
setRouteMatch = ( | ||
id: string, | ||
updater: ( | ||
prev: RouteMatch<TRoutesInfo, AnyRoute>, | ||
) => RouteMatch<TRoutesInfo, AnyRoute>, | ||
) => { | ||
this.__store.setState((prev) => ({ | ||
...prev, | ||
matchesById: { | ||
...prev.matchesById, | ||
[id]: updater(prev.matchesById[id] as any), | ||
}, | ||
})) | ||
} | ||
setRouteMatchData = ( | ||
id: string, | ||
updater: (prev: any) => any, | ||
opts?: { | ||
updatedAt?: number | ||
maxAge?: number | ||
}, | ||
) => { | ||
const match = this.getRouteMatch(id) | ||
if (!match) return | ||
const route = this.getRoute(match.routeId) | ||
const updatedAt = opts?.updatedAt ?? Date.now() | ||
const preloadInvalidAt = | ||
updatedAt + | ||
(opts?.maxAge ?? | ||
route.options.preloadMaxAge ?? | ||
this.options.defaultPreloadMaxAge ?? | ||
5000) | ||
const invalidAt = | ||
updatedAt + | ||
(opts?.maxAge ?? | ||
route.options.maxAge ?? | ||
this.options.defaultMaxAge ?? | ||
Infinity) | ||
this.setRouteMatch(id, (s) => ({ | ||
...s, | ||
error: undefined, | ||
status: 'success', | ||
isFetching: false, | ||
updatedAt: Date.now(), | ||
loaderData: functionalUpdate(updater, s.loaderData), | ||
preloadInvalidAt, | ||
invalidAt, | ||
})) | ||
if (this.state.matches.find((d) => d.id === id)) { | ||
} | ||
} | ||
invalidate = async (opts?: { | ||
matchId?: string | ||
reload?: boolean | ||
}): Promise<void> => { | ||
if (opts?.matchId) { | ||
this.setRouteMatch(opts.matchId, (s) => ({ | ||
...s, | ||
invalid: true, | ||
})) | ||
const matchIndex = this.state.matches.findIndex( | ||
(d) => d.id === opts.matchId, | ||
) | ||
const childMatch = this.state.matches[matchIndex + 1] | ||
if (childMatch) { | ||
return this.invalidate({ matchId: childMatch.id, reload: false }) | ||
} | ||
} else { | ||
this.__store.batch(() => { | ||
Object.values(this.state.matchesById).forEach((match) => { | ||
this.setRouteMatch(match.id, (s) => ({ | ||
...s, | ||
invalid: true, | ||
})) | ||
}) | ||
}) | ||
} | ||
if (opts?.reload ?? true) { | ||
return this.reload() | ||
} | ||
} | ||
getIsInvalid = (opts?: { matchId: string; preload?: boolean }): boolean => { | ||
if (!opts?.matchId) { | ||
return !!this.state.matches.find((d) => | ||
this.getIsInvalid({ matchId: d.id, preload: opts?.preload }), | ||
) | ||
} | ||
const match = this.getRouteMatch(opts?.matchId) | ||
if (!match) { | ||
return false | ||
} | ||
const now = Date.now() | ||
return ( | ||
match.invalid || | ||
(opts?.preload ? match.preloadInvalidAt : match.invalidAt) < now | ||
) | ||
} | ||
} | ||
@@ -1421,24 +1752,14 @@ | ||
function getInitialRouterState(): RouterStore { | ||
function getInitialRouterState(): RouterState<any, any> { | ||
return { | ||
status: 'idle', | ||
latestLocation: null!, | ||
currentLocation: null!, | ||
currentMatches: [], | ||
loaders: {}, | ||
isFetching: false, | ||
resolvedLocation: null!, | ||
location: null!, | ||
matchesById: {}, | ||
matchIds: [], | ||
pendingMatchIds: [], | ||
matches: [], | ||
pendingMatches: [], | ||
lastUpdated: Date.now(), | ||
matchCache: {}, | ||
get isFetching() { | ||
return ( | ||
this.status === 'loading' || | ||
this.currentMatches.some((d) => d.store.state.isFetching) | ||
) | ||
}, | ||
get isPreloading() { | ||
return Object.values(this.matchCache).some( | ||
(d) => | ||
d.match.store.state.isFetching && | ||
!this.currentMatches.find((dd) => dd.id === d.match.id), | ||
) | ||
}, | ||
} | ||
@@ -1451,10 +1772,46 @@ } | ||
function linkMatches(matches: RouteMatch<any, any>[]) { | ||
matches.forEach((match, index) => { | ||
const parent = matches[index - 1] | ||
export type AnyRedirect = Redirect<any, any, any> | ||
if (parent) { | ||
match.__setParentMatch(parent) | ||
} | ||
}) | ||
export type Redirect< | ||
TRoutesInfo extends AnyRoutesInfo = RegisteredRoutesInfo, | ||
TFrom extends TRoutesInfo['routePaths'] = '/', | ||
TTo extends string = '', | ||
> = NavigateOptions<TRoutesInfo, TFrom, TTo> & { | ||
code?: number | ||
} | ||
export function redirect< | ||
TRoutesInfo extends AnyRoutesInfo = RegisteredRoutesInfo, | ||
TFrom extends TRoutesInfo['routePaths'] = '/', | ||
TTo extends string = '', | ||
>(opts: Redirect<TRoutesInfo, TFrom, TTo>): Redirect<TRoutesInfo, TFrom, TTo> { | ||
;(opts as any).isRedirect = true | ||
return opts | ||
} | ||
export function isRedirect(obj: any): obj is AnyRedirect { | ||
return !!obj?.isRedirect | ||
} | ||
export class SearchParamError extends Error {} | ||
export class PathParamError extends Error {} | ||
function escapeJSON(jsonString: string) { | ||
return jsonString | ||
.replace(/\\/g, '\\\\') // Escape backslashes | ||
.replace(/'/g, "\\'") // Escape single quotes | ||
.replace(/"/g, '\\"') // Escape double quotes | ||
} | ||
// A function that takes an import() argument which is a function and returns a new function that will | ||
// proxy arguments from the caller to the imported function, retaining all type | ||
// information along the way | ||
export function lazyFn< | ||
T extends Record<string, (...args: any[]) => any>, | ||
TKey extends keyof T = 'default', | ||
>(fn: () => Promise<T>, key?: TKey) { | ||
return async (...args: Parameters<T[TKey]>): Promise<ReturnType<T[TKey]>> => { | ||
const imported = await fn() | ||
return imported[key || 'default'](...args) | ||
} | ||
} |
import { decode, encode } from './qss' | ||
import { AnySearchSchema } from './routeConfig' | ||
import { AnySearchSchema } from './route' | ||
@@ -4,0 +4,0 @@ export const defaultParseSearch = parseSearchWith(JSON.parse) |
124
src/utils.ts
@@ -10,3 +10,3 @@ export type NoInfer<T> = [T][T extends any ? 0 : never] | ||
export type PickUnsafe<T, K> = K extends keyof T ? Pick<T, K> : never | ||
export type PickExtra<T, K> = Expand<{ | ||
export type PickExtra<T, K> = { | ||
[TKey in keyof K as string extends TKey | ||
@@ -17,3 +17,4 @@ ? never | ||
: TKey]: K[TKey] | ||
}> | ||
} | ||
export type PickRequired<T> = { | ||
@@ -35,2 +36,18 @@ [K in keyof T as undefined extends T[K] ? never : K]: T[K] | ||
type Compute<T> = { [K in keyof T]: T[K] } | never | ||
type AllKeys<T> = T extends any ? keyof T : never | ||
export type MergeUnion<T, Keys extends keyof T = keyof T> = Compute< | ||
{ | ||
[K in Keys]: T[Keys] | ||
} & { | ||
[K in AllKeys<T>]?: T extends any | ||
? K extends keyof T | ||
? T[K] | ||
: never | ||
: never | ||
} | ||
> | ||
export type Values<O> = O[ValueKeys<O>] | ||
@@ -70,14 +87,2 @@ export type ValueKeys<O> = Extract<keyof O, PropertyKey> | ||
export function warning(cond: any, message: string): cond is true { | ||
if (cond) { | ||
if (typeof console !== 'undefined') console.warn(message) | ||
try { | ||
throw new Error(message) | ||
} catch {} | ||
} | ||
return true | ||
} | ||
function isFunction(d: any): d is Function { | ||
@@ -104,1 +109,92 @@ return typeof d === 'function' | ||
} | ||
/** | ||
* This function returns `a` if `b` is deeply equal. | ||
* If not, it will replace any deeply equal children of `b` with those of `a`. | ||
* This can be used for structural sharing between immutable JSON values for example. | ||
* Do not use this with signals | ||
*/ | ||
export function replaceEqualDeep<T>(prev: any, _next: T): T { | ||
if (prev === _next) { | ||
return prev | ||
} | ||
const next = _next as any | ||
const array = Array.isArray(prev) && Array.isArray(next) | ||
if (array || (isPlainObject(prev) && isPlainObject(next))) { | ||
const prevSize = array ? prev.length : Object.keys(prev).length | ||
const nextItems = array ? next : Object.keys(next) | ||
const nextSize = nextItems.length | ||
const copy: any = array ? [] : {} | ||
let equalItems = 0 | ||
for (let i = 0; i < nextSize; i++) { | ||
const key = array ? i : nextItems[i] | ||
copy[key] = replaceEqualDeep(prev[key], next[key]) | ||
if (copy[key] === prev[key]) { | ||
equalItems++ | ||
} | ||
} | ||
return prevSize === nextSize && equalItems === prevSize ? prev : copy | ||
} | ||
return next | ||
} | ||
// Copied from: https://github.com/jonschlinkert/is-plain-object | ||
export function isPlainObject(o: any) { | ||
if (!hasObjectPrototype(o)) { | ||
return false | ||
} | ||
// If has modified constructor | ||
const ctor = o.constructor | ||
if (typeof ctor === 'undefined') { | ||
return true | ||
} | ||
// If has modified prototype | ||
const prot = ctor.prototype | ||
if (!hasObjectPrototype(prot)) { | ||
return false | ||
} | ||
// If constructor does not have an Object-specific method | ||
if (!prot.hasOwnProperty('isPrototypeOf')) { | ||
return false | ||
} | ||
// Most likely a plain Object | ||
return true | ||
} | ||
function hasObjectPrototype(o: any) { | ||
return Object.prototype.toString.call(o) === '[object Object]' | ||
} | ||
export function partialDeepEqual(a: any, b: any): boolean { | ||
if (a === b) { | ||
return true | ||
} | ||
if (typeof a !== typeof b) { | ||
return false | ||
} | ||
if (isPlainObject(a) && isPlainObject(b)) { | ||
return !Object.keys(b).some((key) => !partialDeepEqual(a[key], b[key])) | ||
} | ||
if (Array.isArray(a) && Array.isArray(b)) { | ||
return ( | ||
a.length === b.length && | ||
a.every((item, index) => partialDeepEqual(item, b[index])) | ||
) | ||
} | ||
return false | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
0
1
0
1148590
7
37
10292
2
+ Addedtiny-warning@^1.0.3
+ Added@gisatcz/cross-package-react-context@0.2.0(transitive)
+ Added@tanstack/react-store@0.0.1-beta.134(transitive)
+ Added@tanstack/store@0.0.1-beta.134(transitive)
+ Addedtiny-warning@1.0.3(transitive)
+ Addeduse-sync-external-store@1.2.2(transitive)
- Removed@solidjs/reactivity@^0.0.7
- Removedimmer@^9.0.15
- Removed@solidjs/reactivity@0.0.7(transitive)
- Removedimmer@9.0.21(transitive)