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
[docs] Add precaching Service Worker to exported docs builds #10267
Conversation
@@ -242,6 +242,12 @@ function withRoot(Component) { | |||
}; | |||
} | |||
|
|||
componentDidMount() { | |||
if ('serviceWorker' in navigator) { | |||
navigator.serviceWorker.register('/service-worker.js'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm gonna use sw.js
as the convention.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@msiadak Hold on. I'm making some changes :) |
My bad, will stand by. Thanks for reviewing! |
@msiadak Alright, I'm pretty happy with this pull-request. The documentation now works offline. It's huge. |
@msiadak Thank you. |
* Add service worker to exported docs builds * push forward
Closes #9529
Given that we're deploying a static export of the docs website to Firebase, I think we only need to create a service worker for that case rather than trying to address the builds served with the
docs:dev
anddocs:start
scripts.To accomplish this I used the new beta (v3.0.0-beta.0) of Workbox to crawl the docs/export directory and inject a manifest of the exported files into a simple Service Worker template. I wrapped this action in a new npm script:
docs:build-sw
that I added into thedocs:export
script. If we decide that we want to do something more complex with the Service Worker in the future, I think this can provide a decent starting point.Before taking this approach, I looked into some of the other known methods for automating service workers with next.js, but I wasn't able to get any of them working for the static export case without also using a method like the one in this PR.
I was able to get all of the PWA audits on Lighthouse to pass with this configuration with the exception of the https-related ones, although those should be covered by Firebase hosting.