Skip to content

Fix Qwik Route Loader – Data Not Loading on Navigation

DodaTech Updated 2026-06-24 3 min read

In this tutorial, you'll learn about Fix Qwik Route Loader. We cover key concepts, practical examples, and best practices.

You define a routeLoader$ in a Qwik route, navigate to that route, and the data is undefined or null. Or you navigate away and back, and the loader executes again instead of using cached data.

Wrong ❌

// routes/products/index.tsx
import { component$ } from '@builder.io/qwik';
import { routeLoader$ } from '@builder.io/qwik-city';

// ❌ Missing export or wrong placement
const useProductList = routeLoader$(async () => {
  const res = await fetch('https://api.example.com/products');
  return res.json();
});

export default component$(() => {
  const products = useProductList();    // returns Signal

  return (
    <div>
      {products.value.length} products  <!-- TypeError: Cannot read length of undefined -->
    </div>
  );
});

The error shows `products.value` is `undefined` on first render. The loader hasn't completed yet.

## Right ✅

```tsx
// routes/products/index.tsx
import { component$ } from '@<a href="/design-patterns/builder/">builder</a>.io/qwik';
import { routeLoader$ } from '@<a href="/design-patterns/builder/">builder</a>.io/qwik-city';

export const useProductList = routeLoader$(async () => {
  const res = await fetch('https://api.example.com/products');
  if (!res.ok) throw new Error('Failed to fetch');
  return res.json() as Product[];
});

export default component$(() => {
  const productsSignal = useProductList();   // Signal<Product[] | undefined>

  return (
    <div>
      {productsSignal.value ? (
        <ul>
          {productsSignal.value.map(p => (
            <li key={p.id}>{p.name}</li>
          ))}
        </ul>
      ) : (
        <div>Loading...</div>
      )}
    </div>
  );
});

**Prevent re-fetching on client navigation:**

```tsx
export const useProductList = routeLoader$(
  async ({ cache }) => {
    cache(60000);   // cache for 60 seconds
    const res = await fetch('https://api.example.com/products');
    return res.json();
  },
  // Or use the second argument:
  { maxAge: 60000 }
);

**For parameterised routes:**

```tsx
// routes/products/[id]/index.tsx
export const useProductDetail = routeLoader$(async ({ params, cache }) => {
  cache(300000);   // 5 min cache
  const res = await fetch(`https://api.example.com/products/${params.id}`);
  return res.json();
});

**Debugging — check if the loader runs:**

```tsx
export const useProductList = routeLoader$(async (requestEvent) => {
  console.log('Loader executed for:', requestEvent.url.pathname);
  // Logs on every navigation where cache misses
  // ...
});

## Root Cause

`routeLoader$` returns a **Signal** — it starts with `undefined` until the data resolves. Client‑side navigation re‑executes the loader unless <a href="/system-design/caching/">caching</a> is configured. If the component assumes `value` is immediately available, it throws or renders empty.

## Prevention

- Always check `signal.value` for `undefined`/`null` before rendering.
- Configure `cache()` or `maxAge` to avoid redundant fetches on client navigation.
- Use `routeAction$` for mutations (POST/PUT/DELETE) — not `routeLoader$`.
- Wrap the loader response in a `try/catch` for proper error handling.


## Common Mistakes with route loader

1. **Non-exhaustive pattern matches that compile with warnings then crash at runtime**
2. **Misunderstanding that `String` is `[Char]` with poor performance for large text operations**
3. **Using `foldl` instead of `foldl'` causing stack overflow on large lists**

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

<details style="margin-bottom:12px;border:1px solid #e2e8f0;border-radius:10px;overflow:hidden"><summary style="cursor:pointer;padding:14px 18px;font-weight:600;font-size:1.05rem;background:#f8fafc;border-bottom:1px solid #e2e8f0;color:#1e293b">**Q: How do I access route params inside a loader?**</summary><div style="padding:14px 18px;color:#475569;line-height:1.7;background:#fff"><p>A: <code>routeLoader$(async ({ params }) =&gt; { params.id })</code>.</p>
</div></details><details style="margin-bottom:12px;border:1px solid #e2e8f0;border-radius:10px;overflow:hidden"><summary style="cursor:pointer;padding:14px 18px;font-weight:600;font-size:1.05rem;background:#f8fafc;border-bottom:1px solid #e2e8f0;color:#1e293b">**Q: Can I call `routeLoader$` from another route?**</summary><div style="padding:14px 18px;color:#475569;line-height:1.7;background:#fff"><p>A: No — loaders are scoped to their route file. Use <code>useResource$</code> for cross‑route data.</p>
</div></details><details style="margin-bottom:12px;border:1px solid #e2e8f0;border-radius:10px;overflow:hidden"><summary style="cursor:pointer;padding:14px 18px;font-weight:600;font-size:1.05rem;background:#f8fafc;border-bottom:1px solid #e2e8f0;color:#1e293b">**Q: What's the difference between `routeLoader$` and `useResource$`?**</summary><div style="padding:14px 18px;color:#475569;line-height:1.7;background:#fff"><p>A: <code>routeLoader$</code> runs on the server before rendering. <code>useResource$</code> runs on the client (or during SSR).</p>
</div></details><details style="margin-bottom:12px;border:1px solid #e2e8f0;border-radius:10px;overflow:hidden"><summary style="cursor:pointer;padding:14px 18px;font-weight:600;font-size:1.05rem;background:#f8fafc;border-bottom:1px solid #e2e8f0;color:#1e293b">**Q: How do I invalidate the cache manually?**</summary><div style="padding:14px 18px;color:#475569;line-height:1.7;background:#fff"><p>A: Reload the page or navigate with a timestamp query param. Programmatic invalidation isn't built‑in.</p>
</div></details>

---

*Route loaders are covered in the [DodaTech Qwik City course](https://dodatech.com/courses/qwik).*

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro