Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
644 views
in Technique[技术] by (71.8m points)

caching - Exclude path from workbox routing

I have a custom service worker built with Workbox. This is used on a WordPress site, and it keeps causing issues in the back-end when updates occur (with core or plugins). I'd like to configure my code such that when the user is on a /wp-admin page, the service worker does not serve cached files. Because I can't check window.location, I'm having trouble figuring out how to do this.

To be clear, I don't necessarily want to exclude /wp-admin scripts from caching, I specifically want to stop cached files from being served when the user is on a /wp-admin page. This will 99% of the time be one in the same, but for the hypothetical situation where the user is on the front-end and a wp-admin script is served, I would want that script to be served from cache.

My script is below. How can I achieve my goal?

import { clientsClaim, skipWaiting } from "workbox-core";
import { ExpirationPlugin } from "workbox-expiration";
import { precacheAndRoute } from "workbox-precaching";
import { registerRoute, setCatchHandler } from "workbox-routing";
import { CacheFirst, NetworkFirst } from "workbox-strategies";

skipWaiting();
clientsClaim();

/**
 * Precache "offline" page
 */
precacheAndRoute([
    {
        url: "/offline/",
        revision: __VERSION__,
    },
]);

/**
 * Return "offline" page when visiting pages offline that haven't been cached
 */
setCatchHandler(() => {
    return caches.match("/offline/");
});

/**
 * Cache WordPress content
 */
registerRoute(
    ({ url }) => {
        return (url.pathname && !url.pathname.match(/^/(wp-admin|wp-content|wp-includes|wp-json|wp-login)/) && url.pathname.match(//$/));
    },
    new NetworkFirst({
        cacheName: "clpl-content-cache",
        plugins: [
            new ExpirationPlugin({
                maxAgeSeconds: 7 * 24 * 60 * 60,
            }),
        ],
    })
);

/**
 * Cache CSS files
 */
registerRoute(
    ({ url }) => {
        return (url.pathname && url.pathname.match(/.css$/) && !url.pathname.match(/wp-admin|wp-includes|wp-json/));
    },
    new CacheFirst({
        cacheName: "clpl-css-cache",
        plugins: [
            new ExpirationPlugin({
                maxAgeSeconds: 7 * 24 * 60 * 60,
            }),
        ],
    })
);

/**
 * Cache JS files
 */
registerRoute(
    ({ url }) => {
        return (url.pathname && url.pathname.match(/.js$/) && !url.pathname.match(/wp-admin|wp-includes|wp-json/) && !url.pathname.match(/redirection/));
    },
    new CacheFirst({
        cacheName: "clpl-js-cache",
        plugins: [
            new ExpirationPlugin({
                maxAgeSeconds: 7 * 24 * 60 * 60,
            }),
        ],
    })
);

/**
 * Cache image files
 */
registerRoute(
    ({ url }) => {
        return (url.pathname && url.pathname.match(/.gif|jpeg|jpg|png|svg|webp$/) && !url.pathname.match(/wp-admin|wp-includes|wp-json/));
    },
    new CacheFirst({
        cacheName: "clpl-image-cache",
        plugins: [
            new ExpirationPlugin({
                maxAgeSeconds: 7 * 24 * 60 * 60,
            }),
        ],
    })
);

/**
 * Cache font files
 */
registerRoute(
    ({ url }) => {
        return (url.pathname && url.pathname.match(/.otf|ttf|woff|woff2$/) && !url.pathname.match(/wp-admin|wp-includes|wp-json/));
    },
    new CacheFirst({
        cacheName: "clpl-font-cache",
        plugins: [
            new ExpirationPlugin({
                maxAgeSeconds: 7 * 24 * 60 * 60,
            }),
        ],
    })
);
question from:https://stackoverflow.com/questions/65941140/exclude-path-from-workbox-routing

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...