Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Need to clear browser cache after creating admin #613

Closed
gramakri opened this issue Nov 5, 2020 · 21 comments · Fixed by #854
Closed

Need to clear browser cache after creating admin #613

gramakri opened this issue Nov 5, 2020 · 21 comments · Fixed by #854

Comments

@gramakri
Copy link

gramakri commented Nov 5, 2020

I am creating a navidrome package for Cloudron (at https://git.cloudron.io/cloudron/navidrome-app). I have noticed a peculiar behavior:

  • Install navidrome. Create admin user.
  • Logout
  • I see login screen.
  • Reload navidrome in browser.
  • The UI keeps asking me to create admin user.
    image
  • Trying to create admin user will say "Error: Forbidden"

If I clear the cache, it shows login screen.

@gramakri
Copy link
Author

gramakri commented Nov 5, 2020

The bug is reproducible with chrome and firefox. Also, I can see that the admin user exists in the sqlite database.

sqlite> select * from user; 
a9762f97-8750-4c86-8067-8cd93190243b|admin|Admin||****|1|2020-11-05 00:19:26||2020-11-05 00:19:26|2020-11-05T00:19:26.177282093Z

@deluan
Copy link
Member

deluan commented Nov 5, 2020

Hey, thanks for creating the package!

I can't seem to reproduce the issue here (using Chrome on macOS), but it is happening with Firefox. This used to happen in very early versions, but it's supposed to be fixed since April. I'll investigate a little more and let you know. Thanks!

@gramakri
Copy link
Author

gramakri commented Nov 5, 2020

@deluan After a bit of debugging, I found it's something to do with service worker. If you go to Firefox/Chrome, Dev tools-> Application -> Service Worker -> Unregister and then reload the page, it starts working again.

Also, I can confirm with curl that firstTime is set to false correctly in app config. So it's a frontend issue.

@deluan
Copy link
Member

deluan commented Nov 5, 2020

Thanks for the information. The service worker was introduced recently, when we added PWA capabilities to Navidrome. Will have to figure out how to handle this situation. Seems that it caches the index.html by default.

@certuna
Copy link
Contributor

certuna commented Nov 6, 2020

We're not the only ones: facebook/create-react-app#7700

Apparently you can exclude index.html from the serviceworker cache, but that means no offline-mode for the PWA. Which for ND is not a problem I guess, it has no offline functionality (yet).

@deluan
Copy link
Member

deluan commented Nov 6, 2020

Thanks for the digging, @certuna

It seems that most solutions on the links above will require to "eject" the CRA app, meaning all build and config code will be added to Navidrome's codebase, and will make it impossible to upgrade CRA in the future....

But there is hope: facebook/create-react-app#3171 (comment)
Will try this later this weekend.

@certuna
Copy link
Contributor

certuna commented Nov 7, 2020

That would probably also fix the issue I've just encountered: even though I've updated my server twice (0.36 and 0.37) since I installed the PWA on my iPhone, it still says 0.35 in Menu-About, i.e. it has cached the assets on install and isn't refreshing them, even when I tap the 'refresh' icon.

@deluan
Copy link
Member

deluan commented Nov 7, 2020

@certuna The "refresh" button is used to refresh the data in the current view (ex: list of albums), not the app itself. It is not equivalent to the browser's refresh button.

Regarding the main issue here, the library used by the link I posted above (sw-precache) is deprecated.... and the new version of CRA removed the provided service worker due to too many problems with them without proper configuration

We will need more time to properly configure a service worker for Navidrome and not rely on the default provided by CRA. This will require a bit more work (I never used service workers before), so I thin kthe sensible thing to do for now is to introduce a config option to enable the service worker, and it would be disabled by default.

Thoughts?

@deluan
Copy link
Member

deluan commented Nov 10, 2020

Found another, possibly simpler and more elegant solution: https://felixgerschau.com/create-a-pwa-update-notification-with-create-react-app/

Will try it this week.

@certuna
Copy link
Contributor

certuna commented Nov 10, 2020

That's a great find, looks like the best thing to use. Maybe also use the "refresh" button on the top right to not just reload dynamic content, but also force a reload of the static files at the same time?

@obbardc
Copy link

obbardc commented Nov 17, 2020

I had the same issue when upgrading to a new version using docker on develop branch: the web ui asked me to create a new admin user even though I already have an admin user. Eventually it let me login though.

@deluan
Copy link
Member

deluan commented Nov 17, 2020

For those having this issue, another workaround for now is to Force Reload the UI (Shift+Control+R)

@certuna
Copy link
Contributor

certuna commented Nov 20, 2020

Shift-Control-R not so effective on phone or smart TV unfortunately...

Can we add a quick "clear the serviceworker cache" snippet to the code triggered by the Refresh button top right, like this (seems to do the trick when I run it in the chrome console):

caches.keys().then(function(names) {
    for (let name of names)
        caches.delete(name);
});

(or alternatively, run this snippet when clicking "Logout"). Would least mitigate the problem of the PWA not refreshing its assets after a server upgrade.

@deluan
Copy link
Member

deluan commented Nov 20, 2020

Nice finding, will take a look at adding this. I tried the solution from the post I linked, but it is not as straightforward as it seems. Still have to figure out some loose ends

@TheLonelinessOfHS
Copy link

Hi, same issue here. Hope you can fix this. Thanks.

@deluan
Copy link
Member

deluan commented Feb 10, 2021

Another possible solution to reload assets when a new version is deployed: https://deanhume.com/displaying-a-new-version-available-progressive-web-app/ (thanks @certuna)

@certuna
Copy link
Contributor

certuna commented Mar 17, 2021

I think my PR fixes it, please test. This doesn't do any fancy things like check for new versions or reload assets, it just solves the initially reported bug: it clears all service worker caches on logout.

Also, name of this issue should probably be changed to: Need to clear service worker cache after creating admin

deluan added a commit that referenced this issue Mar 21, 2021
* Update authProvider.js

This fixes #613 : clears the ServiceWorker cache on logout. Based on this code: https://developer.mozilla.org/en-US/docs/Web/API/CacheStorage/keys . Seems to work on macOS and iOS, but please test on other platforms.

* Format code with `prettier`

Co-authored-by: Deluan <deluan@navidrome.org>
@deluan deluan reopened this Mar 21, 2021
@deluan
Copy link
Member

deluan commented Mar 21, 2021

I'll keep the issue open until I test it thoroughly. Thanks @certuna !

@deluan
Copy link
Member

deluan commented Mar 21, 2021

I can confirm this is fixed!! Thanks again @certuna ! Now we have to come-up with the fancy "There's a new version available, please refresh" logic :)

@deluan deluan closed this as completed Mar 21, 2021
@certuna
Copy link
Contributor

certuna commented Mar 22, 2021

I'll make a new issue for that: #875

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 12, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants