├── .gitattributes
├── .gitignore
├── .nuget
├── NuGet.Config
├── NuGet.exe
└── NuGet.targets
├── README.md
├── Sample
├── Web.config
└── index.html
├── StaticWebHelper.sln
└── src
├── FingerPrintHandler.cs
├── NuGet
├── StaticWebHelper.nuspec
└── content
│ └── web.config.transform
├── Properties
└── AssemblyInfo.cs
├── StaticWebHelper.csproj
├── build-nuget.cmd
├── key.snk
└── packages.config
/.gitattributes:
--------------------------------------------------------------------------------
1 | # Auto detect text files and perform LF normalization
2 | * text=auto
3 |
4 | # Custom for Visual Studio
5 | *.cs diff=csharp
6 | *.sln merge=union
7 | *.csproj merge=union
8 | *.vbproj merge=union
9 | *.fsproj merge=union
10 | *.dbproj merge=union
11 |
12 | # Standard to msysgit
13 | *.doc diff=astextplain
14 | *.DOC diff=astextplain
15 | *.docx diff=astextplain
16 | *.DOCX diff=astextplain
17 | *.dot diff=astextplain
18 | *.DOT diff=astextplain
19 | *.pdf diff=astextplain
20 | *.PDF diff=astextplain
21 | *.rtf diff=astextplain
22 | *.RTF diff=astextplain
23 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | ## Ignore Visual Studio temporary files, build results, and
2 | ## files generated by popular Visual Studio add-ons.
3 |
4 | # User-specific files
5 | *.suo
6 | *.user
7 | *.sln.docstates
8 | *.nupkg
9 | */nuget/lib
10 |
11 | # Build results
12 | [Dd]ebug/
13 | [Dd]ebugPublic/
14 | [Rr]elease/
15 | x64/
16 | build/
17 | bld/
18 | [Bb]in/
19 | [Oo]bj/
20 |
21 | # MSTest test Results
22 | [Tt]est[Rr]esult*/
23 | [Bb]uild[Ll]og.*
24 |
25 | #NUNIT
26 | *.VisualState.xml
27 | TestResult.xml
28 |
29 | # Build Results of an ATL Project
30 | [Dd]ebugPS/
31 | [Rr]eleasePS/
32 | dlldata.c
33 |
34 | *_i.c
35 | *_p.c
36 | *_i.h
37 | *.ilk
38 | *.meta
39 | *.obj
40 | *.pch
41 | *.pdb
42 | *.pgc
43 | *.pgd
44 | *.rsp
45 | *.sbr
46 | *.tlb
47 | *.tli
48 | *.tlh
49 | *.tmp
50 | *.tmp_proj
51 | *.log
52 | *.vspscc
53 | *.vssscc
54 | .builds
55 | *.pidb
56 | *.svclog
57 | *.scc
58 |
59 | # Chutzpah Test files
60 | _Chutzpah*
61 |
62 | # Visual C++ cache files
63 | ipch/
64 | *.aps
65 | *.ncb
66 | *.opensdf
67 | *.sdf
68 | *.cachefile
69 |
70 | # Visual Studio profiler
71 | *.psess
72 | *.vsp
73 | *.vspx
74 |
75 | # TFS 2012 Local Workspace
76 | $tf/
77 |
78 | # Guidance Automation Toolkit
79 | *.gpState
80 |
81 | # ReSharper is a .NET coding add-in
82 | _ReSharper*/
83 | *.[Rr]e[Ss]harper
84 | *.DotSettings.user
85 |
86 | # JustCode is a .NET coding addin-in
87 | .JustCode
88 |
89 | # TeamCity is a build add-in
90 | _TeamCity*
91 |
92 | # DotCover is a Code Coverage Tool
93 | *.dotCover
94 |
95 | # NCrunch
96 | *.ncrunch*
97 | _NCrunch_*
98 | .*crunch*.local.xml
99 |
100 | # MightyMoose
101 | *.mm.*
102 | AutoTest.Net/
103 |
104 | # Web workbench (sass)
105 | .sass-cache/
106 |
107 | # Installshield output folder
108 | [Ee]xpress/
109 |
110 | # DocProject is a documentation generator add-in
111 | DocProject/buildhelp/
112 | DocProject/Help/*.HxT
113 | DocProject/Help/*.HxC
114 | DocProject/Help/*.hhc
115 | DocProject/Help/*.hhk
116 | DocProject/Help/*.hhp
117 | DocProject/Help/Html2
118 | DocProject/Help/html
119 |
120 | # Click-Once directory
121 | publish/
122 |
123 | # Publish Web Output
124 | *.[Pp]ublish.xml
125 | *.azurePubxml
126 |
127 | # NuGet Packages Directory
128 | packages/
129 | ## TODO: If the tool you use requires repositories.config uncomment the next line
130 | #!packages/repositories.config
131 |
132 | # Enable "build/" folder in the NuGet Packages folder since NuGet packages use it for MSBuild targets
133 | # This line needs to be after the ignore of the build folder (and the packages folder if the line above has been uncommented)
134 | !packages/build/
135 |
136 | # Windows Azure Build Output
137 | csx/
138 | *.build.csdef
139 |
140 | # Windows Store app package directory
141 | AppPackages/
142 |
143 | # Others
144 | sql/
145 | *.Cache
146 | ClientBin/
147 | [Ss]tyle[Cc]op.*
148 | ~$*
149 | *~
150 | *.dbmdl
151 | *.dbproj.schemaview
152 | *.pfx
153 | *.publishsettings
154 | node_modules/
155 |
156 | # RIA/Silverlight projects
157 | Generated_Code/
158 |
159 | # Backup & report files from converting an old project file to a newer
160 | # Visual Studio version. Backup files are not needed, because we have git ;-)
161 | _UpgradeReport_Files/
162 | Backup*/
163 | UpgradeLog*.XML
164 | UpgradeLog*.htm
165 |
166 | # SQL Server files
167 | *.mdf
168 | *.ldf
169 |
170 | # Business Intelligence projects
171 | *.rdl.data
172 | *.bim.layout
173 | *.bim_*.settings
174 |
175 | # Microsoft Fakes
176 | FakesAssemblies/
177 |
178 | # =========================
179 | # Operating System Files
180 | # =========================
181 |
182 | # OSX
183 | # =========================
184 |
185 | .DS_Store
186 | .AppleDouble
187 | .LSOverride
188 |
189 | # Icon must ends with two \r.
190 | Icon
191 |
192 | # Thumbnails
193 | ._*
194 |
195 | # Files that might appear on external disk
196 | .Spotlight-V100
197 | .Trashes
198 |
199 | # Windows
200 | # =========================
201 |
202 | # Windows image file caches
203 | Thumbs.db
204 | ehthumbs.db
205 |
206 | # Folder config file
207 | Desktop.ini
208 |
209 | # Recycle Bin used on file shares
210 | $RECYCLE.BIN/
211 |
212 | # Windows Installer files
213 | *.cab
214 | *.msi
215 | *.msm
216 | *.msp
217 |
--------------------------------------------------------------------------------
/.nuget/NuGet.Config:
--------------------------------------------------------------------------------
1 |
2 |
126 |
141 |
142 |
\""|'|)(?[^\""']+\.(css|js|png|jpg|jpeg|gif|ico|svg|json|xml|woff|ttf|eot))\k .*?>"); 13 | private static string _cdnPath = ConfigurationManager.AppSettings.Get("cdnPath"); 14 | 15 | public FingerPrintHandler() 16 | { 17 | Uri url; 18 | 19 | if (!string.IsNullOrEmpty(_cdnPath) && !Uri.TryCreate(_cdnPath, UriKind.Absolute, out url)) 20 | throw new UriFormatException("The appSetting 'cdnPath' is not a valid absolute URL"); 21 | } 22 | 23 | public void ProcessRequest(HttpContext context) 24 | { 25 | string file = context.Request.PhysicalPath; 26 | string html = File.ReadAllText(file); 27 | 28 | html = _regex.Replace(html, delegate(Match match) 29 | { 30 | return Print(context, match); 31 | }); 32 | 33 | if ("true".Equals(ConfigurationManager.AppSettings.Get("minify"), StringComparison.OrdinalIgnoreCase)) 34 | { 35 | html = Minify(html); 36 | } 37 | 38 | context.Response.Write(html); 39 | 40 | DateTime lastWrite = File.GetLastWriteTimeUtc(file); 41 | SetConditionalGetHeaders(lastWrite, context); 42 | 43 | context.Response.AddFileDependency(file); 44 | context.Response.Cache.SetValidUntilExpires(true); 45 | context.Response.Cache.SetCacheability(HttpCacheability.ServerAndPrivate); 46 | } 47 | 48 | private static string Minify(string html) 49 | { 50 | var settings = new HtmlMinificationSettings 51 | { 52 | RemoveOptionalEndTags = false, 53 | AttributeQuotesRemovalMode = HtmlAttributeQuotesRemovalMode.Html5, 54 | RemoveRedundantAttributes = false, 55 | WhitespaceMinificationMode = WhitespaceMinificationMode.Aggressive, 56 | MinifyEmbeddedCssCode = true, 57 | MinifyEmbeddedJsCode = true, 58 | MinifyInlineCssCode = true, 59 | MinifyInlineJsCode = true, 60 | }; 61 | 62 | var minifier = new HtmlMinifier(settings); 63 | MarkupMinificationResult result = minifier.Minify(html, generateStatistics: true); 64 | 65 | if (result.Errors.Count == 0) 66 | return result.MinifiedContent; 67 | 68 | return html; 69 | } 70 | 71 | private static string Print(HttpContext context, Match match) 72 | { 73 | string value = match.Value; 74 | string path = match.Groups["href"].Value; 75 | Uri url; 76 | 77 | if (!IsValidUrl(path, out url)) 78 | return value; 79 | 80 | if (!context.IsDebuggingEnabled && !context.Request.IsLocal) // Disable CDN when debugging is on 81 | value = AddCdn(context, value, path); 82 | 83 | string physical = context.Server.MapPath(path); 84 | context.Response.AddFileDependency(physical); 85 | 86 | DateTime lastWrite = File.GetLastWriteTimeUtc(physical); 87 | int index = path.LastIndexOf('.'); 88 | 89 | string pathFingerprint = path.Insert(index, "." + lastWrite.Ticks); 90 | return value.Replace(path, pathFingerprint); 91 | } 92 | 93 | private static string AddCdn(HttpContext context, string value, string path) 94 | { 95 | if (string.IsNullOrEmpty(_cdnPath)) 96 | return value; 97 | 98 | Uri baseUri = new Uri(_cdnPath.TrimEnd('/') + 99 | Path.GetDirectoryName(context.Request.Path) 100 | .Replace("\\", "/") 101 | .TrimEnd('/') + "/"); 102 | 103 | Uri full = new Uri(baseUri, path); 104 | 105 | return value.Replace(path, full.OriginalString); 106 | } 107 | 108 | private static bool IsValidUrl(string path, out Uri url) 109 | { 110 | return Uri.TryCreate(path, UriKind.Relative, out url) && 111 | !url.OriginalString.StartsWith("//"); // Not protocol relative paths since they are absolute 112 | } 113 | 114 | public static void SetConditionalGetHeaders(DateTime lastModified, HttpContext context) 115 | { 116 | HttpResponse response = context.Response; 117 | HttpRequest request = context.Request; 118 | lastModified = new DateTime(lastModified.Year, lastModified.Month, lastModified.Day, lastModified.Hour, lastModified.Minute, lastModified.Second); 119 | 120 | string incomingDate = request.Headers["If-Modified-Since"]; 121 | 122 | response.Cache.SetLastModified(lastModified); 123 | 124 | DateTime testDate = DateTime.MinValue; 125 | 126 | if (DateTime.TryParse(incomingDate, out testDate) && testDate == lastModified) 127 | { 128 | response.ClearContent(); 129 | response.StatusCode = (int)System.Net.HttpStatusCode.NotModified; 130 | response.SuppressContent = true; 131 | } 132 | } 133 | 134 | public bool IsReusable { get { return false; } } 135 | } 136 | } -------------------------------------------------------------------------------- /src/NuGet/StaticWebHelper.nuspec: -------------------------------------------------------------------------------- 1 | 2 |3 | -------------------------------------------------------------------------------- /src/NuGet/content/web.config.transform: -------------------------------------------------------------------------------- 1 | 2 |4 | 16 |StaticWebHelper 5 |1.1.0 6 |StaticWebHelper 7 |Mads Kristensen 8 |Mads Kristensen 9 |false 10 |https://github.com/madskristensen/StaticWebHelper 11 |Helps optimize static websites by fingerprinting resources 12 |Initial release. 13 |Copyright 2014 14 |fingerprint, optimization, cache 15 |3 | 24 | -------------------------------------------------------------------------------- /src/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.InteropServices; 3 | 4 | [assembly: AssemblyTitle("StaticWebHelper")] 5 | [assembly: AssemblyDescription("Helps optimize static websites by fingerprinting resources")] 6 | [assembly: AssemblyConfiguration("")] 7 | [assembly: AssemblyCompany("Mads Kristensen")] 8 | [assembly: AssemblyProduct("StaticWebHelper")] 9 | [assembly: AssemblyCopyright("Copyright © 2014")] 10 | [assembly: AssemblyTrademark("")] 11 | [assembly: AssemblyCulture("")] 12 | 13 | [assembly: ComVisible(false)] 14 | [assembly: Guid("c506efc3-1170-4431-ac39-1c64cb865361")] 15 | 16 | [assembly: AssemblyVersion("1.0.6")] 17 | [assembly: AssemblyFileVersion("1.0.6")] 18 | -------------------------------------------------------------------------------- /src/StaticWebHelper.csproj: -------------------------------------------------------------------------------- 1 | 2 |4 | 7 |5 | 6 | 8 | 23 |9 | 11 |10 | 12 | 14 |13 | 15 | 22 |16 | 21 |17 | 20 |18 | 19 | 3 | -------------------------------------------------------------------------------- /src/build-nuget.cmd: -------------------------------------------------------------------------------- 1 | mkdir NuGet\lib\net40 2 | 3 | copy bin\release\StaticWebHelper.dll NuGet\lib\net40 4 | copy bin\release\WebMarkupMin.Core.dll NuGet\lib\net40 5 | 6 | nuget pack NuGet\staticwebhelper.nuspec 7 | -------------------------------------------------------------------------------- /src/key.snk: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/madskristensen/StaticWebHelper/ef741d8711a51f3d69759af7025d5e8dae574ec7/src/key.snk -------------------------------------------------------------------------------- /src/packages.config: -------------------------------------------------------------------------------- 1 | 2 |4 | 5 | 17 |Debug 6 |AnyCPU 7 |{407CB74E-C62D-4A0D-86D3-BF49C894E32E} 8 |Library 9 |Properties 10 |StaticWebHelper 11 |StaticWebHelper 12 |v4.5 13 |512 14 |..\ 15 |true 16 |18 | 26 |true 19 |full 20 |false 21 |bin\Debug\ 22 |DEBUG;TRACE 23 |prompt 24 |4 25 |27 | 36 |pdbonly 28 |true 29 |bin\Release\ 30 |TRACE 31 |prompt 32 |4 33 |34 | 35 |37 | 39 |true 38 |40 | 42 |key.snk 41 |43 | 53 |44 | 45 | 46 | 47 | 48 | 49 | 52 |..\packages\WebMarkupMin.Core.2.0.0\lib\net40-client\WebMarkupMin.Core.dll 50 |True 51 |54 | 57 |55 | 56 | 58 | 64 |59 | 60 | 61 | 62 | 63 | 65 | 66 | 67 | 72 | 79 |68 | 70 |This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. 69 |71 | 3 | --------------------------------------------------------------------------------4 |