How to async-load locale and inject it into <html lang>? #2166
-
ContextI need the What I've considered1. Middleware
Path-based filtering (skip 2. Cookie (current approach)Reading a For 3.
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
You can resolve this before rendering without putting That gives you a page-render-only prepass: import { createHandler, StartServer } from "@solidjs/start/server";
export default createHandler(
(event) => (
<StartServer
document={({ assets, children, scripts }) => (
<html lang={event.locals.locale ?? "en"}>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
{assets}
</head>
<body>
<div id="app">{children}</div>
{scripts}
</body>
</html>
)}
/>
),
async (event) => {
event.locals.locale = await getUserLocale(event);
return { mode: "stream" };
},
);Declare the local once in your app types: declare global {
namespace App {
interface RequestEventLocals {
locale?: string;
}
}
}This keeps streaming enabled, but the locale lookup completes before the outer HTML shell is created. Within SolidStart's handler, API/server-function responses return before this callback. Static assets are normally served by the adapter/server before the application handler as well. I would not suspend the outer Relevant implementation order:
|
Beta Was this translation helpful? Give feedback.
You can resolve this before rendering without putting
createAsyncinside the document shell.createHandleraccepts an async options callback as its second argument. In the current server implementation, SolidStart handles server functions and API routes first, then awaits that callback, and only afterward callsrenderToStream/renderToString.That gives you a page-render-only prepass: