Skip to content
This repository has been archived by the owner on Aug 10, 2022. It is now read-only.

Workbox advanced recipes #5493

Merged
merged 5 commits into from
Dec 13, 2017
Merged

Workbox advanced recipes #5493

merged 5 commits into from
Dec 13, 2017

Conversation

gauntface
Copy link

Advanced Recipes for docs

@WebFundBot
Copy link

👍


{% include "web/tools/workbox/_shared/alpha.html" %}

## Offer a page reload for users
Copy link
Contributor

Choose a reason for hiding this comment

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

My gut is that we should link to https://redfin.engineering/how-to-fix-the-refresh-button-when-using-service-workers-a8e27af6df68 instead of including a recipe inline, because there are a number of different considerations and tradeoffs that that post does a good job of explaining.

If you'd prefer to include the same code inline rather than linking out then I could offer some line-by-line feedback, but I'll hold off until we decide.


To do this in Workbox you can use the `handle()` method on strategy to make
a custom handler function.

Copy link
Contributor

Choose a reason for hiding this comment

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

Can you include a note here that the developers should ensure FALLBACK_IMAGE_URL is already cached, presumable via the precache manifest?

@WebFundBot
Copy link

👍

@WebFundBot
Copy link

👍

const container = document.querySelector('.new-sw');
container.style.display = 'block';

const button = document.querySelector('button');
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you switch this to container.querySelector('button'), as it's less likely to pick up a random <button> element used elsewhere on the page?


To do this you'll need to add some code to your page and to your service worker.

**Add to your page**
Copy link
Contributor

Choose a reason for hiding this comment

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

The actual HTML will obviously vary, especially if the page is using a framework of some sort, but I think it would help to at least a representative sample of what kind of HTML the JavaScript assumes:

<div class="new-sw" style="display: none">
  <span>Updated content is available.</span>
  <button>Refresh</button>
</div>

button.addEventListener('click', () => {
button.disabled = true;

registration.waiting.postMessage('force-activate');
Copy link
Contributor

Choose a reason for hiding this comment

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

It's worth checking registration.waiting !== null here before calling .postMessage(), just in case something happened out-of-band to cause registration.waiting to go away.

**Add to your page**

```javascript
const showRefreshUI = (registration) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

I tend to avoid arrow functions and const in code that's meant to run inside of a window context, as it breaks parsing the JS in older browsers, and not everyone transpiles.

This will receive a the 'force-activate' message and call `skipWaiting()` and
`clients.claim()` so that the service worker activates immediately and controls
all of the currently open windows, which in turn results in the `
controllerchange` event firing causing the windows to reload.
Copy link
Contributor

Choose a reason for hiding this comment

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

It's the message event, not the controllerchange event, that causes the windows to reload themselves.

@WebFundBot
Copy link

👍

@gauntface gauntface merged commit fff418b into master Dec 13, 2017
@gauntface gauntface deleted the workbox-advanced-recipes branch December 13, 2017 22:17
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants