PWA and Create React App

Ernesto Jara Olveda
2 min readMar 20, 2021

--

We are going to create a progressive web app with CRA. the code will be at the end

To begin with let’s start a new cra.

$ npx create-react-app wb-example && cd wb-example

CRA on the newstest versions remove the serviceWorker.js, so let’s start with it

Copy the above code and paste it on yow serviceWorker.js. That code will let you register a service-worker on localhost.
Modified your index.js

// ADD THE IMPORT AT THE TOP
const config = {
/** @param {ServiceWorkerRegistration} registration */
onSuccess: (registration) => {
console.log("onSuccess", registration);
},
/** @param {ServiceWorkerRegistration} registration */
onUpdate: (registration) => {
console.log("onUpdate", registration);
}
}
register(config);

Now we have it as it used to be. it’s time to really work on what we came here

create a file call ./src/service-worker.js

build yow code

npm run build

now you can

$ serve -s build

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

No responses yet

Write a response