JavaScriptServices 376 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. commit 0c77224f46dd6a0f3d72352a5a44d0d6abee3e22
  2. Author: Steve Sanderson <[email protected]>
  3. Date: Thu Nov 9 10:44:30 2017 -0800
  4. Allow prerendering middleware to pass through non-prerendered responses (important when using dev middleware)
  5. diff --git a/src/Microsoft.AspNetCore.SpaServices.Extensions/Prerendering/SpaPrerenderingExtensions.cs b/src/Microsoft.AspNetCore.SpaServices.Extensions/Prerendering/SpaPrerenderingExtensions.cs
  6. index 554414f887f..dfb6a17712b 100644
  7. --- a/src/Microsoft.AspNetCore.SpaServices.Extensions/Prerendering/SpaPrerenderingExtensions.cs
  8. +++ b/src/Microsoft.AspNetCore.SpaServices.Extensions/Prerendering/SpaPrerenderingExtensions.cs
  9. @@ -113,21 +113,19 @@ namespace Microsoft.AspNetCore.Builder
  10. context.Response.Body = originalResponseStream;
  11. }
  12. - // If it's not a success response, we're not going to have any template HTML
  13. - // to pass to the prerenderer.
  14. - if (context.Response.StatusCode < 200 || context.Response.StatusCode >= 300)
  15. + // If it isn't an HTML page that we can use as the template for prerendering,
  16. + // - ... because it's not text/html
  17. + // - ... or because it's an error
  18. + // then prerendering doesn't apply to this request, so just pass through the
  19. + // response as-is. Note that the non-text/html case is not an error: this is
  20. + // typically how the SPA dev server responses for static content are returned
  21. + // in development mode.
  22. + var canPrerender = IsSuccessStatusCode(context.Response.StatusCode)
  23. + && IsHtmlContentType(context.Response.ContentType);
  24. + if (!canPrerender)
  25. {
  26. - var message = $"Prerendering failed because no HTML template could be obtained. " +
  27. - $"Check that your SPA is compiling without errors. " +
  28. - $"The {nameof(SpaApplicationBuilderExtensions.UseSpa)}() middleware returned " +
  29. - $"a response with status code {context.Response.StatusCode}.";
  30. - if (outputBuffer.Length > 0)
  31. - {
  32. - message += " and the following content: "
  33. - + Encoding.UTF8.GetString(outputBuffer.GetBuffer());
  34. - }
  35. -
  36. - throw new InvalidOperationException(message);
  37. + await outputBuffer.CopyToAsync(context.Response.Body);
  38. + return;
  39. }
  40. // Most prerendering logic will want to know about the original, unprerendered
  41. @@ -160,6 +158,20 @@ namespace Microsoft.AspNetCore.Builder
  42. });
  43. }
  44. + private static bool IsHtmlContentType(string contentType)
  45. + {
  46. + if (string.Equals(contentType, "text/html", StringComparison.Ordinal))
  47. + {
  48. + return true;
  49. + }
  50. +
  51. + return contentType != null
  52. + && contentType.StartsWith("text/html;", StringComparison.Ordinal);
  53. + }
  54. +
  55. + private static bool IsSuccessStatusCode(int statusCode)
  56. + => statusCode >= 200 && statusCode < 300;
  57. +
  58. private static void RemoveConditionalRequestHeaders(HttpRequest request)
  59. {
  60. request.Headers.Remove(HeaderNames.IfMatch);