如何缓存动态 URL - Workbox - Service Worker
How to cache a dynamic URL - Workbox - Service Worker
我正在尝试缓存动态 URL。
像这样工作正常:
workbox.routing.registerRoute(
'https://www.testestest.com/?Q=Start',
workbox.strategies.networkFirst({
networkTimeoutSeconds: 3,
cacheName: 'dynamicentry1',
plugins: [
new workbox.expiration.Plugin({
maxEntries: 50,
}),
],
})
);
但是如果我想得到这样的动态内容:
workbox.routing.registerRoute(
'https://www.testestest.com/?Q=Start&testid=',
workbox.strategies.networkFirst({
networkTimeoutSeconds: 3,
cacheName: 'dynamicentry1',
plugins: [
new workbox.expiration.Plugin({
maxEntries: 50,
}),
],
})
);
如果您单击 https://www.testestest.com/?Q=Start&testid=1,该站点应该被动态缓存。
我该怎么做?
谢谢大家
您可以使用 regular expression instead of a string and get the sort of behavior that you're looking for. There's more information in the Workbox documentation at https://developers.google.com/web/tools/workbox/guides/route-requests#matching_a_route_with_a_regular_expression
设置路线
workbox.routing.registerRoute(
new RegExp('/\?Q=Start'),
workbox.strategies.networkFirst({
networkTimeoutSeconds: 3,
cacheName: 'dynamicentry1',
plugins: [
new workbox.expiration.Plugin({
maxEntries: 50,
}),
],
})
);
我正在尝试缓存动态 URL。
像这样工作正常:
workbox.routing.registerRoute(
'https://www.testestest.com/?Q=Start',
workbox.strategies.networkFirst({
networkTimeoutSeconds: 3,
cacheName: 'dynamicentry1',
plugins: [
new workbox.expiration.Plugin({
maxEntries: 50,
}),
],
})
);
但是如果我想得到这样的动态内容:
workbox.routing.registerRoute(
'https://www.testestest.com/?Q=Start&testid=',
workbox.strategies.networkFirst({
networkTimeoutSeconds: 3,
cacheName: 'dynamicentry1',
plugins: [
new workbox.expiration.Plugin({
maxEntries: 50,
}),
],
})
);
如果您单击 https://www.testestest.com/?Q=Start&testid=1,该站点应该被动态缓存。 我该怎么做?
谢谢大家
您可以使用 regular expression instead of a string and get the sort of behavior that you're looking for. There's more information in the Workbox documentation at https://developers.google.com/web/tools/workbox/guides/route-requests#matching_a_route_with_a_regular_expression
设置路线workbox.routing.registerRoute(
new RegExp('/\?Q=Start'),
workbox.strategies.networkFirst({
networkTimeoutSeconds: 3,
cacheName: 'dynamicentry1',
plugins: [
new workbox.expiration.Plugin({
maxEntries: 50,
}),
],
})
);