Skip to content

Conversation

@jinidev
Copy link
Contributor

@jinidev jinidev commented Dec 5, 2025

Resolves JIRA:
PArent ticket : https://bbc.atlassian.net/browse/WS-1464

https://bbc.atlassian.net/browse/WS-1837

Summary

Service worker changes to render offline page only in PWA app

Code changes

Changes in _app.page.tsx (nextjs) and serviceWorkerComponet index.tsx(express) - to initiate the service worker registration
Changes in SW.js - to accomodate offline page rendering only for PWA installed app
Added 2 custom hooks for service worker registration and to send PWA_STATUS

Testing:

It’s better to test in the preview environment, as localhost testing won’t provide accurate results.
Launch any page in the installed PWA app.
Reload the page or navigate to another page to ensure caching happens properly.
(The PWA requires an initial reload/navigation so caching starts, the PWA_STATUS becomes true, and the app is identified as running in standalone mode.)
Turn off Wi-Fi and then reload the page or navigate to any page to verify that the offline page is displayed.

image

Note this offline page will be only seen for installed pWA app not for canonical sites

SOme edge cases related to service worker on rendering offline page|:

  1. We need a second reload or any navigation request after the PWA app launch to ensure that:

       -       The offline page gets cached if it isn’t already, or
       -       The offline page works correctly even if it is already available in the cache.
    
  2. Right now , if we are getting launched directly to homepage rendered in Express(i.e -/mundo ), then that page doesn't render offline page when user goes offline.

_Note : As part of latest discussions, we are going to release offline page and service worker functionality with these edge cases_

Useful Links

https://web.dev/articles/offline-cookbook#on-install-not
https://web.dev/articles/service-worker-lifecycle#updates
https://googlechrome.github.io/samples/service-worker/custom-offline-page/index.html

if (hasOfflinePageFunctionality) await cache.add(OFFLINE_PAGE);
});
});
console.log(`[SW v${version}] Service Worker loaded.`);
Copy link
Member

Choose a reason for hiding this comment

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

Is it OK if we log these for everyone? I think it's useful for debugging, but maybe there should be an option/flag? e.g. query parameter that enabled console.log)? E.g. https://github.com/bbc/simorgh/blob/latest/docs/Rendering-Ads.mdx?plain=1#L46

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I thought we could keep these logs for a couple of days after the first release to ensure everything is on track or to help with debugging if something breaks. After that, we can remove them.
But if its not advisable , we can have a look into other ways

Copy link
Contributor

@andrewscfc andrewscfc left a comment

Choose a reason for hiding this comment

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

Nice work here, some of my questions likely stem from misunderstandings as this seems pretty complicated to me! Please DM me or we can do a call if any of my queries are unclear

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.

7 participants