前端单页面应用缓存策略

5/13/2020 优化加速

在当前常用的单页面页面开发中。会将生产包发布为:固定名称的html文件和带hash的js、css文件

所以我们需要保证html必须每次都实时请求服务器,但是js和css文件可以强缓存在本地,减少请求加速显示。

nginx配置如下:

  • /、/*.htm、/*.html文件配置协商缓存
  • **/*.js、/*.css文件配置强缓存 **
	location / {
		root   html;
		index  index.html index.htm;

		if ( $request_uri ~* /((.*)\.html?)?$ ) {
			add_header Cache-Control no-cache;
		}
		if ( $request_uri ~* /.*\.(js|css)$ ) {
			add_header Cache-Control max-age=86400;
		}
    }

Loading comments...