From a7488d695b3f647d657f6b3482d5333e1e869771 Mon Sep 17 00:00:00 2001
From: Mistivia <i@mistivia.com>
Date: Tue, 11 Feb 2025 22:34:11 +0800
Subject: cache card db in localStorage

---
 src/control/loading.js | 27 +++++++++++++++++++++------
 1 file changed, 21 insertions(+), 6 deletions(-)

diff --git a/src/control/loading.js b/src/control/loading.js
index 70dd99c..2d0b1ce 100644
--- a/src/control/loading.js
+++ b/src/control/loading.js
@@ -6,18 +6,33 @@ import { setCardDb, setAltId } from '../data/cardDb';
 let isLoading = writable(true);
 
 async function fetchCardDb() {
+    let localVer = localStorage.getItem('card_db_ver');
     try {
         // load card db
-        let response = await fetch("https://raye.mistivia.com/card_db_parts/index.json");
+        let response = await fetch("https://raye.mistivia.com/card_db_parts/version");
         if (!response.ok) {
             throw new Error('Network response was not ok');
         }
         let data = await response.json();
-        let tasks = data.map((i)=>fetch('https://raye.mistivia.com/card_db_parts/' + i));
-        let datas = await Promise.all(tasks);
-        datas = await Promise.all(datas.map((x) => x.text()));
-        data = JSON.parse(datas.join(''));
-        setCardDb(data);
+        data = String(data)
+        if (localVer === data && localStorage.getItem('card_db') !== null) {
+            setCardDb(JSON.parse(localStorage.getItem('card_db')));
+        } else {
+            localVer = data;
+            response = await fetch("https://raye.mistivia.com/card_db_parts/index.json");
+            if (!response.ok) {
+                throw new Error('Network response was not ok');
+            }
+            data = await response.json();
+            let tasks = data.map((i)=>fetch('https://raye.mistivia.com/card_db_parts/' + i));
+            let datas = await Promise.all(tasks);
+            datas = await Promise.all(datas.map((x) => x.text()));
+            data = JSON.parse(datas.join(''));
+            setCardDb(data);
+            localStorage.setItem('card_db_ver', localVer);
+            localStorage.setItem('card_db', datas.join(''));
+        }
+
         // load alt id
         response = await fetch("https://ygocdb.com/api/v0/idChangelog.jsonp");
         if (!response.ok) {
-- 
cgit v1.0

