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

fix(server-renderer): Fix call to serverPrefetch in server renderer with an async setup #10893

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

deleteme
Copy link

@deleteme deleteme commented May 8, 2024

This fixes a bug where the serverPrefetch option would not be called with an async setup method.

About the fix

A prefetches reference was created while instance.sp was null. However, with an async setup, instance.sp has the serverPrefetch methods only after setup resolves.

The fix is achieved by waiting to for setup to resolve before trying to access the the serverPrefetch options from instance.sp.

I added a unit test that reproduces the problem, by adding an async setup to the component definition. FWIW, I considered and experimented with adding more async setup variations of the existing tests, but backed out after they did not reproduce the problem and decided they weren't critical.

Impact of the bug

The bug can break SSR because the server renderer will not wait long enough to produce serializable state.

Specifically, this bug exists when using:

  1. defineNuxtComponent from Nuxt 3 (because it creates an async setup method)
  2. with the vue-apollo package options API (because it defines a serverPrefetch method that resolves when all apollo promises resolve)

The server will make a request, but the renderer doesn't wait for it to finish. Nuxt dispatches app:rendered, serializes whatever state it has (which excludes the in-flight apollo graphql requests). Then the page loads in a blank state, then the client in the browser will issue the now redundant graphql requests, and finally render the complete page.

This problem affects Nuxt 2 apollo apps that are migrating to Nuxt 3 and is a regression in the ecosystem.

… option w/async setup

This fixes a bug where the serverPrefetch option would not be called
with an async setup method.

A `prefetches` reference was created while `instance.sp` was `null`.
However, with an async setup, `instance.sp` has the serverPrefetch
methods only after setup resolves.

The fix is achieved by waiting to for setup to resolve before trying to
access the the serverPrefetch options from `instance.sp`.
Copy link

Size Report

Bundles

File Size Gzip Brotli
runtime-dom.global.prod.js 90.7 kB 34.5 kB 31.1 kB
vue.global.prod.js 148 kB 53.7 kB 48 kB

Usages

Name Size Gzip Brotli
createApp 50.8 kB 19.9 kB 18.1 kB
createSSRApp 54.1 kB 21.2 kB 19.3 kB
defineCustomElement 53.1 kB 20.6 kB 18.8 kB
overall 64.5 kB 24.9 kB 22.6 kB

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

Successfully merging this pull request may close these issues.

None yet

2 participants