前端静态资源缓存的策略:
springmvc 可以添加HTTP缓存头到response中,保存对静态文件的缓存.
Expires策略:
- Expires是Web服务器响应消息头字段,在响应http请求时告诉浏览器在过期时间前浏览器可以直接从浏览器缓存取数据,而无需再次请求。不过Expires 是HTTP 1.0的东西,现在默认浏览器均默认使用HTTP 1.1,所以它的作用基本忽略。Expires 的一个缺点就是,返回的到期时间是服务器端的时间,这样存在一个问题,如果客户端的时间与服务器的时间相差很大(比如时钟不同步,或者跨时区),那么误差就很大,所以在HTTP 1.1版开始,使用Cache-Control: max-age=秒替代。
- Cache-control策略(重点关注):Cache-Control与Expires的作用一致,都是指明当前资源的有效期,控制浏览器是否直接从浏览器缓存取数据还是重新发请求到服务器取数据。只不过Cache-Control的选择更多,设置更细致,如果同时设置的话,其优先级高于Expires。
值可以是public、private、no-cache、no- store、no-transform、must-revalidate、proxy-revalidate、max-age 各个消息中的指令含义如下: Public指示响应可被任何缓存区缓存。 Private指示对于单个用户的整个或部分响应消息,不能被共享缓存处理。这允许服务器仅仅描述当用户的部分响应消息,此响应消息对于其他用户的请求无效。 no-cache指示请求或响应消息不能缓存,该选项并不是说可以设置”不缓存“,容易望文生义~ no-store用于防止重要的信息被无意的发布。在请求消息中发送将使得请求和响应消息都不使用缓存,完全不存下來。 max-age指示客户机可以接收生存期不大于指定时间(以秒为单位)的响应。 min-fresh指示客户机可以接收响应时间小于当前时间加上指定时间的响应。 max-stale指示客户机可以接收超出超时期间的响应消息。如果指定max-stale消息的值,那么客户机可以接收超出超时期指定值之内的响应消息。
以上内容重点参考博客:https://my.oschina.net/leejun2005/blog/369148
基于模板技术的缓存策略:
thymeleaf模板:有配置项,默认是开发缓存的.缓存的是模板编译后的静态文件.
springboot中的静态文件处理:
27.1.5 静态内容
默认情况下,Spring Boot静态文件的查找路径为类路径下的/static(或者/public 或者/public 或者/resources或者 /META-INF/resources ) .It uses the ResourceHttpRequestHandler
from Spring MVC so you can modify that behavior by adding your own WebMvcConfigurerAdapter
and overriding the addResourceHandlers
method.
In a stand-alone web application the default servlet from the container is also enabled, and acts as a fallback, serving content from the root of the ServletContext
if Spring decides not to handle it. Most of the time this will not happen (unless you modify the default MVC configuration) because Spring will always be able to handle requests through the DispatcherServlet
.
默认情况下,对静态文件的请求拦截为/**,可以通过spring.mvc.static-path-pattern来修改,例如:映射所有的静态资源到/resources/**目录下,配置如下:
spring.mvc.static-path-pattern
=/resources/**
还可以通过配置spring.resources.static-locations
来指定静态文件的路径.. If you do this the default welcome page detection will switch to your custom locations, so if there is an index.html
in any of your locations on startup, it will be the home page of the application.
In addition to the ‘standard’ static resource locations above, a special case is made for Webjars content. Any resources with a path in /webjars/**
will be served from jar files if they are packaged in the Webjars format.
![]() |
---|
Do not use the src/main/webapp directory if your application will be packaged as a jar. Although this directory is a common standard, it will only work with war packaging and it will be silently ignored by most build tools if you generate a jar. |
Spring Boot also supports advanced resource handling features provided by Spring MVC, allowing use cases such as cache busting static resources or using version agnostic URLs for Webjars.
To use version agnostic URLs for Webjars, simply add the webjars-locator
dependency. Then declare your Webjar, taking jQuery for example, as "/webjars/jquery/dist/jquery.min.js"
which results in "/webjars/jquery/x.y.z/dist/jquery.min.js"
where x.y.z
is the Webjar version.
![]() |
---|
If you are using JBoss, you’ll need to declare the webjars-locator-jboss-vfs dependency instead of the webjars-locator ; otherwise all Webjars resolve as a 404 . |
To use cache busting, the following configuration will configure a cache busting solution for all static resources, effectively adding a content hash in URLs, such as <link href="/css/spring-2a2d595e6ed9a0b24f027f2b63b134d6.css"/>
:
spring.resources.chain.strategy.content.enabled
=true
spring.resources.chain.strategy.content.paths
=/**
![]() |
---|
Links to resources are rewritten at runtime in template, thanks to a ResourceUrlEncodingFilter , auto-configured for Thymeleaf and FreeMarker. You should manually declare this filter when using JSPs. Other template engines aren’t automatically supported right now, but can be with custom template macros/helpers and the use of the ResourceUrlProvider . |
When loading resources dynamically with, for example, a JavaScript module loader, renaming files is not an option. That’s why other strategies are also supported and can be combined. A "fixed" strategy will add a static version string in the URL, without changing the file name:
spring.resources.chain.strategy.content.enabled
=true
spring.resources.chain.strategy.content.paths
=/**
spring.resources.chain.strategy.fixed.enabled
=true
spring.resources.chain.strategy.fixed.paths
=/js/lib/
spring.resources.chain.strategy.fixed.version
=v12
With this configuration, JavaScript modules located under "/js/lib/"
will use a fixed versioning strategy "/v12/js/lib/mymodule.js"
while other resources will still use the content one <link href="/css/spring-2a2d595e6ed9a0b24f027f2b63b134d6.css"/>
.
See ResourceProperties
for more of the supported options.