Skip to Content
Node与Express开发(第2版)
book

Node与Express开发(第2版)

by Ethan Brown
June 2021
Intermediate to advanced
290 pages
9h 21m
Chinese
Posts & Telecom Press
Content preview from Node与Express开发(第2版)
静态内容
|
191
表17-2:正确的CDN路由配置
URL路径 路由目标/文件源
/api/* Node/Express
服务器、代理,或负载均衡
/*
(除上面以外的任何路径) 静态
CDN
文件存储
我们已经看到了,要想无缝地从开发切换到生产应该怎样组织应用,现在来看看缓存究竟
是怎么做的,以及它是怎样提升性能的。
17.4
 缓存静态资源
不管你是使用
Express
来提供静态资源,还是使用
CDN
,浏览器都会使用以下几个
HTTP
响应头来确定何时以及如何缓存静态资源,理解它们是很有帮助的。
Expires
/
Cache-Control
这两个响应头会告诉浏览器这个资源可以被缓存的最长期限。而浏览器会认真地对待它
们的命令:如果它们告诉浏览器某个文件需要缓存一个月,那么在一个月之内,只要文
件还在缓存里,浏览器就不会重新下载它。但是出于某些无法控制的原因,浏览器可能
会从缓存里永久地删除图片文件,明白这一点很重要。例如,用户可以手动清理缓存,
又或者,浏览器清理掉某个资源是为了给用户访问得更频繁的其他资源腾出空间。这两
个响应头只需要用一个,其中
Expires
适用范围更广,所以应该优先使用。如果资源还
在缓存里且没有过期,那么浏览器根本不会发出
GET
请求,因而性能就得到了提升,尤
其是在移动端。
Last-Modified
/
ETag
这两个标记提供了某种版本化机制:浏览器如果需要获取某个资源,就会在实际下载
之前
先检查这些标记。它仍然会向服务器发送一个
GET
请求,但如果返回的这两个响应
头告诉浏览器这个资源并没有变更,浏览器就不会开始文件的下载 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

面向对象的思考过程(原书第5版)

面向对象的思考过程(原书第5版)

马特 魏斯费尔德
精通模块化JavaScript

精通模块化JavaScript

Nicolás Bevacqua
前端架构设计

前端架构设计

Micah Godbolt

Publisher Resources

ISBN: 9787115565099