Skip to content

fix(nuxt): update useNuxtData to access shared asyncData state #22277

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

Merged
merged 15 commits into from
Mar 15, 2024

Conversation

94726
Copy link
Contributor

@94726 94726 commented Jul 21, 2023 β€’

πŸ”— Linked issue

resolves #19704
resolves #22696

❓ Type of change

  • πŸ“– Documentation (updates to the documentation, readme or JSdoc annotations)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • πŸ‘Œ Enhancement (improving an existing functionality like performance)
  • ✨ New feature (a non-breaking change that adds functionality)
  • 🧹 Chore (updates to the build process or auxiliary tools and libraries)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

πŸ“š Description

This change synchronizes the data value of useNuxtData and useAsyncData.

Currently, they aren't really in sync. This happens because the data in the payload (used by useNuxtData) and asyncData are not the same refs. They're only set to the same values on two occasions:

  1. when a fetch resolved
  2. when asyncData is first called per key and it gets hydrated by the payload

πŸ“ Checklist

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly.

Sorry, something went wrong.

Verified

This commit was signed with the committer’s verified signature.
danielroe Daniel Roe
@stackblitz
Copy link

stackblitz bot commented Jul 21, 2023

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

@danielroe
Copy link
Member

danielroe commented Jul 22, 2023 β€’

Would you be able to add a regression test? This might be a case for doing so in the unit tests directory (see test/nuxt/composables.test.ts)...

@94726
Copy link
Contributor Author

94726 commented Jul 22, 2023 β€’

Will do, but I noticed that my implementation only works after hydration and not on the server which may lead to issues.
Also the default may now get thrown into the payload which doesn't make sense, I guess?

Maybe it's better to change useNuxtData to use nuxt._asyncData instead of the payload. Though that would also make useNuxtData return the default, at times :/
What do you think?

@madsh93
Copy link

madsh93 commented Jul 31, 2023

It may also resolve this #22348

@danielroe
Copy link
Member

Sorry for the long delay in getting back to you. I don't there's an issue with useNuxtData coming up with the default value. (But I do think we should keep the default value out of the payload.)

I'll mark this as draft as it sounds like there's a little more to do here. (And a test would be appreciated.)

@danielroe danielroe marked this pull request as draft August 24, 2023 11:59
@logotip4ik
Copy link
Contributor

logotip4ik commented Aug 27, 2023 β€’

idk, but this does solve issue for me if page is initially loaded (when isHydrating is true). But it still breaks with client side navigation (isHydrating is false, because nuxt tries to get data from nuxt.static.data field, but there is none, for non generated websites). If you also add fallback for .static.data it will solve issue even for client side navigation.

in case any body wondering, here are changed lines in my patch (asyncData.js file):

-  const getCachedData = () => nuxt.isHydrating ? nuxt.payload.data[key] : nuxt.static.data[key];
-  const hasCachedData = () => getCachedData() !== void 0;
+  const getCachedData = () => nuxt.isHydrating ? toRef(nuxt.payload.data, key) : (nuxt.static.data[key] || toRef(nuxt.payload.data, key));
+  const hasCachedData = () => unref(getCachedData()) !== void 0;

upd: Now it breaks refetching asyncData. After first request data will be always cached and therefore never refetched ?

logotip4ik added a commit to logotip4ik/keycap that referenced this pull request Aug 28, 2023

Verified

This commit was signed with the committer’s verified signature.
danielroe Daniel Roe
nuxt/nuxt#22277. Dunno, but this does fix my issue

Verified

This commit was signed with the committer’s verified signature.
danielroe Daniel Roe
@94726
Copy link
Contributor Author

94726 commented Sep 7, 2023

Sorry for the long delay in getting back to you. I don't there's an issue with useNuxtData coming up with the default value. (But I do think we should keep the default value out of the payload.)

I updated useNuxtData to simply use _asyncData instead of the payload. That seemed like the easiest way to ensure synchronization without polluting the payload. The only drawback is that calling useNuxtData before useAsyncData will lead to skipping the initial default as _asyncData already exists. Probably not to much of a problem, but maybe could be changed if necessary.

I also included some tests for the synchronization + additional ones for asyncData default because there were none and I didn't want to break anything :)

@94726 94726 marked this pull request as ready for review September 25, 2023 18:20
@94726
Copy link
Contributor Author

94726 commented Oct 11, 2023

This should also resolve #22696

@codecov-commenter
Copy link

Codecov Report

All modified and coverable lines are covered by tests βœ…

❗ No coverage uploaded for pull request base (main@c069239). Click here to learn what that means.

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #22277   +/-   ##
=======================================
  Coverage        ?   58.76%           
=======================================
  Files           ?        5           
  Lines           ?      861           
  Branches        ?       46           
=======================================
  Hits            ?      506           
  Misses          ?      355           
  Partials        ?        0           

β˜” View full report in Codecov by Sentry.
πŸ“’ Have feedback on the report? Share it here.

@metkm
Copy link

metkm commented Feb 26, 2024

Any idea when this pr will be merged? This would be a good fix and needed I think

@94726
Copy link
Contributor Author

94726 commented Feb 26, 2024 β€’

I just fixed the remaining issues, but I'm not sure whether this is the best approach, as it makes all the _asyncData fields optional only because data is also being controlled by useNuxtData.

I also had the idea of splitting data into a seperate object and using the fact that toRef(reactiveObject, 'key') handles creation/tracking of the key even if it doesn't exist yet. See example.

Not sure what the better approach is here. Would love to get some feedback on it :)

@metkm
Copy link

metkm commented Mar 11, 2024

@danielroe I hate to do this but any chance we can get this released on the next version? I think this bug should be a higher priority because using useNuxtData without sync does not make sense

@danielroe danielroe changed the title fix(nuxt): sync useNuxtData and useAsyncData fix(nuxt): update useNuxtData to access shared asyncData state Mar 15, 2024
@danielroe danielroe merged commit a6d6fde into nuxt:main Mar 15, 2024
35 checks passed
@github-actions github-actions bot mentioned this pull request Mar 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
6 participants