score:0
Ditch all the other ideas and go for the cra-append-sw. It will save you a lot of time and pain.
It's as simple as installing the package, creating the service worker file in your main folder, and modifying your start
and build
commands in package.json file like this:
"start": "cra-append-sw --mode dev --env ./.env ./custom-sw.js && react-scripts start",
"build": "cra-append-sw --env ./.env ./custom-sw.js && react-scripts build",
And then you make sure to register the separate service worker in dev mode:
if ('serviceWorker' in navigator) {
console.log('Trying to register custom sw');
navigator.serviceWorker
.register('./firebase-messaging-sw.js')
.then(function (registration) {
console.log('Registration successful, scope is:', registration.scope);
})
.catch(function (err) {
console.log('Service worker registration failed, error:', err);
});
}
If like me you need access to environment variables in the service worker to initialize the firebase messaging module, this is the only simple option that doesn't require ejecting CRA.
I decided to post this answer because I myself overlooked the comments on the other answer and lost time unnecessarily looking for this solution.
score:2
If you would prefer disable the built in service-worker prior to your initial production deployment, then remove the call to serviceWorkerRegistration.register()
from src/index.js
and add your own service worker.
If your site/app has already been in production then swap it out for serviceWorkerRegistration.unregister().
After the user visits your page that has serviceWorkerRegistration.unregister()
, the service worker will be uninstalled and it might take up to 24 hours for the cache to be invalidated (depending on how the service worker is served)
The other option would be to eject and configure their built in solution.
Source: stackoverflow.com
Related Query
- Workaround for cache size limit in Create React App PWA service worker
- Add Service Worker to Create React App Project
- Adding custom Typescript type declaration for a node module in Create React App
- Create React App PWA ERROR: Does not register a service worker that controls page and start_url
- Create React App PWA - change caching strategy of service worker
- How to include custom JS files in to React create app
- Custom Proxy in Create React App using Typescript
- Create React App adding CORS header
- adding service worker to reactjs app
- adding a custom service worker to create-react-app
- react scripts build service worker not caching custom files
- How do I exclude url's from service worker scope in create react app, without having to eject?
- When I try to register a service worker file in react app it will give me a reference error: navigator is not defined
- Set a global service for import in Create React App
- update PWA react app (using CRA) with user clicked button through service worker
- React app on Vercel - 404 not found error after adding Web Worker
- Deploying a production build of React app to Google App Engine: Service worker registration fails
- Create a secure layer to hide web service from non-authenticated users, in an React App
- create react app with express on azure app service fails to start
- Adding a second html page in a Create React App project
- How to configre Create React Apps default Service Worker to cache assets that are not in react src Context (public Folder)?
- create react app not picking up .env files?
- How to create a React app directly in the current folder
- What is service worker in react js
- What is the difference between NextJs and Create React App
- How to create multiple page app using react
- When to use a react framework such as Next or Gatsby vs Create React App
- React create app hot reload is not always working on linux
- Cannot find module 'caniuse-lite/dist/unpacker/agents' when running create react app
- The react-scripts package provided by Create React App requires a dependency:
More Query from same tag
- Ant design 4.0.1 - Access methods of child form inside class component from parent component
- Using serverside HTML-templates with ReactJS
- React component to access sharepoint
- How to 'throw' my own 'onChange' event in React?
- React JS inheritance
- setState from json object gives undefined [React]
- Uncaught TypeError: Cannot read property 'props' of null in react with google-maps
- React set state variable
- Gatsby server-side rendering of page with client-only routes, based on window location
- Navbar Dropdown expands Nav instead of hovering over
- Add only one selection out of group to array
- What's the purpose of the 3rd argument in useReducer?
- Django HttpResponse returning object instead of stated string
- Redux state showing undefined data inserted in stateStore using axios
- Storybook - Cannot Mock a Nested React-Router-Dom Link without throwing endless NPM errors
- How to import something globally (only once) in a React Project
- how to fixed footer in bottom
- When a Function Component is rendered with a tag, is it treated as an object instance of that function?
- React - How to control export/import limitations
- How to deploy reactapp with heroku-22?
- Unable to Append new Child Components in react
- ReactJs: map state data to checkboxes
- GAE app.yaml settings for Flask backend with React and Flask frontend together on single deployment
- Stop selection of text in next span/div over in ReactJS
- How can I use a React component in other component?
- React efficient way to bind event to many dom elements
- How to track mobx global store changes in componentDidUpdate
- Can I use this scenario to handle protected routes in React?
- How to connect react to html using jsx phrase
- How to obtain the `initialQueryRef` in Relay?