/// <summary> /// load the Page.Items's script /// </summary> /// <param name="page"></param> public static void LoadScript(Page page) { List <string> scriptList = page.Items["ScriptList"] as List <string>; if (scriptList != null) { bool optimizeJS = ConfigurationManager.AppSettings["OptimizeJS"] == "1"; if (optimizeJS) { // cache the scripts if not cached already PageHelpers.Cache(CacheFactory.CreateScriptCache(), scriptList); string url = string.Format("{0}?hash={1}", page.ResolveClientUrl("~/ScriptOptimizer.ashx"), HttpUtility.UrlEncode(PageHelpers.GetHash(scriptList))); AddScriptToPage(page, url); //or use: page.ClientScript.RegisterClientScriptInclude("baseall", url); } else { foreach (string scriptUrl in scriptList) { AddScriptToPage(page, scriptUrl); } } } }
public void ProcessRequest(HttpContext context) { HttpRequest request = context.Request; string hash = request["hash"]; if (!string.IsNullOrEmpty(hash)) { ICache cache = CacheFactory.CreateScriptCache(); string keyName = PageHelpers.GetCacheKey(hash); byte[] responseBytes = cache.Get <byte[]>(keyName); if (responseBytes != null) { HttpResponse response = context.Response; response.AppendHeader("Content-Length", responseBytes.Length.ToString(CultureInfo.InvariantCulture)); response.ContentType = "text/javascript"; response.Cache.SetCacheability(HttpCacheability.Public); response.Cache.SetExpires(DateTime.Now.Add(TimeSpan.FromDays(30))); response.Cache.SetMaxAge(TimeSpan.FromDays(30)); response.Cache.AppendCacheExtension("must-revalidate, proxy-revalidate"); response.OutputStream.Write(responseBytes, 0, responseBytes.Length); } } }