Skip to content

Instantly share code, notes, and snippets.

@m7v
Forked from tkh44/sw.js
Created March 16, 2018 04:02
Show Gist options
  • Save m7v/9b41c41f02e5016f25272e9a91f94172 to your computer and use it in GitHub Desktop.
Save m7v/9b41c41f02e5016f25272e9a91f94172 to your computer and use it in GitHub Desktop.
Example of syncing some redux state with a service worker
const swSupported = !!('serviceWorker' in navigator)
const sendMessage = (messageData) => {
if (!swSupported) {
return
}
navigator.serviceWorker.controller.postMessage(messageData)
}
sendMessage({ sideway: window.sideway })
export const serviceWorkerMiddleware = (store) => (next) => (action) => {
const result = next(action)
if (action.type.indexOf('auth/') === 0) {
sendMessage({ auth: store.getState().auth })
}
return result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment