Skip to content

[vue-server-renderer] support for context.rendered which returns a promise #11256

@eunjae-lee

Description

@eunjae-lee

What problem does this feature solve?

Hi there,

In vue-server-renderer, there is a function createRenderer.
Inside it, it calls context.rendered(context).
What if we allow context.rendered() to return a promise and resolve it if it does?

That's the exact timing between components render and template render.
I want to trigger an HTTP request at that point and include the response into the rendered template.

To rephrase it,

  1. I'd like to see the component tree(which components are there and which props they have, etc)
  2. accordingly fire an HTTP request to my server
  3. and put the response into __INITIAL_STATE__.

Does this make sense? I tried to think of something else, but context.rendered seems to be the most perfect place for this usage.

vue-server-renderer already works fine with serverPrefetch and wait for all serverPrefetchs. Then later it can get vuex store or something to render the template.
But with my proposal, you can do something more than that, sort of in a centralized place which is entry-server.
It doesn't change the API at all, and I don't think it will introduce any confusion to developers.

What does the proposed API look like?

It doesn't require a new API.

The existing code is this:

if (context && context.rendered) {
context.rendered(context);
}

We can, instead, have something like the following:

new Promise((resolve, reject) => {
  if (context && context.rendered) {
    const res = context.rendered(context);
    if (isPromise(res)) {
      return res.then(resolve).catch(reject);
    }
  }
  resolve();
}).then(
  ...
)

It is totally compatible with previous usages.
Let me know if you want me to open a PR for this, or if you have other opinions.

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions