A plain static `import ... from './config.js'` can't gracefully degrade if that file doesn't exist -- a missing module breaks the whole graph before any application code can catch it. So the four consumers (discovery.js, i18n.js, templates.js, router.js) now import BASE_URL/MARKED_URL/ARTICLES_URL from config-default.js instead, which tries a dynamic import() of the optional config.js (catchable, unlike a static import) and merges in whatever it exports, falling back to its own shipped defaults for anything config.js doesn't override -- or doesn't exist at all. Top-level await makes every importing module wait for that resolution first, same as a plain synchronous export. config.js itself is now just an optional, empty-by-default file: only export the specific constant(s) you want to change. Updated the README's "Local configuration" section (and its three cross-references in Running locally / Embedding in an existing page / Pointing articles/ somewhere else) to drop the now-unnecessary "copy this file before first use" step entirely. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AXSVPS3UmmASiiprPfcnUd
34 lines
1.1 KiB
JavaScript
34 lines
1.1 KiB
JavaScript
// Loads HTML templates from templates/ and caches them as DocumentFragments.
|
|
// Each template needs its own <template> element — reusing one would overwrite
|
|
// the shared DocumentFragment, leaving both pointing to the last HTML assigned.
|
|
|
|
import { BASE_URL } from './config-default.js';
|
|
|
|
export const templates = {};
|
|
|
|
const fetchTemplate = name =>
|
|
fetch(`${BASE_URL}templates/${name}.html`).then(r => {
|
|
if (!r.ok) throw new Error(`${name}.html: HTTP ${r.status}`);
|
|
return r.text();
|
|
});
|
|
|
|
export async function loadTemplates() {
|
|
const [cardHtml, articleHtml, authorProfileHtml] = await Promise.all([
|
|
fetchTemplate('card'),
|
|
fetchTemplate('article'),
|
|
fetchTemplate('author-profile'),
|
|
]);
|
|
|
|
const tCard = document.createElement('template');
|
|
tCard.innerHTML = cardHtml;
|
|
templates.card = tCard.content;
|
|
|
|
const tArticle = document.createElement('template');
|
|
tArticle.innerHTML = articleHtml;
|
|
templates.article = tArticle.content;
|
|
|
|
const tAuthorProfile = document.createElement('template');
|
|
tAuthorProfile.innerHTML = authorProfileHtml;
|
|
templates.authorProfile = tAuthorProfile.content;
|
|
}
|