The default service worker that comes with create-react-app doesn't allow for very much configuration. We'll replace that default service worker in two ways.

First, we'll create a blank service worker js file, and use that as our custom service worker.

Next, we'll re-write the default webpack config with react-app-rewired, and utilize the InjectManifest workbox webpack plugin. This will allow us to create a totally custom service worker that still allows us to use workbox, without ejecting our app.

Install:

    "react-app-rewired": "^1.6.2",
"react-scripts": "^2.1.1",
"serve": "^10.1.1",
"workbox-webpack-plugin": "^3.6.3"

Create a config-overrides.js in root folder:

Default create-react-app using 'GenerateSW' function, we want to override with 'InjectManifest' function.

/* config-overrides.js */

const WorkboxWebpackPlugin = require('workbox-webpack-plugin')

module.exports = function override(config, env) {
config.plugins = config.plugins.map(plugin => {
if(plugin.constructor.name === 'GenerateSW') {
return new WorkboxWebpackPlugin.InjectManifest({
swSrc: './src/sw.js', // point to the sw.js file we will create later
swDest: 'service-worker.js' // will be generatedin pulbic folder
})
} return plugin
}) return config
}

Update package.json:

  "scripts": {
"start": "react-app-rewired start",
"build": "react-app-rewired build",
"build:serve": "serve -s build",
"test": "react-app-rewired test",
"eject": "react-scripts eject"
},

Create src/sw.js:

workbox.skipWaiting();
workbox.clientsClaim();

Run:

npm run build

[React] Override webpack config for create-react-app without ejection的更多相关文章

  1. react+babel+webpack初试

    在上一篇,我们简单学习了webpack学习,现在这里我们简单学习一下react+babel+webpack,进行编译react语法jsx以及结合es6写法. 这里我就简单的直接上demo: packa ...

  2. 如何扩展 Create React App 的 Webpack 配置

    如何扩展 Create React App 的 Webpack 配置  原文地址https://zhaozhiming.github.io/blog/2018/01/08/create-react-a ...

  3. 使用create react app教程

    This project was bootstrapped with Create React App. Below you will find some information on how to ...

  4. 深入 Create React App 核心概念

    本文差点难产而死.因为总结的过程中,多次怀疑本文是对官方文档的直接翻译和简单诺列:同时官方文档很全面,全范围的介绍无疑加深了写作的心智负担.但在最终的梳理中,发现走出了一条与众不同的路,于是坚持分享出 ...

  5. 在 .NET Core 5 中集成 Create React app

    翻译自 Camilo Reyes 2021年2月22日的文章 <Integrate Create React app with .NET Core 5> [1] Camilo Reyes ...

  6. react webpack.config.js 入门学习

    在学习react 的时候必然会用到webpack打包工具,webpack的快速入门另外一篇文章中有记录,这里只记录webpack.config.js文件,因为每个项目下都必须配置,通俗的讲,它的作用就 ...

  7. Create React App 安装less 报错

    执行npm run eject 暴露模块 安装 npm i  less less-loader -D 1.打开 react app 的 webpack.config.js const sassRege ...

  8. tap news:week5 0.0 create react app

    参考https://blog.csdn.net/qtfying/article/details/78665664 先创建文件夹 安装create react app 这个脚手架(facebook官方提 ...

  9. 利用 Create React Native App 快速创建 React Native 应用

    本文介绍的 Create-React-Native-App 是非常 Awesome 的工具,而其背后的 Expo 整个平台也让笔者感觉非常的不错.笔者目前公司是采用 APICloud 进行移动应用开发 ...

随机推荐

  1. Codeforces Round #355 (Div. 2) C 预处理

    C. Vanya and Label time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  2. 兼容firstChild和firstElementChild

    1.nextSibling和nextElementSibling 顾名思义,就是找下一个节点 nextSibling在低版本浏览器中可以顺利找到下一个元素节点,而在正常浏览器中找到的包含中间空格. n ...

  3. pagination用法

    pagination用法: 1.html  要用两层div <script src="${app }/pc/js/media/pagination.js"></s ...

  4. ui_modules和ui_method

    ## 06ui.py #coding:utf-8 import tornado.httpserver import tornado.ioloop import tornado.options impo ...

  5. iOS开发者证书申请及应用上线发布详解

    一个小教程登录开发者中心:http://developer.apple.com/ 第零部分:本地生成密钥1.打开mac的钥匙串访问  2.选择钥匙串的证书助理(有些可能是英文的)   3.点击继续后存 ...

  6. input 输入框 光标错位问题 、移动端输入框/input框光标错位问题、微信H5输入框/input框光标错位问题

    在IOS系统下的问题: 搜索出的建议如下: 你应该是用fixed定位做的弹出框,弹出框里面有文本框.fixed在ios上兼容不友好,会造成光标乱跳.建议用better-scroll,或者用absolu ...

  7. 经常用的Jquery图片轮转

    1.HTML结构 <div class="main_view">                 <div class="window"> ...

  8. 利用System.Net.Mail 发送邮件

    我这里只是试了一下发mail的功能,感觉.net自带的发mail是比较全的,还是直接上我的code 参数文章:System.Net.Mail 发送邮件 SMTP协议 using System; usi ...

  9. "科林明伦杯"哈尔滨理工大学第八届程序设计竞赛 题解

    题目链接  Problems Problem A 快速幂累加即可. #include <cstdio> #include <cstring> #include <iost ...

  10. bzoj 5020: [THUWC 2017]在美妙的数学王国中畅游【泰勒展开+LCT】

    参考:https://www.cnblogs.com/CQzhangyu/p/7500328.html --其实理解了泰勒展开之后就是水题呢可是我还是用了两天时间来搞懂啊 泰勒展开是到正无穷的,但是因 ...