You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Sounds like a great way to optimize performance, but i'm very curious if this will serve actual content pre-rendered, or just some sort of placeholder skeleton?
Thanks for making this!
The text was updated successfully, but these errors were encountered:
gijsroge
changed the title
What do you mean by "Meaningful content".
Question: What do you mean by "Meaningful content".
May 31, 2018
Good question! It's actually entirely up to you. If your app does its entire initial rendering synchronously, you'll get that as the pre-rendered HTML. In a lot of cases, it would be more common to end up with a pre-rendered "App Shell", since data loading is generally not included when doing the simplest form of prerendering.
However, this is where prerender-loader gives you control over what happens: if you point it at an async function (like in the second String Prerendering example), you get to decide what to return and how long to wait for data.
// custom-prerender.jsimport'unfetch/polyfill';importMyAppfrom'./components/app';importrenderToStringfrom'preact-render-to-string';exportdefaultasyncfunctionprerenderWithData(params){letres=awaitfetch('https://api.example.com/items.json');letitems=awaitres.json();// now we have data, so we can render something meaningful:returnrenderToString(<MyAppitems={items}/>)}
Note: params gets populated with whatever is passed to prerender-loader's params option
Side note: hydration
In the above example, we're prerendering using "items" data, but that data isn't available when the client code boots up in our browser. We can fix that too:
exportdefaultasyncfunctionprerenderWithData(){letitems=await(awaitfetch('https://api.example.com/items.json')).json();letscript=document.createElement('script');script.textContent=`window.DATA = ${JSON.stringify(items)}`;document.body.appendChild(script);returnrenderToString(<MyAppitems={items}/>)}// now all we have to do is add a check in our MyApp component (or wherever "items" was being used) for window.DATA:constMyApp=({ items =window.DATA})=>(<ul>{items.map(item=><li>{item}</li>)}</ul>)
Sounds like a great way to optimize performance, but i'm very curious if this will serve actual content pre-rendered, or just some sort of placeholder skeleton?
Thanks for making this!
The text was updated successfully, but these errors were encountered: