orhun / CoolModFiles

A web player that plays some cool MOD files randomly 🎶
https://CoolModFiles.com
GNU General Public License v3.0
136 stars 12 forks source link

Page refresh message shown on first visit #56

Closed WKFO closed 1 year ago

WKFO commented 1 year ago

In Firefox 110.0, the first time you enter CMF, it shows a page refresh message on the button, not a first time welcome message.

orhun commented 1 year ago

This is cumbersome.

https://github.com/orhun/CoolModFiles/blob/2f075f1dfbdd4c121c5d5862204e9fd83078040f/pages/index.js#L42-L54

As you can see, we store the variable refresh (bool) in the local storage to check if the page is refreshed or not. So it is set to true when the page gets unloaded. The problem is it always stays as true for the next visits of the landing page.

It is set to false after a "refresh" message is shown but I assume it is not working.

Any ideas @Cthulhu2?

Cthulhu2 commented 1 year ago
  1. Hm... It seems the sessionStorage does what you want.
if (sessionStorage.getItem("refresh") === "true") {
  sessionStorage.setItem("refresh", false);
  setRandomMsg(getRandomFromArray(REFRESH_MESSAGES));
}
...
window.onbeforeunload = function () {
  sessionStorage.setItem("refresh", true);
  return;
} 
  1. Also there is the PerformanceNavigationTiming.type property with reload value in the Performance API. May be it is more standard way.
performance.getEntriesByType("navigation").forEach((entry) => {
  if (entry.type === "reload") {
    setRandomMsg(getRandomFromArray(REFRESH_MESSAGES));
  }
});
orhun commented 1 year ago

Oh yeah, using sessionStorage definitely makes sense. I haven't heard about using the 2nd option.

Do you mind going ahead and creating a PR or should I do it?

Cthulhu2 commented 1 year ago

I haven't heard about using the 2nd option

Me neither. And there are not so many usages. We'll use sessionStorage.