Skip to content

Commit 500c554

Browse files
committed
Only cache 5 pages in session storage
1 parent a2b007c commit 500c554

File tree

1 file changed

+27
-6
lines changed

1 file changed

+27
-6
lines changed

resources/common.js

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -583,18 +583,39 @@ $(function() {
583583
});
584584

585585
$(window).on('beforeunload', function() {
586+
const storageLimit = 5;
587+
const sessionStorageKey = 'oj-content-keys';
588+
586589
let key = `oj-content-${window.location.href}`;
587590
let $contentClone = $('#content').clone();
588591
$contentClone.find('.select2').remove();
589592
$contentClone.find('.select2-hidden-accessible').removeClass('select2-hidden-accessible');
590593
$contentClone.find('.noUi-base').remove();
591594
$contentClone.find('.wmd-button-row').remove();
592-
sessionStorage.setItem(key, JSON.stringify({
593-
"html": $contentClone.html(),
594-
"page": window.page,
595-
"has_next_page": window.has_next_page,
596-
"scrollOffset": $(window).scrollTop(),
597-
}));
595+
596+
let contentData = JSON.stringify({
597+
"html": $contentClone.html(),
598+
"page": window.page,
599+
"has_next_page": window.has_next_page,
600+
"scrollOffset": $(window).scrollTop(),
601+
});
602+
603+
let keys = JSON.parse(sessionStorage.getItem(sessionStorageKey)) || [];
604+
605+
// Remove the existing key if it exists
606+
if (keys.includes(key)) {
607+
keys = keys.filter(k => k !== key);
608+
}
609+
610+
keys.push(key);
611+
612+
if (keys.length > storageLimit) {
613+
let oldestKey = keys.shift();
614+
sessionStorage.removeItem(oldestKey);
615+
}
616+
617+
sessionStorage.setItem(sessionStorageKey, JSON.stringify(keys));
618+
sessionStorage.setItem(key, contentData);
598619
});
599620
if (window.performance &&
600621
window.performance.navigation.type

0 commit comments

Comments
 (0)