-
-
Notifications
You must be signed in to change notification settings - Fork 33.7k
Description
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,
- I'd like to see the component tree(which components are there and which props they have, etc)
- accordingly fire an HTTP request to my server
- 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 serverPrefetch
s. 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:
vue/packages/vue-server-renderer/build.dev.js
Lines 9187 to 9189 in a59e05c
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!