mirror of
https://github.com/netbymatt/ws4kp.git
synced 2026-04-17 00:59:29 -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:
@@ -7,6 +7,7 @@ import { round2 } from './modules/utils/units.mjs';
|
||||
import { parseQueryString } from './modules/share.mjs';
|
||||
import settings from './modules/settings.mjs';
|
||||
import AutoComplete from './modules/autocomplete.mjs';
|
||||
import { loadAllData } from './modules/utils/data-loader.mjs';
|
||||
|
||||
document.addEventListener('DOMContentLoaded', () => {
|
||||
init();
|
||||
@@ -28,7 +29,23 @@ const TXT_ADDRESS_SELECTOR = '#txtAddress';
|
||||
const TOGGLE_FULL_SCREEN_SELECTOR = '#ToggleFullScreen';
|
||||
const BNT_GET_GPS_SELECTOR = '#btnGetGps';
|
||||
|
||||
const init = () => {
|
||||
const init = async () => {
|
||||
// Load core data first - app cannot function without it
|
||||
try {
|
||||
await loadAllData(typeof OVERRIDES !== 'undefined' && OVERRIDES.VERSION ? OVERRIDES.VERSION : '');
|
||||
} catch (error) {
|
||||
console.error('Failed to load core application data:', error);
|
||||
// Show error message to user and halt initialization
|
||||
document.body.innerHTML = `
|
||||
<div>
|
||||
<h2>Unable to load Weather Data</h2>
|
||||
<p>The application cannot start because core data failed to load.</p>
|
||||
<p>Please check your connection and try refreshing.</p>
|
||||
</div>
|
||||
`;
|
||||
return; // Stop initialization
|
||||
}
|
||||
|
||||
document.querySelector(TXT_ADDRESS_SELECTOR).addEventListener('focus', (e) => {
|
||||
e.target.select();
|
||||
});
|
||||
|
||||
53
server/scripts/modules/utils/data-loader.mjs
Normal file
53
server/scripts/modules/utils/data-loader.mjs
Normal file
@@ -0,0 +1,53 @@
|
||||
// Data loader utility for fetching JSON data with cache-busting
|
||||
|
||||
let dataCache = {};
|
||||
|
||||
// Load data with version-based cache busting
|
||||
const loadData = async (dataType, version = '') => {
|
||||
if (dataCache[dataType]) {
|
||||
return dataCache[dataType];
|
||||
}
|
||||
|
||||
try {
|
||||
const url = `/data/${dataType}.json${version ? `?_=${version}` : ''}`;
|
||||
const response = await fetch(url);
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`Failed to load ${dataType}: ${response.status}`);
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
dataCache[dataType] = data;
|
||||
return data;
|
||||
} catch (error) {
|
||||
console.error(`Error loading ${dataType}:`, error);
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
// Load all data types
|
||||
const loadAllData = async (version = '') => {
|
||||
const [travelCities, regionalCities, stationInfo] = await Promise.all([
|
||||
loadData('travelcities', version),
|
||||
loadData('regionalcities', version),
|
||||
loadData('stations', version),
|
||||
]);
|
||||
|
||||
// Set global variables for backward compatibility
|
||||
window.TravelCities = travelCities;
|
||||
window.RegionalCities = regionalCities;
|
||||
window.StationInfo = stationInfo;
|
||||
|
||||
return { travelCities, regionalCities, stationInfo };
|
||||
};
|
||||
|
||||
// Clear cache (useful for development)
|
||||
const clearDataCache = () => {
|
||||
dataCache = {};
|
||||
};
|
||||
|
||||
export {
|
||||
loadData,
|
||||
loadAllData,
|
||||
clearDataCache,
|
||||
};
|
||||
Reference in New Issue
Block a user