Next-Pwa SW.JS多余的

发布于 2025-02-04 15:15:59 字数 2637 浏览 4 评论 0原文

我正在开发运行Next-PWA的NextJS应用程序,但仅在本地环境上工作。但是,当我部署到Vercel时,似乎sw.js是多余的。我花了很多时间为此,但无论如何都会卡住。

这是我的next.config.js

const { withPlugins } = require("next-compose-plugins");
const withPWA = require("next-pwa");
const runtimeCaching = require("next-pwa/cache");

const nextConfig = {
  env: {
    API_URL: "https://multikart-graphql-dun.vercel.app/server.js",
  },

  // if you want to run with local graphQl un-comment below one and comment the above code
  // env: {
  //   API_URL: "http://localhost:4000/graphql",
  // },

  webpack(config, options) {
    config.module.rules.push({
      test: /\.svg$/,
      issuer: {
        test: /\.(js|ts)x?$/,
      },
      use: ["@svgr/webpack"],
    });
    return config;
  },
};

module.exports = withPlugins(
  [
    [withImages],
    [
      withPWA,
      {
        // reactStrictMode: true,
        pwa: {
          dest: "public",
          sw: "sw.js",
          // swSrc: "service-worker.js",
          // scope: "/",
          // register: true,
          // skipWaiting: true,
          // buildExcludes: [/middleware-manifest.json$/],
        },
      },
    ],
  ],
  [nextConfig]
);

,这是我的subtest.json

{
  "short_name": "App",
  "name": "App: App",
  "start_url": "/",
  "display": "standalone",
  "theme_color": "#b8d8e6",
  "background_color": "#b8d8e6",
  "prefer_related_applications": true,
  "icons": [
    {
      "src": "favicon.ico",
      "sizes": "64x64 32x32 24x24 16x16",
      "type": "image/x-icon"
    },
    {
      "src": "/icons/imagedummy192.png",
      "type": "image/png",
      "sizes": "192x192",
      "purpose": "any maskable"
    },
    {
      "src": "/icons/imagedummy512.png",
      "type": "image/png",
      "sizes": "512x512"
    }
  ]
}

评论了config的其余部分(runtimecaching,skipwaiting,regission)

https://github.com/vithub.com/vercel/vercel/next.js/issues/30692 and https://github.com/shadowwalker/next-pwa/next-pwa/issues/sissues/295 < /a>

我还遵循此回购上的基本配置: https:/https:/ /Github.com/shadowwalker/next-pwa/tree/master/examples/minimal

我也关闭了adblocker

,但似乎无效,但sw.js始终有多余的

人可以帮助,我做错了什么?

I am working on NextJS app that runs next-pwa but only works on local environment. But when I deploy to vercel, it seems that sw.js is redundant. I spent so many hours on this but feel stuck anyway.

Here's my next.config.js

const { withPlugins } = require("next-compose-plugins");
const withPWA = require("next-pwa");
const runtimeCaching = require("next-pwa/cache");

const nextConfig = {
  env: {
    API_URL: "https://multikart-graphql-dun.vercel.app/server.js",
  },

  // if you want to run with local graphQl un-comment below one and comment the above code
  // env: {
  //   API_URL: "http://localhost:4000/graphql",
  // },

  webpack(config, options) {
    config.module.rules.push({
      test: /\.svg$/,
      issuer: {
        test: /\.(js|ts)x?$/,
      },
      use: ["@svgr/webpack"],
    });
    return config;
  },
};

module.exports = withPlugins(
  [
    [withImages],
    [
      withPWA,
      {
        // reactStrictMode: true,
        pwa: {
          dest: "public",
          sw: "sw.js",
          // swSrc: "service-worker.js",
          // scope: "/",
          // register: true,
          // skipWaiting: true,
          // buildExcludes: [/middleware-manifest.json$/],
        },
      },
    ],
  ],
  [nextConfig]
);

and here's my manifest.json

{
  "short_name": "App",
  "name": "App: App",
  "start_url": "/",
  "display": "standalone",
  "theme_color": "#b8d8e6",
  "background_color": "#b8d8e6",
  "prefer_related_applications": true,
  "icons": [
    {
      "src": "favicon.ico",
      "sizes": "64x64 32x32 24x24 16x16",
      "type": "image/x-icon"
    },
    {
      "src": "/icons/imagedummy192.png",
      "type": "image/png",
      "sizes": "192x192",
      "purpose": "any maskable"
    },
    {
      "src": "/icons/imagedummy512.png",
      "type": "image/png",
      "sizes": "512x512"
    }
  ]
}

I commented the rest of the config (runTimeCaching, skipWaiting, register), since it makes no difference

I have followed https://github.com/vercel/next.js/issues/30692 and https://github.com/shadowwalker/next-pwa/issues/295

I also have followed basic config on this repo: https://github.com/shadowwalker/next-pwa/tree/master/examples/minimal

I also did turn off the adblocker

but nothing seems to work, and sw.js always redundant

Can anybody help, What did I do wrong? ????

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

2025-02-11 15:16:00

我找到了这个问题的答案。

因此,为了使服务工作者有效,我需要在生产构建时构建服务工作者(npx Next Build)。

构建完成后,我在.gitignore上删除服务工作者,以使其部署到存储库。

然后瞧。下一-PWA起作用。虽然,需要10分钟才能建立服务工作者。

I found an answer to the question.

So to make the service worker works, I needed to build the service worker on production build ( npx next build ).

After the build complete, I remove the service worker on .gitignore to make it deployed to the repository.

And then voilaa.. the next-pwa works. Although, it needs 10 minutes for service worker success to build.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文