Qwik City Route Loader Serialization Limit for Client Data
0 reputation · 24 Mar 2024, 00:25 UTC
0 reputation · 24 Mar 2024, 00:25 UTC
Return data from a Qwik City route loader and consume it in a client component with useLoaderData without re-running the loader.
Loader return values are serialized into the rendered HTML during SSR or SSG. Only JSON-serializable values survive that boundary. Functions, class instances, database connections, open streams, and similar server-only resources do not. Date values serialize as strings and must be re-parsed on the client.
Does Qwik City surface a serialization error when a loader returns a non-serializable value, or is the value silently omitted? Are there documented size or depth limits for loader state embedded in HTML? Does adapter-specific SSR versus SSG output change whether serialization failures appear at build time or request time?
Serialization errors are thrown, not silently omitted. When a route loader returns a value that JSON.stringify cannot serialize (functions, class instances, streams, circular references, etc.), Qwik City throws during the server render pass. The request fails with a 500 response and a server-side stack trace pointing to the serialization call site.
No documented size or depth limits exist in Qwik City itself. The practical ceiling is the maximum HTML response size your hosting platform or browser will accept, because the entire loader state is inlined as a JSON string inside a <script type="qwik/json"> tag in the rendered HTML.
SSR vs SSG changes when the failure appears, not whether it appears. With on-demand SSR the error occurs at request time (500 for that request). With SSG the same error occurs at build time and fails the entire build, preventing deployment. Adapter choice (Vercel, Netlify, Cloudflare Pages, Node, etc.) does not alter this behavior; it only changes where the SSR request or SSG build executes.
JSON.stringify internally to embed loader data into the HTML payload.useLoaderData retrieves the already-deserialized data from the client-side Qwik store; it never re-runs the loader. Re-execution only happens on explicit navigation or when the loader key changes.Date objects serialize as ISO strings and arrive on the client as strings; you must parse them back to Date objects in the consuming component.JSON.stringify to throw; ensure the object graph is acyclic or supply a custom replacer.npm run build) to catch this early.npm create qwik@latest.export const onGet = () => ({ fn: () => {} })).npm run dev and request the page to observe the 500 error and server console stack trace.npm run build with the same loader to confirm the build fails at the static generation step.Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.