Skip to content
This repository has been archived by the owner on Apr 6, 2023. It is now read-only.

fix(nuxt): use shared state for asyncData #7055

Merged
merged 2 commits into from
Aug 30, 2022
Merged

fix(nuxt): use shared state for asyncData #7055

merged 2 commits into from
Aug 30, 2022

Conversation

pi0
Copy link
Member

@pi0 pi0 commented Aug 30, 2022

πŸ”— Linked issue

nuxt/nuxt#13910 #5078 #5738

❓ Type of change

  • πŸ“– Documentation (updates to the documentation or readme)
  • 🐞 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)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

πŸ“š Description

With the initial implementation of useAyncData, we were using a shared promise to avoid running two asyncData promises with the same key at the same time but the fetching state and data was not shared. It makes problems if someone tries to make a higher-order composable that returns a new instance of asyncData with the same key.

This PR resolves it using a shared _asyncData map in nuxtApp interface also adding tests from #5738 by @OhB00.

Remarks:

We probably can iterate over this. Normally each useAsyncData should be unique based on its key and a factory pattern shall be introduced for createAsyncData and createFetch (discussion: nuxt/nuxt#14736)

I will follow up with more PRs to have proper changelog.

πŸ“ Checklist

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

@netlify
Copy link

netlify bot commented Aug 30, 2022

βœ… Deploy Preview for nuxt3-docs canceled.

Name Link
πŸ”¨ Latest commit 5b36d48
πŸ” Latest deploy log https://app.netlify.com/sites/nuxt3-docs/deploys/630de3dd4958f70008f56917

@pi0 pi0 marked this pull request as ready for review August 30, 2022 10:31
@pi0 pi0 merged commit 5a17458 into main Aug 30, 2022
@pi0 pi0 deleted the fix/async-data-state branch August 30, 2022 10:34
} as AsyncData<DataT, DataE>
// Create or use a shared asyncData entity
if (!nuxt._asyncData[key]) {
nuxt._asyncData[key] = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pi0 Have you checked whether this retains reactivity if this is created outside a component setup (eg in plugin), or when it is used after the original component that created it has unmounted?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can try it (as mentioned in the description we need to iterate over to fix the related issue).

BTW really the use case of useAsyncData is to be used within the component lifecycle, not the plugins even if partially possible.

Good point about reactivity after unmounting. Will test this.

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

Successfully merging this pull request may close these issues.

None yet

3 participants