Patch Protocol

TL;DR
  • Navigations update the live page instead of loading a new document
  • Client state, focus, and handlers survive, and the server sends only what changed
  • Enabled per application with the patches option
  • Experimental, coming soon

A server-rendered page usually starts over on every navigation: the browser discards the document (along with every <let>, focused field, and scroll position), then parses and resumes a new one. Persisted pages, built on the patch protocol, keep the live document. When a link or form targets another route in the same application, the server renders that route as a patch: a stream of updates applied directly to the existing page. New structure is created from shells carried by the response, changed values are written in place, and untouched elements keep their client state.

No HTML diffing happens on either side. Because the compiler knows every position where a server value can land, a patch writes values directly to those positions and leaves elements unrelated to server data alone.

Caution

The patch protocol is experimental and not available to try yet. The option, response format, and client runtime may change in any release, including patch releases. See Coming Soon in the August newsletter and the September update for progress so far.

Enabling

Pass patches: true to the Marko Run Vite plugin:

import { defineConfig } from "vite";
import marko from "@marko/run/vite";

export default defineConfig({
  plugins: [marko({ patches: true })],
});

This compiles every template to answer navigations with patches and installs the client router on each page.

Shared Layouts

A layout keeps its client state while the nested page changes. Consider a shop with a cart drawer in the root layout:

src/routes/+layout.marko
<let/cartOpen=false>
<header>
  <a href="/">Shop</a>
  <button onClick() {
    cartOpen = !cartOpen;
  }>
    Cart (${$global.cart.count})
  </button>
</header>
<if=cartOpen>
  <aside class="cart-drawer">
    <for|item| of=$global.cart.items by=(item) => item.id>
      <p>${item.name}</p>
    </for>
  </aside>
</if>
<main>
  <${input.content}/>
</main>
export interface Input {
  content: Marko.Body;
}

<let/cartOpen=false>

<header>
  <a href="/">Shop</a>
  <button onClick() { cartOpen = !cartOpen }>
    Cart (${$global.cart.count})
  </button>
</header>

<if=cartOpen>
  <aside class="cart-drawer">
    <for|item| of=$global.cart.items by=(item) => item.id>
      <p>${item.name}</p>
    </for>
  </aside>
</if>

<main>
  <${input.content}/>
</main>
let/cartOpen=false
header
  a href="/" -- Shop
  button onClick() {
    cartOpen = !cartOpen;
  } -- Cart (${$global.cart.count})
if=cartOpen
  aside class="cart-drawer"
    for|item| of=($global.cart.items) by=(item) => item.id
      p -- ${item.name}
main
  ${input.content}
export interface Input {
  content: Marko.Body;
}

let/cartOpen=false

header
  a href="/" -- Shop
  button onClick() { cartOpen = !cartOpen } -- Cart (${$global.cart.count})

if=cartOpen
  aside class="cart-drawer"
    for|item| of=$global.cart.items by=(item) => item.id
      p -- ${item.name}

main
  ${input.content}
src/routes/products/+page.marko
<h1>Products</h1>
<a href="/about">About us</a>
h1 -- Products
a href="/about" -- About us

With the drawer open, navigating to /about replaces the page inside <main> and updates the cart count from the server response while cartOpen stays true because the layout is patched in place.

Client State

A component rendered in the same position before and after a navigation preserves its <let> values, typed field text, and script-attached DOM state:

<form method="get">
  <input name="q" value=$global.search.q>
  <button>Search</button>
</form>

<let/expanded=false>
<button onClick() { expanded = !expanded }>Filters</button>
<if=expanded>
  <filter-panel/>
</if>
form method="get"
  input name="q" value=$global.search.q
  button -- Search

let/expanded=false
button onClick() { expanded = !expanded } -- Filters
if=expanded
  filter-panel

Submitting the form patches in the new results while keeping the filter panel open and the search input text intact. Because the <input> has no valueChange= handler, user edits stay untouched.

Scripts and Effects

Effects follow standard browser rendering rules: a <script> runs when its content mounts, re-runs when a referenced reactive value changes, and aborts $signal before each rerun and on removal. Content created by a patch runs its effects once the patch applies.

Because patched markup is created on the client, an inline <html-script> inside newly inserted markup is added to the DOM without executing.

Content Security Policy

The client evaluates patch chunks as JavaScript. Pages served with a Content Security Policy require 'unsafe-eval' in script-src for patch navigations; without it, navigations fall back to full document loads.

Supported Pages

Patches apply to Tags API pages rendered through Marko Run route files. Templates rendered outside Marko Run (such as embedded templates) navigate with full document loads.

Page Size

Every page built with patches: true includes the client router, the used subset of the patch runtime, and HTML markers for patchable positions, making the initial document larger than a non-patched build. Subsequent patch responses are smaller than full documents because client-owned state is omitted and previously sent DOM shells are reused.


Contributors

Helpful? You can thank these awesome people! You can also edit this doc if you see any issues or want to improve it.