You need to enable JavaScript to run this app. Next.js PWA首次离线点击链接不显示离线页面问题求助
优惠活动
大模型
产品
解决方案
定价
更多

Next.js PWA首次离线点击链接不显示离线页面问题求助

离线页面跳转异常问题排查(Next.js + next-pwa)

使用next-pwa库,参考官方offline-fallback示例开发,遇到以下问题:

  • 网站加载完成后断开网络,点击任意链接时,预期显示离线页面但实际报错
  • 刷新页面后,离线页面可正常显示

配置文件

next.config.js

const withPWA = require("next-pwa")({
  dest: "public",
  swSrc: "service-worker.js",
});

const nextConfig = withPWA({
  images: {
    domains: [],
  },
  reactStrictMode: true,
  optimizeFonts: false,
});
module.exports = nextConfig;

service-worker.js

import { clientsClaim } from "workbox-core";
import { matchPrecache, precacheAndRoute } from "workbox-precaching";
import { registerRoute, setCatchHandler } from "workbox-routing";
import { NetworkFirst, CacheFirst } from "workbox-strategies";
import { CacheableResponsePlugin } from "workbox-cacheable-response";
import { ExpirationPlugin } from "workbox-expiration";

self.skipWaiting(); // DONT WAIT FOR USER TO CLOSE ALL TAB
clientsClaim(); //  take control ASAP

const WB_MANIFEST = self.__WB_MANIFEST;
// Precache fallback route and image
WB_MANIFEST.push({
  url: "/fallback",
  revision: "1234567890",
});
precacheAndRoute(WB_MANIFEST);

// cache for image
registerRoute(
  ({ request }) => request.destination === "image",
  new CacheFirst({
    cacheName: "images",
    plugins: [
      new CacheableResponsePlugin({
        statuses: [0, 200],
      }),
      new ExpirationPlugin({
        maxEntries: 60,
        maxAgeSeconds: 60 * 60,
      }),
    ],
  })
);

registerRoute(
  /.*/i,
  new NetworkFirst({
    cacheName: "others",
    plugins: [
      new ExpirationPlugin({
        maxEntries: 32,
        maxAgeSeconds: 86400,
      }),
    ],
  })
);

// This "catch" handler is triggered when any of the other routes fail to
// generate a response.
setCatchHandler(({ event }) => {
  switch (event.request.destination) {
    case "document":
      // If using precached URLs:
      return matchPrecache("/fallback");

      break;
    case "image":
      // If using precached URLs:
      return matchPrecache("/static/images/fallback.png");

    case "font":
    // If using precached URLs:
    // return matchPrecache(FALLBACK_FONT_URL);
    //return caches.match('/static/fonts/fallback.otf')
    //break
    default:
      // If we don't have a fallback, just return an error response.
      return matchPrecache("/fallback");
  }
});

manifest.json

{
  "$schema": "https://json.schemastore.org/web-manifest-combined.json",
  "name": "Title",
  "short_name": "Title",
  "start_url": "/",
  "display": "standalone",
  "background_color": "#fff",
  "description": "SOME TEST",
  "icons": [
    {
      "src": "images/touch/manifestIcon48.png",
      "sizes": "48x48",
      "type": "image/png"
    },
    {
      "src": "images/touch/manifestIcon72.png",
      "sizes": "72x72",
      "type": "image/png"
    },
    {
      "src": "images/touch/manifestIcon96.png",
      "sizes": "96x96",
      "type": "image/png"
    },
    {
      "src": "images/touch/manifestIcon96.png",
      "sizes": "144x144",
      "type": "image/png"
    },
    {
      "src": "images/touch/manifestIcon48.png",
      "sizes": "168x168",
      "type": "image/png"
    },
    {
      "src": "images/touch/manifestIcon192.png",
      "sizes": "192x192",
      "type": "image/png"
    }
  ]
}

离线页面(Fallback组件)

import React from "react";
const Fallback = () => {
  return (
    <div className="flex flex-col h-screen w-screen items-center justify-center ">
      <div className="flex w-screen justify-center items-center  ">
        <img src="/fallback.png" className="h-[80px] w-[80px]" />
      </div>
      <div className="items-center justify-center flex font-raleway font-[700] text-[35px] mt-2">
        No Internet
      </div>

      <div className="items-center justify-center text-center mx-10 flex font-raleway text-[20px] mt-5 leading-8">
        <p>
          Looks like you are not connected to the network. Check your setting
          and try again.
        </p>
      </div>
    </div>
  );
};

export default Fallback;

已尝试的排查操作

  • 确认Service Worker已激活并正常运行
  • 确认离线页面及图片已被预缓存

请求帮忙排查问题原因。

内容的提问来源于stack exchange,提问作者Alpha

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 17:17:48