조회수 비정상 증가 방지 로직 추가
- sessionStorage 활용 중복 증가 억제 - CSRF 토큰 기반 조회수 증가 API 호출 처리
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
(function () {
|
||||
'use strict';
|
||||
|
||||
// 조회수 비정상 증가 억제: sessionStorage 로 한 세션 내 중복 증가 방지.
|
||||
var container = document.querySelector('.inquiry-detail-container[data-inquiry-id]');
|
||||
if (!container) return;
|
||||
|
||||
var inquiryId = container.getAttribute('data-inquiry-id');
|
||||
if (!inquiryId) return;
|
||||
|
||||
var viewedKey = 'inquiry_viewed_' + inquiryId;
|
||||
if (sessionStorage.getItem(viewedKey)) {
|
||||
return; // 이미 이번 세션에서 조회 → 증가하지 않음
|
||||
}
|
||||
|
||||
function getCsrfToken() {
|
||||
// 세션 기반 CSRF: 쿠키 대신 <meta name="_csrf">에서 토큰을 읽는다.
|
||||
var meta = document.querySelector('meta[name="_csrf"]');
|
||||
return meta ? meta.getAttribute('content') : '';
|
||||
}
|
||||
|
||||
fetch('/inquiry/' + encodeURIComponent(inquiryId) + '/view', {
|
||||
method: 'POST',
|
||||
credentials: 'same-origin',
|
||||
headers: { 'X-XSRF-TOKEN': getCsrfToken() }
|
||||
})
|
||||
.then(function (res) {
|
||||
if (res.ok) {
|
||||
sessionStorage.setItem(viewedKey, 'true');
|
||||
}
|
||||
})
|
||||
.catch(function (err) {
|
||||
console.error('[inquiry-view] increment error', err);
|
||||
});
|
||||
})();
|
||||
Reference in New Issue
Block a user