[Javascript] 자바스크립트 cache 로드때 코드 갱신 쿠키삭제 캐시삭제 로드 브라우져 캐시 우회하기 관련

2021. 10. 19. 12:45업무관련

728x90
반응형

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

html 캐시삭제, 캐시방지 방법 모음

http://202psj.tistory.com/763

 

Web Storage 웹 스토로지 관련

http://202psj.tistory.com/1187

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

출처: https://stackoverflow.com/questions/15804462/how-to-clear-localstorage-sessionstorage-and-cookies-in-javascript-and-then-ret

 

how to clear localstorage,sessionStorage and cookies in javascript? and then retrieve?

How to completely clear localstorage, sessionStorage and cookies in javascript ? Is there any way one can get these values back after clearing them ?

stackoverflow.com

How to completely clear localstorage, sessionStorage and cookies in javascript ?

Is there any way one can get these values back after clearing them ?

 

how to completely clear localstorage

localStorage.clear();

how to completely clear sessionstorage

sessionStorage.clear();

[...] Cookies ?

var cookies = document.cookie; for (var i = 0; i < cookies.split(";").length; ++i) { var myCookie = cookies[i]; var pos = myCookie.indexOf("="); var name = pos > -1 ? myCookie.substr(0, pos) : myCookie; document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT"; }

is there any way to get the value back after clear these ?

No, there isn't. But you shouldn't rely on this if this is related to a security question.

 

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

출처: https://www.lesstif.com/pages/viewpage.action?pageId=20775788

 

Java Servlet

response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.
response.setHeader("Pragma", "no-cache"); // HTTP 1.0.
response.setDateHeader("Expires", 0); // Proxies.

 

 

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

 

 

모든 브라우저에서 다되는지는 아직 확인 못해보았다~!

 

 

<!-- Cache Refresh-->
<script>
var url = location.href;
var tmpUrl = url + ((/\?/).test(url) ? "&" : "?") + (new Date()).getTime();
var oReq = new XMLHttpRequest();
oReq.open("GET", tmpUrl, false); //또는 oReq.open("GET", tmpUrl, true);

oReq.send(null);

</script>

 

 

 

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

 

출처: http://gafani.tistory.com/entry/Javascript-%EB%B8%8C%EB%9D%BC%EC%9A%B0%EC%A0%B8-%EC%BA%90%EC%8B%9C-%EC%9A%B0%ED%9A%8C%ED%95%98%EA%B8%B0

 

[Javascript] 브라우져 캐시 우회하기

XMLHttpRequest 객체를 이용해 서버와 AJAX 통신을 하던 도중, 다음과 같은 문제가 발생하였습니다.  - A.js 파일을 수정한 후 해당 파일을 비동기로 로드했지만, 수정되지 않은채 로드가 되었습니다.

gafani.tistory.com

XMLHttpRequest 객체를 이용해 서버와 AJAX 통신을 하던 도중, 다음과 같은 문제가 발생하였습니다.

 

 - A.js 파일을 수정한 후 해당 파일을 비동기로 로드했지만, 수정되지 않은채 로드가 되었습니다.

 

구글링 통해 알아본 결과 브라우져에서 캐싱하는듯 합니다. 브라우져에서 캐시를 삭제한 후 다시 로드하니 수정된 내용이 로드되었습니다.

 

조금 더 구글링 해본 결과 Mozilla 사이트에서 해당 내용을 우회할 수 있는 법이 있었습니다. (원문 바로 가기)

 

내용을 보아 하니 캐시를 우회하는 법은 요청하려는 URL 뒤에 timestamp를 붙여주는 것입니다. "?" 또는 "&" 말이지요.

 

샘플을 보시면:

 

http://foo.com/bar.html -> http://foo.com/bar.html?12345 http://foo.com/bar.html?foobar=baz -> http://foo.com/bar.html?foobar=baz&12345

이렇게 말이죠. 

 

하지만 하드코딩하지 않는 이상, 매번 저렇게 하기엔 동적이지 않아 다음처럼 자동으로 URL을 만들어 줄 수 있다고 하네요:

 

var oReq = new XMLHttpRequest(); oReq.open("GET", url + ((/\?/).test(url) ? "&" : "?") + (new Date()).getTime()); oReq.send(null);

 

이렇게 작성 하시면, 호출 할 때마다 현재 timestamp를 URL 뒤에 "?" 또는 "&" 자동으로 붙여 줍니다.



출처: http://gafani.tistory.com/entry/Javascript-브라우져-캐시-우회하기 [Sanctuary]

728x90
반응형