Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Router events runs multiple times. #583

Open
boutzamat opened this issue Jan 24, 2024 · 5 comments
Open

Router events runs multiple times. #583

boutzamat opened this issue Jan 24, 2024 · 5 comments

Comments

@boutzamat
Copy link

When i'm using the Inertia Router start event, all previous events gets logged too.
I'm using the following code:

router.on("start", (event) => {
    console.log(event);

    console.log(`Navigated to ${event}`);

    console.log("--------------");
});

This code is running in the main Layout, so it shouldn't be loaded more than once.

The purple box on the image is the console output when i click any link first time, the red box is when i click it the second time (i get both previous event and current event) and if i click it again it will continue like that (recursively).

Is this a bug, or am i doing it wrong?
Skærmbillede 2024-01-24 kl  14 55 17

@craigrileyuk
Copy link

How are you calling your layout?

It’s looks like the layout setup is running with each page load, this is to be expected if your layout isn’t persistent.

@vitalijalbu
Copy link

In DEV mode maybe it's ok because everything runs twice...

@boutzamat
Copy link
Author

boutzamat commented Jan 26, 2024

How are you calling your layout?

It’s looks like the layout setup is running with each page load, this is to be expected if your layout isn’t persistent.

This is an example of my index page that uses the Layout component:
`<script setup>
import DeveloperLayout from "@/Layouts/DeveloperLayout.vue";
import PrimaryButton from "@/Components/PrimaryButton.vue";
import { PlusIcon } from "@heroicons/vue/24/outline";
import { Link, usePage } from "@inertiajs/vue3";
import { Head } from "@inertiajs/vue3";

const props = usePage().props;
</script>

    <template #header>Mine projekter</template>
    <template #header_button>
        <PrimaryButton type="link" :href="route('project.create')">
            <PlusIcon class="mr-2 w-4" />
            Opret projekt
        </PrimaryButton>
    </template>

    <section v-if="!props.projects" class="rounded-3xl bg-white p-16">
        Du har ingen projekter.
        <Link :href="route('project.create')" class="text-primary underline"
            >Opret dit første projekt her </Link
        >.
    </section>
</DeveloperLayout>

`

Pretty much the out of the box way of loading the DeveloperLayout. The DeveloperLayout is just the default Layout component renamed.
I've tried with a fresh Laravel-Inertia installation too, and the issue is the same.
The event is triggered in the DeveloperLayout component.

@boutzamat
Copy link
Author

In DEV mode maybe it's ok because everything runs twice...

I just tested in production mode. The issue is the same.

@craigrileyuk
Copy link

Try following the instructions for Persistent Layouts instead of wrapping your layout around your page.

The way you've done it, the layout is unmounted and then re-mounted every single time you navigate. This is likely causing the event listener to be registered multiple times.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants