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

Mock SwPush and SwUpdate in development #3

Open
korneel opened this issue Nov 13, 2017 · 7 comments
Open

Mock SwPush and SwUpdate in development #3

korneel opened this issue Nov 13, 2017 · 7 comments

Comments

@korneel
Copy link

korneel commented Nov 13, 2017

With ng serve I get "No provider for SwPush" and "No provider for SwUpdate" because the ServiceWorkerModule is only registered in production environment:

environment.production ? ServiceWorkerModule.register('/ngsw-worker.js') : []

Can SwPush and SwUpdate be mocked in development mode somehow?

@maciejtreder
Copy link

Hello

I have made a work-around for this. You can find it in my repo:

https://github.com/maciejtreder/angular-universal-pwa

What i have done is just empty "fake" file with service worker code, name same as production one ngsw-worker.js:
https://github.com/maciejtreder/angular-universal-pwa/pull/65/files#diff-5eed400881fb205c6c985b6e293a8368

In my development build I am using CopyWebpackPlugin:

new CopyWebpackPlugin( [{from: 'src/ngsw-worker.js', to: '.'}])

https://github.com/maciejtreder/angular-universal-pwa/pull/65/files#diff-bf19058f770313b72ea047161f30e968

@webmaxru
Copy link
Owner

Hello! Thank you for sharing the solution!

@maciejtreder
Copy link

@webmaxru You're welcome!

You should now, that you are the person who got me interested in Angular (at Devoxx Poland 2016)!

I would be more then happy if you would like to review my repo and place your comments!

Of course, the star from you would be more than appreciating!

@korneel
Copy link
Author

korneel commented Nov 14, 2017

Hi, thank you, it's a simple and clean solution 👍 Just register an empty service worker in development.

@cre8
Copy link

cre8 commented Dec 6, 2017

Here is the code I used to register an empty service worker:

  1. create an empty /src/ngsw-worker.dev.js file
  2. register it in development mode: environment.production ? ServiceWorkerModule.register('/ngsw-worker.js') : ServiceWorkerModule.register('/ngsw-worker.dev.js')
  3. add it in your .angular-cli.json file under assets:
    { "glob": "ngsw-worker.dev.js", "input": "./", "output": "./" }

@NateMay
Copy link

NateMay commented Apr 22, 2018

@maciejtreder, awesome mock! It worked, and I will use it for testing. Right after I got your mock working I saw that we can enable the ServiceWorkerModule only for production like this:

ServiceWorkerModule.register('/ngsw-worker.js', { enabled: environment.production }),

it's a bit cleaner in my AppModule IMO.

"[@]angular/service-worker": "^5.2.9",

@maciejtreder
Copy link

Will check ‘enable’ option. It would be much better then mocking.

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

No branches or pull requests

5 participants