Skip to content

Commit

Permalink
fix: unexpected return type of useRouteMeta
Browse files Browse the repository at this point in the history
  • Loading branch information
PeachScript committed Mar 3, 2024
1 parent f2a0b9e commit 88ea15a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
16 changes: 8 additions & 8 deletions src/client/theme-api/useRouteMeta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import { useCallback, useState } from 'react';
import type { IRouteMeta, IRoutesById } from './types';
import { useIsomorphicLayoutEffect } from './utils';

const cache = new Map<string, IRouteMeta | Promise<IRouteMeta>>();
const cache = new Map<string, IRouteMeta>();
const asyncCache = new Map<string, Promise<IRouteMeta>>();
const EMPTY_META = {
frontmatter: {},
toc: [],
Expand All @@ -19,7 +20,6 @@ const ASYNC_META_PROPS = ['texts'];

function getCachedRouteMeta(route: IRoutesById[string]) {
const cacheKey = route.id;
const pendingCacheKey = `${cacheKey}:pending`;

if (!cache.get(cacheKey)) {
const merge = (meta: IRouteMeta = EMPTY_META) => {
Expand All @@ -34,18 +34,18 @@ function getCachedRouteMeta(route: IRoutesById[string]) {
const meta = merge(getRouteMetaById(route.id, { syncOnly: true }));
const proxyGetter = (target: any, prop: string) => {
if (ASYNC_META_PROPS.includes(prop)) {
if (!cache.get(pendingCacheKey)) {
if (!asyncCache.get(cacheKey)) {
// load async meta then replace cache
cache.set(
pendingCacheKey,
getRouteMetaById(route.id).then((full) =>
cache.set(cacheKey, merge(full)).get(cacheKey),
asyncCache.set(
cacheKey,
getRouteMetaById(route.id)!.then(
(full) => cache.set(cacheKey, merge(full)).get(cacheKey)!,
),
);
}

// throw promise to trigger suspense
throw cache.get(pendingCacheKey);
throw asyncCache.get(cacheKey);
}

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

0 comments on commit 88ea15a

Please sign in to comment.