Examples
Every live fragment on this site, with the source that produced it and what the compiler knows about it. The output beside each source is not a screenshot — it is that fragment, rendered by this page.
15 examples, each a fragment under app/fragments/examples/ in this application. Every one is compiled by the build that served you this page, rendered by the same renderer the rest of the site uses, and shown beside the bytes on disk that produced it.
The smallest fragment
examples/badgeOne hole, one sealed template. This is the unit everything else is made of.
app/fragments/examples/badge.tsximport { fragment } from '@weftjs/core'
/** A fragment with one hole. The smallest thing this framework compiles. */
export default fragment(({ label }: { label: string }) => <span class="pill">{label}</span>)
What the compiler knows
Holes
| Binding | Hole | Escape |
|---|---|---|
label | text | escape |
What the client wires on adoption
| Op | Binds | Where |
|---|---|---|
text | label | text at [0] |
- Sealed templates
- 1
- Version
de3e2a47aa3d5266eda10b0e4057fd4b
Open the file at that path. Nothing else produced the markup beside it.
What a wildcard route renders
examples/crumbsThe file is routes/docs/[...].tsx and the path it matched is data — so a breadcrumb is a list hole, and nothing in it knows how deep the URL was.
app/fragments/examples/crumbs.tsximport { fragment } from '@weftjs/core'
interface Crumb {
href: string
label: string
}
/**
* What a wildcard route renders: the segments it matched, as one row template. The `<ol>` is not
* decoration — a list must be the only child of its element, so the trail gets its own element and
* the current page sits outside it (`E_LIST_NOT_SOLE_CHILD`). See `spec/compiler/supported-subset.md`.
*/
export default fragment(({ trail, here }: { trail: Crumb[]; here: string }) => (
<nav class="crumbs" aria-label="Breadcrumb">
<ol>
{trail.map((crumb) => (
<li>
<a href={crumb.href}>{crumb.label}</a>
</li>
))}
</ol>
<span aria-current="page">{here}</span>
</nav>
))
What the compiler knows
Holes
| Binding | Hole | Escape |
|---|---|---|
trail | list | trusted-raw |
here | text | escape |
What the client wires on adoption
No wiring: nothing on this template changes after it is painted, so the client attaches nothing to it.
- Sealed templates
- 2
- Version
614ac66035a0ffc69ffa3546997aecf7
Escape elision is a type question
examples/totalTwo holes, one annotation apart. Open the facts below: count is proven-safe and name is escape, and nothing in the file said so.
Olive oil — 3 items
app/fragments/examples/total.tsximport { fragment } from '@weftjs/core'
/**
* Escape elision is a type question.
*
* `count` is a number, so no value it can hold needs escaping and the compiler drops the escape
* call entirely — the hole's class is `proven-safe`. `name` is a string, so it keeps `escape`.
* Nothing here declares either; the difference is the annotation two lines down.
*/
export default fragment(({ name, count }: { name: string; count: number }) => (
<p class="total">
<b>{name}</b> — {count} items
</p>
))
What the compiler knows
Holes
| Binding | Hole | Escape |
|---|---|---|
name | text | escape |
count | text | proven-safe |
What the client wires on adoption
No wiring: nothing on this template changes after it is painted, so the client attaches nothing to it.
- Sealed templates
- 1
- Version
7917963794bd383bd071fb38c4601047
This is why the examples here are real files: elision needs a checker, and a snippet nobody type-checked has no types to elide by.
A list is one row template
examples/listThree rows, two sealed templates. Page weight tracks content rather than markup.
OIL-2L | Olive oil, 2L | 14 |
RICE-5 | Basmati rice, 5kg | 9 |
DATE-1 | Medjool dates, 1kg | 12 |
app/fragments/examples/list.tsximport { fragment } from '@weftjs/core'
interface Row {
sku: string
name: string
price: number
}
/**
* A list is one sealed row template, projected once per item.
*
* This is why page weight tracks content rather than markup: a fifty-row table is fifty rows of
* values and two templates, and a delta over it addresses the rows that changed.
*/
export default fragment(({ rows }: { rows: Row[] }) => (
<table class="rows">
<tbody>
{rows.map((row) => (
<tr>
<td>
<code>{row.sku}</code>
</td>
<td>{row.name}</td>
<td class="num">{row.price}</td>
</tr>
))}
</tbody>
</table>
))
What the compiler knows
Holes
| Binding | Hole | Escape |
|---|---|---|
rows | list | trusted-raw |
What the client wires on adoption
No wiring: nothing on this template changes after it is painted, so the client attaches nothing to it.
- Sealed templates
- 2
- Version
c7dd63a67cc17895d6951efb08edd865
A component hole
examples/cardThe card renders the badge. Two sealed templates, and the instance is a projection of the caller’s values rather than a value of its own.
Olive oil, 2L
newapp/fragments/examples/card.tsximport { fragment } from '@weftjs/core'
import Badge from './badge.tsx'
/**
* A fragment rendering another fragment.
*
* `<Badge/>` is not mounted and it is not a function call at runtime: the compiler resolves the tag
* to a sealed template and leaves a component hole that projects this fragment's values into it. So
* one `Badge` used a hundred times is one sealed template and a hundred projections.
*/
export default fragment(({ title, label }: { title: string; label: string }) => (
<article class="card">
<h3>{title}</h3>
<Badge label={label} />
</article>
))
What the compiler knows
Holes
| Binding | Hole | Escape |
|---|---|---|
title | text | escape |
c0 | component | trusted-raw |
What the client wires on adoption
No wiring: nothing on this template changes after it is painted, so the client attaches nothing to it.
- Sealed templates
- 2
- Version
e9199c0717e062dd18da20ba9fa37b51
Which is what keeps a component transparent to a delta: a change to the parent binding is a change to the child’s hole, with no path syntax to invent.
A read becomes a key component
examples/greetingOne ctx.cookie call. The facts below show cookie:currency in the read set, which is where the cache key and Vary come from.
Prices shown in .
app/fragments/examples/greeting.tsximport { fragment, type Ctx } from '@weftjs/core'
/**
* A fragment that reads the request, and therefore keys on it.
*
* `ctx.cookie('currency')` taints `cookie:currency`, so this fragment's cache entry is per currency
* and the response says `Vary: Cookie`. Nothing declares that. Every read here is a call the
* compiler can name statically, which is why a read it cannot name is a build error rather than a
* cache key with a hole in it.
*/
export default fragment(async (ctx: Ctx) => {
const currency = ctx.cookie('currency') ?? 'USD'
return <p class="greeting">Prices shown in {currency}.</p>
})
What the compiler knows
Holes
| Binding | Hole | Escape |
|---|---|---|
currency | text | escape |
What the client wires on adoption
No wiring: nothing on this template changes after it is painted, so the client attaches nothing to it.
- Sealed templates
- 1
- Version
45122ec2139bdfe756602f978ec0fcea
A read the compiler cannot name statically is E_UNTRACKED_EFFECT — a build error rather than a key with a hole in it.
Identity contains itself
examples/privateOne ctx.user() read makes this region private. Because it is a slot rather than part of the document, the document stays shared.
Signed in as
app/fragments/examples/private.tsximport { fragment, type Ctx } from '@weftjs/core'
/**
* One identity read, and the region can never be a shared cache entry.
*
* Because it is a *slot* rather than part of the document, the document stays shared and only this
* region is per-user. That containment is the whole reason a page with a signed-in corner is not a
* page that has to be private.
*/
export default fragment(async (ctx: Ctx) => {
const user = await ctx.user()
return <p class="private">Signed in as {user}</p>
})
What the compiler knows
Holes
| Binding | Hole | Escape |
|---|---|---|
user | text | escape |
What the client wires on adoption
No wiring: nothing on this template changes after it is painted, so the client attaches nothing to it.
- Sealed templates
- 1
- Version
6eec7f5c8738807d4f75fa53191d73b1
A region that cannot be part of the shell
examples/feedReading the clock taints time, which forces a TTL. The rows are one template projected per item, so what arrives late is values rather than markup.
- Olive oil, 2L14
- Basmati rice, 5kg9
- Medjool dates, 1kg12
app/fragments/examples/feed.tsximport { fragment, type Ctx } from '@weftjs/core'
interface Item {
id: number
title: string
price: number
}
/**
* The streaming example. Reading the clock taints `time` and forces a TTL. See
* `spec/kernel/cache.md`. `count` is a prop rather than `items.length`, since a hole binds a value
* and `.length` is an expression the compiler has no lowering for (`E_EXPRESSION_UNSUPPORTED`).
*/
export default fragment(({ count, items }: { count: number; items: Item[] }, ctx: Ctx) => {
const generated = ctx.now()
return (
<section class="feed">
<p class="meta">
<span class="count">{count}</span> items · generated at <span class="at">{generated}</span>
</p>
<ol class="items">
{items.map((item) => (
<li data-id={item.id}>
<span class="title">{item.title}</span>
<span class="price">{item.price}</span>
</li>
))}
</ol>
</section>
)
})
What the compiler knows
Holes
| Binding | Hole | Escape |
|---|---|---|
count | text | proven-safe |
generated | text | proven-safe |
items | list | trusted-raw |
What the client wires on adoption
No wiring: nothing on this template changes after it is painted, so the client attaches nothing to it.
- Sealed templates
- 2
- Version
1f665f431601c5cb7cfc718484232c50
The facts below list time as the read. That single entry is why a policy with no ttl on this route is a build error.
What a region sends when it is out of budget
examples/placeholderNo holes at all: this template is constant, so rendering it is a buffer copy. A fallback that had to query something could fail the way the region it stands in for did.
This part of the page is not here yet. The rest of it is unaffected.
app/fragments/examples/placeholder.tsximport { fragment } from '@weftjs/core'
/**
* The bytes a region sends when it degrades: honest, cheap, and visibly incomplete.
*
* It has no holes at all, so it is constant — one sealed template whose version never changes and
* whose render is a buffer copy. That is what makes it usable as a placeholder in a budget: a
* fallback that had to query something could fail the same way the region it stands in for did.
*/
export default fragment(() => (
<div class="degraded" data-degraded>
<p>This part of the page is not here yet. The rest of it is unaffected.</p>
</div>
))
What the compiler knows
Holes
No holes: every byte of this template is constant.
What the client wires on adoption
No wiring: nothing on this template changes after it is painted, so the client attaches nothing to it.
- Sealed templates
- 1
- Version
9102e55e49cd988e420ba7d201d8451b
Declared as a slot’s placeholder. The page is still a page, and the part that is missing says so.
A signal and a derived value
examples/signalsThe derived expression travels as a tree the client evaluates. There is no closure on the wire and no component to hydrate.
app/fragments/examples/signals.tsximport { fragment, signal } from '@weftjs/core'
import { quantity } from '../../intents/quantity.ts'
/**
* A signal, and a value derived from it. `qty() * unitPrice` travels as a tree the client
* evaluates, no closure or hydration needed. See `spec/ir/template-ir-2.md`.
*
* `value={qty()}` needs a property write, not an attribute write, once somebody has typed. See
* `spec/compiler/supported-subset.md`: Controls. `onInput` names an intent — the only inbound
* wiring op — or typing into this would do nothing. See `spec/ir/template-ir-2.md`: Wiring table.
*/
export default fragment(({ unitPrice }: { unitPrice: number }) => {
const qty = signal(1)
return (
<form class="signals">
<label>
quantity
<input type="number" name="qty" min="0" value={qty()} onInput={quantity} />
</label>
<p class="line">
<span class="unit">{unitPrice}</span> × <output class="qty">{qty()}</output> ={' '}
<output class="total">{qty() * unitPrice}</output>
</p>
<p class="over" data-over={qty() > 9}>
over nine
</p>
</form>
)
})
What the compiler knows
Holes
| Binding | Hole | Escape |
|---|---|---|
qty | attr | proven-safe |
unitPrice | text | proven-safe |
qty | text | proven-safe |
d0 | text | proven-safe |
d1 | attr | proven-safe |
What the client wires on adoption
| Op | Binds | Where |
|---|---|---|
prop | qty | value at [0, 0, 0] |
event | intent 858420 | input at [0, 0, 0] |
text | qty | text at [0, 1, 1] |
text | d0 | text at [0, 1, 2] |
attr | d1 | data-over at [0, 2] |
| Signal | Type | Initial |
|---|---|---|
qty | number | 1 |
- Sealed templates
- 1
- Version
334da034466c5f38bbb4b26c5b0e20d5- Derived
d0 ← qty, unitPrice,d1 ← qty
The facts below list a wiring entry per node that reads the signal. That is the cost model: bindings, not components.
Ordinary anchors
examples/linksNothing here opts into staging. A link is a link, and what the client does behind it is a deployment’s decision rather than an author’s.
app/fragments/examples/links.tsximport { fragment } from '@weftjs/core'
interface Link {
href: string
label: string
/** Whether this is the page you are on. Per item, because a row receives only its own item. */
here: boolean
}
/**
* Ordinary anchors — nothing opts a link into instant navigation. See `spec/client/navigation.md`.
* `here` is a field on the item, not a `current` prop compared inside the row: a row receives only
* its own item, and reading an outer value is `E_OUT_OF_ROW_SCOPE`. See `spec/compiler/supported-subset.md`.
*/
export default fragment(({ links }: { links: Link[] }) => (
<nav class="links">
{links.map((link) => (
<a href={link.href} data-current={link.here}>
{link.label}
</a>
))}
</nav>
))
What the compiler knows
Holes
| Binding | Hole | Escape |
|---|---|---|
links | list | trusted-raw |
What the client wires on adoption
No wiring: nothing on this template changes after it is painted, so the client attaches nothing to it.
- Sealed templates
- 2
- Version
e2dcca59235428eb573ac44a2f978541
A page that links nowhere never carries the staging model. That is why this is an anchor rather than a component.
A mutation with no JavaScript at all
examples/helpfulA form, a hidden field and a POST to the route the intent manifest generated. This one is live: pressing it dispatches a real intent in this process.
app/fragments/examples/helpful.tsximport { fragment } from '@weftjs/core'
/**
* A mutation with no JavaScript on the page at all.
*
* The form posts to `/_weft/i/docs.helpful`, which is the route the intent manifest generated from
* `app/intents/feedback.ts`. Phase A dispatch means a real status code, a cookie the server may set
* `HttpOnly`, and a 303 back here — the three things a `fetch` handler cannot give you and the
* reason the no-JavaScript path is the *first* path rather than the fallback.
*
* The same intent, unchanged, is what a client would dispatch over the channel. There is no second
* endpoint and no second handler.
*/
export default fragment(({ page, count }: { page: string; count: number }) => (
<form class="vote" method="post" action="/_weft/i/docs.helpful">
<input type="hidden" name="page" value={page} />
<button type="submit">Useful</button>
<span class="count">{count}</span>
</form>
))
What the compiler knows
Holes
| Binding | Hole | Escape |
|---|---|---|
page | attr | escape |
count | text | proven-safe |
What the client wires on adoption
No wiring: nothing on this template changes after it is painted, so the client attaches nothing to it.
- Sealed templates
- 1
- Version
951dcf634875ff1de1098eb7eb26b16b
Phase A dispatch is what makes a real status, an HttpOnly cookie and a 303 available — the three things a fetch handler cannot give you. The count is real, it is this process’s, and it moves when you press the button — which took three declarations agreeing. The intent names docs.votes in writes, so ctx.revalidate may fire it. The body is a live region tagged with the same name, so the write drops it. And the route carries that tag too: a document held for an hour under no tag is a second cache the invalidation cannot reach, and while it was missing every layer here worked perfectly and the number stayed at zero. W_DOCUMENT_OUTLIVES_INVALIDATION exists so the next person is told instead of guessing. The cost is named too: this page is L0_LIVE rather than a file, which is the trade a page about writing has a reason to make and the other twenty-one guide pages do not.
One template, three wire forms
examples/pricesThe same fragment, and one price changed. The table under it is the three encodings measured on this render rather than quoted from a benchmark.
Your basket
| Olive oil, 2L | 14 |
| Basmati rice, 5kg | 9 |
| Medjool dates, 1kg | 12 |
Total
app/fragments/examples/prices.tsximport { fragment } from '@weftjs/core'
interface Line {
sku: string
name: string
price: number
}
/**
* The fragment the wire-form comparison on `/guide/live-regions` is computed from.
*
* Nothing about it is special — a heading, a total and one row template — and that is the point.
* `html`, `patch` and `delta` are three encodings of the *same* sealed template, so the byte counts
* beside it are three ways of saying one price changed, not three implementations.
*/
export default fragment(({ heading, total, lines }: { heading: string; total: number; lines: Line[] }) => (
<section class="prices">
<h3>{heading}</h3>
<table class="rows">
<tbody>
{lines.map((line) => (
<tr data-sku={line.sku}>
<td>{line.name}</td>
<td class="num">{line.price}</td>
</tr>
))}
</tbody>
</table>
<p class="total">
Total <output>{total}</output>
</p>
</section>
))
What the compiler knows
Holes
| Binding | Hole | Escape |
|---|---|---|
heading | text | escape |
lines | list | trusted-raw |
total | text | proven-safe |
What the client wires on adoption
No wiring: nothing on this template changes after it is painted, so the client attaches nothing to it.
- Sealed templates
- 2
- Version
3fdd7374ceb2f6e5a794ff4329425df2
A region, and nothing in it about being remote
examples/remoteThere is nothing to add. What crosses the boundary is a warp frame — the protocol every render already produces — so the composite does not learn a second runtime.
- Olive oil, 2L
- Olive oil, 5L
app/fragments/examples/remote.tsximport { fragment } from '@weftjs/core'
interface Hit {
sku: string
name: string
}
/**
* A region: a fragment that happens to render on another deployment.
*
* There is nothing in this file about being remote, and there is nothing to add. What crosses the
* boundary is a warp frame — the same protocol every render already produces — so the composite
* embedding it does not learn a second runtime, and a failure over there degrades this one hole
* rather than the page.
*/
export default fragment(({ query, hits }: { query: string; hits: Hit[] }) => (
<section class="region" data-region="search">
<p class="meta">
Results for <strong>{query}</strong>, from the search deployment
</p>
<ul class="hits">
{hits.map((hit) => (
<li data-sku={hit.sku}>{hit.name}</li>
))}
</ul>
</section>
))
What the compiler knows
Holes
| Binding | Hole | Escape |
|---|---|---|
query | text | escape |
hits | list | trusted-raw |
What the client wires on adoption
No wiring: nothing on this template changes after it is painted, so the client attaches nothing to it.
- Sealed templates
- 2
- Version
848311be085c69637f7df64cf0323779
A read with three values is an axis
examples/deviceLow cardinality is what lets a read become an ahead-of-time permutation rather than a branch taken per request.
This deployment thinks you are on a device, so the plan reachable from here is the one built for that tier.
app/fragments/examples/device.tsximport { fragment, type Ctx } from '@weftjs/core'
/**
* A read with three possible values, which is what makes it an ahead-of-time permutation.
*
* `ctx.device()` taints `device`, and the plan can carry a branch per value rather than a branch
* taken per request: low cardinality is the property that lets a read become an axis. The fragment
* does not know which one it will be rendered for, and does not need to.
*/
export default fragment(async (ctx: Ctx) => {
const tier = ctx.device()
return (
<p class="device">
This deployment thinks you are on a <strong>{tier}</strong> device, so the plan reachable from here is
the one built for that tier.
</p>
)
})
What the compiler knows
Holes
| Binding | Hole | Escape |
|---|---|---|
tier | text | escape |
What the client wires on adoption
No wiring: nothing on this template changes after it is painted, so the client attaches nothing to it.
- Sealed templates
- 1
- Version
63c7968954e0ae5445e34cdda1d5153e
The facts below show device in the read set. The plan can carry a branch per value; the fragment does not know which one it is rendered for.