How to ask for persistent storage permission in Google Chrome
TL;DR: You can’t. Google Chrome is made by a bunch of morons who think they know better than anyone what you want to do.
Firefox’s way
Here is how it works on Mozilla’s Firefox:
- You ask for the permission:
navigator.storage.persist()
… and it’s not granted because it was not generated by a user input. This is to prevent annoying behavior from websites asking for permissions without explaining why and can be disrupting the site usage. - You add a button to your page:
<button onclick="navigator.storage.persist()">Enable persistent storage</button>
. This way you can explain to the user why the site/app needs persistent storage, if it’s optional or not. The user clicks, Firefox shows a dialog asking for the permission to be granted, the user decides to grant or not the permission. If the user grants it, then the permission is granted.
Quite simple, straightforward and makes sense. The user is in control of what he wants to do.
Chrome’s way
Now here is what happens on Google Chrome:
- You ask for the permission:
navigator.storage.persist()
… and it’s not granted. - You add a button to your page:
<button onclick="navigator.storage.persist()">Enable persistent storage</button>
. The user clicks, and the permission is not granted. No dialog is shown.
What the hell?
Checking navigator.permissions.query({name:'persistent-storage'}).then((result) => console.log('result', result))
will show state: 'prompt'
…
Yeah right, where is the prompt then?
Basically, Google Chrome decides if it thinks your site/app is worthy of persistent storage or not.
In various places is said what the criteria are, but I haven’t found an official Google-authored page stating it. Usually something like that:
- How high is the level of site engagement?
- Has the site been installed?
- Has the site been bookmarked?
- Has the site been granted permission to show notifications?
Given the first point is not actionable (bullshit value with no definition, so no way to measure it… something that does not have its place in any software API), we can try the others:
- I bookmarked my app, the Persistent Storage permission was not granted;
- I installed my app, the Persistent Storage permission was not granted;
- I granted permission to show notifications, the Persistent Storage permission was not granted.
Maybe it’s another sneaky way from Google to:
- make developers use Chrome-only APIs so people can only use Google Chrome spyware;
- make developers not store user’s data on user’s devices but on “the cloud”, preferably on Google servers;
- make developers not create PWAs but create Android apps instead, so Google can tax them, force them to follow Google’s own agenda, and steal user’s data more easily.
It looks like a privacy-respecting local-only PWA is not something Google wants.
Anyway, the message is quite clear, what Googlers think of “normal” people: 🖕
Let them be assured that the feeling is mutual.