โจ How to make your React app a PWA with Create React App 4

I am a full stack developer, YouTuber and blogger!
Search for a command to run...

I am a full stack developer, YouTuber and blogger!
No comments yet. Be the first to comment.
Hey all ๐๐ Webhooks are a powerful tool that allow applications to communicate with each other in real-time. They are often used to send notifications or updates from one system to another without requiring manual intervention. If you know the bas...

Hey everyone! It's been a while since I wrote my last article. But here I am with another topic that might be informational to you ๐. This time I am writing about why you should avoid the onclick attribute in your HTML and JavaScript code. This ar...

โ What and Why Memoriez โ This is a very important question because it makes things clear as to why this app is needed. I built Memoriez because I honestly found it hard to write and maintain journal/diary entries daily. It's not easy to maintain a j...

Explained with a simple project

๐ Hey developers! This post is about how you can sort import statements in your JavaScript/TypeScript/React/Node etc projects easily with Prettier in VS Code when you format the code. https://www.youtube.com/watch?v=QQWgN0_gUxI What will you achieve...

Create React App or react-scripts is a great tool to scaffold out our React Apps. And one of the best parts of it was that we got a readymade serviceWorker.js file with it which would help us make the React app offline so easily.
But with the react-scripts version 4, whenever we use the command npx create-react-app app-name, we don't get the service worker file.
But no worries, we can easily generate a React App with the service worker file again, but we have to use a template. So instead of just running create-react-app, we should add a flag to it and this is how the new command looks like
# JavaScript
npx create-react-app app-name --template cra-template-pwa
# TypeScript
npx create-react-app app-name --template cra-template-pwa-typescript
And that will generate two service worker files, one of them will be for the registration, and the other one the service worker itself.

In the index.js file, you'll get the import and the .unregister() method as before


You can simply change the .unregister() to .register() to register the service worker and now, when the app is built, it'll be capable to run offline. You'll see a button on the right side of your address bar if you are using Chrome or Edge or any other Chromium based browser.

On installing, it'll cache your app and it'll be able to run, even if you are online.
To convert an existing app, you can simply copy the code, create the required files and paste it from a gist that I created.
Also consider checking out the video on this topic if you want.
I hope this post helped you, make sure to ๐ like it and share it to others.
๐ฌ Let me know your React apps which are PWAs in the comments.
Thanks for reading! โ