Skip to content

How to pass data from child route to the parent layout? #14240

Answered by david-plugge
vhtmui asked this question in Q&A
Discussion options

You must be logged in to vote

For page metadata i usually go the route of defining a schema inside app.d.ts

interface PageMetadata {
  title: string;
  ...
}

declare global {
  namespace App {
    interface PageData {
      meta: PageMetadata
    }
  }
}

In every +page.{ts,js} file i provide the page metadata:

export const load = () => {
  return {
    meta: {
      title: 'This is the page title!'
    }
  }
}

A component Seo.svelte which is placed inside the root layout takes care of rendering the metadata:

<script lang="ts">
  import { page } from '$app/stores';

  const {
    title,
    ...
  } = $derived($page.data.meta);
</script>

<svelte:head>
  <title>{title}</title>
  ...
<svelte:head>

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by vhtmui
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants