Skip to content
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

[gatsby-plugin-offline] adding features to Service Worker #10331

Closed
spektakl opened this issue Dec 6, 2018 · 7 comments
Closed

[gatsby-plugin-offline] adding features to Service Worker #10331

spektakl opened this issue Dec 6, 2018 · 7 comments
Assignees
Labels
type: question or discussion Issue discussing or asking a question about Gatsby

Comments

@spektakl
Copy link

spektakl commented Dec 6, 2018

Hi all!

We are developing a PWA using GatsbyJS and one of the plugins we're using is [gatsby-plugin-offline]. It's all working ok, but we need a way to extend this service worker somehow and the plugin seems to be somehow limited.

We are using dynamic URLs to get info, so initially, it caches few static assets and no much more. But we want to use the service worker to catch API calls to a third service provider and to manage push notifications to user.

But we can't see how to implement this, as every time we build the project, the generated service worker has only the features that the plugin manages, and things like push notifications are not included. So, if we manually add them to the generated sw.js, we can lost them on the next project build.

Any ideas of how to manage this scenario?

Thanks! :)

@jgierer12 jgierer12 added the type: question or discussion Issue discussing or asking a question about Gatsby label Dec 6, 2018
@gatsbot gatsbot bot added the stale? Issue that may be closed soon due to the original author not responding any more. label Feb 5, 2019
@gatsbot
Copy link

gatsbot bot commented Feb 5, 2019

Hiya!

This issue has gone quiet. Spooky quiet. 👻

We get a lot of issues, so we currently close issues after 30 days of inactivity. It’s been at least 20 days since the last update here.

If we missed this issue or if you want to keep it open, please reply here. You can also add the label "not stale" to keep this issue open!

Thanks for being a part of the Gatsby community! 💪💜

@gatsbot
Copy link

gatsbot bot commented Feb 16, 2019

Hey again!

It’s been 30 days since anything happened on this issue, so our friendly neighborhood robot (that’s me!) is going to close it.

Please keep in mind that I’m only a robot, so if I’ve closed this issue in error, I’m HUMAN_EMOTION_SORRY. Please feel free to reopen this issue or create a new one if you need anything else.

Thanks again for being part of the Gatsby community!

@gatsbot gatsbot bot closed this as completed Feb 16, 2019
@KyleAMathews KyleAMathews reopened this Feb 16, 2019
@m-allanson m-allanson added not stale and removed stale? Issue that may be closed soon due to the original author not responding any more. labels Feb 18, 2019
@vtenfys vtenfys self-assigned this Feb 18, 2019
@vtenfys
Copy link
Contributor

vtenfys commented Feb 19, 2019

Hi there, sorry for the slow reply. There's currently a PR open which adds support for custom SW code which can persist between builds: #11626

With this, you'll be able to append your custom code automatically - your existing code should work just fine since it's appended in more-or-less the same way as you would do it manually.

But we want to use the service worker to catch API calls to a third service provider and to manage push notifications to user.

gatsby-plugin-offline uses Workbox behind the scenes, so you can modify the configuration as described here: https://developers.google.com/web/tools/workbox/modules/workbox-build#full_generatesw_config

This link is made more prominent in the plugin's README in the same PR mentioned above. In addition, you can use the Workbox APIs in your custom appended code. For example:

workbox.routing.registerRoute(<regex>, workbox.strategies.cacheFirst(), 'POST');

This would use the cacheFirst strategy for URLs matching the regex. Note that for third party URLs, the regex has to match the entire URL, not just part of it - e.g. /https:\/\/thirdpartydomain.com\/.+\.js/ would match all *.js files on thirdpartydomain.com. You can read more about this in the Workbox documentation: https://developers.google.com/web/tools/workbox/modules/workbox-routing#how_to_register_a_regular_expression_route

@ghost
Copy link

ghost commented May 15, 2019

Hi @spektakl,

I have exactly the same need as you. I want gatsby-plugin-offline to keep working (for its cache management), and I need to override it for other functionalities like push notifications. I don't want to create my own service worker from scratch because I still need gatsby-plugin-offline.

Did you managed to do it ?

@elie222
Copy link
Contributor

elie222 commented May 25, 2019

Also interested in adding push notifications to my Gatsby site.

UPDATE
This may help:
https://spectrum.chat/gatsby-js/general/web-push-notifications-with-gatsby~5e6abe4f-e015-4966-8e6b-0f5a6b53fa3c

@Rajarml
Copy link

Rajarml commented May 27, 2019

I've used the link provided by @elie222 but it misses a way to minify the extra push notification script.
The field "importScripts" should take an object with a file path ("src/push-notification.js" for instance) and transforming it in the sw.js into importScripts("./push-notification.abcd1234.js").

@vtenfys
Copy link
Contributor

vtenfys commented Aug 30, 2019

We just published a new major version of gatsby-plugin-offline (3.0.0) which adds support for this feature!

To do this, write a script you want appended to your sw.js and then use the option appendScript in your gatsby.config.js. For more details, please see the README of gatsby-plugin-offline.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: question or discussion Issue discussing or asking a question about Gatsby
Projects
None yet
Development

No branches or pull requests

8 participants