Turn your Next.js application into a PWA

PWA can make your app look native, faster, updatable and offline-ready. In this post, we learn how to make a PWA with a Next.js application.

·4 min read
Cover Image for Turn your Next.js application into a PWA

PWAs can give your applications a feeling of being native apps: in some ways, they can even be better.

  1. Give your application a native feeling:
    • For example, if you open the Twitter website, have you noticed that you can install it on your system just like a native app? You can do the same with your own application and let your users access your app straight from their operating systems.
  2. Prevent outdated and cached client bundles:
    • PWAs can help you prevent caching issues of outdated client-side bundles when you deploy a new version of your app: in fact, thanks to service workers, the clients running your app can get notified when you deploy a new version: you'll be able to ask your users to update the application and avoid issues such as an outdated cached Javascript bundle.

In this guide, I want to show you how to turn your existing Next.js application into a PWA.

Installing next-pwa

The package next-pwa is a zero-config package to build your application as a PWA. It's extremely simple to use, and does most of the job.

First, we want to install the package with npm:

npm i -D next-pwa

Tweaking your Next.js to make a PWA

After installing the next-pwa package, we'll need to decorate the Next.js configuration:

next.config.js
import withPWA from 'next-pwa'; import runtimeCaching from 'next-pwa/cache.js'; const isProduction = process.env.NODE_ENV === 'production'; const config = { // here goes your Next.js configuration }; const nextConfig = withPWA({ dest: 'public', disable: !isProduction, runtimeCaching })( config ); export default nextConfig;

The above is enough to make your application a PWA!

Ignoring build files from Git

Because of the various JS files generated during the build process, you may want to ignore these an avoid checking them in your Git repository. To do so, add the following lines yo your .gitignore file:

public/sw.js public/workbox-*.js

Adding the manifest.json file

The manifest.json file contains the configuration for the PWA application that browsers can read, and we can create it in the public folder, so that it gets added at the root of your website.

For example, you can take the below and use it with your application's configuration.

manifest.json
{ "name": "My PWA", "short_name": "my-pwa-app", "icons": [ { "src": "/icons/android-chrome-192x192.png", "sizes": "192x192", "type": "image/png", "purpose": "any maskable" }, { "src": "/icons/android-chrome-384x384.png", "sizes": "384x384", "type": "image/png" }, { "src": "/icons/icon-512x512.png", "sizes": "512x512", "type": "image/png" } ], "theme_color": "#FFFFFF", "background_color": "#FFFFFF", "start_url": "/dashboard", "display": "standalone", "orientation": "portrait" }

This also needs to be referenced within the <Head> component:

<link rel="manifest" href="/manifest.json" />

Notifying clients when a new version is available

Underneath, next-pwa uses workbox, a library by Google that abstracts working with PWAs: we will use workbox to detect when a new version of the application is available.

To notify customers that a new version is updated, we will create a component that will display a popup which will prompt them to update the application.

The components below Modal and Button aren't defined, but simply replace them with your implementations.

PwaUpdater.tsx
declare global { interface Window { wb: { messageSkipWaiting(): void; register(): void; addEventListener(name: string, callback: () => unknown); } } } const PwaUpdater = () => { const [isOpen, setIsOpen] = useState(false); const onConfirmActivate = () => wb.messageSkipWaiting(); useEffect(() => { wb.addEventListener('controlling', () => { window.location.reload(); }); wb.addEventListener('waiting', () => setIsOpen(true)); wb.register(); }, []); return ( <Modal isOpen={isOpen} setIsOpen={setIsOpen} heading={'New version available!'} > <div> Hey, a new version is available! Please click below to update. </div> <Button onClick={onConfirmActivate}>Reload and update</Button> <Button oncClick={() => setIsOpen(false)}>Cancel</Button> </Modal> ); } export default PwaUpdater;

Because this component uses window, we need to ensure not to render it on the server. For example, if we add this to our root app, we would do something like this:

_app.tsx
import dynamic from 'next/dynamic'; const PwaUpdater = dynamic(() => import(`./PwaUpdater`), { ssr: false }); function App({Component, appProps}: AppProps) { return ( <YourProvider> <PwaUpdater /> <Component {...appProps} /> </YourProvider> ); }

And that's pretty much it!



Read more about Tutorials

Cover Image for Building an AI Writer SaaS with Next.js and Supabase

Building an AI Writer SaaS with Next.js and Supabase

·57 min read
Learn how to build an AI Writer SaaS with Next.js and Supabase - from writing SEO optimized blog posts to managing subscriptions and billing.
Cover Image for Announcing the Data Loader SDK for Supabase

Announcing the Data Loader SDK for Supabase

·8 min read
We're excited to announce the Data Loader SDK for Supabase. It's a declarative, type-safe set of utilities to load data into your Supabase database that you can use in your Next.js or Remix apps.
Cover Image for Adding AI capabilities to your Next.js SaaS with Supabase and HuggingFace

Adding AI capabilities to your Next.js SaaS with Supabase and HuggingFace

·20 min read
In this tutorial, we will learn how to use add AI capabilities to your SaaS using Supabase Vector, HuggingFace models and Next.js Server Components.
Cover Image for Building an AI-powered Blog with Next.js and WordPress

Building an AI-powered Blog with Next.js and WordPress

·17 min read
Learn how to build a blog with Next.js 13 and WordPress and how to leverage AI to generate content.
Cover Image for Using Supabase Vault to store secrets

Using Supabase Vault to store secrets

·6 min read
Supabase Vault is a Postgres extension that allows you to store secrets in your database. This is a great way to store API keys, tokens, and other sensitive information. In this tutorial, we'll use Supabase Vault to store our API keys
Cover Image for Introduction to Next.js Server Actions

Introduction to Next.js Server Actions

·9 min read
Next.js Server Actions are a new feature introduced in Next.js 13 that allows you to run server code without having to create an API endpoint. In this article, we'll learn how to use them.