question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Persistent Layouts don't render with Inertia/Svelte

See original GitHub issue

Versions:

  • @inertiajs/inertia version: 0.10.1
  • @inertiajs/inertia-svelte version: 0.7.4

Describe the problem:

Following the example from the documentation at https://inertiajs.com/pages I set up my Svelte component to use a persistent layout.

<style>
  h1 { font-size: var(--size-12); }
</style>

<h1>{greeting}</h1>
<p>I'm an example home page.</p>

<script>
  import { inertia, Link } from '@inertiajs/inertia-svelte';
  import DefaultLayout from '@/layouts/default.svelte';

  export const layout = DefaultLayout;
  export let greeting;
</script>

The default layout looks like this:

<nav>Insert Navigation here</nav>
<main>
  <slot></slot>
</main>

The layout does not render.

However if I change this to a “non-persistent layout” it works exactly as expected.

<DefaultLayout>
  <h1>{greeting}</h1>
  <p>I'm an example home page.</p>
</DefaultLayout>

<script>
  import { inertia, Link } from '@inertiajs/inertia-svelte';
  import DefaultLayout from '@/layouts/default.svelte';

  // export const layout = DefaultLayout;
  export let greeting;
</script>

I’m not getting any errors, but I do see the following warnings in the console: Screen Shot 2021-09-12 at 11 34 42 AM

Specifically the message <Home> received an unexpected slot "default". seems related, but note that this warning appears whether with both versions (persistent and non-persistent), and googling it turns up this issue which indicates this may not be a real error.

Also since the code works exactly correctly using the “non-persistent” version, I’m inclined to think that something in the inertia plumbing isn’t hooked up right.

For reference, I also tried doing this by setting it up as a default layout in the main function:

import '@/styles/application.css'
import { createInertiaApp } from '@inertiajs/inertia-svelte'
import DefaultLayout from '@/layouts/default.svelte';

createInertiaApp({
  resolve: name => {
    const page = import(`./pages/${name}.svelte`);
    page.layout = page.layout || DefaultLayout;
    return page;
  },
  setup({ el, App, props }) {
    new App({ target: el, props })
  },
})

This had no effect, the result was exactly the same as using export layout from the home page component.

Steps to reproduce:

Described in detail above.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5

github_iconTop GitHub Comments

5reactions
burlesonacommented, Sep 13, 2021

Hi @pedroborges, thanks for the help, that fixed it. Sorry for what turned out to be a mistake reading the docs.

For future reference in case anyone else lands here by googling, setting the default layout in my main function didn’t work because I’m bundling with Vite, and thus using import() instead of require(). Import is async, so I needed to add async/await to get it to work.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Pages - Inertia.js
Persistent layouts. While it's simple to implement layouts as children of the page components, it does force the layout instance to be destroyed...
Read more >
Default Persistent Layout In Laravel + Inertia + Vite
Layouts /DefaultLayoutFile.vue'. You can use the following code to get default layout working with Inertia and Vite:
Read more >
Convenient way to use layouts in Inertia - Bojan Angjelkoski
If you never used the render function in your Vue apps, it can take three arguments, first one is a Vue instance (Vue...
Read more >
Vite + Inertia.js persistent layouts - Laracasts
I'm definitely not sure this is the right way so if someone knows of a better way please feel free to share :)....
Read more >
How To Use Inertia.js in Your Laravel Projects - Kinsta
After the database and server process queries and deliver them to the browser, the page is rendered. But SPAs are different. The application ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found