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.

`rc.5` breaks hydrating `useFetch` data

See original GitHub issue

Environment

  • Operating System: Darwin
  • Node Version: v16.15.0
  • Nuxt Version: 3.0.0-rc.5
  • Package Manager: pnpm@7.5.0
  • Builder: vite
  • User Config: modules, runtimeConfig, kql, typescript
  • Runtime Modules: @vueuse/nuxt@8.9.2, nuxt-kql@0.10.1
  • Build Modules: -

Reproduction

ℹ️ Edit: See minimal reproductions from @KoenCa in the comments below.

Reproduction with nuxt-kql module

Repo (RC 5): https://github.com/johannschopplich/kirby-nuxt-starterkit/tree/feat/nuxt-3-rc.5 👉 Live demo of this repo: https://superlative-gelato-9c9293.netlify.app (Inspect the bug in the console)

Repo (RC 4): https://github.com/johannschopplich/kirby-nuxt-starterkit 👉 Live demo of this repo: https://kirby-nuxt-starterkit.netlify.app

Describe the bug

Server-side fetched data can’t be hydrated in the client. A custom key is passed to useFetch (the same on client & server), but on the client the data for the same key seems not be able to be fetchable.

With disabled build minification, the console (client) throws:

TypeError: data.value is null

In rc.4, everything worked correctly.

Additional context

No response

Logs

👀 View the console error
TypeError: data.value is null
    NuxtJS 63
        setup
        callWithErrorHandling
        setupStatefulComponent
        setupComponent
        mountComponent
        hydrateNode
        hydrateSuspense
        hydrateNode
        hydrateSubTree
        componentUpdateFn
        run
        update
        setupRenderEffect
        mountComponent
        hydrateNode
        hydrateSubTree
        componentUpdateFn
        run
        update
        setupRenderEffect
        mountComponent
        hydrateNode
        hydrateSubTree
        componentUpdateFn
        run
        update
        setupRenderEffect
        mountComponent
        hydrateNode
        hydrateSubTree
        componentUpdateFn
        run
        update
        setupRenderEffect
        mountComponent
        hydrateNode
        hydrateChildren
        hydrateElement
        hydrateNode
        hydrateChildren
        hydrateFragment
        hydrateNode
        hydrateSubTree
        componentUpdateFn
        run
        update
        setupRenderEffect
        mountComponent
        hydrateNode
        hydrateSuspense
        hydrateNode
        hydrateSubTree
        componentUpdateFn
        run
        update
        setupRenderEffect
        mountComponent
        hydrateNode
        hydrate
        mount
        mount
        initApp
        async*
entry-d9af1809.mjs:1091:13

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:10
  • Comments:15 (7 by maintainers)

github_iconTop GitHub Comments

7reactions
pi0commented, Jul 14, 2022

Thanks for making reproduction and detailed issues. With RC.5, for the default key instead of generating a hash from dynamic url passed to the useFetch, we auto-generate the key based on static code location where useAyncData, useFetch or useState are used. (#4955)


A hash collision is initial provided reproduction was happening (app.vue-331 and app.vue-385 both resolving to same 875093775). It is fixed in the latest ohash library using secure SHA256. To fix, you can upgrade lockfile:

npx nuxi@latest upgrade --force

@WamiqM With your reproduction, there is another edge case BTW that is different from hash collision. Since now key is based on static code location of useAsyncData, all 3 composables are depending on same key. You need to compute the key by yourself. Otherwise it works but each call updates all 3.

(Docs need to be improved)

Example:

import { hash } from 'ohash'

export const apiFetch = (url: string, fetchOptions: any = {}) => {
  return useFetch(url, {
    baseURL: 'https://api.github.com/',
    key: hash(['api-fetch', url, fetchOptions]), // <---
    ...fetchOptions,
    headers: {
      ...fetchOptions.headers,
    },
  });
};

Note: You can use any custom key instead of hashing input or manually pass as 3rd argument. Hash is just probably safest

Note: For nested fetch composables, I would recommend to switch to useState with explicit key and $fetch.

4reactions
KoenCacommented, Jul 14, 2022

I also ran into this problem. useFetch seems to be broken in some way. I have code like in this example: https://v3.nuxtjs.org/guide/features/data-fetching/#using-async-setup .

After upgrading to rc. 5 it breaks. The first useFetch runs again on the client and the second useFetch doesn’t run. Rolling back to rc. 4 fixes it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

useFetch
Need to fetch some data? Try this one out. It's an isomorphic fetch hook. That means it works with SSR (server side rendering)....
Read more >
React 18 - New Toys, New Footguns, & New Possibilities.
Here's a 'How to React.js' for Release 18. A practical guide to the new features, and how to cut through the new layers...
Read more >
Finite element tree crown hydrodynamics model (FETCH ...
We use FETCH to conduct sensitivity analysis of transpirational dynamics to changes in canopy structure via two scaling parameters for branch ...
Read more >
nuxt: Versions | Openbase
Support ordering of head metadata; Faster head hydration and deduping. ⚠️ Breaking changes: The shortcuts viewport and charset can only be used within...
Read more >
Saving a Memory (WRITE) Settings for the Entire RC-5 ...
You can connect your computer here and use it to back up or recover data. Owner's Manual. © 2020 Roland Corporation. Saving a...
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