From 6cee554760efea908da4d57d5555ad977c97dd86 Mon Sep 17 00:00:00 2001 From: Qstick Date: Thu, 3 May 2018 21:50:27 -0400 Subject: [PATCH] Fixed: Fix caching spec for Initialize.js --- .../Frontend/CacheableSpecification.cs | 3 +- .../Frontend/InitializeJsModule.cs | 46 ++++++++++++++----- 2 files changed, 37 insertions(+), 12 deletions(-) diff --git a/src/Lidarr.Http/Frontend/CacheableSpecification.cs b/src/Lidarr.Http/Frontend/CacheableSpecification.cs index 7d934530f..0d85374d2 100644 --- a/src/Lidarr.Http/Frontend/CacheableSpecification.cs +++ b/src/Lidarr.Http/Frontend/CacheableSpecification.cs @@ -30,6 +30,7 @@ public bool IsCacheable(NancyContext context) if (context.Request.Path.StartsWith("/signalr", StringComparison.CurrentCultureIgnoreCase)) return false; if (context.Request.Path.EndsWith("index.js")) return false; + if (context.Request.Path.EndsWith("initialize.js")) return false; if (context.Request.Path.StartsWith("/feed", StringComparison.CurrentCultureIgnoreCase)) return false; if (context.Request.Path.StartsWith("/log", StringComparison.CurrentCultureIgnoreCase) && @@ -46,4 +47,4 @@ public bool IsCacheable(NancyContext context) return true; } } -} \ No newline at end of file +} diff --git a/src/Lidarr.Http/Frontend/InitializeJsModule.cs b/src/Lidarr.Http/Frontend/InitializeJsModule.cs index 279b12d6b..b9595ca99 100644 --- a/src/Lidarr.Http/Frontend/InitializeJsModule.cs +++ b/src/Lidarr.Http/Frontend/InitializeJsModule.cs @@ -1,4 +1,5 @@ using System.IO; +using System.Text; using Nancy; using Nancy.Responses; using NzbDrone.Common.EnvironmentInfo; @@ -12,6 +13,10 @@ public class InitializeJsModule : NancyModule private readonly IConfigFileProvider _configFileProvider; private readonly IAnalyticsService _analyticsService; + private static string _apiKey; + private static string _urlBase; + private string _generatedContent; + public InitializeJsModule(IConfigFileProvider configFileProvider, IAnalyticsService analyticsService) @@ -19,6 +24,9 @@ public InitializeJsModule(IConfigFileProvider configFileProvider, _configFileProvider = configFileProvider; _analyticsService = analyticsService; + _apiKey = configFileProvider.ApiKey; + _urlBase = configFileProvider.UrlBase; + Get["/initialize.js"] = x => Index(); } @@ -30,25 +38,41 @@ private Response Index() private Stream GetContentStream() { - var urlBase = _configFileProvider.UrlBase; + var text = GetContent(); + var stream = new MemoryStream(); var writer = new StreamWriter(stream); - writer.WriteLine("window.Lidarr = {"); - writer.WriteLine($" apiRoot: '{urlBase}/api/v1',"); - writer.WriteLine($" apiKey: '{_configFileProvider.ApiKey}',"); - writer.WriteLine($" release: '{BuildInfo.Release}',"); - writer.WriteLine($" version: '{BuildInfo.Version.ToString()}',"); - writer.WriteLine($" branch: '{_configFileProvider.Branch.ToLower()}',"); - writer.WriteLine($" analytics: {_analyticsService.IsEnabled.ToString().ToLowerInvariant()},"); - writer.WriteLine($" urlBase: '{urlBase}',"); - writer.WriteLine($" isProduction: {RuntimeInfo.IsProduction.ToString().ToLowerInvariant()}"); - writer.WriteLine("};"); + writer.Write(text); writer.Flush(); stream.Position = 0; return stream; } + + private string GetContent() + { + if (RuntimeInfo.IsProduction && _generatedContent != null) + { + return _generatedContent; + } + + var builder = new StringBuilder(); + builder.AppendLine("window.Lidarr = {"); + builder.AppendLine($" apiRoot: '{_urlBase}/api/v1',"); + builder.AppendLine($" apiKey: '{_apiKey}',"); + builder.AppendLine($" release: '{BuildInfo.Release}',"); + builder.AppendLine($" version: '{BuildInfo.Version.ToString()}',"); + builder.AppendLine($" branch: '{_configFileProvider.Branch.ToLower()}',"); + builder.AppendLine($" analytics: {_analyticsService.IsEnabled.ToString().ToLowerInvariant()},"); + builder.AppendLine($" urlBase: '{_urlBase}',"); + builder.AppendLine($" isProduction: {RuntimeInfo.IsProduction.ToString().ToLowerInvariant()}"); + builder.AppendLine("};"); + + _generatedContent = builder.ToString(); + + return _generatedContent; + } } }