Resilient Interfaces & Progressive Enhancement

Rendering Strategies & Data Loading


Learning Objectives

  • You can choose when a route produces its initial HTML and reads its data.
  • You can connect a server load to a page without serializing private fields.
  • You can distinguish initial document content from later browser updates.

Part 2 introduced browser and server rendering. We will now return useful page content before browser JavaScript runs. The first exercise connects an API-backed load to a catalogue page; its application server and API client are supplied.

Choose when the content is produced

Rendering choiceWhen it produces contentA suitable use
PrerenderingAt build time, before a visitor requests the page.Public documentation that can wait for another build to change.
Server renderingFor a request, using data available to the server.A current catalogue item or a permitted private document.
Client renderingAfter JavaScript starts in the browser.Interactive updates after an initial page or summary.

A route can combine server HTML with later browser interaction. Choose according to what the initial document needs, how fresh that information must be and where its code can run. A private page needs request identity before returning private data; a large interactive chart can add browser work after an initial summary.

Rendering two routes

0 / 5 points

A public help page changes only when documentation is published. A private appointment page must read the current session and permissions on each request. Which arrangement fits those stated needs?

The core client’s static adapter exports files. Request-time loads and actions require a running server, which these exercises supply.

Return load data to a page

The load export in +page.server.ts runs on the server. This small example returns a public representation:

src/routes/catalog/+page.server.ts
export const load = () => ({
courses: [{ code: "WEB", title: "Web software" }],
});

The matching page receives that result through its data prop:

<script lang="ts">
let { data }: { data: { courses: { code: string; title: string }[] } } = $props();
</script>
<h1>Course catalog</h1>
<ul>
{#each data.courses as course (course.code)}
<li>{course.code}: {course.title}</li>
{/each}
</ul>

An API-backed load can await a helper before returning. Its request event provides params.id for a route folder named [id] and a request-aware fetch. The exercise’s typed helper accepts that fetch function. Keep the result with this request; a module-level cached record could incorrectly serve another request or hide a later API change.

If the load cannot provide the page, SvelteKit’s error(404, "Listing unavailable") stops it with that HTTP status. An unavailable API should produce a service error such as 503. Catch transport errors around the API call and let SvelteKit’s errors reach the page response. The supplied exercise client handles the transport and status outcomes.

Select data before returning it

A server-only module keeps its code on the server. Its returned page data is serialized for the browser, including fields the component never displays. Select public fields before returning an API record; do not return its password hash, raw session cookie or unrelated private fields.

The checks follow one catalogue item with JavaScript disabled:

  1. The browser opens /catalog/12. The heading is “Introduction”, with “Current catalogue description” and category “Web development”.
  2. The document response contains those values in its initial HTML.
  3. After the API title changes, another request returns a document whose heading is “Changed catalogue title”.

A catalogue page response

0 / 35 points

Edit routes/catalog/[id]/+page.server.ts and routes/catalog/[id]/+page.svelte. Paths are relative to src/. Use Files, or Show file tree in a narrow editor, to open both files. The remaining SvelteKit application and API client are supplied.

Load and page data

createApi(fetch) constructs the public client using this request’s framework fetch. Its asynchronous catalog(id) method reads a catalogue item. positiveId(params.id) converts the route text to a positive numeric ID or throws 404. The client throws 404 for missing items and 503 when the service is unavailable; let those outcomes reach the page response.

Read the item for the current route on every document request. This is a public read, so do not forward visitor credentials. The API record can contain extra private fields. Return only this page-data shape:

type PageData = {
item: {
id: number;
title: string;
description: string;
category: string;
};
};

Render the title as an h1, and the description and category as ordinary text. The document response must contain these values even when JavaScript is disabled. A later request after an API title change must show the changed title.

Item 99 is unpublished and must remain unavailable even when the visitor has a seller cookie.

Choose Submit solution. Initial HTML: 15 points; fresh request data: 10; safe public data and error outcomes: 10.

The page now receives a selected representation from its load. In the next case, inspect which account fields reach the serialized page data.

Page data from an account record

0 / 5 points

A server-only load has assembled this object from account data and the request’s session. The page displays only the display name.

const account = {
id: 17,
displayName: "Rin",
passwordHash: "example-password-hash",
sessionToken: "example-session-token",
};

Which return value supplies the needed page data without exposing the other fields to the browser?

The SvelteKit loading documentation describes server-only and universal loads. The next chapter applies the same loading pattern to a private page whose data depends on the request’s identity.

Check Your Understanding

  1. When does each rendering choice read data and produce HTML?
  2. Why do request-time actions need a running server?
  3. Why can returning an undisplayed secret still leak it?
  4. Which observation distinguishes initial HTML from a later DOM update?