Fix Qwik Optimizer Error – Serialization or Resumability Failure
In this tutorial, you'll learn about Fix Qwik Optimizer Error. We cover key concepts, practical examples, and best practices.
You write a Qwik component with $() and useSignal(). The dev server shows "Error: [ERROR] Could not serialize the value" — or the production build fails with "Qwik Optimizer: Cannot find symbol".
Wrong ❌
// ❌ Non-serializable value in a useSignal
export default component$(() => {
const fetch = useSignal(() => fetchData()); // function is not serializable
const date = useSignal(new Date()); // Date is serializable, but...
const map = useSignal(new Map()); // Map is not serializable
return <div>{date.value.toISOString()}</div>;
});
Qwik throws:
Error: [Qwik Optimizer] Value at 'fetch' is not serializable.
Only plain objects, arrays, and primitives are supported.
Right ✅
// ✅ Serializable values only
import { component$, useSignal, useVisibleTask$, $ } from '@builder.io/qwik';
export default component$(() => {
const count = useSignal(0);
const data = useSignal<null | string[]>(null);
const timestamp = useSignal<string | null>(null);
// Use useVisibleTask$ for side effects
useVisibleTask$(async () => {
timestamp.value = new Date().toISOString(); // store serializable string
const res = await fetch('/api/data');
data.value = await res.json(); // plain JSON
});
return (
<div>
<p>Count: {count.value}</p>
<p>Timestamp: {timestamp.value}</p>
<button onClick$={() => count.value++}>+</button>
</div>
);
});
Fix optimizer "Cannot find symbol" errors:
// ❌ Inline function not hoisted
export default component$(() => {
return <button onClick$={() => console.log('click')}>Click</button>;
});
// ✅ Inline $ is fine — Qwik handles it
export default component$(() => {
return <button onClick$={() => console.log('click')}>Click</button>;
});
// ❌ But this fails — dynamic import inside $
import { delay } from '~/utils/delay';
export default component$(() => {
return <button onClick$={async () => {
await delay(1000); // ← optimizer can't resolve this
}}>Click</button>;
});
// ✅ Use $() to wrap the import
import { delay } from '~/utils/delay';
export default component$(() => {
const handleClick = $(async () => {
await delay(1000);
});
return <button onClick$={handleClick}>Click</button>;
});
Debug optimizer output:
# See what the optimizer produces
npx qwik optimize --verbose
# Check dist/ for the serialized bundles
Root Cause
Qwik serializes component state into HTML for resumability. Only JSON‑serializable values (primitives, plain objects, arrays) can be stored in signals. Functions, Maps, Sets, Dates (non‑serialized), Promises, and class instances break serialization.
Prevention
- Store only primitives, plain objects, or arrays in
useSignal(). - Use
useVisibleTask$()for side effects — notuseSignal(). - Use
$()to wrap closures that Qwik must hoist for lazy loading. - Never pass non‑serializable props across Qwik components.
Common Mistakes with optimizer error
- Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors
- Non-exhaustive pattern matches that compile with warnings then crash at runtime
- Misunderstanding that
Stringis[Char]with poor performance for large text operations
These mistakes appear frequently in real-world QWIK code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Qwik serialization is covered in the DodaTech Qwik Framework course.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro