private static string DownloadCssFile(string url, bool acceptJs = false) { string cssFileCacheKey = $"speedy-external-css-{url}"; string cssFileCache = HttpContext.Current.Cache[cssFileCacheKey] as string; if (SpeedyGenerationSettings.IsDebugModeEnabled()) { cssFileCache = null; } if (!string.IsNullOrWhiteSpace(cssFileCache)) { return(cssFileCache); } else { var uri = HttpContext.Current.Request.Url; var host = uri.Scheme + Uri.SchemeDelimiter + uri.Host + ":" + uri.Port; ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true; var client = new RestClient(host + url); var request = new RestRequest(Method.GET) { RequestFormat = DataFormat.Json }; if (acceptJs) { request.AddHeader("Accept", "application/javascript"); request.AddHeader("Content-Type", "application/javascript"); } // or automatically deserialize result // return content type is sniffed but can be explicitly set via RestClient.AddHandler(); try { var response2 = client.Execute(request); if (response2?.Content != null) { CacheObject(cssFileCacheKey, response2.Content, GetDependencies(null)); } return(response2.Content); } catch (Exception ex) { Diagnostics.Log.Error("Download CSS SpeedyAssetLinksGenerator", ex); } } return(string.Empty); }
public static SpeedyAssetLinks GenerateDeferedLinks(IThemesProvider themesProvider) { if (AssetContentRefresher.IsPublishing() || IsAddingRendering()) { return(new SpeedyAssetLinks()); } var assetsGenerator = new SpeedyAssetLinksGenerator(); var links = assetsGenerator.GenerateSpeedyAssetLinks(themesProvider); var linkSpeedy = new SpeedyAssetLinks(links); var AreScriptsDeferred = SpeedyGenerationSettings.IsCriticalJavascriptEnabledAndPossible(Sitecore.Context.Item); if (AreScriptsDeferred) { string deferredSriptsCacheKey = $"speedy-deferred-page-scripts-{Sitecore.Context.Item.ID}"; string preloadSriptsCacheKey = $"speedy-preload-page-scripts-{Sitecore.Context.Item.ID}"; string deferredSriptsCache = HttpContext.Current.Cache[deferredSriptsCacheKey] as string; string preloadSriptsCache = HttpContext.Current.Cache[preloadSriptsCacheKey] as string; if (SpeedyGenerationSettings.IsDebugModeEnabled()) { deferredSriptsCache = null; preloadSriptsCache = null; } if (!string.IsNullOrWhiteSpace(deferredSriptsCache) && !string.IsNullOrWhiteSpace(preloadSriptsCache)) { linkSpeedy.ClientScriptsRendered = deferredSriptsCache; linkSpeedy.ClientScriptsPreload = preloadSriptsCache; } else { assetsGenerator.GenerateSpeedyScripts(linkSpeedy); CacheObject(deferredSriptsCacheKey, linkSpeedy.ClientScriptsRendered, GetDependencies(null)); CacheObject(preloadSriptsCacheKey, linkSpeedy.ClientScriptsPreload, GetDependencies(null)); } } else { var linksa = AssetLinksGenerator.GenerateLinks(new ThemesProvider()); linkSpeedy.Scripts = linksa.Scripts; } return(linkSpeedy); }
private static string DownloadCssFile(string url, bool acceptJs = false) { string cssFileCacheKey = $"speedy-external-css-{url}"; string cssFileCache = HttpContext.Current.Cache[cssFileCacheKey] as string; if (SpeedyGenerationSettings.IsDebugModeEnabled()) { cssFileCache = null; } if (!string.IsNullOrWhiteSpace(cssFileCache)) { return(cssFileCache); } else { var uri = HttpContext.Current.Request.Url; var host = uri.Scheme + Uri.SchemeDelimiter + uri.Host + ":" + uri.Port; // or automatically deserialize result // return content type is sniffed but can be explicitly set via RestClient.AddHandler(); try { WebClient client = new WebClient(); string reply = client.DownloadString(host + url); // The below is commented out, and originally slipped through to master by mistake. // If your testing the module in development and don't have a valid SSL certificate, you might need to comment it in. //#if DEBUG // ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true; //#endif if (!string.IsNullOrWhiteSpace(reply)) { CacheObject(cssFileCacheKey, reply, GetDependencies(null)); } return(reply); } catch (Exception ex) { Diagnostics.Log.Error("Download CSS SpeedyAssetLinksGenerator", ex); } } return(string.Empty); }
public static SpeedyLayoutModel GetSpeedyLayoutModel() { string text = GetPageKey(HttpContext.Current.Request.Url.AbsolutePath) + "speedysettings"; SpeedyLayoutModel speedyModel = HttpContext.Current.Cache[text] as SpeedyLayoutModel; if (SpeedyGenerationSettings.IsDebugModeEnabled()) { speedyModel = null; } if (speedyModel != null) { return(speedyModel); } SpeedyLayoutModel model = new SpeedyLayoutModel(); model.CacheKey = text; model.SpeedyEnabled = SpeedyGenerationSettings.IsSpeedyEnabledForPage(Sitecore.Context.Item); model.SpeedyJsEnabled = false; model.SpeedyCssEnabled = false; bool isOnePassCookieEnabledAndPresent = SpeedyGenerationSettings.IsOnePassCookieEnabled(Sitecore.Context.Item) && !string.IsNullOrWhiteSpace(HttpContext.Current.Request[SpeedyConstants.CookieNames.OnePassCookieName]); model.ByPassNotDetected = string.IsNullOrWhiteSpace(HttpContext.Current.Request[SpeedyConstants.ByPass.ByPassParameter]) && !isOnePassCookieEnabledAndPresent; if (!model.ByPassNotDetected) { model.SpeedyEnabled = false; } model.VanillaJavasriptAllLoads = GetVanillaJavascriptAllLoades(); if (model.SpeedyEnabled && model.ByPassNotDetected) { BuildSpeedy(model); } else { model.AssetLinks = AssetLinksGenerator.GenerateLinks(new ThemesProvider()); } return(model); }
public virtual SpeedyAssetLinks GenerateSpeedyAssetLinks(IThemesProvider themesProvider) { AssetsArgs assetsArgs = new AssetsArgs(); CorePipeline.Run("assetService", assetsArgs); string text = GenerateCacheKey(assetsArgs.GetHashCode()) + "speedylinks-" + Sitecore.Context.Item.ID; SpeedyAssetLinks assetLinks = HttpContext.Current.Cache[text] as SpeedyAssetLinks; if (SpeedyGenerationSettings.IsDebugModeEnabled()) { assetLinks = null; } if (assetLinks == null || _configuration.RequestAssetsOptimizationDisabled) { assetLinks = new SpeedyAssetLinks(); if (!assetsArgs.AssetsList.Any()) { return(assetLinks); } assetsArgs.AssetsList = (from a in assetsArgs.AssetsList orderby a.SortOrder select a).ToList(); foreach (AssetInclude assets in assetsArgs.AssetsList) { if (assets is ThemeInclude) { AddThemeIncludeSpeedy(assets as ThemeInclude, assetLinks, themesProvider); } else if (assets is UrlInclude) { AddUrlIncludeSpeedy(assets as UrlInclude, assetLinks); } else if (assets is PlainInclude) { AddPlainInclude(assets as PlainInclude, assetLinks); } } CacheLinks(text, assetLinks, GetDependencies(this.DatabaseRepository)); } return(assetLinks); }
private static void LoadCssIntoModel(SpeedyLayoutModel model, SpeedyAssetLinks speedyLinks) { string largeCiticalCssBlockCacheKey = $"speedy-entire-css-critical-page-block-{Sitecore.Context.Item.ID}"; string largeCriticalCssBlockCache = HttpContext.Current.Cache[largeCiticalCssBlockCacheKey] as string; if (SpeedyGenerationSettings.IsDebugModeEnabled()) { largeCriticalCssBlockCache = null; } if (!string.IsNullOrWhiteSpace(largeCriticalCssBlockCache)) { model.CriticalHtml = largeCriticalCssBlockCache; } else { model.CriticalHtml = BuildEntireCssBlock(speedyLinks); CacheObject(largeCiticalCssBlockCacheKey, model.CriticalHtml, GetDependencies(null)); } model.SpecialCaseCriticalCss = Sitecore.Context.Item.Fields[SpeedyConstants.Fields.SpecialCaseCriticalCss].Value; }