diff --git a/src/main/resources/static/js/djb/inquiry-view.js b/src/main/resources/static/js/djb/inquiry-view.js new file mode 100644 index 0000000..32f093d --- /dev/null +++ b/src/main/resources/static/js/djb/inquiry-view.js @@ -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: 쿠키 대신 에서 토큰을 읽는다. + 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); + }); +})();