feat: SSR 请求级作用域(AsyncLocalStorage)+ homeStore 工厂化 + layoutCache 作用域适配
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import type { Board, ForumStats, RecentComment, RecentUser, TagCount } from '../api/types';
|
||||
import { getScope } from '../ssr/scope';
|
||||
|
||||
const BOARDS_KEY = 'j13-cache-boards';
|
||||
const STATS_KEY = 'j13-cache-stats';
|
||||
@@ -6,7 +7,12 @@ const RECENT_COMMENTS_KEY = 'j13-cache-recent-comments';
|
||||
const RECENT_USERS_KEY = 'j13-cache-recent-users';
|
||||
const TAGS_KEY = 'j13-cache-tags';
|
||||
|
||||
/** SSR 请求期写入 RequestScope.kv;客户端写 sessionStorage */
|
||||
function readJson<T>(key: string): T | null {
|
||||
const scope = getScope();
|
||||
if (scope) {
|
||||
return (scope.kv.get(key) as T | undefined) ?? null;
|
||||
}
|
||||
try {
|
||||
const raw = sessionStorage.getItem(key);
|
||||
return raw ? (JSON.parse(raw) as T) : null;
|
||||
@@ -16,6 +22,11 @@ function readJson<T>(key: string): T | null {
|
||||
}
|
||||
|
||||
function writeJson(key: string, value: unknown) {
|
||||
const scope = getScope();
|
||||
if (scope) {
|
||||
scope.kv.set(key, value);
|
||||
return;
|
||||
}
|
||||
try {
|
||||
sessionStorage.setItem(key, JSON.stringify(value));
|
||||
} catch {
|
||||
@@ -53,6 +64,12 @@ export function getCachedTags(): TagCount[] {
|
||||
|
||||
/** 右栏是否已有可展示的 session 缓存(含空列表) */
|
||||
export function hasCachedAside(): boolean {
|
||||
const scope = getScope();
|
||||
if (scope) {
|
||||
return scope.kv.has(RECENT_COMMENTS_KEY)
|
||||
|| scope.kv.has(RECENT_USERS_KEY)
|
||||
|| scope.kv.has(TAGS_KEY);
|
||||
}
|
||||
try {
|
||||
return sessionStorage.getItem(RECENT_COMMENTS_KEY) != null
|
||||
|| sessionStorage.getItem(RECENT_USERS_KEY) != null
|
||||
|
||||
Reference in New Issue
Block a user