android-browser-helper icon indicating copy to clipboard operation
android-browser-helper copied to clipboard

Add na offline-first-run Trusted Web Activity demo

Open andreban opened this issue 3 years ago • 6 comments

When a PWA using Trusted Web Activity is installed from an application store, the Service Worker and therefore its offline capabilities won't be installed until it runs for the first time.

This creates a risk where, if the user is offline when they run the app for the first time, they will see the Chrome Dinosaur instead of an application specific offline page.

A workaround for this issue is to check for connectivity when the application runs for the first time and show a custom Activity in that case. When the web application is launched successfully for the first time, that information is stored and future offline launches can use the web app offline capability.

  • [ ] Iteration 1 (#272): The Android application checks for connectivity before launching the web app and launches a custom offline activity if connection is not available.
  • [ ] Iteration 2: Information on the web application being launched successfully is stored in the shared storage and future offline launches will use the web app instead of the Android falllback.

andreban avatar Mar 24 '21 16:03 andreban

Adding the related bubblewrap implementation that we can use as inspiration for iteration 2:

https://github.com/GoogleChromeLabs/bubblewrap/blob/main/packages/core/src/lib/features/FirstRunFlagFeature.ts

demianrenzulli avatar Mar 24 '21 17:03 demianrenzulli

As discussed, we'll first work in getting the iteration 1 approved and uploaded to the repo, and then we'll work on iteration 2 on top of it.

@andreban: question about iteration 2: Taking a look at the helper class you've shared, I don't see exactly how the following things are being checked:

  • How do we know if the service worker registration succeeded in the first run and the fallback page is available in the cache.
  • (Related to the previous point): how can we make sure that, after a successful first run, the user hasn't cleaned up the browser storage, and therefore, removed the SW and offline page for the TWA?

I assume that for that functionality to be made, we would need to communicate Android code (from the demo), with web code (to check for SW registration and caches), and that's not possible.

Thoughts are welcome.

demianrenzulli avatar Mar 25 '21 13:03 demianrenzulli

To my knowledge, there's no way to know that the Service Worker was successfully installed from the host app, so we just trust that launchTwa() launched something - Either the TWA or the fallback approach.

@PEConn in case he has any ideas here.

andreban avatar Mar 25 '21 14:03 andreban

Also adding here Andre's idea of checking when the TWA connects to the host app's service: DelegationService, to make sure that the TWA has successfully launched.

demianrenzulli avatar Mar 25 '21 14:03 demianrenzulli

With the demo and article published I think we can close this one.

demianrenzulli avatar May 17 '21 16:05 demianrenzulli

This has been so helpful. Now that this issue is solved, how can this be implemented? Is it through the manifest.json or do we copy these files into a related folder?

aakinseyef avatar Oct 22 '21 20:10 aakinseyef