aspnetcore-angular-universal
aspnetcore-angular-universal copied to clipboard
Try/Catch around await Prerenderer.RenderToString(...)
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?
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";
}