From c17b4e29ff7f148bf4921f0e8779612f82578853 Mon Sep 17 00:00:00 2001 From: Ludy Date: Sun, 12 Jan 2025 14:46:46 +0100 Subject: [PATCH] cache the release notes in the browser session (#2673) # Description Please provide a summary of the changes, including relevant motivation and context. Closes https://github.com/Stirling-Tools/Stirling-PDF/security/code-scanning/164 ## Checklist - [x] I have read the [Contribution Guidelines](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/CONTRIBUTING.md) - [x] I have performed a self-review of my own code - [ ] I have attached images of the change if it is UI based - [ ] I have commented my code, particularly in hard-to-understand areas - [ ] If my code has heavily changed functionality I have updated relevant docs on [Stirling-PDFs doc repo](https://github.com/Stirling-Tools/Stirling-Tools.github.io/blob/main/docs/) - [x] My changes generate no new warnings - [ ] I have read the section [Add New Translation Tags](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/HowToAddNewLanguage.md#add-new-translation-tags) (for new translation tags only) --- src/main/resources/templates/releases.html | 50 +++++++++++++++++++--- 1 file changed, 44 insertions(+), 6 deletions(-) diff --git a/src/main/resources/templates/releases.html b/src/main/resources/templates/releases.html index a00f4d4e..c8e6373d 100644 --- a/src/main/resources/templates/releases.html +++ b/src/main/resources/templates/releases.html @@ -274,9 +274,31 @@ function formatText(text) { const container = document.createElement('div'); + if (!text || typeof text !== 'string') { + console.error('Invalid input to formatText:', text); + return container; + } + + let textWithoutComments; + try { + textWithoutComments = text.replace( + //, + '' + ); + } catch (error) { + console.error('Error in replace operation:', error); + return container; + } + // Split the text into lines - const textWithoutComments = text.replace(//g, ''); - const lines = textWithoutComments.split('\n'); + let lines; + try { + lines = textWithoutComments.split('\n'); + } catch (error) { + console.error('Error in split operation:', error); + return container; + } + let currentList = null; lines.forEach(line => { @@ -390,10 +412,26 @@ async function loadReleases() { while (container.firstChild) { container.removeChild(container.firstChild); } + const cachedReleases = sessionStorage.getItem('releases'); - const response = await fetch(GITHUB_API + '/releases'); - if (!response.ok) throw new Error('Failed to fetch releases'); - const releases = await response.json(); + let releases; + if (cachedReleases) { + releases = JSON.parse(cachedReleases); + console.log("Read from storage"); + } else { + const response = await fetch(GITHUB_API + '/releases'); + if (!response.ok) { + if (response.status === 403) { + throw new Error('API rate limit exceeded'); + } + if (response.status === 404) { + throw new Error('Repository not found'); + } + throw new Error('Failed to fetch releases'); + } + releases = await response.json(); + sessionStorage.setItem('releases', JSON.stringify(releases)); + } // Sort releases by version number (descending) releases.sort((a, b) => compareVersions(b.tag_name, a.tag_name)); @@ -470,4 +508,4 @@ async function loadReleases() { /*]]>*/ - \ No newline at end of file +