inertiajs / inertia

Inertia.js lets you quickly build modern single-page React, Vue and Svelte apps using classic server-side routing and controllers.
https://inertiajs.com
MIT License
6.02k stars 405 forks source link

Inertia crashes when a layer prop is not sent from server #1848

Closed honzahana closed 2 months ago

honzahana commented 2 months ago

Version:

Describe the problem:

If I put a prop into Layer and this prop is not sent from the server, Intertia crashes and stops working. It crashes even if the default value is set in Svelte.

Laravel source code:

Laravel routes file ./routes/web.php:

<?php

use App\Http\Controllers\FormControler;
use Illuminate\Support\Facades\Route;
use Inertia\Inertia;

Route::inertia('/page1', 'PageOne');

Route::get('/page2', function () {
    return Inertia::render('PageTwo', [
        'user' => [
            'name' => 'honza',
            'email' => 'honza@example.com'
        ],
    ]);
});

Layout file ./resources/js/Pages/AppLayout.svelte:

<script>
    export let user = {name:'unknown',email:'unknown'};
</script>
<header>
    This is AppLayout, user: {user.name}
</header>
<main>
    <slot />
</main>

Page one ./resources/js/Pages/PageOne.svelte:

<script context="module">
    export { default as layout } from './AppLayout.svelte'
</script>
<script>
    import { Link } from '@inertiajs/svelte'
</script>

<p>This is page one.</p>
<p>Go to <Link href="/page2">Page two</Link></p>

Page two ./resources/js/Pages/PageTwo.svelte:

<script context="module">
    export { default as layout } from './AppLayout.svelte'
</script>
<script>
    import { Link } from '@inertiajs/svelte'
</script>

<p>This is page two.</p>
<p>Go to <Link href="/page1">Page one</Link></p>

Steps to reproduce:

  1. Run npm run dev or npm run build and php artisan serve
  2. If I open page one in the browser /page1, everything works as expected. The user is marked as unknown.
  3. Then I click on the link to page two. Everything also works correctly. The user is honza as expected.
  4. After that I click on the link to page one and nothing happens. Inertia crashes.

crash

I found that if a prop is not an object, but only a string Inertia will not crash.