Skip to content

Commit 4a6b168

Browse files
committed
perf(api): mirror app settings in localStorage to avoid startup flicker
- Read from localStorage in when available - Mirror writes in for instant availability on next launch - Keeps DB as source of truth; localStorage is a best-effort cache
1 parent a842181 commit 4a6b168

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

src/lib/api.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1740,6 +1740,13 @@ export const api = {
17401740
*/
17411741
async getSetting(key: string): Promise<string | null> {
17421742
try {
1743+
// Fast path: check localStorage mirror to avoid startup flicker
1744+
if (typeof window !== 'undefined' && 'localStorage' in window) {
1745+
const cached = window.localStorage.getItem(`app_setting:${key}`);
1746+
if (cached !== null) {
1747+
return cached;
1748+
}
1749+
}
17431750
// Use storageReadTable to safely query the app_settings table
17441751
const result = await this.storageReadTable('app_settings', 1, 1000);
17451752
const setting = result?.data?.find((row: any) => row.key === key);
@@ -1758,6 +1765,14 @@ export const api = {
17581765
*/
17591766
async saveSetting(key: string, value: string): Promise<void> {
17601767
try {
1768+
// Mirror to localStorage for instant availability on next startup
1769+
if (typeof window !== 'undefined' && 'localStorage' in window) {
1770+
try {
1771+
window.localStorage.setItem(`app_setting:${key}`, value);
1772+
} catch (_ignore) {
1773+
// best-effort; continue to persist in DB
1774+
}
1775+
}
17611776
// Try to update first
17621777
try {
17631778
await this.storageUpdateRow(

0 commit comments

Comments
 (0)