From: Eduardo San Martin Morote Date: Thu, 14 Aug 2025 13:42:27 +0000 (+0200) Subject: chore: comments and todos X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=4be3c51d0930cb6d912243480b9ac970f267b3df;p=thirdparty%2Fvuejs%2Frouter.git chore: comments and todos --- diff --git a/packages/router/src/experimental/router.ts b/packages/router/src/experimental/router.ts index 396ccdda..870ef703 100644 --- a/packages/router/src/experimental/router.ts +++ b/packages/router/src/experimental/router.ts @@ -206,7 +206,8 @@ export interface EXPERIMENTAL_RouteRecord_Base */ // alias?: string | string[] - // TODO: + // TODO: deprecate, expose utils to compare resolved routes, and document + // how to create a meta field that does the same /** * Before Enter guard specific to this record. Note `beforeEnter` has no * effect if the record has a `redirect` property. @@ -301,7 +302,8 @@ export interface EXPERIMENTAL_RouteRecordNoramlized_Base { */ updateGuards: Set - // FIXME: remove the need for these + // FIXME: remove the need for this, it's only needed if using options API + // for in-options navigation guards (beforeRouteLeave, beforeRouteUpdate's this) instances: Record } @@ -323,9 +325,6 @@ export interface EXPERIMENTAL_RouteRecordNormalized_Matchable // TODO: // redirect?: unknown - // TODO: - // props: Record - components: Record } @@ -567,33 +566,14 @@ export interface EXPERIMENTAL_Router readonly options: EXPERIMENTAL_RouterOptions } -// export interface EXPERIMENTAL_RouteRecordRaw extends NEW_MatcherRecordRaw { -// /** -// * Arbitrary data attached to the record. -// */ -// meta?: RouteMeta -// -// components?: Record -// component?: unknown -// -// redirect?: unknown -// // TODO: Not needed -// score: Array -// } -// -// -// function normalizeRouteRecord( -// record: EXPERIMENTAL_RouteRecordRaw -// ): EXPERIMENTAL_RouteRecordNormalized { -// // FIXME: implementation -// return { -// name: __DEV__ ? Symbol('anonymous route record') : Symbol(), -// meta: {}, -// ...record, -// children: (record.children || []).map(normalizeRouteRecord), -// } -// } - +/** + * Creates an experimental Router that allows passing a resolver instead of a + * routes array. This router does not have `addRoute()` and `removeRoute()` + * methods and is meant to be used with unplugin-vue-router by generating the + * resolver from the `pages/` folder + * + * @param options - Options to initialize the router + */ export function experimental_createRouter( options: EXPERIMENTAL_RouterOptions ): EXPERIMENTAL_Router { @@ -604,13 +584,6 @@ export function experimental_createRouter( history: routerHistory, } = options - // FIXME: can be removed, it was for migration purposes - if (__DEV__ && !routerHistory) - throw new Error( - 'Provide the "history" option when calling "createRouter()":' + - ' https://router.vuejs.org/api/interfaces/RouterOptions.html#history' - ) - const beforeGuards = useCallbacks>() const beforeResolveGuards = useCallbacks>() const afterGuards = useCallbacks() @@ -632,6 +605,7 @@ export function experimental_createRouter( return !!resolver.getRecord(name) } + // TODO: replace usage with resolver.resolve() function locationAsObject( to: RouteLocationRaw | RouteLocationNormalized, currentLocation: string = currentRoute.value.path @@ -645,6 +619,7 @@ export function experimental_createRouter( type TRecord = EXPERIMENTAL_RouteRecordNormalized type _resolveArgs = // TODO: is it worth suppoting the absolute location variants? + // I think that a dev only runtime error is better because then types get a bit complex // | [absoluteLocation: `/${string}`, currentLocation?: undefined] | [ relativeLocation: string, @@ -653,7 +628,7 @@ export function experimental_createRouter( ] // | [ // absoluteLocation: ResolverLocationAsPathAbsolute, - // // Same as above + // // TODO: Same as above // // currentLocation?: NEW_LocationResolved | undefined // currentLocation?: undefined, // ] @@ -748,12 +723,15 @@ export function experimental_createRouter( pushWithRedirect(resolve(...args), true) function handleRedirectRecord(to: RouteLocation): RouteLocationRaw | void { - const lastMatched = to.matched[to.matched.length - 1] - if (lastMatched && lastMatched.redirect) { - const { redirect } = lastMatched + const redirect = to.matched.at(-1)?.redirect + if (redirect) { let newTargetLocation = typeof redirect === 'function' ? redirect(to) : redirect + // TODO: we should be able to just resolve(newTargetLocation) + // maybe we need a way to return the current location: return [redirect, current] + // (to, from) => [redirect, from] // relative to current location + // (to, from) => [redirect, to] // relative to target location if (typeof newTargetLocation === 'string') { newTargetLocation = newTargetLocation.includes('?') || newTargetLocation.includes('#') @@ -765,6 +743,7 @@ export function experimental_createRouter( newTargetLocation.params = {} } + // TODO: should be removed if we use the resolve method if ( __DEV__ && newTargetLocation.path == null && @@ -938,12 +917,7 @@ export function experimental_createRouter( function runWithContext(fn: () => T): T { const app: App | undefined = installedApps.values().next().value - // FIXME: remove safeguard and ensure - // TODO: remove safeguard and bump required minimum version of Vue - // support Vue < 3.3 - return typeof app?.runWithContext === 'function' - ? app.runWithContext(fn) - : fn() + return app?.runWithContext ? app.runWithContext(fn) : fn() } // TODO: refactor the whole before guards by internally using router.beforeEach @@ -1410,7 +1384,7 @@ export function experimental_createRouter( // see above started = true push(routerHistory.location).catch(err => { - if (__DEV__) warn('Unexpected error when starting the router:', err) + if (__DEV__) warn('Unexpected error on initial navigation:', err) }) }