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

useFetch called twice after layout switching #21798

Closed
c-schwan opened this issue Jun 26, 2023 · 7 comments · Fixed by #21823
Closed

useFetch called twice after layout switching #21798

c-schwan opened this issue Jun 26, 2023 · 7 comments · Fixed by #21823

Comments

@c-schwan
Copy link
Contributor

c-schwan commented Jun 26, 2023

Environment

Nuxt Version: 3.6.1

Reproduction

https://stackblitz.com/edit/nuxt-j8hefc

Describe the bug

useFetch is called twice after the layout changed.

pages/index.vue (called useFetch) -> pages/other (different layout) -> pages/index.vue (called twice useFetch)

Additional context

No response

Logs

No response

@danielroe
Copy link
Member

For now, you can avoid passing a reactive route slug to useFetch. To resolve, we can look at ensuring the route is synced in layout suspense forks (though this will be tricky).

@c-schwan
Copy link
Contributor Author

c-schwan commented Jun 27, 2023

For now, you can avoid passing a reactive route slug to useFetch. To resolve, we can look at ensuring the route is synced in layout suspense forks (though this will be tricky).

thx @danielroe, i modified the stackblitz without any reactive route params in the useFetch. The behavior is still the same.

Workaround has no effect.

const { data } = await useFetch( 'https://chroniclingamerica.loc.gov/search/titles/results/', { params: { terms: 'oakland', page: 1, format: 'json' } } );

I was looking at #21585 but i think that does not solve this problem

@c-schwan
Copy link
Contributor Author

@danielroe can you please remove the workaround label, removing the reactivity from useFetch does not make any difference.

@prashantpalikhe
Copy link
Contributor

@c-schwan

Why not just use definePageMeta({ layout: 'empty' }) in the empty.vue page instead of using <NuxtLayout /> there too, which is already used in app.vue.

I think that fixes the issue for you.

@c-schwan
Copy link
Contributor Author

c-schwan commented Jul 1, 2023

@c-schwan

Why not just use definePageMeta({ layout: 'empty' }) in the empty.vue page instead of using <NuxtLayout /> there too, which is already used in app.vue.

I think that fixes the issue for you.

that's true in this cause, but im using multiable slots in layout or is there a way to pass content to a different slot in layout without using NuxtLayout?

@prashantpalikhe
Copy link
Contributor

You can define the <NuxtLayout /> in the pages (passing different slots), and remove it from app.vue.

In your page, it would be something like

<!-- /pages/empty.vue -->
<template>
  <NuxtLayout name="empty">
    <template #slot1>...</template>
    <template #slot2>...</template>
  </NuxtLayout>
</template>

@c-schwan
Copy link
Contributor Author

c-schwan commented Jul 2, 2023

@prashantpalikhe thx for help, but then i have to define it on each page (with default layout). this causes the problem that i loose the keepalive state navigating between default layout pages

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

Successfully merging a pull request may close this issue.

3 participants