aspnet / JavaScriptServices

[Archived] This repository has been archived
Apache License 2.0
3.03k stars 518 forks source link

Re-rendering with serialized data sent to browser #740

Closed larstbone closed 7 years ago

larstbone commented 7 years ago

Angular Universal indicates it can be configured to send serialized cached data to the browser. I would love to take advantage of this feature, but cannot see where to start.

I'm assuming I understand this feature correctly. My hope is that if my first route requires DataA and DataB to be loaded, that these are queried for during the re-rendering on the server, and then somehow sent to the browser so that these do not have to be queried again.

Here's where I find the feature mentioned on Angular Universal. https://universal.angular.io/overview/

SteveSandersonMS commented 7 years ago

In your boot-server.ts file, you can change the resolve call so that it also supplies a globals value, e.g:

const myData = { ... your stuff here ... };
resolve({
    html: html,
    globals: { myData }
});

Then, in boot-client.ts, you can access window.myData to get the data transferred from the server. You can use this to initialise your application however you want.

In the ReactRedux template, this is all set up for you, because there's an official "correct" way to do it. In the Angular template, it isn't set up for you, because there's no single official way to do it - the type of data you're transferring, and how to initialise your application with it, will depend on you. Hope that helps!