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

fix(nuxt3): handle hydration for error and pending state #2130

Merged
merged 4 commits into from
Nov 24, 2021
Merged

Conversation

danielroe
Copy link
Member

πŸ”— Linked issue

resolves nuxt/nuxt#12885
resolves nuxt/bridge#174

❓ 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

This PR includes pending/error state within the payload. Otherwise a page that (for example) checked v-if="error" would fail to hydrate between client/server. Because we're including pending state as well, we can use that to detect whether we need to refetch data on client side.

πŸ“ Checklist

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

@danielroe danielroe added bug Something isn't working bridge nuxt3 labels Nov 24, 2021
@danielroe danielroe requested a review from pi0 November 24, 2021 11:31
@danielroe danielroe self-assigned this Nov 24, 2021
@netlify
Copy link

netlify bot commented Nov 24, 2021

βœ”οΈ Deploy Preview for nuxt3-docs canceled.

πŸ”¨ Explore the source changes: cebd845

πŸ” Inspect the deploy log: https://app.netlify.com/sites/nuxt3-docs/deploys/619e96a8e7e17a000737e00a

@pi0
Copy link
Member

pi0 commented Nov 24, 2021

By changing flatten structure of payload into { data, error?, pending? } for each key, we are increasing HTML size.

  • pending state is really a runtime-specific flag to indicate we have a pending promise and is available as nuxt.App._asyncDataPromises. It should not be hydrated and useLazy* composables need to set it to true (and false if hydrated).

  • error should be explicitly consumed by by user (to display in component or log). Adding it to payload, not only reduces performance but also adds security risk of exposing server errors to public. PS: A client-side retry strategy can be used to obtain payload for errors like timeouts.

@pi0 pi0 added the pending label Nov 24, 2021
@pi0 pi0 removed the pending label Nov 24, 2021
Copy link
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.

LGTM (didn't locally test hydration changes hope is good :D)

@pi0 pi0 changed the title fix(nuxt3): include error and pending values in asyncData state fix(nuxt3): handle hydration for error and pending state Nov 24, 2021
@pi0 pi0 merged commit 955fa36 into main Nov 24, 2021
@pi0 pi0 deleted the fix/asyncdata-state branch November 24, 2021 19:59
@nndnha
Copy link

nndnha commented Nov 25, 2021

  • error should be explicitly consumed by by user (to display in component or log). Adding it to payload, not only reduces performance but also adds security risk of exposing server errors to public. PS: A client-side retry strategy can be used to obtain payload for errors like timeouts.

@pi0

const { error } = await useFetch('https://jsonplaceholder.typicode.com/404');

This line of code can run on both server and client with the same result and I can say that the server error for this action is already public so it was completely unrelated to Nuxt. I just want to get the http status code(404 in this case) and those recent commits transformed it from FetchError to Boolean then how can we get the status code on client side?https://stackblitz.com/edit/github-ygz9df-hnnre8?file=app.vue

@@ -39,7 +40,7 @@ export default (ctx, inject) => {
globalName: 'nuxt',
payload: proxiedState(process.client ? ctx.nuxtState : ctx.ssrContext.nuxt),
_asyncDataPromises: [],
isHydrating: ctx.isHMR,
isHydrating: true,
Copy link

Choose a reason for hiding this comment

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

This change also fixes bug, where useLazyAsyncData was called again on client side during hydration.

@danielroe danielroe added the 3.x label Jan 19, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
3.x bridge bug Something isn't working nuxt3
Projects
None yet
4 participants