简体   繁体   English

如何使用create-react-app从服务工作者中排除一些网址?

[英]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的解决方案:

  1. Add react-app-rewired to your project, eg: yarn add react-app-rewired --save-dev react-app-rewired添加到您的项目中,例如: yarn add react-app-rewired --save-dev
  2. Replace react-scripts in the scripts section of your package.json with react-app-rewired react-app-rewired替换package.json scripts部分中的react-scripts
  3. Place a file called config-overrides.js in the root of your project. 在项目的根目录中放置一个名为config-overrides.js的文件。 In this file you can add entries to the service-worker configuration. 在此文件中,您可以将条目添加到服务工作者配置。 For example: 例如:
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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM