feat(prerender): ability to push payload and model to boot func #1588

Closed
opened 2025-08-09 17:20:47 +00:00 by fergalmoran · 0 comments
Owner

Originally created by @laskoviymishka on 6/1/2016

  • closes #68 by adding payload provider
  • closes #66 by pushing context cookies to boot func
  • closes #107 by adding asp-prerender-model attribute

Implemented API:

Payload provider
Right now there is an ability to implement some common PayloadProvider which could add some neccesary information to every prerender func call. To implement such provider you could use following API:

services.AddPrerender(provider => new PrerenderOptions
            {
                PayloadProvider = context => new[] { new { Food = "Borsch", Drink = "Vodka" } },
            });

provider is just regular IServiceProvider with all registred services.
context is just a regular HttpContext with all required info for your request.
After specifying these PayloadProvider you could access this array of object via params object:

import * as aspnet from 'aspnet-prerendering';
let boot: aspnet.BootFunc = function (params, model) {
  console.log(params.payload);
};

Accessing model
To access model you could add following code to your View.cshtml

<app asp-prerender-module="ClientApp/boot-server"
     asp-prerender-model="Model"
     asp-prerender-webpack-config="webpack.config.js">Loading...</app>

And then access to passed model:

import * as aspnet from 'aspnet-prerendering';
let boot: aspnet.BootFunc = function (params, model) {
  console.log(model);
};

Cookies
Now it possible to catch aspnet request cookies at server prerender. You may perform these with following code:

import * as aspnet from 'aspnet-prerendering';
let boot: aspnet.BootFunc = function (params, model) {
   console.log(params.cookies);
};

cc @SteveSandersonMS

Any feedback would be great for these one.

*Originally created by @laskoviymishka on 6/1/2016* - closes #68 by adding payload provider - closes #66 by pushing context cookies to boot func - closes #107 by adding asp-prerender-model attribute Implemented API: **Payload provider** Right now there is an ability to implement some common `PayloadProvider` which could add some neccesary information to every prerender func call. To implement such provider you could use following API: ``` services.AddPrerender(provider => new PrerenderOptions { PayloadProvider = context => new[] { new { Food = "Borsch", Drink = "Vodka" } }, }); ``` `provider` is just regular `IServiceProvider` with all registred services. `context` is just a regular `HttpContext` with all required info for your request. After specifying these `PayloadProvider` you could access this array of object via params object: ``` import * as aspnet from 'aspnet-prerendering'; let boot: aspnet.BootFunc = function (params, model) { console.log(params.payload); }; ``` **Accessing model** To access model you could add following code to your `View.cshtml` ``` <app asp-prerender-module="ClientApp/boot-server" asp-prerender-model="Model" asp-prerender-webpack-config="webpack.config.js">Loading...</app> ``` And then access to passed model: ``` import * as aspnet from 'aspnet-prerendering'; let boot: aspnet.BootFunc = function (params, model) { console.log(model); }; ``` **Cookies** Now it possible to catch aspnet request cookies at server prerender. You may perform these with following code: ``` import * as aspnet from 'aspnet-prerendering'; let boot: aspnet.BootFunc = function (params, model) { console.log(params.cookies); }; ``` cc @SteveSandersonMS Any feedback would be great for these one.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github/JavaScriptServices#1588
No description provided.