spatie / server-side-rendering

Server side rendering JavaScript in a PHP application
https://sebastiandedeyne.com/posts/2018/server-side-rendering-javascript-from-php
MIT License
602 stars 34 forks source link

Data hydration between node process and client #52

Closed mercs600 closed 2 years ago

mercs600 commented 2 years ago

First of all, great job! I would like to ask is there any way to hydrate data between Vue App and PHP environment ? Now we can pass context and environment data to application, so we can get data from PHP to VueJS app and this is ok. What in case when I'm getting data from serverPrefetch (ssr hook) and I want to pass them to the client - in that case we should share our context from node process to PHP. When it comes to node environment only there is no issue, but is it possible to do this together with PHP ?

EDIT: I have done something like this. It does not look like much, but it works...

Is there better way ?

if (typeof dispatch !== 'undefined') {
    renderToString(app, (err, html) => {
        if (err) {
            throw new Error(err)
        }
        html = `
        <script type="text/javascript">
        var __CONTEXT__ = ${JSON.stringify(app.$store.state)};
        </script>
        ${html}`
        // eslint-disable-next-line no-undef
        dispatch(html)
    })
}