mirror of
https://github.com/netbymatt/ws4kp.git
synced 2026-04-14 15:49:31 -07:00
Refactor data loading: move from inline JSON to client-side fetch
- Remove large JSON data injection from EJS templates - Add client-side data-loader utility with cache-busting support - Create server endpoints for JSON data with long-term caching - Add graceful failure handling if core data fails to load - Copy JSON data files to dist/data for static hosting - Update app initialization to load data asynchronously - Set serverAvailable flag for static builds in gulp task This reduces HTML payload size and enables better caching strategies for both server and static deployment modes.
This commit is contained in:
20
index.mjs
20
index.mjs
@@ -58,9 +58,6 @@ const renderIndex = (req, res, production = false) => {
|
||||
version,
|
||||
OVERRIDES,
|
||||
query: req.query,
|
||||
travelCities,
|
||||
regionalCities,
|
||||
stationInfo,
|
||||
});
|
||||
};
|
||||
|
||||
@@ -137,6 +134,23 @@ if (!process.env?.STATIC) {
|
||||
app.get('/playlist.json', playlist);
|
||||
}
|
||||
|
||||
// Data endpoints - serve JSON data with long-term caching
|
||||
const dataEndpoints = {
|
||||
travelcities: travelCities,
|
||||
regionalcities: regionalCities,
|
||||
stations: stationInfo,
|
||||
};
|
||||
|
||||
Object.entries(dataEndpoints).forEach(([name, data]) => {
|
||||
app.get(`/data/${name}.json`, (req, res) => {
|
||||
res.set({
|
||||
'Cache-Control': 'public, max-age=31536000, immutable',
|
||||
'Content-Type': 'application/json',
|
||||
});
|
||||
res.json(data);
|
||||
});
|
||||
});
|
||||
|
||||
if (process.env?.DIST === '1') {
|
||||
// Production ("distribution") mode uses pre-baked files in the dist directory
|
||||
// 'npm run build' and then 'DIST=1 npm start'
|
||||
|
||||
Reference in New Issue
Block a user