0

ワークステーションを使って登録されたルートの下からクエリ文字列 "?screenSize ="を無視する方法はありますか!私は正規表現を使用することができますどのように私は下のシナリオでそれを書くだろうか?基本的には、screenSizeクエリーストリングが何であっても、キャッシュと一致するようにしています。 私はプラグインが適用されて表示されていない:ワークボックス使用時にキャッシュされたURLからURLクエリ文字列を無視する方法はありますか?

workboxSW.router.registerRoute('https://example.com/data/image?screenSize=980', 
workboxSW.strategies.cacheFirst({ 
    cacheName: 'mycache', 
    cacheExpiration: { 
     maxEntries: 50 
    }, 
    cacheableResponse: {statuses: [0, 200]} 
}) 
); 

cachedResponseWillBeUsedプラグインをしようとした後 enter image description here

答えて

1

あなたは戦略を設定するときに渡すcachedResponseWillBeUsed pluginを書き込むことによってこれを行うことができるようになります。

// See https://workboxjs.org/reference-docs/latest/module-workbox-runtime-caching.RequestWrapper.html#.cachedResponseWillBeUsed 
const cachedResponseWillBeUsed = ({cache, request, cachedResponse}) => { 
    // If there's already a match against the request URL, return it. 
    if (cachedResponse) { 
    return cachedResponse; 
    } 

    // Otherwise, return a match for a specific URL: 
    const urlToMatch = 'https://example.com/data/generic/image.jpg'; 
    return caches.match(urlToMatch); 
}; 

const imageCachingStrategy = workboxSW.strategies.cacheFirst({ 
    cacheName: 'mycache', 
    cacheExpiration: { 
     maxEntries: 50 
    }, 
    cacheableResponse: {statuses: [0, 200]}, 
    plugins: [{cachedResponseWillBeUsed}] 
}); 


workboxSW.router.registerRoute(
    new RegExp('^https://example\.com/data/'), 
    imageCachingStrategy 
); 
+0

urlがexample.com/data/{id}/image?screenSize=980の場合は、上記の方法でこのタイプのURLを使用する可能性はありますか? – seUser

+0

ハードコードされたURLをキャッシュキーとして使用するように応答を更新し、正規表現をより一般的にするようにしました。キャッシュのルックアップを決定する際にURLの '{id} '部分がどのように働くのか、実際にはわかりません - 考慮する必要がありますか?それは無視すべきでしょうか?それに応じてコードを調整する必要があります。 –

+0

はい、私は動的IDを使用する別のURLを持っている、今それは上記のURLのタイプのための上記と同じことをすることは可能ですか? 'ignoreSearch:true'を使用するのと同じように – seUser

関連する問題