0

我正在使用 nextJS 版本“10.0.9”和 next-pwa 版本“^5.0.6”,它们一起工作得很好,我的应用程序无法满足,但由于某些原因,我需要为所有路由添加前缀,所以我添加“basePath”属性并在 next.config.js 文件中将其设置为“/recharge-cards”,但之后我的应用程序变得可卸载,我尝试了很多方法,将范围和 subdomainPrefix 更改为“/recharge-cards”并更改我的 _document.js 上的文件路径,但似乎没有任何效果。

下面你会找到我的文件,所以帮我解决这个问题

清单.json

{
  "name": "next-pwa",
  "short_name": "next-pwa",
  "display": "standalone",
  "orientation": "portrait",
  "theme_color": "#FFFFFF",
  "background_color": "#FFFFFF",
  "start_url": "/recharge-cards",
  "icons": [
    {
      "src": "/android-chrome-192x192.png",
      "sizes": "192x192",
      "type": "image/png",
      "purpose": "maskable"
    },
    {
      "src": "/icon-512x512.png",
      "sizes": "512x512",
      "type": "image/png"
    }
  ]
}

next.config.js

const withPWA = require('next-pwa');

module.exports = withPWA({
    basePath: "recharge-cards",
    pwa: {
    dest: 'public',
  },
});

_document.js(头部)

<Head>
  <meta name='application-name' content={APP_NAME} />
  <meta name='apple-mobile-web-app-capable' content='yes' />
  <meta name='apple-mobile-web-app-status-bar-style' content='default' />
  <meta name='apple-mobile-web-app-title' content={APP_NAME} />
  <meta name='description' content={APP_DESCRIPTION} />
  <meta name='format-detection' content='telephone=no' />
  <meta name='mobile-web-app-capable' content='yes' />
  <meta name='theme-color' content='#FFFFFF' />
  <link rel='apple-touch-icon' sizes='180x180' href='/apple-touch-icon.png' />
  <link rel='manifest' href='/manifest.json' />
  <link rel='shortcut icon' href='/favicon.ico' />
</Head>

项目文件

在此处输入图像描述

4

2 回答 2

1

我也在为此苦苦挣扎,我的解决方案是确保start_url在我的清单中以/.

例如"start_url": "/recharge-cards/"

于 2021-10-06T03:05:19.377 回答
1

经过多次实验,我解决了它,下面你会找到我的解决方案

清单.json

{
  "name": "Nana digital goods",
  "short_name": "Nana Digital",
  "display": "standalone",
  "orientation": "portrait",
  "theme_color": "#227a40",
  "background_color": "#fff",
  "start_url": "/recharge-cards/",
  "icons": [
    {
      "src": "/recharge-cards/assets/images/android-chrome-192x192.png",
      "sizes": "192x192",
      "type": "image/png",
      "purpose": "maskable"
    },
    {
      "src": "/recharge-cards/assets/images/android-chrome-512x512.png",
      "sizes": "512x512",
      "type": "image/png"
    }
  ]
}

next.config.js

const withPWA = require('next-pwa');
const nextTranslate = require('next-translate');
const path = require('path');

module.exports = withPWA({
    trailingSlash: true,
    basePath: '/recharge-cards',
    async redirects() {
    return [
      {
        source: '/',
        destination: '/recharge-cards',
        basePath: false,
        permanent: false,
      },
    ]
  },
    webpackDevMiddleware: config => {
        config.watchOptions = {
            poll: 1000,
            aggregateTimeout: 300
        }
        return config
    },
    sassOptions: {
        includePaths: [path.join(__dirname, 'styles')]
    },
    publicRuntimeConfig: {},
  ...nextTranslate(),
    pwa: {
    dest: 'public',
        subdomainPrefix: '/recharge-cards/',
    scope: '/'
  },
});

_document.js(头部)

<meta charSet="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
<meta httpEquiv="X-UA-Compatible" content="IE=edge" />
<meta name='application-name' content={APP_NAME} />
<meta name='apple-mobile-web-app-capable' content='yes' />
<meta name='apple-mobile-web-app-status-bar-style' content='default' />
<meta name='apple-mobile-web-app-title' content={APP_NAME} />
<meta name='description' content={APP_DESCRIPTION} />
<meta name='format-detection' content='telephone=no' />
<meta name='mobile-web-app-capable' content='yes' />
<meta name='theme-color' content='#227a40' />
<title>{this.title}</title>
<link rel='apple-touch-icon' sizes='180x180' href='/recharge-cards/apple-touch-icon.png' />
<link rel='manifest' href='/recharge-cards/manifest.json' />
<link rel='shortcut icon' href='/recharge-cards/favicon.ico' />
<link rel="icon" type="image/png" sizes="32x32" href="/recharge-cards/assets/images/favicon-32x32.png" />
<link rel="icon" type="image/png" sizes="16x16" href="/recharge-cards/assets/images/favicon-16x16.png" />

项目文件

在此处输入图像描述

于 2021-04-01T15:33:02.463 回答