Course outline

Svelte / Basic: reactive components and ownership / Lesson 7

Page head and meaningful metadata

Use Svelte head content to give each public page a clear title and description consistent with its visible content.

About 3 min readContent reviewed
On this page

Lesson goals

  • Add head content at the component top level.
  • Keep metadata aligned with page content.
  • Verify server output and navigation.

Understand the head contract

A document title helps identify a page in browser tabs and other navigation contexts. A description can summarize public content for consumers that use it, though no search presentation is guaranteed. Svelte provides a special head element for such document metadata. Keep the title and description accurate to the actual page rather than copying an unrelated label across routes.

Write a static head

The example puts the special element at component top level and pairs its title with a visible heading. During server rendering Svelte exposes head output separately from body output. The snippet requires a Svelte project and host; it is not a complete HTML document. The description describes this synthetic page and makes no claim about indexing or ranking.

svelte Svelte component fragment
<svelte:head>
  <title>Workshop overview</title>
  <meta name="description" content="Explore the workshop topics and activities." />
</svelte:head>
<main><h1>Workshop overview</h1><p>Choose a topic to begin.</p></main>
Expected output
The document title is Workshop overview.

The head provides document metadata while main contains the visible heading and introduction.

Use page data deliberately

A component can receive public title and description values and use them in both head and body. The parent must provide appropriate strings for the current route; avoid placing secrets or private record details in metadata. Validate and authorize the source at its owner. This example uses Svelte props syntax and requires a compatible compiler and a caller supplying the two values.

svelte Svelte component fragment
<script>
  let { title, description } = $props();
</script>
<svelte:head>
  <title>{title}</title>
  <meta name="description" content={description} />
</svelte:head>
<main><h1>{title}</h1><p>{description}</p></main>
Expected output
The document title and heading reflect the supplied title.

The same caller-provided values supply the title, description and visible page content.

Define ownership and scope

The head element must be at the component top level, not nested inside a block or ordinary element. Decide which page owns its title so a layout and page do not unintentionally compete. When metadata depends on route data, handle a missing record with an appropriate public error page rather than publishing a stale title from a previous route.

Verify direct load and navigation

Request a public page directly and inspect the rendered head as well as the visible heading. Navigate to a second page and check that its title and description match its own content. Test missing data and a private record separately. Browser title changes help orientation, but they do not replace semantic headings, keyboard focus behavior or server authorization.

Common errors and fixes

Head markup fails to compile.

Why it happens
The special element was nested inside another element or block.
How to fix it
Place it at the component top level.
Prevent it next time
Compile a minimal page with the head element.

The browser shows a title from another route.

Why it happens
The current page does not own or update its metadata.
How to fix it
Provide the current public page title.
Prevent it next time
Test navigation between distinct pages.

Private information appears in metadata.

Why it happens
Unreviewed record data was placed in a public document head.
How to fix it
Remove the exposed data and review the affected response.
Prevent it next time
Classify and authorize data before rendering it.

2 practice exercises

1. Where should svelte:head appear in the component?

Show worked solution

At the top level, outside ordinary elements and blocks. It contributes document head content separately from the visible body.

2. Does a correct description guarantee a search result snippet?

Show worked solution

No. It provides an accurate description for consumers, but search engines decide whether and how to use it. Verify visible page content and public metadata independently.

Key takeaways

  • Keep head content at component top level.
  • Match public metadata to the visible page.
  • Verify direct loads, navigation and data exposure.

Official references