From f6f43bfbef94736d1661cc8ffbcb4dc16fe10828 Mon Sep 17 00:00:00 2001 From: Kevin Wu Date: Thu, 4 Jan 2024 20:35:21 -0800 Subject: [PATCH 1/5] feat: disable PWA in dev --- next.config.js | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/next.config.js b/next.config.js index ba3e418..87a5d0f 100644 --- a/next.config.js +++ b/next.config.js @@ -1,13 +1,14 @@ /** @type {import('next').NextConfig} */ const runtimeCaching = require("next-pwa/cache"); -const withPWA = require('next-pwa')({ - dest: 'public', +const withPWA = require("next-pwa")({ + dest: "public", register: true, skipWaiting: true, - runtimeCaching -}) + runtimeCaching, + disable: process.env.NODE_ENV === "DEVELOPMENT", +}); module.exports = withPWA({ - reactStrictMode: false -}) + reactStrictMode: false, +}); From 527c7968099807fc6de7043d3257afbc6133263e Mon Sep 17 00:00:00 2001 From: Kevin Wu Date: Thu, 4 Jan 2024 20:36:38 -0800 Subject: [PATCH 2/5] feat: add caching --- components/search/Search.tsx | 8 ++++---- components/search/query-db.ts | 19 +++++++++++++++++-- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/components/search/Search.tsx b/components/search/Search.tsx index dbf4ed9..37a1e1a 100644 --- a/components/search/Search.tsx +++ b/components/search/Search.tsx @@ -164,9 +164,9 @@ const Search = () => { try { const universityParam = university; const geParam = !ge.includes("GE") ? ge : ge.split(" ")[1]; - const data = await queryDatabase(universityParam, geParam); + const courses = await queryDatabase(universityParam, geParam); - setCourses(data.courses); + setCourses(courses); setLoading(false); setError(false); @@ -287,7 +287,7 @@ const Search = () => { searchGE={ge} />
-
+
Search Filters
@@ -317,7 +317,7 @@ const Search = () => {
-
+
Sort By:
= {}; + +export async function queryDatabase( + university: string, + ge: string, +): Promise { + console.log(cache); + if (cache[university + ge]) { + return cache[university + ge]; + } + const universityParam = encodeURIComponent(university); const geParam = encodeURIComponent(ge); @@ -11,7 +23,10 @@ export async function queryDatabase(university: string, ge: string) { } const data = await response.json(); - return data; + + cache[university + ge] = data.courses; + + return data.courses; } catch (error) { console.error("Error:", error); throw error; From 1ddccfd3ecb57a929b1dcef8539b86fe687c0ca6 Mon Sep 17 00:00:00 2001 From: Kevin Wu Date: Thu, 4 Jan 2024 20:38:19 -0800 Subject: [PATCH 3/5] --amend --- components/search/query-db.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/components/search/query-db.ts b/components/search/query-db.ts index 58de2bc..89a6858 100644 --- a/components/search/query-db.ts +++ b/components/search/query-db.ts @@ -6,7 +6,6 @@ export async function queryDatabase( university: string, ge: string, ): Promise { - console.log(cache); if (cache[university + ge]) { return cache[university + ge]; } From 3970ce50cbca6bad8c70d093504784908ad41548 Mon Sep 17 00:00:00 2001 From: Kevin Wu Date: Thu, 4 Jan 2024 20:44:32 -0800 Subject: [PATCH 4/5] feat: make sure cache <= 30 minutes --- components/search/query-db.ts | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/components/search/query-db.ts b/components/search/query-db.ts index 89a6858..80b799a 100644 --- a/components/search/query-db.ts +++ b/components/search/query-db.ts @@ -1,13 +1,20 @@ import { CollegeObject } from "./Search"; -const cache: Record = {}; +const cache: Record = {}; export async function queryDatabase( university: string, ge: string, ): Promise { - if (cache[university + ge]) { - return cache[university + ge]; + const cacheKey = university + ge; + + if (cache[cacheKey] && cache[cacheKey][0]) { + const [cachedDate, cachedData] = cache[cacheKey]; + + // If not older than 30 minutes, return cached courses + if ((new Date().getTime() - cachedDate.getTime()) / (1000 * 60) <= 30) { + return cachedData; + } } const universityParam = encodeURIComponent(university); @@ -23,7 +30,7 @@ export async function queryDatabase( const data = await response.json(); - cache[university + ge] = data.courses; + cache[university + ge] = [new Date(), data.courses]; return data.courses; } catch (error) { From e7021d96d5d6a29c93162a96cd765acd56887940 Mon Sep 17 00:00:00 2001 From: Kevin Wu Date: Thu, 4 Jan 2024 20:49:35 -0800 Subject: [PATCH 5/5] refactor: use cacheKey --- components/search/query-db.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/search/query-db.ts b/components/search/query-db.ts index 80b799a..b5db4da 100644 --- a/components/search/query-db.ts +++ b/components/search/query-db.ts @@ -30,7 +30,7 @@ export async function queryDatabase( const data = await response.json(); - cache[university + ge] = [new Date(), data.courses]; + cache[cacheKey] = [new Date(), data.courses]; return data.courses; } catch (error) {