This commit is contained in:
parent
fb2f6079cc
commit
934c94cb5c
50 changed files with 10464 additions and 758 deletions
|
|
@ -1,24 +1,59 @@
|
|||
// Head-element setup for catcrafts.net. The Crafter.Graphics Dom partition
|
||||
// only exposes element creation under <body>, so anything that has to live in
|
||||
// <head> (title, stylesheet link, favicon link) gets injected from this
|
||||
// loader. EnableWasiBrowserRuntime auto-picks up every *.js in cfg.files and
|
||||
// emits a <script type="module"> tag for it ahead of runtime.js, so this
|
||||
// runs before the wasm module starts and styles are in place by first paint.
|
||||
// Head-element setup for catcrafts.net.
|
||||
//
|
||||
// The Crafter.Graphics Dom partition only exposes element creation under <body>,
|
||||
// so anything that has to live in <head> — title, stylesheet, favicon, viewport
|
||||
// — is injected from this loader. EnableWasiBrowserRuntime emits a
|
||||
// <script type="module"> for every *.js in cfg.files ahead of runtime.js, so
|
||||
// this runs before the wasm module starts and styles are in place by first
|
||||
// paint.
|
||||
//
|
||||
// EVERYTHING HERE IS CONDITIONAL, and that matters. There are two ways a
|
||||
// document reaches the browser:
|
||||
//
|
||||
// 1. Server-rendered by catcrafts-server, which already emitted a correct
|
||||
// per-route <title> plus the stylesheet, favicon and viewport. It marks
|
||||
// that with <meta name="cc-ssr">.
|
||||
// 2. The static fallback shell — Caddy serves the wasm bundle's index.html
|
||||
// when the backend is down. That document has a bare <head> and an empty
|
||||
// <body>, so the app has to supply all of it.
|
||||
//
|
||||
// Running unconditionally broke case 1: it replaced the route's real title with
|
||||
// the generic site name, and appended a second stylesheet, favicon and viewport
|
||||
// tag. So each addition checks whether the server already did the job.
|
||||
|
||||
document.title = "Catcrafts.net";
|
||||
const ssr = document.querySelector('meta[name="cc-ssr"]') !== null;
|
||||
|
||||
const styles = document.createElement("link");
|
||||
styles.rel = "stylesheet";
|
||||
styles.href = "styles.css";
|
||||
document.head.appendChild(styles);
|
||||
function ensure(selector, build) {
|
||||
if (document.querySelector(selector)) return;
|
||||
document.head.appendChild(build());
|
||||
}
|
||||
|
||||
const favicon = document.createElement("link");
|
||||
favicon.rel = "icon";
|
||||
favicon.type = "image/svg+xml";
|
||||
favicon.href = "favicon.svg";
|
||||
document.head.appendChild(favicon);
|
||||
// Only claim the title when the server did not set one. On an SSR'd page the
|
||||
// existing title is route-specific and strictly better than anything this file
|
||||
// knows. "catcrafts.wasm" is what Crafter.Build's index.html template hardcodes,
|
||||
// so it counts as unset.
|
||||
if (!ssr && (!document.title || document.title === "catcrafts.wasm")) {
|
||||
document.title = "Catcrafts";
|
||||
}
|
||||
|
||||
const viewport = document.createElement("meta");
|
||||
viewport.name = "viewport";
|
||||
viewport.content = "width=device-width, initial-scale=1.0";
|
||||
document.head.appendChild(viewport);
|
||||
ensure('link[rel="stylesheet"]', () => {
|
||||
const el = document.createElement("link");
|
||||
el.rel = "stylesheet";
|
||||
el.href = "/styles.css";
|
||||
return el;
|
||||
});
|
||||
|
||||
ensure('link[rel="icon"]', () => {
|
||||
const el = document.createElement("link");
|
||||
el.rel = "icon";
|
||||
el.type = "image/svg+xml";
|
||||
el.href = "/favicon.svg";
|
||||
return el;
|
||||
});
|
||||
|
||||
ensure('meta[name="viewport"]', () => {
|
||||
const el = document.createElement("meta");
|
||||
el.name = "viewport";
|
||||
el.content = "width=device-width, initial-scale=1";
|
||||
return el;
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in a new issue