next-pwa

repository·master·Indexed 20 days ago

https://github.com/ducanhgh/next-pwa

A suite of tools powered by Workbox for adding Progressive Web App (PWA) capabilities to Next.js applications. The package @ducanh2912/next-pwa provides features such as front-end navigation caching (via cacheOnFrontEndNav), custom service worker logic injection, manual PWA lifecycle and registration control, and offline fallbacks for routes, images, or fonts. It also supports integration with next-i18next and custom servers like Fastify.

Tokens
13.1K
Snippets
46
Records
57
Agent score
71%

What's inside next-pwa

  1. Control the PWA lifecycle and service worker registration

    master

    By default, next-pwa handles service worker registration automatically. However, you can gain more control by manually managing the registration process and listening to lifecycle events. This approach allows you to:

    1. Control Registration: Decide exactly when the service worker is registered instead of letting it happen automatically.
    2. Handle Lifecycle Events: Add event listeners to react to service worker changes.
    3. Prompt for Updates: Implement a pattern to prompt users to reload the page when a new version of the service worker is available (e.g., using Workbox advanced recipes).
  2. Understand default precaching behavior

    master

    By default, @ducanh2912/next-pwa uses a specific set of regex patterns to determine which files are precached. The default exclusion pattern is:

    [//_next/static/.*(?<!\.p)\.woff2/, /^\.map$/, /^manifest.*\.js$/]

    Excluded by default:

    • Fonts: next/font subsets are excluded to prevent the precache manifest from becoming too large.
    • .map files: Source maps are excluded as they are not required by end-users.
    • .manifest.*.js files: Webpack-generated manifests (including the precache manifest itself) are excluded to avoid redundant or unnecessary caching.
  3. Understand offline support in @ducanh2912/next-pwa

    master

    @ducanh2912/next-pwa provides built-in offline capabilities:

    • Automatic Precaching: JavaScript, CSS, and image assets are precached out of the box.
    • Runtime Caching: Pages are cached automatically as you visit them.
    • Offline Fallbacks: You can explicitly enable offline fallbacks to provide custom handling for assets or pages that have not yet been cached.
  4. Getting started with the PWA suite documentation

    master

    To run the documentation site locally, follow these steps from the root folder of the project:

    1. Install dependencies using pnpm i.
    2. Build the packages using pnpm build.
    3. Navigate to the docs directory and start the development server with pnpm dev.
    4. Access the documentation at http://localhost:3000.

    Note: These commands must be run in the root folder of the repository.

    pnpm i
    pnpm build
    cd docs && pnpm dev
  5. Setup @ducanh2912/next-pwa in Next.js

    master

    To enable PWA support in your Next.js project, wrap your next.config.js with the withPWA function exported as the default from @ducanh2912/next-pwa. At a minimum, you should provide the dest option to specify the directory where the service worker files will be generated (typically public).

    const withPWA = require("@ducanh2912/next-pwa").default({
      dest: "public",
    });
    
    module.exports = withPWA({
      // Your Next.js config
    });
  6. Implement Web Push with next-pwa using a custom worker

    master

    This example demonstrates how to integrate Web Push notifications into a Next.js application by utilizing a custom service worker.

    Workflow Summary:

    1. Generate VAPID Keys: Use the provided script to generate the necessary VAPID keys for secure push communication.
    2. Configure Environment: Store the generated keys in a .env file so the application can access them.
    3. Subscription Management: In a production environment, you should send the user's subscription data to your backend server to enable server-initiated notifications.

    Note on Service Workers: When using custom workers for Web Push, ensure your .gitignore is configured to prevent build artifacts from being committed.

    # 1. Bootstrap the example
    npx create-next-app --example https://github.com/DuCanhGH/next-pwa/tree/master/examples/web-push web-push-app
    
    # 2. Generate VAPID keys
    pnpm vapid
    
    # 3. Build and start
    pnpm build
    pnpm start
  7. Serve a Next.js PWA with a custom server

    master

    If you are not using Next.js's built-in server, you can use a custom server (like fastify) to serve your PWA assets. The custom server must be configured to serve sw.js and precache scripts statically from the public directory to ensure full offline support and proper PWA functionality.

    Note: It is still recommended to use Next.js's built-in server whenever possible.

    # To run the custom server example:
    cd examples/custom-server
    pnpm build
    pnpm start
  8. Recommended .gitignore for next-pwa

    master

    When using next-pwa, the build process generates service worker files and precache manifests in the public directory. To prevent these generated files from being committed to your version control system, add the following patterns to your .gitignore file:

    **/public/precache.*.js
    **/public/sw.js
  9. Bootstrap the custom worker example using create-next-app

    master

    You can bootstrap a new Next.js project using the custom worker example template with any of the following package managers:

    # Using npx
    npx create-next-app --example https://github.com/DuCanhGH/next-pwa/tree/master/examples/custom-worker custom-worker-app
    
    # Using yarn
    yarn create next-app --example https://github.com/DuCanhGH/next-pwa/tree/master/examples/custom-worker custom-worker-app
    
    # Using pnpm
    pnpm create next-app --example https://github.com/DuCanhGH/next-pwa/tree/master/examples/custom-worker custom-worker-app
    
    # Using bun
    bun create next-app --example https://github.com/DuCanhGH/next-pwa/tree/master/examples/custom-worker custom-worker-app
  10. Bootstrap the lifecycle and register workflow example

    master

    You can bootstrap a new Next.js application using the lifecycle and register workflow pattern by using the create-next-app command with the specific example template from this repository.

    npx create-next-app --example https://github.com/DuCanhGH/next-pwa/tree/master/examples/lifecycle lifecycle-app
  11. Enable caching on front-end navigation

    master

    To cache assets during client-side (front-end) navigation in Next.js, you no longer need custom implementation code. Since version 5.2.1 of @ducanh2912/next-pwa, you can enable this behavior directly via the configuration object by setting cacheOnFrontEndNav to true.

    // next.config.js
    const withPWA = require('@ducanh2912/next-pwa')({
      cacheOnFrontEndNav: true,
      // other config options
    })
    
    module.exports = withPWA({
      // your next.config.js config
    })