Dev/Js.css.Jsp

[web] 웹의 정적 리소스의 버전 명시를 통한 캐시 전략

pu3vig 2022. 9. 21. 16:59
728x90
  • target:  정적 리소스 버전 명시를 통한 캐시 무시

 


  • method: 
<!-- 일반적인 방식 -->
<link rel="stylesheet" type="text/css" href="/common/css/common.css?v=20120902"/>
<!-- css 내부 -->
body {url('/my_bg.gif');}


<!-- 개선방식(Fixed Version Strategy) -->
<link rel="stylesheet" type="text/css" href="/resources-20120902/common/css/common.css"/>
<!-- css 내부 -->
body {url('../../my_bg.gif');}


<!-- 개선방식(ContentVersion Strategy) -->
<link rel="stylesheet" type="text/css" href="/common/css/common-89b08a8a902m2947a9d0a.css"/>

 


  • desc: 

*자세한 설명은 게시글 가장 아래 source에 해당하는 원글을 참고

*아래 내용은 읽고 이해한대로 요약 정리한 내용(source 1. 참고)


1. Cache-Control(source 7. 참고) 관련

- 일반적으로 정적리소스 캐싱 방지를 위하여 아래와 같이 설정

<META HTTP-EQUIV="Cache-Control" CONTENT="no-cache">
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<META HTTP-EQUIV="Expires" CONTENT="-1">

 

2. 정적자원 캐시 수명 관련

- RFC 2068(source 2. 참고) 에서는 모든 Contents의 캐시 수명을 최대 1년으로 설정하는 가이드라인이 존재

- 따라서 캐시 별도의 캐시 수명은 최대 1년(31,536,000초 = 365일)까지 설정가능

- 캐시 수명은 Cache-Control을 통해 설정

<META HTTP-EQUIV="Cache-Control" CONTENT="max-age=31536000">

- 캐시 수명이 설정되는 경우, 재배포등을 진행하여도, disk cache(source 3. 참고)를 사용하기 때문에, 사용자 웹 브라우저에서는 변경된 소스로 갱신되지 않는 현상 확인

 

 

3. Spring에서의 Version Strategy

 3.1) Fixed Version Strategy(source 4. 참고)

 - Apache 설정(source 6. 참고)을 통해서 앞의 경로가 바뀌어도 항상 지정된 파일을 읽도록 설정

<!-- .jsp에서 설정(resources-20120902 부분은 페이지 재배포시 변경) -->
<link rel="stylesheet" type="text/css" href="/resources-20120902/common/css/common.css"/>

<!-- 실제 서버상에 배포한 경로가 /common/css/common.css 인 경우, httpd.conf에서 mod_rewrite.so 모듈을 활성화하고, RewriteRule을 작성 -->

<!-- 또는 jstl을 사용하여 적용 -->
<link rel="stylesheet" type="text/css" href="${url:resource('/common/css/common.css')}"/>

- Springboot 기준 application.yml에 아래 설정 추가

chain:
  strategy:
    fixed:
      enabled: true
      version: 20120902

 

 3.2) ContentVersion Strategy(source 5. 참고)

 - 리소스 파일을 MD5로 변환하여 리소스 파일명 사이에 -${MD5}를 추가

<!-- common.js 내용을 MD5로 변환하여 파일명을 common-${MD5 HashCode}.js 로 변경 -->
<link rel="stylesheet" type="text/css" href="/common/css/common-89b08a8a902m2947a9d0a.css"/>

 

- Springboot 기준 application.yml에 아래 설정 추가

chain:
  strategy:
    content:
      enabled: true

 


  • source:

1. 작성시 참고 게시글

http://kwon37xi.egloos.com/4735742

 

웹의 정적 리소스의 버전 명시를 통한 캐시 전략

나는 언제부터인가 웹의 정적 리소스(*.js,*.css, *.jpg,...) 들은 항상 1년간 캐시하도록 설정을하고 있다.이렇게 할 경우 일단 한 번 읽은 리소스는 다시 읽지 않기 때문에 대역폭을 아끼고 성능을

kwon37xi.egloos.com

https://adunhansa.tistory.com/259

 

http와 스프링 (4) : Cache Control 과 정적자원들의 관리

쉬어가기.. 과거의 기록 리마인드 우려먹기(^0^) 시리즈 목차 https://adunhansa.tistory.com/261 http와 스프링(0) - 연재를 시작하며.. 운 좋게, 좋은 스터디원분들과 좋은 책을 만나 HTTP 스터디를 시작하며

adunhansa.tistory.com

 

2. RFC 2068 캐시 수명 최대 1년 관련 정보

https://www.rfc-editor.org/rfc/rfc2068#section-14.21

 

RFC 2068: Hypertext Transfer Protocol -- HTTP/1.1

 

www.rfc-editor.org

 

3. 브라우저 캐시 기능(Disk Cache / Memory Cache) 관련

https://mr-zero.tistory.com/195

 

[자료] cache-control 옵션

출처 : http://www.joy24.net/101 브라우저 캐시의 필요성 브라우저 캐시는 클라이언트 시스템에 있는 디스크 또는 메모리를 이용하기 때문에 클라이언트 캐시(client cache)라고도 합니다. 한 번 접속했

mr-zero.tistory.com

 

4. Fixed Version Strategy

https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/servlet/resource/FixedVersionStrategy.html

 

FixedVersionStrategy (Spring Framework 5.3.23 API)

A VersionStrategy that relies on a fixed version applied as a request path prefix, e.g. reduced SHA, version name, release date, etc. This is useful for example when ContentVersionStrategy cannot be used such as when using JavaScript module loaders which a

docs.spring.io

https://www.mscharhag.com/spring/resource-versioning-with-spring-mvc?fbclid=IwAR18kGPacCOEzABAbuqWANzxxEs2-QseXhaH2pohDDn4fuvb8mfeEOGwOUg 

 

Spring MVC resource versioning with ContentVersionStrategy

When serving static resources, it is common practice to append some kind of version information to the resource URL. This allows the browser to cache resources for an unlimited time. Whenever the content of the resource is changed, the version information

www.mscharhag.com

 

5. ContentVersion Strategy

https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/servlet/resource/ContentVersionStrategy.html

 

ContentVersionStrategy (Spring Framework 5.3.23 API)

 

docs.spring.io

 

6. mod_rewrite.so를 이용한 RewriteRule 옵션 설정 (Apache 설정)

https://httpd.apache.org/docs/2.2/ko/misc/rewriteguide.html

 

URL 재작성 지침서 - Apache HTTP Server Version 2.2

Please note This document refers to the 2.2 version of Apache httpd, which is no longer maintained. The active release is documented here. If you have not already upgraded, please follow this link for more information. You may follow this link to go to the

httpd.apache.org

 

7. Cache-Control 관련

https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control

 

Cache-Control - HTTP | MDN

The Cache-Control HTTP header field holds directives (instructions) — in both requests and responses — that control caching in browsers and shared caches (e.g. Proxies, CDNs).

developer.mozilla.org

 

 

 

 

 

 

 

 

 

728x90