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

feat: useNuxtData composable#9262

Merged
pi0 merged 25 commits intonuxt:mainfrom
mahdiboomeri:feat/nuxt-data
Dec 5, 2022
Merged

feat: useNuxtData composable#9262
pi0 merged 25 commits intonuxt:mainfrom
mahdiboomeri:feat/nuxt-data

Conversation

@mahdiboomeri
Copy link
Copy Markdown
Contributor

@mahdiboomeri mahdiboomeri commented Nov 22, 2022

🔗 Linked issue

resolves nuxt/nuxt#15582
resolves nuxt/nuxt#14924

❓ 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

It introduces a new useNuxtData composable.

📝 Checklist

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

@codesandbox
Copy link
Copy Markdown

codesandbox Bot commented Nov 22, 2022

CodeSandbox logoCodeSandbox logo  Open in CodeSandbox Web Editor | VS Code | VS Code Insiders

@atinux
Copy link
Copy Markdown
Member

atinux commented Nov 24, 2022

I really like this idea of optimistic UI.

I would personally rename it to getNuxtData().

Happy to get feedback also from other team member

@pi0
Copy link
Copy Markdown
Member

pi0 commented Nov 24, 2022

Related PR to access data: #7569

I think useNuxtData makes sense (comparing to useData() in other PR and getNuxtData()). We use use* prefix for composables that use implicit context/CAPI. And also nuxt in the name makes sense.

Comparing to #7569:

  • We could support key param to get one data
  • We should return a ref IMO to allow changing value by key

@mahdiboomeri
Copy link
Copy Markdown
Contributor Author

Thanks for looking into this!
I've updated the PR base on @pi0's suggestion

Comment thread packages/nuxt/src/app/composables/asyncData.ts Outdated
@pi0 pi0 mentioned this pull request Nov 28, 2022
7 tasks
@mahdiboomeri mahdiboomeri requested a review from pi0 December 2, 2022 20:29
Comment thread docs/content/1.docs/3.api/1.composables/use-nuxt-data.md Outdated
Copy link
Copy Markdown
Member

@pi0 pi0 left a comment

Choose a reason for hiding this comment

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

💯

Comment thread docs/content/1.docs/3.api/1.composables/use-nuxt-data.md Outdated
Copy link
Copy Markdown
Member

@danielroe danielroe left a comment

Choose a reason for hiding this comment

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

We should not call useNuxtData within a request handler. We need to update the docs here.

@pi0 pi0 requested a review from danielroe December 5, 2022 12:21
Copy link
Copy Markdown
Member

@danielroe danielroe left a comment

Choose a reason for hiding this comment

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

Thanks ❤️

Comment thread packages/nuxt/src/app/composables/asyncData.ts Outdated
pi0 and others added 2 commits December 5, 2022 13:54
@pi0
Copy link
Copy Markdown
Member

pi0 commented Dec 5, 2022

Thanks again dear @mahdiboomeri <3 A short explainer of recent changes:

  • useNuxtData requires an explicit key. This makes usage easier and also explicit for keys (excluding auto generated keys). Also allows more flexibility to change the implementation in the future.
  • Return value of useNuxtData is { data: Ref }. This makes it consistent with other data composables and also ensures to have upward compatibility to add any other fields related to data
  • Docs updated as daniel suggested to cache value of useNuxtData() outside of handler. However we restore context and it is always available on client-side, it cannot always work in async conditions where nuxt context is lost. It also makes examples easier to read as we call it once.

@pi0 pi0 merged commit 9c478ee into nuxt:main Dec 5, 2022
@mahdiboomeri mahdiboomeri deleted the feat/nuxt-data branch December 5, 2022 14:13
@pi0 pi0 mentioned this pull request Dec 15, 2022
6 tasks
@danielroe danielroe added the 3.x label Jan 19, 2023
@danielroe danielroe mentioned this pull request Jan 20, 2023
danielroe added a commit that referenced this pull request Jan 21, 2023
Co-authored-by: Daniel Roe <daniel@roe.dev>
Co-authored-by: Pooya Parsa <pooya@pi0.io>
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.

Accessing async data cache object with a new composable useData composable

4 participants