Skip to main content
GitHub

Streaming SSR

Streaming SSR eliminates the tradeoff between fast paint and real content. The shell — chrome, navigation, above-the-fold layout — renders and streams immediately. Slower data-dependent segments arrive over the same connection without blocking the initial paint.

How it works

Without streaming, the server waits for all data to resolve before sending any HTML — slow queries block the entire response. Pulse splits the view into a shell (sent immediately) and deferred segments (sent as placeholders, then replaced when data resolves).

Deferred segments arrive as chunks of HTML over the same connection — no extra requests, no client-side JavaScript required to swap content in.

Enabling streaming

To use streaming, the view is an object of named segment functions rather than a single function. The spec declares which segments are in the shell and which are deferred:

export default {
  route: '/dashboard',
  server: {
    user:  async (ctx) => auth.getUser(ctx.cookies.sessionId),   // fast
    feed:  async ()    => db.feed.latest(),                       // slow
    stats: async (ctx) => analytics.summary(ctx.params.id),       // slow
  },
  stream: {
    shell:    ['header', 'nav'],     // rendered immediately
    deferred: ['feed', 'stats'],     // streamed when their data resolves
    scope: {                         // which fetchers each segment waits for
      header: ['user'], nav: ['user'],
      feed:   ['feed'], stats: ['stats'],
    },
  },
  view: {
    header: (state, server) => `
      <header class="site-header">
        <a href="/">Dashboard</a>
        <span>Hello, ${server.user.name}</span>
      </header>
    `,
    nav: () => `
      <nav>
        <a href="/dashboard">Home</a>
        <a href="/settings">Settings</a>
      </nav>
    `,
    feed: (state, server) => `
      <section class="feed">
        ${server.feed.map(item => `<article>${item.title}</article>`).join('')}
      </section>
    `,
    stats: (state, server) => `
      <div class="stats">
        <p>Page views: ${server.stats.views}</p>
        <p>Conversions: ${server.stats.conversions}</p>
      </div>
    `,
  },
}

Deferred placeholders

While deferred segments are loading, Pulse renders a <pulse-deferred id="pd-[name]"> placeholder in their place. When a segment resolves, its HTML is streamed in a <template> tag alongside a small nonce'd inline script that swaps the placeholder for the template's content — no client JS framework involved, just replaceWith().

The swap is done with a tiny inline script — not a separate JS bundle. Deferred streaming works even on pages with no hydration (hydrate omitted).

Server data and streaming

Every server fetcher starts at once, in parallel. What streaming changes is what each part of the page waits for. Declare stream.scope to say which fetchers each segment needs: the shell is written as soon as its own fetchers resolve, a slow fetcher that only a deferred segment uses never holds it up, and each deferred segment streams as soon as its fetchers resolve:

server: {
  user:  async (ctx) => auth.getUser(ctx.cookies.sessionId),   // fast
  feed:  async ()    => db.feed.latest(),                       // slow
},
stream: {
  shell:    ['header'],
  deferred: ['feed'],
  scope: { header: ['user'], feed: ['feed'] },   // the header never waits for the feed
},
A segment that is left out of scope (or all of them, if you omit it) is given every fetcher, so it waits for all of them. Split data across named fetchers, as above, so that scope has something to separate: one fetcher that returns everything cannot be waited for in parts.

When to use streaming

ScenarioUse streaming?
Page with fast server data (< 20ms)No — standard SSR is simpler and fast enough
Page with slow database queriesYes — stream the shell while data loads
Pages with above-the-fold and below-the-fold contentYes — shell renders above the fold immediately
API or raw response endpointsNo — use raw responses

Server-level streaming option

Streaming is enabled by default in createServer. Disable it globally with stream: false:

createServer(specs, {
  stream: false,  // disable streaming for all specs
})

Even with global streaming enabled, only specs that declare a stream field will use chunked responses. All other specs use regular buffered SSR.

Streaming sends one page in parts. To update pages that are already open, see live updates: the server broadcasts shared data to every connected browser.
Streaming is most beneficial on pages with large datasets or slow queries. For most pages, the speed of Pulse's synchronous rendering means streaming adds unnecessary complexity.