簡體   English   中英

如何使用sw-precache runtime緩存googleapis.com

[英]How to cache googleapis.com using sw-precache runtimeCaching

我在網站上使用了素材圖標和roboto字體,並且一直在嘗試使用sw-precache runtimeCaching API對其進行緩存,但是它無法正常工作,我不確定自己是否做對了。 我需要我的服務人員從緩存中獲取這些文件。 這是我的代碼。 我將其集成到我的任務中:

gulp.task('create-sw', ['watchCss', 'watchJS'], callback => {
    preCache.write(path.join('.', 'sw.js'), {
        runtimeCaching: [{
            urlPattern: /^https:\/\/fonts\.googleapis\.com$/,
            handler: 'cacheFirst',
            options: {
                cache: {
                    maxEntries: 10,
                    name: 'google-apis'
            }
        },
    }],
        staticFileGlobs: [
            './script.js',
            './manifest.json',
            './app/**/*.{html,json}',
            './dist/**/*.{js,css,eot,ttf,woff,woff2}',
            './**/*.{png,jpeg,jpg,svg,gif}',
            './index.html',
        ],
        stripPrefix: '.',
        maximumFileSizeToCacheInBytes: 15000000,
    }, callback)
});

我正在嘗試緩存這些鏈接:

<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:regular,bold,italic,thin,light,bolditalic,black,medium&amp;lang=en">

這是我的GULP任務,為Instagram圖像生成緩存:

gulp.task('generate-service-worker', function(callback) {
var swPrecache = require('sw-precache');
var rootDir = 'app';

swPrecache.write(`${rootDir}/service-worker.js`, {
    staticFileGlobs: [rootDir + '/**/*.{php,js,html,css,png,jpg,gif,svg,eot,ttf,woff,json}', './'],
    stripPrefix: rootDir,
    directoryIndex: false,
    maximumFileSizeToCacheInBytes: 10097152,
    runtimeCaching: [{
        urlPattern: /^https:\/\/scontent\.cdninstagram\.com/,
        handler: 'networkFirst',
        options: {
            cache: {
                maxEntries: 50,
                name: 'instagram-images-cache'
            }
        }
    }]
}, callback);

});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM