Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Runtime caching with inject Manifest #62

Closed
R0N1n-dev opened this issue Jul 5, 2023 · 1 comment
Closed

Runtime caching with inject Manifest #62

R0N1n-dev opened this issue Jul 5, 2023 · 1 comment

Comments

@R0N1n-dev
Copy link

Cannot seem to get runtime caching to allow for caching of api data.

vite.config.js

import { defineConfig } from "vite";
import Components from "unplugin-vue-components/vite";
import vue from "@vitejs/plugin-vue2";
import { VitePWA } from "vite-plugin-pwa";

//Runtime Caching

export default defineConfig({
  alias: {
    "@": require("path").resolve(__dirname, "src"),
  },
  plugins: [
    vue(),
    Components(),
    VitePWA({
      //Runtime
      workbox: {
        runtimeCaching: [
          {
            urlPattern: "https://www.themealdb.com/*", // Adjust the URL pattern to match your API endpoint
            handler: "NetworkFirst",
            options: {
              cacheName: "mealDb", // Name for your cache
              expiration: {
                maxEntries: 500,
                maxAgeSeconds: 60 * 60 * 24,
                purgeOnQuotaError: true,
              },
              cacheableResponse: { statuses: [0, 200] },
            },
          },
        ],
      },
      strategies: "injectManifest",
      srcDir: "src",
      filename: "sw.js",
      injectManifest: {
        globPatterns: [
          // all packaged resources are stored here
          "assets/*",
          // add HTML and other resources for the root directory
          "*.{svg,png,jpg}",
          "*.html",
          //"manifest.webmanifest",
          // be careful, not to add sw.js
        ],
      },
      manifest: {
        name: "Test Project",
        short_name: "Test",
        theme_color: "#ffffff",
        id: "/",
        description: "Vue PWA ",
        display: "standalone",
        background_color: "#ffffff",
        icons: [
          {
            src: "/android-chrome-192x192.png",
            sizes: "192x192",
            type: "image/png",
          },
          {
            src: "/android-chrome-512x512.png",
            sizes: "512x512",
            type: "image/png",
          },
          {
            src: "/android-chrome-512x512.png",
            sizes: "512x512",
            type: "image/png",
            purpose: "any maskable",
          },
        ],
      },
    }),
  ],
});

sw.js

import { cleanupOutdatedCaches, precacheAndRoute } from "workbox-precaching";

self.addEventListener("message", (event) => {
  if (event.data && event.data.type === "SKIP_WAITING") self.skipWaiting();
});
cleanupOutdatedCaches();
precacheAndRoute(self.__WB_MANIFEST);

Your proposed solution
@R0N1n-dev workbox-build only works in one mode, if you are using custom sw (injectManifest strategy), you MUST include runtimeCaching in your service worker logic, check this for example: https://github.com/elk-zone/elk/blob/main/service-worker/sw.ts#L56-L94

Those entries are the equivalent to your workbox.runtimeCaching handler: when using generateSW strategy, workbox-build will generate a registerRoute per plugin in the sw output.

In summary, when using generateSW workbox-build will be configured using workbox pwa plugin configuration entry; when using injectManifest strategy workbox-build will be configured using injectManifest pwa plugin configuration entry.

I really wish you guys wouldn't close the issue b4 confirmation that ur proposed solution is working, because it is not and now i will reopen it till i get a working solution

@brc-dd
Copy link
Collaborator

brc-dd commented Jul 5, 2023

#61 is reopened. There is no need to be rude.

@brc-dd brc-dd closed this as not planned Won't fix, can't repro, duplicate, stale Jul 5, 2023
@vite-pwa vite-pwa locked as too heated and limited conversation to collaborators Jul 5, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants