0
0
Svelteframework~10 mins

Layout files (+layout.svelte) - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Layout files (+layout.svelte)
Start App
Load +layout.svelte
Render layout HTML
Insert child page content
Display full page
User navigates
Reuse layout, update child content
Display updated page
The app loads the +layout.svelte file first, renders its HTML, then inserts the child page content inside it. On navigation, the layout stays, only child content changes.
Execution Sample
Svelte
<script>
  export let data;
</script>
<header>Site Header</header>
<main>
  <slot />
</main>
<footer>Site Footer</footer>
This layout file wraps every page with a header, main content area, and footer. The <slot /> shows the child page content.
Execution Table
StepActionLayout RenderedChild Content InsertedPage Displayed
1App starts, load +layout.svelteHeader, main, footer HTML createdNoLayout visible, no child content yet
2Child page content loadedLayout remainsChild page HTML inserted in <slot />Full page visible with layout and child
3User navigates to new pageLayout reused, no re-renderNew child page HTML insertedPage updates with new child content
4User refreshes pageLayout re-renderedChild content inserted againFull page visible again
5ExitN/AN/AApp running with layout and child content
💡 App runs continuously; layout stays loaded while child content updates on navigation.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
layoutHTMLundefinedRendered header, main, footerReused same layout HTMLRe-rendered layout HTMLRendered layout HTML
childContentundefinedPage 1 content insertedPage 2 content insertedPage 2 content insertedCurrent page content
Key Moments - 2 Insights
Why does the layout not re-render on every page navigation?
Because the layout file (+layout.svelte) is designed to wrap pages and stays mounted. Only the child content inside <slot /> changes, as shown in execution_table step 3.
What does the <slot /> tag do inside +layout.svelte?
<slot /> is a placeholder where the child page content is inserted. This is why in step 2 and 3 the child content appears inside the layout.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is inserted into the layout at step 2?
AChild page content inside <slot />
BA new layout file
CNothing, layout is empty
DOnly header and footer
💡 Hint
Check the 'Child Content Inserted' column at step 2 in the execution_table.
At which step does the layout get re-rendered after navigation?
AStep 3
BStep 4
CStep 1
DLayout never re-renders
💡 Hint
Look at the 'Layout Rendered' column in the execution_table for step 4.
If the <slot /> tag was removed from +layout.svelte, what would happen?
ALayout would not render at all
BChild content would appear twice
CChild page content would not appear inside the layout
DNothing changes
💡 Hint
Refer to the key_moments about the role of in inserting child content.
Concept Snapshot
Layout files (+layout.svelte) wrap pages with shared UI.
Use <slot /> to insert child page content.
Layout loads once and stays mounted.
Child content changes on navigation inside <slot />.
This improves performance and UI consistency.
Full Transcript
In Svelte, the +layout.svelte file acts as a wrapper for all pages in a folder. When the app starts, it loads the layout first, rendering shared parts like header and footer. The child page content is inserted inside the <slot /> tag in the layout. When the user navigates, the layout stays loaded and only the child content changes inside the slot. This means the layout does not re-render on every navigation, improving speed and keeping UI consistent. Removing the slot would prevent child pages from showing inside the layout. This visual trace shows how the layout and child content load and update step-by-step.