mirror of
https://github.com/aspnet/JavaScriptServices.git
synced 2025-12-24 10:40:23 +00:00
refactor: apply default vs transform to xproj
refactor(spa-services): clean code refactor(node-services): clean code, extract classes nto separate files refactor(angular-services): prime cache cleanup
This commit is contained in:
@@ -9,11 +9,10 @@
|
||||
<ProjectGuid>421807e6-b62c-417b-b901-46c5dedaa8f1</ProjectGuid>
|
||||
<RootNamespace>Microsoft.AspNetCore.AngularServices</RootNamespace>
|
||||
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">..\artifacts\obj\$(MSBuildProjectName)</BaseIntermediateOutputPath>
|
||||
<OutputPath Condition="'$(OutputPath)'=='' ">..\artifacts\bin\$(MSBuildProjectName)\</OutputPath>
|
||||
<OutputPath Condition="'$(OutputPath)'=='' ">.\bin\</OutputPath>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup>
|
||||
<SchemaVersion>2.0</SchemaVersion>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.targets" Condition="'$(VSToolsPath)' != ''" />
|
||||
</Project>
|
||||
</Project>
|
||||
@@ -3,42 +3,51 @@ using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Mvc.Rendering;
|
||||
using Microsoft.AspNetCore.Html;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace Microsoft.AspNetCore.AngularServices {
|
||||
public static class PrimeCacheHelper {
|
||||
public static async Task<HtmlString> PrimeCache(this IHtmlHelper html, string url) {
|
||||
namespace Microsoft.AspNetCore.AngularServices
|
||||
{
|
||||
public static class PrimeCacheHelper
|
||||
{
|
||||
public static async Task<HtmlString> PrimeCache(this IHtmlHelper html, string url)
|
||||
{
|
||||
// TODO: Consider deduplicating the PrimeCache calls (that is, if there are multiple requests to precache
|
||||
// the same URL, only return nonempty for one of them). This will make it easier to auto-prime-cache any
|
||||
// HTTP requests made during server-side rendering, without risking unnecessary duplicate requests.
|
||||
|
||||
if (string.IsNullOrEmpty(url)) {
|
||||
if (string.IsNullOrEmpty(url))
|
||||
{
|
||||
throw new ArgumentException("Value cannot be null or empty", nameof(url));
|
||||
}
|
||||
|
||||
try {
|
||||
try
|
||||
{
|
||||
var request = html.ViewContext.HttpContext.Request;
|
||||
var baseUri = new Uri(string.Concat(request.Scheme, "://", request.Host.ToUriComponent(), request.PathBase.ToUriComponent(), request.Path.ToUriComponent(), request.QueryString.ToUriComponent()));
|
||||
var baseUri =
|
||||
new Uri(string.Concat(request.Scheme, "://", request.Host.ToUriComponent(),
|
||||
request.PathBase.ToUriComponent(), request.Path.ToUriComponent(),
|
||||
request.QueryString.ToUriComponent()));
|
||||
var fullUri = new Uri(baseUri, url);
|
||||
var response = await new HttpClient().GetAsync(fullUri.ToString());
|
||||
var responseBody = await response.Content.ReadAsStringAsync();
|
||||
return new HtmlString(FormatAsScript(url, response.StatusCode, responseBody));
|
||||
} catch (Exception ex) {
|
||||
var logger = (ILogger)html.ViewContext.HttpContext.RequestServices.GetService(typeof (ILogger));
|
||||
if (logger != null) {
|
||||
logger.LogWarning("Error priming cache for URL: " + url, ex);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
var logger = (ILogger)html.ViewContext.HttpContext.RequestServices.GetService(typeof(ILogger));
|
||||
logger?.LogWarning("Error priming cache for URL: " + url, ex);
|
||||
return new HtmlString(string.Empty);
|
||||
}
|
||||
}
|
||||
|
||||
private static string FormatAsScript(string url, HttpStatusCode responseStatusCode, string responseBody)
|
||||
{
|
||||
return string.Format(@"<script>window.__preCachedResponses = window.__preCachedResponses || {{}}; window.__preCachedResponses[{0}] = {1};</script>",
|
||||
JsonConvert.SerializeObject(url),
|
||||
JsonConvert.SerializeObject(new { statusCode = responseStatusCode, body = responseBody })
|
||||
);
|
||||
}
|
||||
=>
|
||||
"<script>" +
|
||||
"window.__preCachedResponses = window.__preCachedResponses || {{}}; " +
|
||||
$"window.__preCachedResponses[{JsonConvert.SerializeObject(url)}] " +
|
||||
$"= {JsonConvert.SerializeObject(new { statusCode = responseStatusCode, body = responseBody })};" +
|
||||
"</script>";
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user