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

How to trigger refresh of PWA #64

Closed
jlewi opened this issue Apr 19, 2024 · 10 comments
Closed

How to trigger refresh of PWA #64

jlewi opened this issue Apr 19, 2024 · 10 comments

Comments

@jlewi
Copy link
Owner

jlewi commented Apr 19, 2024

VSCode For Web Is A Progressive Web App
Refreshing the page doesn't seem to redownload assets.
This can lead to version skew because the backend might be newer than the app.

You can use chrome developer console -> Application Page and clear site data. It seems like you can potentially select what to clear so you don't clear VSCode settings.

Still this is far from a good solution. Ideall the app should automatically check if its out of date and trigger a refresh

@jlewi
Copy link
Owner Author

jlewi commented May 7, 2024

It looks like the autoUpdateInterval is currently being set to 0 which disables autoUpdates
curl http://localhost:8080/viewer/app.js

function goappSetupAutoUpdate(registration) {
  const autoUpdateInterval = "0";
  if (autoUpdateInterval == 0) {
    return;
  }

  window.setInterval(() => {
    registration.update();
  }, autoUpdateInterval);
}

@jlewi
Copy link
Owner Author

jlewi commented May 7, 2024

@jlewi
Copy link
Owner Author

jlewi commented May 7, 2024

So it looks like one issue is that if we rebuild the WASM application but don't restart the static server the GOAPP version doesn't change. You can verify this by looking at the GOAPP_VERSION

curl http://localhost:8080/viewer/app.js  | grep GOAPP_VERSION

Intuitively this makes sense as the logic that sets the GOAPP_VERSION in the server isn't being retriggered just because we rebuilt the frontend.

@jlewi
Copy link
Owner Author

jlewi commented May 7, 2024

Page describing the app lifecycle
https://go-app.dev/lifecycle

@jlewi
Copy link
Owner Author

jlewi commented May 7, 2024

@jlewi
Copy link
Owner Author

jlewi commented May 7, 2024

We register the handler here

router.Any(logsviewer.AppPath+"/*any", gin.WrapH(http.StripPrefix(logsviewer.AppPath, viewerApp)))

So we could potentially change the Version on the handler each time it is fetched.

Per the https://go-app.dev/lifecycle to trigger an update we need to do two things

  1. Update the GoApp version so the client can tell its changed
  2. Do a refresh of the page

@jlewi
Copy link
Owner Author

jlewi commented May 7, 2024

It looks like updating the Version on the handler won't work because the rendered app.js are cached inside the handler.

https://github.com/maxence-charriere/go-app/blob/4af747695016174dfbe4502d0754da7ea1b4807b/pkg/app/http.go#L362

Here's my failed attempt
42596ac#diff-94cfcc8d852fd2c1a81d8083d620e03bdf600d42e498a9dfd51e8c115a82b4bfR274

So we probably need to recreate the handler in its entirety.

@jlewi
Copy link
Owner Author

jlewi commented May 7, 2024

Rather than just reloading the client application. It might make more sense just to create a simple program that watches for file changes and automatically

  1. Recompiles the frontend and server
  2. Restarts the server

@jlewi
Copy link
Owner Author

jlewi commented May 7, 2024

In #96 I added a status bar which displays the goapp version. This makes it easy to see when the app has been reloaded.
It looks the steps to refresh the PWA and pick up any changes are as falls

  1. Build the WASM app
  2. Build the server
  3. Start the server
  4. In the browser click refresh
  5. In the browser do a 2nd refresh

I think the first refresh is necessary to trigger the service worker to check for changes to the app and download them. Once the changes are downloaded the second refresh is needed to actually load them.

I think we could potentially eliminate the need for two refreshes by using the autoUpdate mechanism or there might be a notification method to automatically trigger the client to check for an update.

Notably though we don't have to clear application site data. That was annoying because it meant we lost all of our local settings which were saved in local storage.

Now that we have a recipe we can close this issue.

@jlewi jlewi closed this as completed May 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant