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

Add PWA Functionality #708

Merged
merged 7 commits into from
Apr 23, 2024
Merged

Add PWA Functionality #708

merged 7 commits into from
Apr 23, 2024

Conversation

cgjgh
Copy link
Contributor

@cgjgh cgjgh commented Mar 25, 2024

Description

Integrate PWA functionality with Vite-PWA, which includes a service worker, is installable across platforms such as Windows, iOS, and Android, implements caching with automatic updates, cleans up outdated caches, and features automatic switching of the status bar between light and dark modes.

Necessary service worker, workbox and web manifest files are generated in build folder at build time.

Related Issue(s)

Closes #88

Checklist

  • I have read the contribution guidelines
  • Suitable unit/system level tests have been added and they pass --No tests added-minimal changes
  • Documentation has been updated
    • Upgrade instructions
    • Configuration details
    • Concepts
  • Changes flowforge.yml?
    • Issue/PR raised on FlowFuse/helm to update ConfigMap Template
    • Issue/PR raised on FlowFuse/CloudProject to update values for Staging/Production

Labels

  • Backport needed? -> add the backport label
  • Includes a DB migration? -> add the area:migration label

@bartbutenaers
Copy link
Contributor

Hi @cgjgh,
Thanks for this PR!

Recently I wanted to create a shortcut to my flowforge dashboard D2 on my Android smartphone, but there was no "Add to home screen" menu option available in Chrome. After some searching the reason seemed to be that dashboard D2 was not a PWA...

Very useful PR!
Bart

@colinl
Copy link
Contributor

colinl commented Mar 25, 2024

I believe the Add to Homescreen capability was added a few weeks ago. It certainly now works for me.

@cgjgh
Copy link
Contributor Author

cgjgh commented Mar 25, 2024

Add to Home Screen option was available on iOS at least, but seemed to only make a web shortcut which ran in a full screen browser. No caching or background worker which could now be used to receive push notifications for instance.

Also I’ve frequently gotten blank white screens on launch which lasted until Safari browser cache was cleared. This pull request fixes issue.

Also Dashboard logo did not appear on installed app icon, now present in iOS, Windows and Android.

@bartbutenaers
Copy link
Contributor

background worker which could now be used to receive push notifications for instance

@cgjgh
Thanks for the clarification!
Don't want to go off-topic, but I am not really familiar with PWA.
I would appreciate if you could add some feedback about push notifications to this issue. I have about 99% of my web-push ui node completed (also by registering a service worker in my own custom ui node). But now I am a bit confused whether you mean (native) push notifications or web push notifications, why you need pwa for a service worker, and so on. Would love to hear your thoughts in that other issue.
Thanks!!!

@cgjgh
Copy link
Contributor Author

cgjgh commented Mar 25, 2024

@bartbutenaers

But now I am a bit confused whether you mean (native) push notifications or web push notifications,

To clarify, notifications from a PWA will show up as native device push notifications, originating from and linking to PWA app, which will have its own icon and window instead of browser’s.

why you need pwa for a service worker,

As I understand service workers are not exclusive to PWAs, but rather PWAs build on top of them. PWA can also work offline, not relevant in the case of Node Red, but can help with caching to improve launch speed, providing a more native app feel. As such, they support various native functionality such as push notifications, badges on PWA icon, geolocation, biometric authentication, etc, features that are more heavily restricted on iOS requiring web app to be installed as a PWA.

Example PWA - test notifications

References

Making PWAs Installable

Caching

Notifications and Push API Example

Example Notification on lock screen

IMG_2438

Badges on home screen icon

IMG_2439

PWA notification settings in iOS settings

IMG_2443

@bartbutenaers
Copy link
Contributor

Thanks!!
Features like Geolocation, ... are of course very useful features for home automation.
I will mention you in the other issue focussed on push notifications, because I don't want to hijack your PR discussion.

Copy link
Collaborator

@joepavitt joepavitt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Firstly, thanks for the PR! Really appreciate the contribution, and keen to get this merged asap.

Feedback:

  • We seem to be missing a package-lock.json from your dependency updates, which is preventing the automated build through our GH actions?
  • I've installed locally, and run our tests and there are many failures, but will need to investigate in more detail in order to understand the causes. With your local deployment, please do run npm run cy:run in order to run our E2E tests and ensure no regressions/problems are introduced.

ui/public/apple-touch-icon.png Show resolved Hide resolved
@cgjgh
Copy link
Contributor Author

cgjgh commented Mar 26, 2024

I will look into the above errors later today, think some updated files were not added to commit.

@cgjgh cgjgh force-pushed the main branch 2 times, most recently from eccf331 to 7333265 Compare March 27, 2024 01:29
Add PWA functionality using Vite-PWA,
-Includes service worker
-Cross platform installable: Windows, iOS, Android
-Implements caching with automatic updates and outdated cache cleanup
-Auto light and dark mode switching of status bar
@joepavitt
Copy link
Collaborator

The E2E Tests are now failing here in line with how they failed when I run locally. Cypress required an on load event to be fired from the page to determine that the UI has updated after a page navigation, something in these changes is preventing that event from firing.

@fencer
Copy link

fencer commented Apr 5, 2024

is there a plan when this will be released? looking forward for this functionality.

@joepavitt
Copy link
Collaborator

is there a plan when this will be released? looking forward for this functionality.

One the E2E tests pass - waiting on @cgjgh to take a look. The service worker introduced is preventing the onLoad event firing from the browser currently.

Disabled service worker in cypress tests by intercepting request and returning empty service worker file
Copy link
Contributor Author

@cgjgh cgjgh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes made, ready for further review.

@cgjgh
Copy link
Contributor Author

cgjgh commented Apr 6, 2024

Load event not firing seems to be an ongoing issue with service workers and Cypress as can be seen here: cypress-io/cypress#27501.

For now the only solution seems to be to disable service worker while testing in Cypress, which I've done by intercepting request for sw.js and returning empty object preventing it from registering:

cy.intercept('/dashboard/sw.js', { body: undefined })

Let me know whether you approve of this fix .

@cgjgh cgjgh requested a review from joepavitt April 6, 2024 02:14
@joepavitt
Copy link
Collaborator

Thanks @cgjgh - re running the tests action now, I'm away on vacation until Tuesday 16th, so can do a full and proper review then.

Unless @Steve-Mcl or @sherbastian can take a look in the mean time - but no pressure to, as I'd still need to cut a release to make this available for all anyway

@joepavitt
Copy link
Collaborator

joepavitt commented Apr 18, 2024

Trying to run locally, but missing vite-plugin-pwa as a dependency when running the npm run build, not sure how the run build in GH passed 😕

Need to add "vite-plugin-pwa": "^0.19.8" to the devDependencies

@cgjgh
Copy link
Contributor Author

cgjgh commented Apr 19, 2024

@joepavitt Fixed, please try again.
I apologize for all the inconveniences caused by all this back and forth.

@bartbutenaers
Copy link
Contributor

@cgjgh
I am not sure if you are following the Discourse forum, so mentioning you here. There is a PWA related question. Maybe it let ring some bells in your head...

@Steve-Mcl
Copy link
Contributor

@bartbutenaers it might be possible to add the pages as shortcuts in the manifest.

Here is a good article: https://medium.com/going-fullstack/adding-shortcuts-to-progressive-web-apps-53e249b06aaf

@colinl
Copy link
Contributor

colinl commented Apr 28, 2024

it might be possible to add the pages as shortcuts in the manifest

For me the biggest problem is that it appears not to be possible in Android to rename the shortcut on the home page. The result is, I presume, that I will not be able tell which shortcut is for which system. Perhaps the app name could be provided via settings. js. It might be good to be able to change the icon too.

@Paul-Reed
Copy link
Contributor

Paul-Reed commented Apr 28, 2024

@Paul-Reed May I ask what platform you're using? Haven't done any much testing on Android, Wondering what the experience is there.

Android 14 on a Pixel 6.
The Node-RED server is Oracle cloud running Ubuntu 22.04.4.

It seems much more solid than the 'web shortcut', the data is up to date as soon as it's opened, and it is framed very nicely to fit the screen.

Don't know if the 'Server disconnected' popup is part of PWA, but it's a really useful addition, and gives me greater confidence to view my data.

Don't know it it's possible, but the NR icon does not look good (especially on a dark theme), can we use our own icons?

logo

@joepavitt
Copy link
Collaborator

Which version of Android are you on @colinl ? And which browser?

When I click "Add to Home Screen" I get presented with the option to name the shortcut

SmartSelect_20240428_093339_Chrome.jpg

@joepavitt
Copy link
Collaborator

Don't know if the 'Server disconnected' popup is part of PWA, but it's a really useful addition

Separate addition that we put into 1.8.0

For the icon, it's not configurable at the moment, but that's partly my fault as I asked @cgjgh to change it to white background as per other icons used, we can change the default

@Paul-Reed
Copy link
Contributor

Paul-Reed commented Apr 28, 2024

When I click "Add to Home Screen" I get presented with the option to name the shortcut

Joe that looks as though you are creating a web shortcut & not installing the app??
I believe that you should see this instead...

app

@colinl
Copy link
Contributor

colinl commented Apr 28, 2024

Which version of Android are you on @colinl ? And which browser?

Android version 14. Brave and Chrome browsers. You should not be getting the Add to Home Screen any more, as @Paul-Reed says. However, there seems to be something odd going on. I removed all my shortcuts and the installed web app and restarted the phone. Now, when I try to open the dashboard in either browser it just appears to hang with a blank screen. I do get the Install App option on the dropdown menu though.

@colinl
Copy link
Contributor

colinl commented Apr 28, 2024

I do get the Install App option on the dropdown menu though.

Unfortunately the app doesn't seem to work, I just get the blank screen! So for the moment I have no access at all.

@colinl
Copy link
Contributor

colinl commented Apr 28, 2024

Hmm, not sure what is going on here. I think it may be to do with the fact that I am using Cloudflare ZeroTrust with Google Oauth access control. I think the reason that I was getting the blank page was that it was waiting for the authorisation. I have managed to get it working again, it will be interesting to see what happens the next time the auth times out and I need to login again with google acct.

@Paul-Reed
Copy link
Contributor

Still working ok here.

I add the app by selecting 'add to home screen' from the context menu, that then presents the 'install app' (like the screenshot above), and then get a confirmation pop-up that the app was successfully installed.
I've just repeated the process again.

@colinl
Copy link
Contributor

colinl commented Apr 28, 2024

Still working ok here.

On the home screen do you see the icon with the white surround, I was getting that, but now I am not, and although it says it is installing the app I am not conviced it is, it looks just like a shortcut. If you hold down on the icon what options do you see? The first time I installed it I had different options, but having cleared everything and starting again I just get the App Info and Remove options like a regular shortcut.

@Paul-Reed
Copy link
Contributor

On the home screen do you see the icon with the white surround

Yes

If you hold down on the icon what options do you see?

Screenshot_20240428-103126.png

@joepavitt
Copy link
Collaborator

Joe that looks as though you are creating a web shortcut & not installing the app??
I believe that you should see this instead...

You're right, I am on 1.8.0 though, but accessing via local network - so maybe Chrome doesn't let me "install" it

@Steve-Mcl
Copy link
Contributor

Joe that looks as though you are creating a web shortcut & not installing the app??
I believe that you should see this instead...

You're right, I am on 1.8.0 though, but accessing via local network - so maybe Chrome doesn't let me "install" it

I believe it needs to be HTTPS.

If you try from a FF instance, you should see "install app" for v1.8.0 and the add shortcut is no longer offered.

That's why I suggested we could/should serve dynamic manifest with the pages offered as shortcuts. See previous comment.

As well as shortcuts, the app name should be set by the base name or some other property in the users flows. That is because if you "install" more than one dashboard site as an app, all of your application names installed on your device will all be exactly the same.

@colinl
Copy link
Contributor

colinl commented Apr 28, 2024

@Paul-Reed Does Site Settings let you edit the name and icon?

@Paul-Reed
Copy link
Contributor

Does Site Settings let you edit the name and icon?

Unfortunately no, there is no option for either the name or icon.

@colinl
Copy link
Contributor

colinl commented Apr 28, 2024

Unfortunately no, there is no option for either the name or icon.

I asked because, at some point, I did see such an option, but I have since removed the app and now can't get it to install again. It asks if I want to install it and I say yes, but then it just comes up with the Add to Home screen options. I guess that the install is failing for some reason and it is dropping back to Add to Home Screen. I am using https.

@Paul-Reed
Copy link
Contributor

Paul-Reed commented Apr 28, 2024 via email

@colinl
Copy link
Contributor

colinl commented Apr 28, 2024

Yes, it didn't seem to help. I will have to steal one of the family's phones to see if it will install there.

@cgjgh
Copy link
Contributor Author

cgjgh commented Apr 28, 2024

Not sure if this will help, but I had some strange behaviors with version 1.8.0 as well, blank screens, etc. I updated to 1.8.1 and after uninstalling and clearing website data and cache, seems to work well.

Also, I tried installing Dashboard on two different Android device emulators, Android 14 and 15 using both Chrome and Brave. All scenarios installed and didn't get any blank screens or anything, although I did notice that Install App option was not always immediately (about 30 second delay) available and showed instead Add to home screen which would just add a web shortcut.

Also, I noticed that it had the Chrome icon as well on app icon which is inconsistent with PWA app icon behavior.
Screenshot 2024-04-28 143032

Chrome and Android versions:
Screenshot 2024-04-28 144521

As for setting PWA app for which webpage to open, that's predetermined in the generated manifest file with "start_url":"./" , so can not be changed by user when installing.

The option @Steve-Mcl mentioned could be a workaround for now, but if PWA functionality is to be retained, I'd suggest solving this in the future by changing the root paths in ui-base config of Dashboard 2.0, which is currently still in development I understand. This would create different root paths for the individual dashboards allowing them to be installed separately.
Screenshot 2024-04-28 145549

@colinl
Copy link
Contributor

colinl commented Apr 29, 2024

I think I have worked out what is going on with the blank screen I have been getting sometimes.
As I said earlier I use Google Oauth to authenticate and Cloudflare ZeroTrust.
Before the PWA capability, if re-authentication was required, reloading the page (by swiping down in Android) attempted to reload the dashboard and I was redirected to the Google login screen by Cloudflare so I could login and then the dashboard loaded.
Now, with the PWA version, if the authentication has expired then I just get a blank screen and there does not seem to be a way of forcing a reload so I am stuck.

To recover the situation I have to browse to the node-red root url (not the dashboard) and then I get the login screen before the editor opens. Then if I go back to the dashboard application it opens ok.

@TotallyInformation
Copy link

It sounds like the client code needs to have something that picks up a 401 response code from a client request and triggers a standard redirect to an endpoint defined in the D2 config.

@colinl
Copy link
Contributor

colinl commented Apr 29, 2024

I have added issue #817 to track this problem

@Paul-Reed
Copy link
Contributor

Is there a logic to which dashboard page is initially opened, when the 'app' is opened?
I thought it may be the page which is listed topmost in the dashboard layout config, but doesn't appear to be.

@colinl
Copy link
Contributor

colinl commented May 3, 2024

Does it open the same one as if you open it in a PC browser window? It does for me. There is an issue #728 (not PWA related) saying that it does not necessarily open the one at the top of the list, but I have not confirmed that myself.

@Paul-Reed
Copy link
Contributor

Does it open the same one as if you open it in a PC browser window?

Yes, maybe just a coincidence, but the page that it always opens by default is the first page that I created after installing DB2.

@colinl
Copy link
Contributor

colinl commented May 3, 2024

the page that it always opens by default is the first page that I created after installing DB2.

I think that is what the issue i linked to says.

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

Successfully merging this pull request may close these issues.

Manifest PWA Configuration for Node-RED Dashboard
8 participants