SpaPrerenderingExtensions - Add an option to pass HttpStatusCode from Client App #275

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

Originally created by @naveedahmed1 on 12/11/2017

Currently there doesn't seem to be any builtin option to pass the HTTP Status code from client app to server in case of pre-rendering.

Looking at the code at:
https://github.com/aspnet/JavaScriptServices/blob/dev/src/Microsoft.AspNetCore.SpaServices.Extensions/Prerendering/SpaPrerenderingExtensions.cs

It seems that currently there isn't any convenient way to pass Status Code in case of Prerendering.

What if renderResult from Prerenderer.RenderToString could also contain the StatusCode, which developers of the SPA could add through createServerRenderer

And then in ServePrerenderResult we can check if there is any StatusCode received along with HTML, we could set it.

private static async Task ServePrerenderResult(HttpContext context, RenderToStringResult renderResult)
        {
            context.Response.Clear();

            if (!string.IsNullOrEmpty(renderResult.RedirectUrl))
            {
                context.Response.Redirect(renderResult.RedirectUrl);
            }
            else
            {
                // The Globals property exists for back-compatibility but is meaningless
                // for prerendering that returns complete HTML pages
                if (renderResult.Globals != null)
                {
                    throw new InvalidOperationException($"{nameof(renderResult.Globals)} is not " +
                        $"supported when prerendering via {nameof(UseSpaPrerendering)}(). Instead, " +
                        $"your prerendering logic should return a complete HTML page, in which you " +
                        $"embed any information you wish to return to the client.");
                }

                context.Response.ContentType = "text/html";
                //set http status code
                context.Response.StatusCode = renderResult.StatusCode > 0 ? renderResult.StatusCode: (int)HttpStatusCode.OK;

                await context.Response.WriteAsync(renderResult.Html);
            }
        }
*Originally created by @naveedahmed1 on 12/11/2017* Currently there doesn't seem to be any builtin option to pass the HTTP Status code from client app to server in case of pre-rendering. Looking at the code at: https://github.com/aspnet/JavaScriptServices/blob/dev/src/Microsoft.AspNetCore.SpaServices.Extensions/Prerendering/SpaPrerenderingExtensions.cs It seems that currently there isn't any convenient way to pass Status Code in case of Prerendering. What if `renderResult `from `Prerenderer.RenderToString `could also contain the `StatusCode`, which developers of the SPA could add through `createServerRenderer` And then in `ServePrerenderResult` we can check if there is any StatusCode received along with HTML, we could set it. ``` private static async Task ServePrerenderResult(HttpContext context, RenderToStringResult renderResult) { context.Response.Clear(); if (!string.IsNullOrEmpty(renderResult.RedirectUrl)) { context.Response.Redirect(renderResult.RedirectUrl); } else { // The Globals property exists for back-compatibility but is meaningless // for prerendering that returns complete HTML pages if (renderResult.Globals != null) { throw new InvalidOperationException($"{nameof(renderResult.Globals)} is not " + $"supported when prerendering via {nameof(UseSpaPrerendering)}(). Instead, " + $"your prerendering logic should return a complete HTML page, in which you " + $"embed any information you wish to return to the client."); } context.Response.ContentType = "text/html"; //set http status code context.Response.StatusCode = renderResult.StatusCode > 0 ? renderResult.StatusCode: (int)HttpStatusCode.OK; await context.Response.WriteAsync(renderResult.Html); } } ```
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github/JavaScriptServices#275
No description provided.