unplugin-vue-router
Advanced tools
Comparing version 0.1.1 to 0.1.2
@@ -6,3 +6,3 @@ import * as unplugin from 'unplugin'; | ||
import { Ref, AllowedComponentProps, ComponentCustomProps, VNodeProps, UnwrapRef, VNode, ComputedRef } from 'vue'; | ||
export { a as DataLoader, D as DefineLoaderOptions } from './defineLoader-b5f64d0e.js'; | ||
export { a as DataLoader, D as DefineLoaderOptions } from './defineLoader-68ee3023.js'; | ||
@@ -9,0 +9,0 @@ declare function createRoutesContext(options: ResolvedOptions): { |
@@ -1,3 +0,3 @@ | ||
import { a as DataLoader } from './defineLoader-b5f64d0e.js'; | ||
export { a as DataLoader, D as DefineLoaderOptions, d as _defineLoader, s as _stopScope } from './defineLoader-b5f64d0e.js'; | ||
import { a as DataLoader } from './defineLoader-68ee3023.js'; | ||
export { a as DataLoader, D as DefineLoaderOptions, d as _defineLoader, s as _stopScope } from './defineLoader-68ee3023.js'; | ||
import { Router } from 'vue-router'; | ||
@@ -16,4 +16,4 @@ import 'vue'; | ||
} | ||
declare function setupDataFetchingGuard(router: Router): (() => void) | undefined; | ||
declare function setupDataFetchingGuard(router: Router, initialState?: Record<string, unknown>): Record<string, unknown> | null | undefined; | ||
export { LoaderSymbol as _LoaderSymbol, setupDataFetchingGuard as _setupDataFetchingGuard }; |
@@ -41,3 +41,3 @@ "use strict"; | ||
} | ||
function createDataCacheEntry(options) { | ||
function createDataCacheEntry(options, initialState) { | ||
return withinScope(() => ({ | ||
@@ -48,3 +48,3 @@ pending: (0, import_vue.ref)(false), | ||
loaders: /* @__PURE__ */ new Set(), | ||
data: (0, import_vue.ref)(), | ||
data: (0, import_vue.ref)(initialState), | ||
params: {}, | ||
@@ -84,3 +84,4 @@ query: {}, | ||
cacheTime: 1e3 * 5, | ||
lazy: false | ||
lazy: false, | ||
key: "" | ||
}; | ||
@@ -129,9 +130,14 @@ function defineLoader(nameOrLoader, _loaderOrOptions, opts) { | ||
const pendingLoad = () => pendingPromise; | ||
function load(route, router, parent) { | ||
function load(route, router, parent, initialState) { | ||
const hasCacheEntry = cache.has(router); | ||
const needsNewLoad = !hasCacheEntry || shouldFetchAgain(cache.get(router), route); | ||
const initialData = initialState && initialState[options.key]; | ||
if (!hasCacheEntry) { | ||
cache.set(router, createDataCacheEntry(options)); | ||
cache.set(router, createDataCacheEntry(options, initialData)); | ||
} | ||
const entry = cache.get(router); | ||
if (initialData) { | ||
entry.when = 0; | ||
return Promise.resolve(); | ||
} | ||
const needsNewLoad = !hasCacheEntry || shouldFetchAgain(entry, route); | ||
const { isReady, pending, error } = entry; | ||
@@ -171,3 +177,4 @@ const { lazy } = options; | ||
cache, | ||
load | ||
load, | ||
options | ||
}; | ||
@@ -237,5 +244,5 @@ dataLoader[IsLoader] = true; | ||
var LoaderSymbol = Symbol(); | ||
var added = false; | ||
function setupDataFetchingGuard(router) { | ||
if (added) { | ||
var ADDED_SYMBOL = Symbol(); | ||
function setupDataFetchingGuard(router, initialState) { | ||
if (ADDED_SYMBOL in router) { | ||
console.warn( | ||
@@ -246,4 +253,5 @@ "[vue-router]: Data fetching guard added twice. Make sure to remove the extra call." | ||
} | ||
added = true; | ||
return router.beforeEach((to) => { | ||
router[ADDED_SYMBOL] = true; | ||
const fetchedState = {}; | ||
router.beforeEach((to) => { | ||
return Promise.all( | ||
@@ -255,3 +263,18 @@ to.matched.flatMap((route) => route.meta[LoaderSymbol]).filter((moduleImport) => moduleImport).map( | ||
loaders.map((loader) => { | ||
return loader._.load(to, router); | ||
const { | ||
options: { key }, | ||
cache | ||
} = loader._; | ||
return loader._.load( | ||
to, | ||
router, | ||
void 0, | ||
initialState | ||
).then(() => { | ||
if (!initialState) { | ||
if (key) { | ||
fetchedState[key] = cache.get(router).data.value; | ||
} | ||
} | ||
}); | ||
}) | ||
@@ -261,4 +284,7 @@ ); | ||
) | ||
).then(() => true); | ||
).then(() => { | ||
initialState = void 0; | ||
}); | ||
}); | ||
return initialState ? null : fetchedState; | ||
} | ||
@@ -265,0 +291,0 @@ // Annotate the CommonJS export names for ESM import in node: |
{ | ||
"name": "unplugin-vue-router", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"packageManager": "pnpm@7.9.0", | ||
@@ -5,0 +5,0 @@ "description": "File based typed routing for Vue Router", |
Sorry, the diff of this file is not supported yet
252416
6921