GithubHelp home page GithubHelp logo

guojianbin / staticwebhelper Goto Github PK

View Code? Open in Web Editor NEW

This project forked from madskristensen/staticwebhelper

0.0 2.0 0.0 541 KB

Perf optimize static websites hosted on IIS

C# 97.07% Batchfile 2.93%

staticwebhelper's Introduction

StaticWebHelper

Build status

This NuGet package helps with static .html files. Here's what it does:

  1. Minifies HTML including embedded script and style blocks
  2. FingerPrints references to images, script and css files
  3. Makes it easy to serve static resources from CDNs or cookieless domains
  4. Handles Conditional GET requests (304's)
  5. No code required. It works automatically after installing the NuGet Package.

It works by registering an HTTP Handler that takes over .html files. After doing the transformations, it output caches the response until the .html file itself or one of the referenced resources are updated on disk.

Install the NuGet Package

1. Minifying HTML

To minify the HTML, StaticWebHeper uses WebMarkupMin.Core for the best and safest minification. You can enable the minification through an appSetting:

<add key="minify" value="true" />

2. FingerPrinting

FingerPrinting is the process of appending a file version to any referenced resources. This is important in order to do cache busting, so the resources can have far-future experication dates.

Take this JavaScript reference:

<script src="script/menu.js"></script>

The browser will automatically cache scripts/menu.js, so that when you update the file, the browser will serve the old version from its cache.

To fix that, we need FingerPrinting. With StaticWebHelper the above script tag will be rendered like this:

<script src="script/menu.111559900000122255.js"></script>

The numbers are the DateTime.Ticks of when the file was last changed.

A URL rewrite is automatically being added to the web.config in order to handle the new URL.

2. CDN or cookieless domain

To serve static files such as images, JavaScript and CSS files from a CDN or a cookieless domain is good for the performance of any website.

Typically, a sub domain is used to serve static files and with StaticWebHelper this is now easy to use. Instead of serving our JavaScript file from /scripts/menu.js we can now easily update the reference to point to the sub domain/CDN.

<add key="cdnPath" value="http://static.mysite.com" />

The rendered script tag will then look like this:

<script src="http://static.mysite.com/script/menu.111559900000122255.js"></script>

staticwebhelper's People

Contributors

madskristensen avatar taritsyn avatar bbodenmiller avatar rsaladrigas avatar

Watchers

James Cloos avatar Allen avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.