Writing to Browser API localStorage

Is it possible to store data in localStorage of Browser API before loading a URL?

“Possible”? Sure. “Advisable”? I’d say “probably not”. localStorage is what they call in poker a “dominated hand”. Whatever you’re trying to do with it, there is a better way. If you’re trying to communicate amongst various parts of your app, use whatever’s idiomatic for your framework. For Angular, that would be a mutually injected service. If you’re trying to persist data across runs of your app, use Ionic Storage or Native Storage, because they have better persistence guarantees than localStorage does.

I want to store a user token for other url to read at loading time when opening it with Capacitor’s Browser API. It’s basically a user data cookie, so this site loads without asking to log in.

You might need to be more explicit about what platforms and maybe even specific versions of them you’re trying to support. iOS in particular seems to have made some changes about scoping of data storage with SFSafariViewController, which appears to be what Capacitor’s Browser API uses internally.

Currently I’m targeting Android 9 and 10. I’m using Ionic 5 and Capacitor 2. Hopefully, it works serving the app to Chrome too.