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:
Eddy G
2025-06-26 17:21:23 -04:00
parent 7f7cb96231
commit 517cafe40a
5 changed files with 96 additions and 25 deletions

View File

@@ -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'