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

ServiceWorker not built in 4.x (next) #9776

Open
zaneclaes opened this issue Oct 10, 2020 · 21 comments
Open

ServiceWorker not built in 4.x (next) #9776

zaneclaes opened this issue Oct 10, 2020 · 21 comments

Comments

@zaneclaes
Copy link

Describe the bug

./build/service-worker.js is no longer generated at all (and there are no error messages) with the latest versions of CRA (4.x) using @next.

Did you try recovering your dependencies?

Yes.

Which terms did you search for in User Guide?

N/A

Environment

(paste the output of the command here.)

Steps to reproduce

(Write your steps here:)

  1. npx create-react-app test
  2. cd test
  3. npm install --save react-scripts@next
  4. npm run build
  5. ls -la ./build

Expected behavior

./build/service-worker.js should be present.

Actual behavior

The file is missing. This causes a problem with apps which are upgrading from prior CRA versions, as they continue to reference the cached service worker (so the web app continues to cache old assets).

Reproducible demo

Just run the commands above.

@max-programming
Copy link

@zaneclaes I deployed it to netlify. Do you know how can I fix this issue without having a build folder locally?

@Empty2k12
Copy link

This does not only affect @next. I just reproduced it by downgrading from next to the newly released 4.0.0 from 4 days ago.

Steps to repro:

npx create-react-app cra-no-sw --template typescript
cat ./cra-no-sw/build/service-worker.js
cat: ./cra-no-sw/build/service-worker.js: No such file or directory

@adueck
Copy link

adueck commented Nov 3, 2020

@Empty2k12 Yes, exact same problem here.

When I start a new app with

npx create-react-app my-app

The service worker is nowhere to be seen. 😢

Also the familiar section for registering the service worker in src/index.js is gone

This is no longer there 👇

import * as serviceWorker from './serviceWorker';

...

// If you want your app to work offline and load faster, you can change
// unregister() to register() below. Note this comes with some pitfalls.
// Learn more about service workers: https://bit.ly/CRA-PWA
serviceWorker.register()

@max-programming
Copy link

Actually, I think SW is replaced by reportWebVitals. We need to create our own service workers

@neolefty
Copy link

neolefty commented Nov 3, 2020

The 4.0.0 changelog says:

We've switched to the Workbox InjectManifest plugin...

But I think I need more information. When I make a fresh project, it doesn't create a service worker either with the normal template or with --template typescript, nor are there instructions in the code to add one.

Is this an incomplete feature in 4.0.0?

@dwelle
Copy link

dwelle commented Nov 3, 2020

Turns out you need to create your own service-worker.js file. There was no mention of it in the release notes, and even the new docs aren't super clear on this.

I haven't investigated this fully, so in the interim I just copy/pasted what CRA@3 was building and modified it to use the new manifest accessible from self.__WB_MANIFEST. You can check it out here: https://github.com/excalidraw/excalidraw/pull/2320/files. Use at your own risk :).

@adueck
Copy link

adueck commented Nov 4, 2020

Turns out you need to create your own service-worker.js file.

This is surprising, and disappointing (?). I loved using create-react-app because it always created a solid, installable PWA without having to worry about rolling a service-worker.js, which can be a bit of a minefield.

I really hope CRA doesn't drop the excellent ready-made service worker. 🥺

@kivervinicius
Copy link

I believe that there are more complaints about forcing a service worker by default than not, but it is extremely simple to continue using serviceWorker.

npx create-react-app my-app --template cra-template-pwa

@max-programming
Copy link

So I think there is no way except for making a custom service worker

@zaneclaes
Copy link
Author

It would be nice if there were a decent upgrade path, here. Or just a documented one.

For those upgrading to 4.x in an existing project, this is an extremely confusing change. In my case, it transparently broke my application due to a sudden lack of service workers. As others have noted, the upgrade docs are woefully insufficient.

In any case, my "fix" was to create a dummy app:

npx create-react-app my-app --template cra-template-pwa-typescript

And copy over the relevant code:

  • reportWebVitals.ts
  • service-worker.ts
  • serviceWorkerRegistration.ts

As well as the initialization code in index.tsx.

@chrisfinazzo
Copy link

I can confirm @adueck's findings as well. These files are not in the base template at all.

I am new to React and working to port a small microsite from Jekyll just to get a feel for things. The import in index.js and the register function are there, but that's it.

Maddening.

@stale
Copy link

stale bot commented Dec 25, 2020

This issue has been automatically marked as stale because it has not had any recent activity. It will be closed in 5 days if no further activity occurs.

@stale stale bot added the stale label Dec 25, 2020
@dwelle
Copy link

dwelle commented Dec 25, 2020

not quite resolved

@adueck
Copy link

adueck commented Dec 30, 2020

Are there plans to re-include the ServiceWorker in 4.x onwards? @kivervinicius recommended using npx create-react-app my-app --template cra-template-pwa but that's an old template and seems unmaintained. I would really, really love for there to be a nice built in service worker available like in the good old days of CRA 3.x 🙇‍♂️

@sureshdube
Copy link

It would be nice if there were a decent upgrade path, here. Or just a documented one.

For those upgrading to 4.x in an existing project, this is an extremely confusing change. In my case, it transparently broke my application due to a sudden lack of service workers. As others have noted, the upgrade docs are woefully insufficient.

In any case, my "fix" was to create a dummy app:

npx create-react-app my-app --template cra-template-pwa-typescript

And copy over the relevant code:

  • reportWebVitals.ts
  • service-worker.ts
  • serviceWorkerRegistration.ts

As well as the initialization code in index.tsx.

I tried these steps but it didn't work for me. Do I need to follow any more steps?

kfarnung added a commit to kfarnung/puzztool that referenced this issue Feb 3, 2021
CRA v4 moved the service worker out of the core and into a template.
Incorporate the template into the app.

facebook/create-react-app#9776
kfarnung added a commit to kfarnung/puzztool that referenced this issue Feb 3, 2021
CRA v4 moved the service worker out of the core and into a template.
Incorporate the template into the app.

facebook/create-react-app#9776
kfarnung added a commit to kfarnung/puzztool that referenced this issue Feb 3, 2021
CRA v4 moved the service worker out of the core and into a template.
Incorporate the template into the app.

facebook/create-react-app#9776
kfarnung added a commit to kfarnung/puzztool that referenced this issue Feb 3, 2021
CRA v4 moved the service worker out of the core and into a template.
Incorporate the template into the app.

facebook/create-react-app#9776
kfarnung added a commit to puzztool/puzztool that referenced this issue Feb 3, 2021
CRA v4 moved the service worker out of the core and into a template.
Incorporate the template into the app.

facebook/create-react-app#9776
@caprica-Six
Copy link

I have just tried a new CRA with service worker and works fine, we just need to add as mentioned above, the following:

  • add src/service-worker.js and serviceWorkerRegistration.js
  • add import * as serviceWorker from './serviceWorkerRegistration' in index.js
  • index.js - register serviceWorker.register();

Here's my package.json
`"dependencies": {

"@testing-library/jest-dom": "^5.11.4",
"@testing-library/react": "^11.1.0",
"@testing-library/user-event": "^12.1.10",
"react": "^17.0.1",
"react-dom": "^17.0.1",
"react-scripts": "4.0.3",
"web-vitals": "^1.0.1"

},`

re-Run yarn build.
Check build folder, you should now have the compiled service-worker.js & service-worker.js.map files.

Did the same for an existing project, imported new files, but updated react scripts to v4.
It now works fine.

Hope that helps someone.

@max-programming
Copy link

max-programming commented Mar 16, 2021

I have just tried a new CRA with service worker and works fine, we just need to add as mentioned above, the following:

  • add src/service-worker.js and serviceWorkerRegistration.js
  • add import * as serviceWorker from './serviceWorkerRegistration' in index.js
  • index.js - register serviceWorker.register();

Here's my package.json
`"dependencies": {

"@testing-library/jest-dom": "^5.11.4",
"@testing-library/react": "^11.1.0",
"@testing-library/user-event": "^12.1.10",
"react": "^17.0.1",
"react-dom": "^17.0.1",
"react-scripts": "4.0.3",
"web-vitals": "^1.0.1"

},`

re-Run yarn build.
Check build folder, you should now have the compiled service-worker.js & service-worker.js.map files.

Did the same for an existing project, imported new files, but updated react scripts to v4.
It now works fine.

Hope that helps someone.

Yeah that's an effective way to do it but with the command npx create-react-app my-app --template cra-template-pwa does that automatically. But without web vitals. Read more

@chrisfinazzo
Copy link

I had to uninstall and reinstall my global packages and update my project's Node version - mostly as a way of getting around CRA being really out of date - but I can confirm the pwa template approach @max-programming suggests does in fact, work.

hangy added a commit to hangy/azure-boards-estimate that referenced this issue May 10, 2021
It's been removed in the current create-react-app, see
facebook/create-react-app#9776
hangy added a commit to hangy/azure-boards-estimate-1 that referenced this issue Aug 3, 2021
It's been removed in the current create-react-app, see
facebook/create-react-app#9776
@psiservices-justin-sullard
Copy link
Contributor

Just a guess but perhaps the issue being encountered is related to an issue resolved in this PR: #11640

@stale
Copy link

stale bot commented Jan 8, 2022

This issue has been automatically marked as stale because it has not had any recent activity. It will be closed in 5 days if no further activity occurs.

@stale stale bot added the stale label Jan 8, 2022
@ghacosta
Copy link

This is still an issue.

@stale stale bot removed the stale label Jan 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests