[英]How can i exclude some urls from service worker with create-react-app?
I have app using create-react-app and service-worker generated on-a-build. 我有使用create-react-app和service-worker生成的应用程序。
I checked out a lot of issues and articles, but still not found usable answer. 我检查了很多问题和文章,但仍然找不到可用的答案。
What is a best practice to add something like urlBlockList to create-react-app service worker? 将urlBlockList之类的内容添加到create-react-app服务工作者的最佳实践是什么?
I think react eject
is not the most appropriate solution 我认为
react eject
不是最合适的解决方案
Resolved 解决
Best solution that I found - use https://github.com/liuderchi/sw-precache-cra and add unwanted urls to custom sw-config 我找到的最佳解决方案-使用https://github.com/liuderchi/sw-precache-cra并将不需要的URL添加到自定义sw-config
Example: 例:
// sw-config.js
module.exports = {
runtimeCaching: [
{
urlPattern: '/unwantedurl',
handler: 'networkFirst',
},
],
};
Here's a solution that works with CRA 3: 这是适用于CRA 3的解决方案:
yarn add react-app-rewired --save-dev
yarn add react-app-rewired --save-dev
react-scripts
in the scripts
section of your package.json
with react-app-rewired
react-app-rewired
替换package.json
scripts
部分中的react-scripts
config-overrides.js
in the root of your project. config-overrides.js
的文件。 In this file you can add entries to the service-worker configuration. module.exports = function override(config, env) {
const swPrecacheConfig = config.plugins.find(
plugin => plugin.constructor.name === "GenerateSW"
);
// Prevent some URLs from being cached by the service worker
const blacklist = swPrecacheConfig.config.navigateFallbackBlacklist;
blacklist.push(/\/oauth2\/authorization\//);
blacklist.push(/\/login\/oauth2\//);
return config;
};
The configuration above excludes urls with /oauth2/authorization/
and /login/oauth2/
from the service-worker. 上面的配置从服务工作者处排除带有
/oauth2/authorization/
和/login/oauth2/
url。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.