fix rating and fav

This commit is contained in:
2026-08-13 21:14:20 +02:00
parent 402a043b57
commit 9ec07e8e6c
9 changed files with 53 additions and 17 deletions

View File

@@ -4145,6 +4145,7 @@ window.cancelAnimFrame = (function () {
.then(r => r.json())
.then(data => {
if (data.success) {
if (window.invalidateItemCache) window.invalidateItemCache(id);
const newOc = data.is_oc;
ocBtn.dataset.isOc = newOc;
ocBtn.setAttribute('title', newOc ? 'Remove OC status' : 'Mark as OC');
@@ -4486,7 +4487,13 @@ window.cancelAnimFrame = (function () {
.then(r => r.json())
.then(res => {
toggleBtn._activeRequestsCount = Math.max(0, (toggleBtn._activeRequestsCount || 1) - 1);
// Evict cached item HTML immediately on success so navigating back fetches fresh content,
// even if the user already navigated to a different item while this request was in flight.
if (res && res.success && window.invalidateItemCache) {
window.invalidateItemCache(postid);
}
// Verify we are still on the same post (prevent dynamic/PJAX page leaks)
const currentIdStr = window.getCurrentItemId();
const currentPostId = currentIdStr ? parseInt(currentIdStr, 10) : null;
@@ -4502,10 +4509,6 @@ window.cancelAnimFrame = (function () {
if (window.renderTags) {
window.renderTags(res.tags);
}
// Evict the cached item HTML so navigating back fetches fresh content
if (window.invalidateItemCache) {
window.invalidateItemCache(postid);
}
} else {
revert();
window.flashMessage('Error: ' + (res.msg || 'Failed to update rating'), 3000, 'error');
@@ -7148,14 +7151,21 @@ window.cancelAnimFrame = (function () {
window.loadPageAjax = loadPageAjax;
window.loadItemAjax = loadItemAjax;
// Invalidate all itemCacheMap entries that contain a given item ID.
// Call this after mutating an item (e.g. adding a tag) so the next
// Invalidate all itemCacheMap entries that match a given item ID or slug.
// Call this after mutating an item (e.g. rating, favorite, tags, OC) so the next
// AJAX navigation fetches fresh HTML instead of serving stale cache.
window.invalidateItemCache = (itemId) => {
if (!itemId) return;
const needle = `/${itemId}`;
for (const key of itemCacheMap.keys()) {
if (key.includes(needle)) {
if (itemId === undefined || itemId === null || itemId === '') return;
const strId = String(itemId).trim();
for (const [key, val] of itemCacheMap.entries()) {
const urlPath = key.split('?')[0];
const segments = urlPath.split('/').filter(Boolean);
const lastSegment = segments[segments.length - 1];
if (lastSegment === strId) {
itemCacheMap.delete(key);
continue;
}
if (val && typeof val.html === 'string' && val.html.includes(`data-item-id="${strId}"`)) {
itemCacheMap.delete(key);
}
}
@@ -8697,6 +8707,10 @@ class NotificationSystem {
return;
}
if (window.invalidateItemCache) {
window.invalidateItemCache(data.item_id);
}
// Check if we are currently viewing this item
const currentIdStr = window.getCurrentItemId();
const currentId = currentIdStr ? parseInt(currentIdStr, 10) : null;
@@ -8780,6 +8794,10 @@ class NotificationSystem {
handleFavoritesUpdate(data) {
if (!data || !data.item_id) return;
if (window.invalidateItemCache) {
window.invalidateItemCache(data.item_id);
}
// Check if we are currently viewing this item
const currentIdStr = window.getCurrentItemId();
if (!currentIdStr || parseInt(currentIdStr, 10) !== parseInt(data.item_id, 10)) return;