|
| 1 | +--- |
| 2 | +"marko": minor |
| 3 | +--- |
| 4 | + |
| 5 | +Add the `<context>` core tag: a provider/consumer mechanism resolved |
| 6 | +through the render tree (dynamic extent), not lexical authoring structure, |
| 7 | +so a provider works correctly even when consumers are reached through |
| 8 | +passed content. |
| 9 | + |
| 10 | +```marko |
| 11 | +<context=settings> |
| 12 | + <header/> // sees settings |
| 13 | + <main/> // sees settings |
| 14 | +</context> |
| 15 | +``` |
| 16 | + |
| 17 | +```marko |
| 18 | +<context/settings from="<settings-provider>"/> |
| 19 | +``` |
| 20 | + |
| 21 | +A provider whose value is server-only (literals, module/`static` values, |
| 22 | +`$global`-derived expressions) contributes zero bytes to the client |
| 23 | +bundle: no provider import, no registration, no subscription machinery. |
| 24 | + |
| 25 | +Mutable providers (values derived from `let`/`input` state) fan changes |
| 26 | +out to consumers fine-grained: each consumer re-runs individually when |
| 27 | +the provided value changes, resumed pages reattach subscriptions without |
| 28 | +re-rendering, and consumers created client-side (eg inside an `<if>` |
| 29 | +toggled after resume) resolve the provider through its serialized branch |
| 30 | +entry. Subscription wiring is only serialized for consumers that can |
| 31 | +observe the value client-side (an unread consumer costs nothing), and |
| 32 | +same-scope consumers of one provider share a single composed fan-out |
| 33 | +entry. The bind shorthand makes a provider writable: |
| 34 | + |
| 35 | +```marko |
| 36 | +<let/cart=[]/> |
| 37 | +<context:=cart> |
| 38 | + <checkout-panel/> // may assign to its consumed `cart` variable |
| 39 | +</context> |
| 40 | +``` |
| 41 | + |
| 42 | +A writable consumer's assignment invokes the provider's change handler, |
| 43 | +so the write lands in provider state and fans back out to every |
| 44 | +consumer. |
| 45 | + |
| 46 | +A template may consume its own context (`from=` resolving to the file |
| 47 | +itself): the consume resolves the nearest instance in the render tree, |
| 48 | +which enables recursive components and consume-inside-provide patterns. |
| 49 | +A self-consume of a server-only value stays in the zero-bytes tier. |
| 50 | + |
| 51 | +A server-only context consumed by content that first renders in the |
| 52 | +browser (a toggled `<if>`, appended `<for>` items, re-rendered dynamic |
| 53 | +content) still works: compiler reach analysis spots client-re-renderable |
| 54 | +regions that may consume it and lazily serializes the provider box (the |
| 55 | +server-computed value plus a resolution link) only on those pages, so |
| 56 | +every other page serializes nothing. A server-only provider itself must |
| 57 | +originate from the server render. |
| 58 | + |
| 59 | +Also fixes a serializer slot-encoding bug (surfaced by root providers |
| 60 | +with `serializedGlobals`): two partials for the same slot now encode a |
| 61 | +signed delta instead of silently shifting every later scope by one. |
0 commit comments