Skip to content
This repository was archived by the owner on Apr 8, 2020. It is now read-only.

Commit fbe41f4

Browse files
Better handle errors during prerendering
1 parent de3fd30 commit fbe41f4

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

src/Microsoft.AspNetCore.SpaServices.Extensions/Prerendering/SpaPrerenderingExtensions.cs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,13 @@ public static void UseSpaPrerendering(
7575
}
7676
}
7777

78+
// If we're building on demand, do that first
79+
var buildOnDemandTask = lazyBuildOnDemandTask.Value;
80+
if (buildOnDemandTask != null)
81+
{
82+
await buildOnDemandTask;
83+
}
84+
7885
// It's no good if we try to return a 304. We need to capture the actual
7986
// HTML content so it can be passed as a template to the prerenderer.
8087
RemoveConditionalRequestHeaders(context.Request);
@@ -94,11 +101,17 @@ public static void UseSpaPrerendering(
94101
context.Response.Body = originalResponseStream;
95102
}
96103

97-
// If we're building on demand, do that first
98-
var buildOnDemandTask = lazyBuildOnDemandTask.Value;
99-
if (buildOnDemandTask != null && !buildOnDemandTask.IsCompleted)
104+
// If it's not a success response, we're not going to have any template HTML
105+
// to pass to the prerenderer.
106+
if (context.Response.StatusCode < 200 || context.Response.StatusCode >= 300)
100107
{
101-
await buildOnDemandTask;
108+
var message = $"Prerendering failed because no HTML template could be obtained. Check that your SPA is compiling without errors. The {nameof(SpaApplicationBuilderExtensions.UseSpa)}() middleware returned a response with status code {context.Response.StatusCode}";
109+
if (outputBuffer.Length > 0)
110+
{
111+
message += " and the following content: " + Encoding.UTF8.GetString(outputBuffer.GetBuffer());
112+
}
113+
114+
throw new InvalidOperationException(message);
102115
}
103116

104117
// Most prerendering logic will want to know about the original, unprerendered

0 commit comments

Comments
 (0)