aspnetcore-angular-universal icon indicating copy to clipboard operation
aspnetcore-angular-universal copied to clipboard

Try/Catch around await Prerenderer.RenderToString(...)

Open Flood opened this issue 7 years ago • 1 comments

I am trying to add a try/catch method around the whole prerendering like this:

 try {
     prerenderResult = await Prerenderer.RenderToString(...);

 } catch (Exception e) {
     //TODO: Fix logging
 }

 if (prerenderResult != null) {
                ViewData["SpaHtml"] = prerenderResult.Html; // our <app> from Angular
                ViewData["Title"] = prerenderResult.Globals["title"]; // set our <title> from Angular
                ViewData["Styles"] = prerenderResult.Globals["styles"]; // put styles in the correct place
                ViewData["Scripts"] = prerenderResult.Globals["scripts"]; // scripts (that were in our header)
                ViewData["Meta"] = prerenderResult.Globals["meta"]; // set our <meta> SEO tags
                ViewData["Links"] = prerenderResult.Globals["links"]; // set our <link rel="canonical"> etc SEO tags
                ViewData["TransferData"] = prerenderResult.Globals["transferData"]; // our transfer data set to window.TRANSFER_CACHE = {};
            }

return View();

If I simulate an error, the view is returned but it's empty. And in the console there is an error message: Error: The selector "app-root" did not match any elements.

How can I skip the prerendered version if something breaks, and just return the app without prerender?

Flood avatar May 15 '18 13:05 Flood

FYI: It worked when I hard coded SpaHtml like this:

if (prerenderResult != null) {
        ViewData["SpaHtml"] = prerenderResult.Html; // our <app> from Angular
        ViewData["Title"] = prerenderResult.Globals["title"]; // set our <title> from Angular
        ViewData["Styles"] = prerenderResult.Globals["styles"]; // put styles in the correct place
        ViewData["Scripts"] = prerenderResult.Globals["scripts"]; // scripts (that were in our header)
        ViewData["Meta"] = prerenderResult.Globals["meta"]; // set our <meta> SEO tags
        ViewData["Links"] = prerenderResult.Globals["links"]; // set our <link rel="canonical"> etc SEO tags
        ViewData["TransferData"] = prerenderResult.Globals["transferData"]; // our transfer data set to window.TRANSFER_CACHE = {};
      } else {
        ViewData["SpaHtml"] = "<app-root>Loading...</app-root>"; // our <app> from Angular
        ViewData["Title"] = "My Title";
      }

Flood avatar May 25 '18 07:05 Flood