Skip to content

Commit

Permalink
fix: fallback useRouteMeta logic and handle errors caused by custom r…
Browse files Browse the repository at this point in the history
…outeMeta (#2137)

* fix: fallback useRouteMeta logic and handle errors caused by custom routeMeta

* fix: throw cache

* fix: 防止函数无用执行

* fix: 调整if

* fix: throw 两个分支

* fix: 提取变量
  • Loading branch information
Jinbao1001 authored Jun 5, 2024
1 parent 24901be commit b2a6ed3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 23 deletions.
26 changes: 16 additions & 10 deletions src/client/theme-api/useRouteMeta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,32 @@ function getCachedRouteMeta(route: IRoutesById[string]) {
Object.keys(route.meta).forEach((key) => {
(meta as any)[key] ??= (route.meta as any)[key];
});
meta.frontmatter = deepmerge(meta.frontmatter, route.meta.frontmatter, { arrayMerge: (_destinationArray, sourceArray) => sourceArray });
meta.frontmatter = deepmerge(meta.frontmatter, route.meta.frontmatter, {
arrayMerge: (_destinationArray, sourceArray) => sourceArray,
});
}
return meta;
};
const meta = merge(getRouteMetaById(route.id, { syncOnly: true }));
const proxyGetter = (target: any, prop: string) => {
if (ASYNC_META_PROPS.includes(prop)) {
if (!asyncCache.get(cacheKey)) {
const routeMetaPromise = getRouteMetaById(route.id);
// load async meta then replace cache
asyncCache.set(
cacheKey,
getRouteMetaById(route.id)!.then(
(full) => cache.set(cacheKey, merge(full)).get(cacheKey)!,
),
);
if (routeMetaPromise) {
asyncCache.set(
cacheKey,
routeMetaPromise.then(
(full) => cache.set(cacheKey, merge(full)).get(cacheKey)!,
),
);
}
}

// throw promise to trigger suspense
throw asyncCache.get(cacheKey);
const currentCache = asyncCache.get(cacheKey);
if (currentCache) {
throw currentCache;
}
}

return target[prop];
Expand All @@ -61,7 +68,6 @@ function getCachedRouteMeta(route: IRoutesById[string]) {
const ret = new Proxy(meta, {
get: proxyGetter,
});

cache.set(cacheKey, ret);
}

Expand Down
21 changes: 8 additions & 13 deletions src/templates/meta/exports.ts.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -138,17 +138,16 @@ export function getRouteMetaById<T extends { syncOnly?: boolean }>(
id: string,
opts?: T,
): T extends { syncOnly: true }
? IRouteMeta
: Promise<IRouteMeta> {
const routeMeta: IRouteMeta = {
frontmatter: {},
toc: [],
texts: [],
};
? IRouteMeta | undefined
: Promise<IRouteMeta> | undefined {
if (filesMeta[id]) {
const { frontmatter, toc, textGetter, tabs } = filesMeta[id];
routeMeta.frontmatter = frontmatter;
routeMeta.toc = toc;
const routeMeta: IRouteMeta = {
frontmatter,
toc,
texts: [],
};

if (opts?.syncOnly) {
if (tabs) {
Expand Down Expand Up @@ -176,10 +175,6 @@ export function getRouteMetaById<T extends { syncOnly?: boolean }>(
});
}
}
if (opts?.syncOnly) {
return routeMeta;
}
return Promise.resolve(routeMeta);
}

/**
Expand Down

0 comments on commit b2a6ed3

Please sign in to comment.