baby_in_magento

feiu

May 10th, 2024
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. import {
  2. VALID_SERVICE_WORKER_ENVIRONMENT,
  3. handleMessageFromSW
  4. } from '@magento/peregrine/lib/util/swUtils';
  5.  
  6. let swRegistration = null;
  7.  
  8. export const getRegistration = () => {
  9. return swRegistration;
  10. }
  11.  
  12. export const registerSW = () => {
  13. if (VALID_SERVICE_WORKER_ENVIRONMENT && globalThis.navigator && window.navigator?.serviceWorker) {
  14. window.navigator?.serviceWorker?.register('/sw.js')
  15. .then((registration) => {
  16. swRegistration = registration;
  17. console.log('SW Registered', registration);
  18. })
  19. .catch(() => {
  20. /**
  21. * console.* statements are removed by webpack
  22. * in production mode. window.console.* are not.
  23. */
  24. window.console.warn('Failed to register SW.');
  25. });
  26.  
  27. navigator.serviceWorker.addEventListener('message', e => {
  28. const { type, payload } = e.data;
  29. handleMessageFromSW(type, payload, e);
  30. });
  31. }
  32. };
  33.  
Advertisement
Add Comment
Please, Sign In to add comment