feat: use StaleWhileRevalidate for scripts poloicy; v1.104.2

This commit is contained in:
Azgaar 2024-09-20 13:04:47 +02:00
parent d3ba6dd95b
commit 3d1f268003
5 changed files with 33 additions and 40 deletions

View file

@ -1990,8 +1990,8 @@
</button> </button>
<button <button
id="optionsReset" id="optionsReset"
data-tip="Click to restore default options (page will be reloaded)" data-tip="Click to restore default options and reload the page"
onclick="restoreDefaultOptions()" onclick="cleanupData()"
> >
Reset to defaults Reset to defaults
</button> </button>

View file

@ -666,10 +666,7 @@ async function generate(options) {
title: "Generation error", title: "Generation error",
width: "32em", width: "32em",
buttons: { buttons: {
"Clear data": function () { "Cleanup data": cleanupData,
localStorage.clear();
localStorage.setItem("version", VERSION);
},
Regenerate: function () { Regenerate: function () {
regenerateMap("generation error"); regenerateMap("generation error");
$(this).dialog("close"); $(this).dialog("close");

View file

@ -703,12 +703,6 @@ async function openTemplateSelectionDialog() {
HeightmapSelectionDialog.open(); HeightmapSelectionDialog.open();
} }
// remove all saved data from LocalStorage and reload the page
function restoreDefaultOptions() {
localStorage.clear();
location.reload();
}
// Sticked menu Options listeners // Sticked menu Options listeners
byId("sticked").addEventListener("click", function (event) { byId("sticked").addEventListener("click", function (event) {
const id = event.target.id; const id = event.target.id;

9
sw.js
View file

@ -1,7 +1,7 @@
importScripts("https://storage.googleapis.com/workbox-cdn/releases/6.2.0/workbox-sw.js"); importScripts("https://storage.googleapis.com/workbox-cdn/releases/6.2.0/workbox-sw.js");
const {Route, registerRoute} = workbox.routing; const {Route, registerRoute} = workbox.routing;
const {CacheFirst, NetworkFirst} = workbox.strategies; const {CacheFirst, NetworkFirst, StaleWhileRevalidate} = workbox.strategies;
const {CacheableResponsePlugin} = workbox.cacheableResponse; const {CacheableResponsePlugin} = workbox.cacheableResponse;
const {ExpirationPlugin} = workbox.expiration; const {ExpirationPlugin} = workbox.expiration;
@ -18,8 +18,11 @@ registerRoute(
registerRoute( registerRoute(
({request, url}) => ({request, url}) =>
request.destination === "script" && !url.pathname.endsWith("min.js") && !url.pathname.includes("versioning.js"), request.destination === "script" &&
new CacheFirst({ !url.pathname.endsWith("min.js") &&
!url.pathname.includes("versioning.js") &&
!url.contains("google"),
new StaleWhileRevalidate({
cacheName: "fmg-scripts", cacheName: "fmg-scripts",
plugins: [ plugins: [
new CacheableResponsePlugin({statuses: [0, 200]}), new CacheableResponsePlugin({statuses: [0, 200]}),

View file

@ -12,7 +12,7 @@
* *
* Example: 1.102.2 -> Major version 1, Minor version 102, Patch version 2 * Example: 1.102.2 -> Major version 1, Minor version 102, Patch version 2
*/ */
const VERSION = "1.104.1"; const VERSION = "1.104.2";
if (parseMapVersion(VERSION) !== VERSION) alert("versioning.js: Invalid format or parsing function"); if (parseMapVersion(VERSION) !== VERSION) alert("versioning.js: Invalid format or parsing function");
{ {
@ -21,7 +21,9 @@ if (parseMapVersion(VERSION) !== VERSION) alert("versioning.js: Invalid format o
if (loadingScreenVersion) loadingScreenVersion.innerText = `v${VERSION}`; if (loadingScreenVersion) loadingScreenVersion.innerText = `v${VERSION}`;
const storedVersion = localStorage.getItem("version"); const storedVersion = localStorage.getItem("version");
if (compareVersions(storedVersion, VERSION, {patch: false}).isOlder) setTimeout(showUpdateWindow, 6000); if (compareVersions(storedVersion, VERSION, {major: true, minor: true, patch: false}).isOlder) {
setTimeout(showUpdateWindow, 6000);
}
function showUpdateWindow() { function showUpdateWindow() {
const changelog = "https://github.com/Azgaar/Fantasy-Map-Generator/wiki/Changelog"; const changelog = "https://github.com/Azgaar/Fantasy-Map-Generator/wiki/Changelog";
@ -34,7 +36,7 @@ if (parseMapVersion(VERSION) !== VERSION) alert("versioning.js: Invalid format o
<ul> <ul>
<strong>Latest changes:</strong> <strong>Latest changes:</strong>
<li>Style: ability to set letter spacing</li> <li>Labels: ability to set letter spacing</li>
<li>Zones update</li> <li>Zones update</li>
<li>Notes Editor: on-demand AI text generation</li> <li>Notes Editor: on-demand AI text generation</li>
<li>New style preset: Dark Seas</li> <li>New style preset: Dark Seas</li>
@ -44,42 +46,39 @@ if (parseMapVersion(VERSION) !== VERSION) alert("versioning.js: Invalid format o
<li>Preview villages map</li> <li>Preview villages map</li>
<li>Ability to render ocean heightmap</li> <li>Ability to render ocean heightmap</li>
<li>Scale bar styling features</li> <li>Scale bar styling features</li>
<li>Vignette visual layer and vignette styling options</li>
</ul> </ul>
<p>Join our <a href="${discord}" target="_blank">Discord server</a> and <a href="${reddit}" target="_blank">Reddit community</a> to ask questions, share maps, discuss the Generator and Worlbuilding, report bugs and propose new features.</p> <p>Join our <a href="${discord}" target="_blank">Discord server</a> and <a href="${reddit}" target="_blank">Reddit community</a> to ask questions, share maps, discuss the Generator and Worlbuilding, report bugs and propose new features.</p>
<span><i>Thanks for all supporters on <a href="${patreon}" target="_blank">Patreon</a>!</i></span>`; <span><i>Thanks for all supporters on <a href="${patreon}" target="_blank">Patreon</a>!</i></span>`;
const buttons = {
Ok: function () {
$(this).dialog("close");
localStorage.setItem("version", VERSION);
}
};
if (storedVersion) {
buttons.Cleanup = () => {
clearCache();
localStorage.clear();
localStorage.setItem("version", VERSION);
location.reload();
};
}
$("#alert").dialog({ $("#alert").dialog({
resizable: false, resizable: false,
title: "Fantasy Map Generator update", title: "Fantasy Map Generator update",
width: "28em", width: "28em",
position: {my: "center center-4em", at: "center", of: "svg"}, position: {my: "center center-4em", at: "center", of: "svg"},
buttons buttons: {
"Cleanup data": () => cleanupData(),
"Don't show again": function () {
$(this).dialog("close");
localStorage.setItem("version", VERSION);
}
}
}); });
} }
}
async function cleanupData() {
await clearCache();
localStorage.clear();
localStorage.setItem("version", VERSION);
localStorage.setItem("disable_click_arrow_tooltip", "true");
location.reload();
}
async function clearCache() { async function clearCache() {
const cacheNames = await caches.keys(); const cacheNames = await caches.keys();
return Promise.all(cacheNames.map(cacheName => caches.delete(cacheName))); return Promise.all(cacheNames.map(cacheName => caches.delete(cacheName)));
} }
}
function parseMapVersion(version) { function parseMapVersion(version) {
let [major, minor, patch] = version.split("."); let [major, minor, patch] = version.split(".");