Caching and Props

The view helpers support fine-grained caching, backed by Redis. It is off by default and enabled with use_caching: true, see Configuration. The cache-key rules below only apply when caching is on.

The only difference between these two view helpers is which props are part of the cache key and available during server-side rendering:

  • svelte: all props, except hydrate: props.
  • svelte_overlay: no props, except ssr: props.

Props that are not part of the cache key are only available in the browser, after hydration.

Everything visible on first page load, before any user interaction, should be server-rendered.

To check, reload the page in the browser: if nothing flickers, SSR works.

For components whose initial state differs per request (e.g. per user), consider turning caching off (options: { cached: false }) but keep SSR on, see View Helpers.

svelte

<%= svelte(
      "ProductCard",
      product: @product.to_svelte,
      hydrate: { request_id: request.uuid }
    ) %>
<script>
  let { product, request_id } = $props();
  // request_id is undefined during SSR, available after hydration
</script>
  • The cache key depends on product, but not on request_id
  • ssr: is not allowed on svelte

svelte_overlay

For dropdowns, menus, modals and similar patterns, where only a small part (e.g. the button) is server-rendered and cached. First-level props are browser-only; server-rendered props must be given via ssr:.

<%= svelte_overlay(
      "UserMenu",
      user: current_user.to_svelte,
      ssr: { button_label: "User-Menu" }
    ) %>
<script>
  let { button_label, user } = $props();
  // user is undefined during SSR, available after hydration
</script>
  • The cache key depends on button_label, but not on user
  • hydrate: is not allowed on svelte_overlay