weft
ir 2.6.0 · warp 1.8.0
Part two · Step 5 of 19

Data, and the first cache key

Load something. Watch the cache class change without declaring one.

Goal

Read something per request and watch the cache key derive itself from that read — without writing a key, because there is nowhere to write one.

A slot’s load is where data comes from. Its result is the value set the fragment renders with — there is no separate props step and no context to thread.

sketch — not compiled
// app/fragments/today.tsx
import { fragment } from '@weftjs/core'

interface Item { sku: string; name: string; price: number }

export default fragment(({ items }: { items: Item[] }) => (
  <ul>
    {items.map((item) => (
      <li><code>{item.sku}</code> {item.name} — {item.price}</li>
    ))}
  </ul>
))
sketch — not compiled
// app/routes/index.data.ts
export default defineRoute({
  head: { title: 'Shop' },
  slots: {
    body: { fragment: 'today', load: async () => ({ items: await db.today() }) },
    aside: { html: '<p>Open 9–6.</p>' },
  },
})

2 · Now make it vary

Read the request inside the fragment and the page changes class. One line, and nothing declares the consequence:

sketch — not compiled
export default fragment(({ items }: { items: Item[] }, ctx) => {
  const currency = ctx.cookie('currency') ?? 'USD'
  return <ul data-currency={currency}>…</ul>
})
BeforeAfter
reads nothing → static, resolvable at build timereads cookie:currency → shared, keyed by value, Vary: Cookie

npx weft build now refuses to write this page as a file, and says which read stopped it. That refusal is the whole design in one line: the cheapest tier is not something you opt into, it is something a read takes you out of.