-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
12 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,18 @@ | ||
import type { LayoutServerLoad } from './$types'; | ||
|
||
// Create a simple cache store | ||
let cache = { | ||
appCats: null, | ||
appsOverview: null, | ||
companyTypes: null, | ||
lastFetched: 0 | ||
}; | ||
|
||
// Cache duration (e.g., 24 hours in milliseconds) | ||
const CACHE_DURATION = 24 * 60 * 60 * 1000; | ||
|
||
export const load: LayoutServerLoad = async ({ fetch }) => { | ||
// Check if cache is valid | ||
if ( | ||
cache.appCats && | ||
cache.appsOverview && | ||
cache.companyTypes && | ||
Date.now() - cache.lastFetched < CACHE_DURATION | ||
) { | ||
return { | ||
appCats: cache.appCats, | ||
appsOverview: cache.appsOverview, | ||
companyTypes: cache.companyTypes | ||
}; | ||
} | ||
|
||
// If cache is invalid or empty, fetch new data | ||
try { | ||
const [appCats, appsOverview, companyTypes] = await Promise.all([ | ||
fetch(`http://localhost:8000/api/categories`).then((res) => res.json()), | ||
fetch(`http://localhost:8000/api/apps/overview`).then((res) => res.json()), | ||
fetch(`http://localhost:8000/api/companies/types`).then((res) => res.json()) | ||
]); | ||
console.log(`root layout load appCats, appsOverview, companyTypes start`); | ||
const [appCats, appsOverview, companyTypes] = await Promise.all([ | ||
fetch(`http://localhost:8000/api/categories`).then((res) => res.json()), | ||
fetch(`http://localhost:8000/api/apps/overview`).then((res) => res.json()), | ||
fetch(`http://localhost:8000/api/companies/types`).then((res) => res.json()) | ||
]); | ||
|
||
// Update cache | ||
cache = { | ||
appCats, | ||
appsOverview, | ||
companyTypes, | ||
lastFetched: Date.now() | ||
}; | ||
console.log(`root layout load appCats, appsOverview, companyTypes end`); | ||
|
||
return { | ||
appCats, | ||
appsOverview, | ||
companyTypes | ||
}; | ||
} catch (error) { | ||
console.error('Error loading data:', error); | ||
return { | ||
appCats: [], | ||
appsOverview: [], | ||
companyTypes: [] | ||
}; | ||
} | ||
return { | ||
appCats: appCats, | ||
appsOverview: appsOverview, | ||
companyTypes: companyTypes | ||
}; | ||
}; |