Fix issues and add linting
This commit is contained in:
@@ -3,37 +3,63 @@ import path from 'node:path';
|
||||
import { parseSiteBundle, type SiteBundle } from './site-bundle.ts';
|
||||
|
||||
const REPO_ROOT = process.env.REPO_ROOT || '.';
|
||||
const TTL = parseInt(process.env.SITE_DATA_TTL_MS || '2000', 10);
|
||||
const TTL = parseInt(process.env.SITE_DATA_TTL_MS || '500', 10);
|
||||
|
||||
let cached: { data: SiteBundle; loadedAt: number } | null = null;
|
||||
|
||||
/** Fallback bundle used when content files are missing or unreadable. */
|
||||
function fallbackBundle(): SiteBundle {
|
||||
return {
|
||||
siteContext: {
|
||||
businessName: 'Site',
|
||||
tone: 'professional and friendly',
|
||||
},
|
||||
sections: [],
|
||||
events: { events: [] },
|
||||
};
|
||||
}
|
||||
|
||||
export function loadSiteData(): SiteBundle {
|
||||
const now = Date.now();
|
||||
if (cached && now - cached.loadedAt < TTL) {
|
||||
return cached.data;
|
||||
}
|
||||
|
||||
const siteContextRaw = JSON.parse(
|
||||
fs.readFileSync(path.join(REPO_ROOT, 'site-context.json'), 'utf-8')
|
||||
);
|
||||
try {
|
||||
const siteContextRaw = JSON.parse(
|
||||
fs.readFileSync(path.join(REPO_ROOT, 'site-context.json'), 'utf-8')
|
||||
);
|
||||
|
||||
const eventsRaw = JSON.parse(
|
||||
fs.readFileSync(path.join(REPO_ROOT, 'content/events.json'), 'utf-8')
|
||||
);
|
||||
|
||||
const sectionsDir = path.join(REPO_ROOT, 'content/sections');
|
||||
const sectionRaws: unknown[] = [];
|
||||
if (fs.existsSync(sectionsDir)) {
|
||||
for (const file of fs.readdirSync(sectionsDir).filter(f => f.endsWith('.json'))) {
|
||||
let eventsRaw: unknown = { events: [] };
|
||||
const eventsPath = path.join(REPO_ROOT, 'content/events.json');
|
||||
if (fs.existsSync(eventsPath)) {
|
||||
try {
|
||||
sectionRaws.push(JSON.parse(fs.readFileSync(path.join(sectionsDir, file), 'utf-8')));
|
||||
eventsRaw = JSON.parse(fs.readFileSync(eventsPath, 'utf-8'));
|
||||
} catch {
|
||||
// skip invalid files
|
||||
// Use empty events if file is corrupt
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const data = parseSiteBundle(siteContextRaw, eventsRaw, sectionRaws);
|
||||
cached = { data, loadedAt: now };
|
||||
return data;
|
||||
const sectionsDir = path.join(REPO_ROOT, 'content/sections');
|
||||
const sectionRaws: unknown[] = [];
|
||||
if (fs.existsSync(sectionsDir)) {
|
||||
for (const file of fs.readdirSync(sectionsDir).filter(f => f.endsWith('.json'))) {
|
||||
try {
|
||||
sectionRaws.push(JSON.parse(fs.readFileSync(path.join(sectionsDir, file), 'utf-8')));
|
||||
} catch {
|
||||
// skip invalid files
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const data = parseSiteBundle(siteContextRaw, eventsRaw, sectionRaws);
|
||||
cached = { data, loadedAt: now };
|
||||
return data;
|
||||
} catch {
|
||||
// If site-context.json is missing or corrupt, return a minimal fallback
|
||||
// so the SSR server doesn't crash on every request.
|
||||
const fb = fallbackBundle();
|
||||
cached = { data: fb, loadedAt: now };
|
||||
return fb;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user