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

Implement basic offline experience for Venia (PWA) #374

Closed
3 tasks
ericerway opened this issue Oct 16, 2018 · 6 comments
Closed
3 tasks

Implement basic offline experience for Venia (PWA) #374

ericerway opened this issue Oct 16, 2018 · 6 comments
Assignees

Comments

@ericerway
Copy link

ericerway commented Oct 16, 2018

This issue is for the following packages:

[ ] venia-concept
[ ] pwa-buildpack
[X] peregrine
[ ] pwa-module
[ ] pwa-devdocs

Description

As a shopper, I want basic feedback on the Venia storefront when my device is having connectivity issues or offline so that I know that I can have an uninterrupted shopping experience.

This is a requirement for PWA per Google Lighthouse and applies to all of the Venia storefront.

Assumptions

  • Dependent on examples of the Venia storefront
  • Dependent on 200 response type to respond
  • Browser messaging for this scenario is not sufficient (must use service worker)
  • Utilizes Workbox for basic behaviors/needs (service worker debugging in DevDocs; ref.)

Tasks

@ericerway ericerway changed the title Implement HTTP redirect for HTTPS (PWA) Implement basic response when offline (PWA) Oct 16, 2018
@ericerway ericerway changed the title Implement basic response when offline (PWA) Implement basic offline experience for Venia (PWA) Oct 16, 2018
@ericerway
Copy link
Author

Kamino cloned this issue to magento-research/pwa-studio

@ericerway
Copy link
Author

Marking as in progress from @pcvonz and team -- preview here: http://upward.bargreen.io

@pcvonz
Copy link
Contributor

pcvonz commented Oct 26, 2018

Some notes on the obstacles I've encountered so far. Let me know if anything sounds incorrect:

Problem: Refreshing a page when offline results in a cache miss if that page wasn't refreshed directly. When first visiting Venia, upward will return an html file, however, every subsequent load doesn't, which creates a problem when trying to cache every visited page.

Possible Solution: This is a pretty easy fix: Cache requests in the MagentoRouteHandler.
Recipe for how to access the cache in the Workbox docs.


Problem: The fetchRoute function of resolveUnkownRoute in Peregrine makes a POST request to the graphql endpoint. Caching of graphql queries in venia is solved pretty easily with apollo-cache-persist, but that isn't the case with Peregrine.

Possible Solution: (slap dash, probably not what we want)

Bypass MagentoRouteHandler when offline and request the html file from Cache.

Possible Solution 2:

Probably the way it should be done 😅

Cache each query to urlResolver to localStorage:

    {
        '/': {data: {urlResolver: {type: "CMS_PAGE", id: 2}}}
        '/crown-summit-backpack.html': {data: {urlResolver: {type: "PRODUCT", id: 3}}}
    }

Is it weird if we use apollo-cache-persist in Venia and then cache the urlResolver queries manually? I guess if we're caching a lot of stuff in Peregrine we may want to add the ability to hook in different storage providers(?)


Related issue in the Workbox repo

Hopefully that all makes sense, I'm still learning this as I go along. It's also pretty late on a Friday 😄

@zetlen @rowan-m

@pcvonz
Copy link
Contributor

pcvonz commented Nov 2, 2018

Possible solution 3 (for the fetchRoute POST problem):

Looks like the ability to make a GET request to the graphql endpoint has already come up. There is an issue for it in the graphql-ce repo[1]. Once this is implemented, caching the response from fetchRoute should be easy.

[1] Related issue

@rowan-m
Copy link
Contributor

rowan-m commented Nov 5, 2018

Great progress so far! Adding a few comments here on things I've seen. They may not all be relevant to this issue, but I wanted to make sure it's down.

  1. As per the Lighthouse audit, the manifest is missing the 192px and 512px icons.
  2. As per the Lighthouse audit, the manifest is missing a "theme_color"
  3. Definitely happier with only trying to cache GET requests.
  4. The manifest has a .webmanifest extension, which is fine but .json is more the convention. Sending the content-type: application/json header would be good though. I don't see any header currently.
  5. Not sure about the homescreen entry in the upward config. Given that at least 3 icon sizes are needed, I feel like this is better served as static content.
  6. Enhancement: Offline page should should be able to intelligently continue to the content once the connection is restored. This should be with a combination of listening for the online event and a configurable timeout (default to 30 sec) when the the request will be retried.
  7. Enhancement: Ideally, the user should have some indication when they are being served offline content. This is tricky from a UX point of view because it's hard to tell the difference between just using the cache for a slow connection versus really being offline. A reasonable compromise is showing a persistent indicator (toast, banner, etc) when navigator.onLine is not true.
  8. Discussion: Product pages probably need more sophisticated caching based on business, commercial, or even regulatory needs. Cache needs an expiry at the least. Should other aspects be changed when serving from cache - for example, showing the price?

@pcvonz
Copy link
Contributor

pcvonz commented Nov 6, 2018

Offline indicator demo:
Notification appears at the top of the page (not fixed). Once the user is online again it disappears and loads the page.
online_offline_online

jimbo pushed a commit that referenced this issue Nov 28, 2018
* Update readme

- Add troubleshooting section for `connection not secure`

* Add web app manifest, offline mode

- Add Apollo simple persistence for persistent cache of graphql queries
- Add html files to SW runtime cache
- Fix venia favicon (fixes serving images via upward in general)
- Add new venia icons

* Fix venia config

* Offline mode integrated into resolveUnknownRoute

- If offline loads component from localStorage
- If offline and component not in cache, load NotFound root component
- Add fallback page to venia-upward yml
- Switch to inject-manifest in webpack

* Add inject-mainfest option to PWA-Buildpack

* Add tests

- Add test for offline functionality of resolveUnkownRoute
- Add test for injectManifest of ServiceWorkerPlugin

* Add venia icons to manifest, fix injectManifest

- SW.js is now served with `Service-Worker-Allowed` header
- manifest icons are moved to the dist directory with
`copy-webpack-plugin`
- manifest.webmanifest -> manifest.json

* Add copy-webpack-plugin

* Add offline indicator

* Style offline indicator, fix offline to online

- `getRouteComponent` now checks if the current route has the
`NotFoundComponent` stored in state. If so, reload the route when
online

Progress towards #374

* Add online indicator

- Shows online status when switching from offline -> online
- Animation for both online and offline

* Clean up, comment

* Revert upward change

* Update ServiceWorkerPlugin to accept config

- ServiceWorkerPlugin takes in `injectManifestConfig`
- Update tests
- Cache chunks via workbox-webpack

* Rename `media` directory to `images`

Resolves `/media` proxying to the magento backend instead of to the
`/dist` folder to serve static content

* Add theme color to head

* Remove fallback code. Opening seprate issue.

* Switch test url from store.com to example.com

* Address PR feedback

* Fix small issues and lint
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants