Last active
December 26, 2015 16:59
-
-
Save markis/7184282 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class JavascriptHandler : IHttpHandler | |
{ | |
private readonly static bool enabledAutoMinified = ConfigurationManager.AppSettings["enableAutoMinified"] == "true"; | |
private readonly static Minifier minifier = new Minifier(); | |
private readonly static CodeSettings minifierSettings = new CodeSettings() | |
{ | |
/* Minifier code settings here */ | |
}; | |
bool IHttpHandler.IsReusable | |
{ | |
get { return false; } | |
} | |
void IHttpHandler.ProcessRequest(HttpContext context) | |
{ | |
/* | |
* logic for getting folder name and file info here | |
* | |
*/ | |
// if the user sent us a "if-modified-since" header, compare the date to the cache date | |
DateTime modifiedSince; | |
var header = context.Request.Headers["If-Modified-Since"]; | |
if (header != null && DateTime.TryParse(header, out modifiedSince) && webInfo.DateTime == modifiedSince) | |
{ | |
// send a 304 and exit out | |
context.Response.StatusCode = 304; | |
context.ApplicationInstance.CompleteRequest(); | |
} | |
else | |
{ | |
// set the cache headers | |
var cacheHeader = context.Response.Cache; | |
cacheHeader.SetValidUntilExpires(true); // cache this server-side | |
cacheHeader.SetCacheability(HttpCacheability.Public); //tell the client to cache this file | |
//cache for a long time, we are 100% dependant on the JS control | |
// to give us a unique name everytime the underlying files change | |
cacheHeader.SetExpires(DateTime.Today.AddDays(365)); | |
cacheHeader.SetMaxAge(TimeSpan.FromDays(365)); //cache for a long time | |
cacheHeader.SetLastModified(webInfo.DateTime); //inform the client of what we know to be the lastmodified date | |
// IIS won't gzip the request if the return content type isn't the following: | |
context.Response.ContentType = "application/x-javascript"; | |
//get the folder/file that correspond to this request | |
var javascriptFolder = context.Server.MapPath(folderPath); | |
var javascriptFile = javascriptFolder + ".js"; | |
string[] files = new string[] { }; | |
if (Directory.Exists(javascriptFolder)) | |
{ | |
files = Directory.GetFiles(javascriptFolder); | |
} | |
else if (File.Exists(javascriptFile)) | |
{ | |
files = new[] { javascriptFile }; | |
} | |
//run through all the files in the folder minify them and concate them | |
foreach (var fileName in files) | |
{ | |
var js = File.ReadAllText(fileName); | |
if (enabledAutoMinified) | |
{ | |
var minifiedJs = minifier.MinifyJavaScript(js, minifierSettings); | |
// write the minified js directly into the output stream | |
context.Response.Output.Write(minifiedJs); | |
// Microsoft Ajax Minifier removes the last semi-colon | |
// since we are concatenating then we should put it back | |
// line break so that we can deciminate the different files | |
context.Response.Output.WriteLine(";"); | |
foreach (var error in minifier.ErrorList) | |
{ | |
// write any errors into the trace so we can debug later | |
context.Trace.Warn("minifier", error.ToString()); | |
} | |
} | |
else | |
{ | |
// if we aren't minifying then just concatenate all the js files | |
context.Response.Output.Write(js); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment