feat: add subdomain-based site context with SSR resolution

Resolve the active site from the request Host header on the server and
expose it through a SiteContext provider so the app can vary rendering
per subdomain. Serialize the resolved site into the document shell
(data-site attribute + window.__SITE__) so client hydration matches the
server render. Add host-based Vercel rewrites mapping each subdomain to
its site root, and include unit tests for the site resolution logic.
This commit is contained in:
2026-07-23 09:18:55 -04:00
parent 0385090f32
commit 92890ce259
7 changed files with 552 additions and 35 deletions

View File

@@ -1,19 +1,29 @@
// @refresh reload
import { createHandler, StartServer } from "@solidjs/start/server";
import { getSiteFromEvent } from "~/server/site-context-server";
import { setServerSite } from "~/context/SiteContext";
export default createHandler(() => (
<StartServer
document={({ assets, children, scripts }) => (
<html lang="en">
<head>
<meta charset="utf-8" />
<meta
name="viewport"
content="width=device-width, initial-scale=1, maximum-scale=1"
/>
<link rel="icon" href="/favicon.ico" />
<script>
{`
export default createHandler((event) => {
// Resolve the active site from the request Host header once per SSR pass,
// then bind it so the SiteContext provider returns the right value during
// the initial server render. Also serialized into the document shell
// (`<html data-site>` + `window.__SITE__`) so client hydration matches.
const site = getSiteFromEvent(event);
setServerSite(site);
return (
<StartServer
document={({ assets, children, scripts }) => (
<html lang="en" data-site={site.id}>
<head>
<meta charset="utf-8" />
<meta
name="viewport"
content="width=device-width, initial-scale=1, maximum-scale=1"
/>
<link rel="icon" href={site.faviconPath} />
<script>
{`
(function() {
const STORAGE_KEY = 'theme-override';
const stored = localStorage.getItem(STORAGE_KEY);
@@ -22,14 +32,17 @@ export default createHandler(() => (
document.documentElement.classList.add(isDark ? 'dark' : 'light');
})();
`}
</script>
{assets}
</head>
<body>
<div id="app">{children}</div>
{scripts}
</body>
</html>
)}
/>
));
</script>
{/* Hydration data for SiteContext — must run before the app bundle. */}
<script>{`window.__SITE__=${JSON.stringify(site.id)};`}</script>
{assets}
</head>
<body>
<div id="app">{children}</div>
{scripts}
</body>
</html>
)}
/>
);
});