Remember that some browsers cache things you're not familiar with before

Source: Internet
Author: User

Browser cache, have seen a lot of this information, has always felt that the operation should deal with the matter, I have not hands-on operations, so understand not deep, also easy to forget.

Recently looked at Nodejs do static server, a little bit in-depth understanding, so take notes


Read some of the articles

Cache-control,expires,last-modified

The process of caching

Simple implementation of Nodejs


Read some of the articles

Https://developers.google.com/web/fundamentals/performance/optimizing-content-efficiency/http-caching?hl=zh-cn

Http://www.laruence.com/2010/03/05/1332.html

http://www.alloyteam.com/2012/03/web-cache-2-browser-cache/

http://blog.csdn.net/eroswang/article/details/8302191

https://cnodejs.org/topic/4f16442ccae1f4aa27001071


Cache-control,expires,if-modified-since,last-modified

Some commonly used in the request header, the response header to set the cache of something

Expires

The Expires header field provides a date and time that the response is considered invalid after that date and time. Invalid cache entries are not normally cached

The Expires field receives a value in the following format: "Expires:sun, 03:37:26 GMT" For example, I want to set the time for 30 days, Nodejs inside is the new date (the new date (). GetTime () +60*6 0*24*30). toUTCString ()

This has to be used in conjunction with last-modified.

When the requested file, is cached in the browser, the next time the request, if it is directly in the browser to enter the address, will not send the request

If the force ctrl+r will send a request, the server based on the request header last-modified and the request file modification time comparison, if not modified return 304, not as the client returns the file, save bandwidth, the client found that the return is 304, read the file from the browser

I've been supporting it since http/1.0.

If there is Cache-control Max-age will overwrite expires


Cache-control

This has a lot of value to set up, I can understand only 4 no-cache,max-age,public,private

No-cache

Well, that's a good understanding, not caching.

Max-age

Set is a number of seconds, in this time will not like the server to send a request, if forced to refresh, return 304, like expires

The format is as follows cache-control:max-age=315360000

Public

The response is cached and shared among multiple users

Private

The response can only be used as a private cache and can no longer be shared among users. For example, when a post form is submitted, it will help you retain what you have already filled out, indicating that each user has a cache


Last-modified

Last-modified is the time the file was last modified

The format is as follows Last-modified:thu, 08:50:29 GMT

This is set in the response header.

If-modified-since

The browser side receives the server side last-modified, records down, the next time sends the request, the request head surface takes the if-modified-since, its value is the value which the previous last-modified returns

The process of caching

Just some personal understanding, not necessarily all right, static server side with Nodejs processing


1. When the browser is first accessed, all the files are downloaded

2. When the server receives the request, if the request is a static resource (temporarily put the picture, style, JS specified as static resources), return to the head inside write Last-modified,expires,cache-control, Last-modified is telling the file the last modification time, expires and Cache-control are telling the browser that these files can be slow to exist in the browser

3. When the browser accesses the address again, if it is entered directly in the browser, the cached file request is not sent out,

4. If forced refresh (ctrl+r), then the request will be sent out, to the server side, according to the request header if-modified-since compared to the file last-modified, if not modified, will not return the contents of the file, return status code 304,


Simple implementation of Nodejs
varHTTP = require ("http");varFS = require ("FS");varurl = require ("url");varQueryString = Require ("querystring"); Http.createserver (function(req,res) {varGurl =Req.url, pathname=Url.parse (gurl). Pathname; if(Pathname.indexof ("Favicon.ico") >=0){        varico = Fs.readfilesync ("./favicon.ico"); Res.writehead (200,{            "Content-type": "Image/x-icon"}) Res.end (ICO); return; }    if(Pathname.indexof ("/static/") ==0){        varRealpath = __dirname +pathname;        Dealwithstatic (Pathname,realpath,res,req); return; }}). Listen (5555);functiondealwithstatic (pathname,realpath,res,req) {fs.exists (Realpath,function(exists) {if(!exists) {Res.writehead (50U,{                "Content-type": "Text/html"            }); Res.end ("Not find!!!"); }Else{            varmmiestring =/\. ([a-z]+$)/i.exec (pathname) [1], CacheTime= 2*60*60, Mmietype; Switch(mmiestring) { Case"HTML" :                 Case"HTM"  :                 Case"xml": Mmietype = "text/html";  Break;  Case"CSS": mmietype = "Text/css";  Break;  Case"JS": Mmietype = "Text/plain";  Break;  Case"png": Mmitype = "Image/png";  Break;  Case"JPG": Mmitype = "Image/jpeg";  Break; default: Mmietype = "Text/plain"; }            varFileInfo =Fs.statsync (Realpath), lastmodied=fileInfo.mtime.toUTCString (), Modifiedsince= req.headers[' If-modified-since ']            if(modifiedsince && lastmodied = =modifiedsince) {Res.writehead (304, "Not Modified");                Res.end (); return; } fs.readfile (Realpath,function(err,file) {if(Err) {Res.writehead (500,{                        "Content-type": "Text/plain"                    });                Res.end (ERR); }Else{                    varD =NewDate (); varexpires =NewDate (D.gettime () +10*60*1000); Res.writehead (200,{                        "Content-type": Mmietype,"Expires": Expires.toutcstring (),"Cache-control": "max-age=" +CacheTime,"Last-modified": lastmodied});                Res.end (file);        }            }); }    });}


Remember that some browsers cache things you're not familiar with before

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.