2

快速提问:

虽然浏览器将我的应用程序识别为有效的 PWA,并在移动浏览器上显示安装弹出窗口。在灯塔中审核时,它显示脱机时不响应,并返回200。我已经验证我的应用程序外壳是从应用程序缓存的(附截图)。

详细信息和背景: 我使用 js 和 jquery 在 laravel 上构建了 PWA。对于资产编译,我使用的是 laravel mix,对于 PWA 框架,我使用的是工作箱。还要明确一点,我没有使用任何 SPA 框架/库。

简而言之,我使用更简单的组件构建了一个自定义水疗中心。

对于 PWA 部分,我正在使用工作箱和 laravel mix。这是我的服务人员 manifest.json 和混合文件。请注意,我使用 sw.js 并使用 laravel-mix 生成 prod-sw.js。这个 prod-sw.js 用于生产

webpack.mix.js

require('laravel-mix-versionhash');
const mix = require('laravel-mix');

const workboxPlugin = require('workbox-webpack-plugin');
mix.webpackConfig({
    output: {
        filename: '[name].[contenthash].js',
        publicPath: ''
    }, 
    plugins: [
        new workboxPlugin.InjectManifest({
            swSrc: 'sw.js', // more control over the caching
            swDest: 'prod-sw.js', // the service-worker file name
            importsDirectory: 'service-worker', // have a dedicated folder for sw files
            exclude: [/\.map$/, /mix-manifest\.json$/, /mix\..*\.js$/, /manifest\.json$/, /service-worker\.js$/, /OneSignalSDKWorker\.js$/],
            include: [/template\..*\.html$/, /\.js$/, /app\..*\.css$/, /logo.png$/],
            templatedUrls: {
              '/pwa': 'version-' + process.env.MIX_APP_VERSION,
            },
        })
    ],
})

mix.sass('resources/assets/sass/homepage.scss', 'public/css/')
    .sass('resources/assets/sass/amp.scss', 'public/css/')
    .sass('resources/assets/sass/app.scss','public/css/');

if (mix.inProduction()){
    mix.versionHash();
}else{
    mix.sourceMaps();
}

sw.js

var version = 123;

workbox.core.setLogLevel(workbox.core.LOG_LEVELS.silent);
workbox.skipWaiting();
workbox.clientsClaim();

const precacheController = new workbox.precaching.PrecacheController();


// Cache all scripts and stylesheets using an extension whitelist
workbox.routing.registerRoute(new RegExp('.(?:css|html|ico)$'),
    workbox.strategies.staleWhileRevalidate({
        cacheName: 'static-resources',
        /* plugins: [
        new workbox.expiration.Plugin({
            maxEntries: 35,
            maxAgeSeconds:1*24*60*60,
        }),
        ], */
    })
);

workbox.routing.registerRoute(new RegExp('.*(?:pwa\.|vendors\.).*\.js$'),
    workbox.strategies.cacheFirst({
        cacheName: 'js-cache-' + version,
        plugins: [
        new workbox.expiration.Plugin({
            maxAgeSeconds: 8*24*60*60,
        }),
        ], 
    })
);

workbox.routing.registerRoute('/pwa',
    workbox.strategies.cacheFirst({
        cacheName: 'html-cache-' + version,
    })
);


workbox.precaching.precacheAndRoute(self.__precacheManifest);

清单.json

{
    "short_name": "Example",
    "name": "Example",
    "description": "Example App Description",
    "gcm_sender_id": "1234567890",
    "start_url" : "/pwa",
    "display": "standalone",
    "orientation": "portrait",
    "theme_color": "#FFFFFF",
    "background_color": "#FFFFFF",
    "icons": [
        {
            "src": "https://cdn.example.com/assets/icons/icon-48x48.png",
            "sizes": "48x48",
            "type": "image/png"
        }
    ]
}

附上清单文件、缓存存储和灯塔报告的快照供您参考。Manifest.json缓存存储灯塔

4

1 回答 1

0

我有同样的问题,但没有提供templateUrl财产。它仅在 devTools 中为我离线工作,但不是完全离线,并且灯塔给出了与您相同的结果。

于 2018-11-21T15:11:51.273 回答