shadowwalker / next-pwa

Zero config PWA plugin for Next.js, with workbox 🧰
MIT License
3.71k stars 312 forks source link

withPWA precaches on every route, but I want it to do that only on specific routes #494

Open EricDotSmith opened 9 months ago

EricDotSmith commented 9 months ago

The main issue I'm trying to figure out right now is I noticed workbox/the service worker that withPWA sets up, starts precaching everything I specified but on EVERY route, such as our login page. But I would like to restrict this to just precache when on specific routes like /test or /other so it doesn't bog down my entire website for no reason.

Is there a way to do this. Here is my current config

await import("./src/env.mjs");
import { nanoid } from "nanoid";
import i18n from "./next-i18next.config.js";
import withPWA from "next-pwa";

const buildId = nanoid();

const pwa = withPWA({
  dest: "public",
  scope: "/test",
  additionalManifestEntries: [
    { url: "/logo.svg", revision: "1" },

    { url: "/test", revision: "1" },
    { url: "/other", revision: "1" },

    { url: "/en/test", revision: "1" },
    { url: "/en/other", revision: "1" },
    { url: `_next/data/${buildId}/en/test.json`, revision: "1" },
    { url: `_next/data/${buildId}/en/other.json`, revision: "1" },

    { url: "/es/test", revision: "1" },
    { url: "/es/other", revision: "1" },
    { url: `_next/data/${buildId}/es/test.json`, revision: "1" },
    { url: `_next/data/${buildId}/es/other.json`, revision: "1" },

    { url: `_next/data/${buildId}/en.json`, revision: "1" },
    { url: `_next/data/${buildId}/es.json`, revision: "1" },

    // ... other routes
  ],
});

/** @type {import("next").NextConfig} */
const config = pwa({
  generateBuildId: () => buildId,
  reactStrictMode: true,

  /**
   * If you have `experimental: { appDir: true }` set, then you must comment the below `i18n` config
   * out.
   *
   * @see https://github.com/vercel/next.js/issues/41980
   */
  i18n: i18n.i18n,
});

export default config;