Skip to content
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

feat(vue-app): context.beforeSerialize method #9332

Merged
merged 14 commits into from Jun 4, 2021
Merged

Conversation

dovca
Copy link

@dovca dovca commented May 24, 2021

Edited by @Atinux

Types of changes

  • Bug fix (a non-breaking change which fixes an issue)
  • New feature (a non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Description

This PR adds a beforeSerialize method in the context that can be used to modify window.__NUXT__ after waiting for serverPrefetch, fetch and asyncData, store actions and so on. This will solve hydration issues with plugins like @nuxtjs/apollo and others.

⚠️ Note that beforeSerialize expects a function to be synchronous now since the rendered method is synchronous in Vue: https://github.com/vuejs/vue/blob/dev/src/server/create-renderer.js#L89

The official documented way to wait until the ssr renderer has finished and inject the final state is using ssrContext.rendered.

Resolves: #9155, resolves #8620, resolves #8852 and possibly resolves nuxt-modules/apollo#352 after some more work in that repo.

Checklist:

  • My change requires a change to the documentation.
  • I have updated the documentation accordingly. (PR: feat: add beforeSerialize website-v2#1475)
  • I have added tests to cover my changes (if not applicable, please state why)
  • All new and existing tests are passing.

Will be happy to write up the docs if/when this gets accepted.

@dovca dovca changed the title feat(ssr) Implement afterNuxtRender context method feat: Implement afterNuxtRender context method May 24, 2021
@dovca dovca changed the title feat: Implement afterNuxtRender context method feat(ssr): Implement afterNuxtRender context method May 24, 2021
@Atinux Atinux dismissed a stale review via 217c918 June 2, 2021 22:28
@codecov-commenter
Copy link

codecov-commenter commented Jun 2, 2021

Codecov Report

Merging #9332 (cfb9816) into dev (fa12cf1) will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff           @@
##              dev    #9332   +/-   ##
=======================================
  Coverage   65.15%   65.15%           
=======================================
  Files          94       94           
  Lines        4107     4107           
  Branches     1125     1125           
=======================================
  Hits         2676     2676           
  Misses       1153     1153           
  Partials      278      278           
Flag Coverage Δ
unittests 65.15% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.


Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update fa12cf1...cfb9816. Read the comment docs.

@Atinux Atinux requested review from Atinux, danielroe and pi0 June 2, 2021 22:32
@Atinux
Copy link
Member

Atinux commented Jun 2, 2021

Hi @dovca

Thank you for the pull request, I believe this is a nice implementation and needed since the new fetch.

I let Pooya and Daniel take a look too at it, if good, you can start adding a PR to nuxt/nuxtjs.org repo to document it.

Atinux
Atinux previously approved these changes Jun 2, 2021
danielroe
danielroe previously approved these changes Jun 3, 2021
@Atinux
Copy link
Member

Atinux commented Jun 3, 2021

Another thing in mind, we may also just move beforeNuxtRender so it works for any scenario.

afterNuxtRendermethod is not really good: we are still before Nuxt render the HTML

EDIT: we have no choice since using a method in rendered() has to be synchronous (see https://github.com/vuejs/vue/blob/80e7730946538e0371e213100a0fe81299c2f4b2/src/server/create-renderer.js#L89)

@Atinux
Copy link
Member

Atinux commented Jun 3, 2021

Ideally, we should have beforeNuxtRender inside rendered and force it to be synchronous, but this would lead to a breaking change.

One solution might be to rename beforeNuxtRender to beforeRender and deprecate beforeNuxtRender.

@pi0
Copy link
Member

pi0 commented Jun 3, 2021

We can keep beforeNuxtRender since name is valid, asynchronous hook and already used by many plugins (deprecating is not necessary) but agree that afterNuxtRender naming is not good. What about beforeSerialize() ?

@Atinux Atinux dismissed stale reviews from ghost , danielroe, and themself via d9979a4 June 3, 2021 13:49
@danielroe danielroe dismissed a stale review via 537ae8f June 3, 2021 14:42
@danielroe danielroe dismissed a stale review via bf27d52 June 3, 2021 14:45
@danielroe danielroe dismissed a stale review via 88f525a June 3, 2021 16:46
@Atinux Atinux changed the title feat(ssr): Implement afterNuxtRender context method feat(ssr): Implement context.beforeSerialize method Jun 3, 2021
danielroe
danielroe previously approved these changes Jun 3, 2021
packages/types/app/index.d.ts Outdated Show resolved Hide resolved
packages/vue-app/template/server.js Outdated Show resolved Hide resolved
test/fixtures/basic/pages/before-serialize.vue Outdated Show resolved Hide resolved
Atinux
Atinux previously approved these changes Jun 3, 2021
danielroe
danielroe previously approved these changes Jun 3, 2021
Atinux added a commit to nuxt/website-v2 that referenced this pull request Jun 4, 2021
@Atinux
Copy link
Member

Atinux commented Jun 4, 2021

Added the PR for docs: nuxt/website-v2#1475

clarkdo
clarkdo previously approved these changes Jun 4, 2021
@pi0 pi0 dismissed stale reviews from clarkdo, danielroe, Atinux, and ghost via 5c70ce9 June 4, 2021 14:20
@pi0 pi0 changed the title feat(ssr): Implement context.beforeSerialize method feat(vue-app): context.beforeSerialize method Jun 4, 2021
@pi0 pi0 merged commit 9f02e5d into nuxt:dev Jun 4, 2021
@fabis94
Copy link

fabis94 commented Jul 9, 2021

Hi, when will this be released?

@pi0
Copy link
Member

pi0 commented Jul 9, 2021

Hi @fabis94 it will be released with 2.16 (next minor -- no eta now) but in the meantime, you can try switching from nuxt to nuxt-edge

@pi0 pi0 mentioned this pull request Aug 11, 2021
@danielroe danielroe added the 2.x label Jan 18, 2023
@danielroe danielroe mentioned this pull request Jan 19, 2023
@danielroe danielroe mentioned this pull request Feb 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
7 participants