fix(changelog): improve cache serialization and require app parameter

- Ensure proper JSON serialization when saving cache to localStorage
- Handle string values in sanitizeCachePayload with JSON parsing
- Make app parameter required for all cache operations
- Add debug logging for troubleshooting cache issues
This commit is contained in:
Quorafind 2025-10-12 17:27:16 +08:00
parent 59a2d42f9f
commit 8fca37040a
3 changed files with 50 additions and 17 deletions

View file

@ -50,7 +50,9 @@ export class ChangelogView extends ItemView {
}
const isBeta = manifestVersion.toLowerCase().includes("beta");
const cached = getCachedChangelog(manifestVersion, isBeta);
const cached = getCachedChangelog(manifestVersion, isBeta, this.app);
console.log("[TG]", cached);
if (!cached) {
return;
}

View file

@ -36,11 +36,25 @@ const isChangelogCacheEntry = (
};
const sanitizeCachePayload = (value: unknown): ChangelogCachePayload => {
if (!value || typeof value !== "object") {
if (!value) {
return {};
}
const payload = value as Partial<Record<CacheChannel, unknown>>;
let payloadCandidate: unknown = value;
if (typeof payloadCandidate === "string") {
try {
payloadCandidate = JSON.parse(payloadCandidate);
} catch {
return {};
}
}
if (!payloadCandidate || typeof payloadCandidate !== "object") {
return {};
}
const payload = payloadCandidate as Partial<Record<CacheChannel, unknown>>;
const sanitized: ChangelogCachePayload = {};
if (isChangelogCacheEntry(payload.stable)) {
@ -66,13 +80,21 @@ const getStorage = (): Storage | null => {
}
};
const loadCache = (app?: App): ChangelogCachePayload => {
const loadCache = (app: App): ChangelogCachePayload => {
try {
if (typeof app?.loadLocalStorage === "function") {
return sanitizeCachePayload(app.loadLocalStorage(STORAGE_KEY));
const raw = app.loadLocalStorage(STORAGE_KEY);
if (!raw) {
return {};
}
console.log("[ChangelogCache]", raw);
return sanitizeCachePayload(raw);
} catch (error) {
console.warn("[ChangelogCache] Failed to load via app localStorage", error);
console.warn(
"[ChangelogCache] Failed to load via app localStorage",
error,
);
}
const storage = getStorage();
@ -88,19 +110,25 @@ const loadCache = (app?: App): ChangelogCachePayload => {
return sanitizeCachePayload(JSON.parse(raw));
} catch (error) {
console.warn("[ChangelogCache] Failed to load via window localStorage", error);
console.warn(
"[ChangelogCache] Failed to load via window localStorage",
error,
);
return {};
}
};
const saveCache = (cache: ChangelogCachePayload, app?: App): void => {
const saveCache = (cache: ChangelogCachePayload, app: App): void => {
try {
if (typeof app?.saveLocalStorage === "function") {
app.saveLocalStorage(STORAGE_KEY, cache);
return;
}
const serialized =
!cache.stable && !cache.beta ? "{}" : JSON.stringify(cache);
app.saveLocalStorage(STORAGE_KEY, serialized);
return;
} catch (error) {
console.warn("[ChangelogCache] Failed to save via app localStorage", error);
console.warn(
"[ChangelogCache] Failed to save via app localStorage",
error,
);
}
const storage = getStorage();
@ -116,14 +144,17 @@ const saveCache = (cache: ChangelogCachePayload, app?: App): void => {
storage.setItem(STORAGE_KEY, JSON.stringify(cache));
} catch (error) {
console.warn("[ChangelogCache] Failed to save via window localStorage", error);
console.warn(
"[ChangelogCache] Failed to save via window localStorage",
error,
);
}
};
export const getCachedChangelog = (
version: string,
isBeta: boolean,
app?: App,
app: App,
): ChangelogCacheEntry | null => {
const cache = loadCache(app);
const channel = getChannelKey(isBeta);

File diff suppressed because one or more lines are too long