data fetching
onMouted fetches every time i reroute to the main page but i only want it to run every time you reload any page
useFetch or useAsyncData composables. These composables are designed to fetch data only once in universal rendering, which means they won't refetch unnecessarily when navigating between pages.onMounted to fetch data, it will indeed run every time you navigate to the page. To address your specific issue, you have a few options:useFetch or useAsyncData instead of onMounted:lazy option with useFetch:useFetch and checking the navigation type:useFetch, let's explore alternative solutions that are more suitable for your situation.useAsyncData in this case. useAsyncData is more flexible and can work with custom data fetching logic, including composables. Here's how you can approach this:useAsyncData with your composable:lazy option:useAsyncData:useAsyncData is designed to work with any asynchronous function, including custom composables. It provides the same benefits as useFetch in terms of SSR compatibility and avoiding unnecessary refetches.useAsyncData, you can refer to the Nuxt documentation on useAsyncData (https://nuxt.com/docs/api/composables/use-async-data).useRequestHeaders:useRequestHeaders composable:useRequestFetch:useRequestFetch composable:useCookie:useCookie composable:getCookie and setCookie from the h3 package:lazylazyuseCookieuseCookiegetCookiesetCookieh3 <script setup>
const { data } = await useFetch('/api/data')
</script> <script setup>
const { data, refresh } = await useFetch('/api/data', { lazy: true })
</script> <script setup>
const { data, refresh } = await useFetch('/api/data', {
lazy: true,
server: false
})
onMounted(() => {
if (performance.getEntriesByType('navigation')[0].type === 'reload') {
refresh()
}
})
</script> <script setup>
const { data, refresh } = await useAsyncData(
'uniqueKey',
() => yourComposable()
)
</script> <script setup>
const { data, refresh } = await useAsyncData(
'uniqueKey',
() => yourComposable(),
{ lazy: true }
)
onMounted(() => {
if (performance.getEntriesByType('navigation')[0].type === 'reload') {
refresh()
}
})
</script> <script setup>
function useCustomAsyncData(key, handler) {
const { data, refresh } = useAsyncData(key, handler, { lazy: true })
onMounted(() => {
if (performance.getEntriesByType('navigation')[0].type === 'reload') {
refresh()
}
})
return { data, refresh }
}
const { data, refresh } = useCustomAsyncData('uniqueKey', () => yourComposable())
</script> export default defineEventHandler(event => {
let counter = getCookie(event, 'counter') || 0
setCookie(event, 'counter', ++counter)
return { counter }
}) <script setup lang="ts">
const headers = useRequestHeaders(['cookie'])
const { data } = await useFetch('/api/me', { headers })
</script> <script setup lang="ts">
const requestFetch = useRequestFetch()
const { data: forwarded } = await useAsyncData(() => requestFetch('/api/cookies'))
</script> <script setup lang="ts">
const cookie = useCookie('myCookie')
cookie.value = 'myValue'
</script>