Skip to content

Commit 4be3c51

Browse files
committed
chore: comments and todos
1 parent 4ea2f81 commit 4be3c51

File tree

1 file changed

+24
-50
lines changed

1 file changed

+24
-50
lines changed

packages/router/src/experimental/router.ts

Lines changed: 24 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,8 @@ export interface EXPERIMENTAL_RouteRecord_Base
206206
*/
207207
// alias?: string | string[]
208208

209-
// TODO:
209+
// TODO: deprecate, expose utils to compare resolved routes, and document
210+
// how to create a meta field that does the same
210211
/**
211212
* Before Enter guard specific to this record. Note `beforeEnter` has no
212213
* effect if the record has a `redirect` property.
@@ -301,7 +302,8 @@ export interface EXPERIMENTAL_RouteRecordNoramlized_Base {
301302
*/
302303
updateGuards: Set<NavigationGuard>
303304

304-
// FIXME: remove the need for these
305+
// FIXME: remove the need for this, it's only needed if using options API
306+
// for in-options navigation guards (beforeRouteLeave, beforeRouteUpdate's this)
305307
instances: Record<string, unknown>
306308
}
307309

@@ -323,9 +325,6 @@ export interface EXPERIMENTAL_RouteRecordNormalized_Matchable
323325
// TODO:
324326
// redirect?: unknown
325327

326-
// TODO:
327-
// props: Record<string, _RouteRecordProps>
328-
329328
components: Record<string, RawRouteComponent>
330329
}
331330

@@ -567,33 +566,14 @@ export interface EXPERIMENTAL_Router
567566
readonly options: EXPERIMENTAL_RouterOptions
568567
}
569568

570-
// export interface EXPERIMENTAL_RouteRecordRaw extends NEW_MatcherRecordRaw {
571-
// /**
572-
// * Arbitrary data attached to the record.
573-
// */
574-
// meta?: RouteMeta
575-
//
576-
// components?: Record<string, unknown>
577-
// component?: unknown
578-
//
579-
// redirect?: unknown
580-
// // TODO: Not needed
581-
// score: Array<number[]>
582-
// }
583-
//
584-
//
585-
// function normalizeRouteRecord(
586-
// record: EXPERIMENTAL_RouteRecordRaw
587-
// ): EXPERIMENTAL_RouteRecordNormalized {
588-
// // FIXME: implementation
589-
// return {
590-
// name: __DEV__ ? Symbol('anonymous route record') : Symbol(),
591-
// meta: {},
592-
// ...record,
593-
// children: (record.children || []).map(normalizeRouteRecord),
594-
// }
595-
// }
596-
569+
/**
570+
* Creates an experimental Router that allows passing a resolver instead of a
571+
* routes array. This router does not have `addRoute()` and `removeRoute()`
572+
* methods and is meant to be used with unplugin-vue-router by generating the
573+
* resolver from the `pages/` folder
574+
*
575+
* @param options - Options to initialize the router
576+
*/
597577
export function experimental_createRouter(
598578
options: EXPERIMENTAL_RouterOptions
599579
): EXPERIMENTAL_Router {
@@ -604,13 +584,6 @@ export function experimental_createRouter(
604584
history: routerHistory,
605585
} = options
606586

607-
// FIXME: can be removed, it was for migration purposes
608-
if (__DEV__ && !routerHistory)
609-
throw new Error(
610-
'Provide the "history" option when calling "createRouter()":' +
611-
' https://router.vuejs.org/api/interfaces/RouterOptions.html#history'
612-
)
613-
614587
const beforeGuards = useCallbacks<NavigationGuardWithThis<undefined>>()
615588
const beforeResolveGuards = useCallbacks<NavigationGuardWithThis<undefined>>()
616589
const afterGuards = useCallbacks<NavigationHookAfter>()
@@ -632,6 +605,7 @@ export function experimental_createRouter(
632605
return !!resolver.getRecord(name)
633606
}
634607

608+
// TODO: replace usage with resolver.resolve()
635609
function locationAsObject(
636610
to: RouteLocationRaw | RouteLocationNormalized,
637611
currentLocation: string = currentRoute.value.path
@@ -645,6 +619,7 @@ export function experimental_createRouter(
645619
type TRecord = EXPERIMENTAL_RouteRecordNormalized
646620
type _resolveArgs =
647621
// TODO: is it worth suppoting the absolute location variants?
622+
// I think that a dev only runtime error is better because then types get a bit complex
648623
// | [absoluteLocation: `/${string}`, currentLocation?: undefined]
649624
| [
650625
relativeLocation: string,
@@ -653,7 +628,7 @@ export function experimental_createRouter(
653628
]
654629
// | [
655630
// absoluteLocation: ResolverLocationAsPathAbsolute,
656-
// // Same as above
631+
// // TODO: Same as above
657632
// // currentLocation?: NEW_LocationResolved<TRecord> | undefined
658633
// currentLocation?: undefined,
659634
// ]
@@ -748,12 +723,15 @@ export function experimental_createRouter(
748723
pushWithRedirect(resolve(...args), true)
749724

750725
function handleRedirectRecord(to: RouteLocation): RouteLocationRaw | void {
751-
const lastMatched = to.matched[to.matched.length - 1]
752-
if (lastMatched && lastMatched.redirect) {
753-
const { redirect } = lastMatched
726+
const redirect = to.matched.at(-1)?.redirect
727+
if (redirect) {
754728
let newTargetLocation =
755729
typeof redirect === 'function' ? redirect(to) : redirect
756730

731+
// TODO: we should be able to just resolve(newTargetLocation)
732+
// maybe we need a way to return the current location: return [redirect, current]
733+
// (to, from) => [redirect, from] // relative to current location
734+
// (to, from) => [redirect, to] // relative to target location
757735
if (typeof newTargetLocation === 'string') {
758736
newTargetLocation =
759737
newTargetLocation.includes('?') || newTargetLocation.includes('#')
@@ -765,6 +743,7 @@ export function experimental_createRouter(
765743
newTargetLocation.params = {}
766744
}
767745

746+
// TODO: should be removed if we use the resolve method
768747
if (
769748
__DEV__ &&
770749
newTargetLocation.path == null &&
@@ -938,12 +917,7 @@ export function experimental_createRouter(
938917

939918
function runWithContext<T>(fn: () => T): T {
940919
const app: App | undefined = installedApps.values().next().value
941-
// FIXME: remove safeguard and ensure
942-
// TODO: remove safeguard and bump required minimum version of Vue
943-
// support Vue < 3.3
944-
return typeof app?.runWithContext === 'function'
945-
? app.runWithContext(fn)
946-
: fn()
920+
return app?.runWithContext ? app.runWithContext(fn) : fn()
947921
}
948922

949923
// TODO: refactor the whole before guards by internally using router.beforeEach
@@ -1410,7 +1384,7 @@ export function experimental_createRouter(
14101384
// see above
14111385
started = true
14121386
push(routerHistory.location).catch(err => {
1413-
if (__DEV__) warn('Unexpected error when starting the router:', err)
1387+
if (__DEV__) warn('Unexpected error on initial navigation:', err)
14141388
})
14151389
}
14161390

0 commit comments

Comments
 (0)