使用服务人员和推送通知进行响应

时间:2019-02-19 15:58:08

标签: reactjs service-worker create-react-app workbox

一些初步考虑:

"react": "^16.8.2",
"react-scripts": "2.1.5"

我创建了一个新的react应用,我需要实现推送通知。 在完成this教程之后,我能够在5分钟内启动并运行,但是现在我必须在React应用程序中实施相同的策略(kinda)。

我面临的问题是我能够订阅Notification API,但是我不确定如何编辑service-worker.js文件以添加事件侦听器来捕获push事件(Google指南中的Handle a Push Event章)

2 个答案:

答案 0 :(得分:4)

可以使用Create React App自定义您的服务人员,但这可能非常困难且棘手。

开箱即用,CRA使用Workbox GenerateSW Webpack插件来生成service-worker.js文件,并且您无法向其中注入任何代码(could是使用CRA @ 1,不再可用自CRA @ 2起)

您有几种策略,我将从最简单的策略开始。

解决方案1:提供您自己的服务工作者文件

  • src/index.js中启用服务工作者:
    // serviceWorker.unregister()
    serviceWorker.register()
    
  • src/serviceWorker.js中的
  • 注册您的自定义文件:

    // if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) {
    if ('serviceWorker' in navigator) {
    
    // const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`;
    const swUrl = `${process.env.PUBLIC_URL}/custom-service-worker.js`;
    

    您必须在运行开发服务器时更改原因,CRA为service-worker.js提供了mock

  • public/文件夹中,创建custom-service-worker.js文件。 Webpack将按原样复制build/文件夹中的

优点 :快速,肮脏的胜利

缺点 :您的自定义文件未使用Webpack处理(没有导入),并且您必须自己实现网络缓存逻辑(假设您需要PWA),因为您正在绕过Workbox插件

解决方案2:将您的代码附加到生成的服务人员中

有一个适用的模块:cra-append-sw。 您负责提供附加的代码。

优点 :设置简单,利用了GenerateSW的优势

缺点 :附加的代码使用Babel / Webpack处理,但不使用CRA的配置(您可以选择退出)。仍然使用GenerateSW为您处理网络缓存。不确定在本地开发时是否可以使用

解决方案3:在自定义服务工作者文件中使用Workbox

  • 应用解决方案#1的前两个步骤:更改src/index.jssrc/serviceWorker.js

  • src/文件夹中,创建custom-service-worker.js文件。它会由Webpack处理,因此您可以使用ES2016 / TypeScript语法并导入模块

    /* eslint no-restricted-globals: "off" */
    import * as precaching from 'workbox-precaching'
    // your own imports
    
    if (self.__precacheManifest) {
    precaching.precacheAndRoute(self.__precacheManifest)
    }
    
    // your own code
    
  • 安装react-app-rewire

    • npm add --save-dev react-app-rewired
    • package.json中的"scripts"中,将react-scripts替换为react-app-rewired
  • 调整Webpack配置:在根文件夹中创建config-overrides.js

    const WebpackBeforeBuildPlugin = require('before-build-webpack')
    const WorkboxWebpackPlugin = require('workbox-webpack-plugin')
    const path = require('path')
    const merge = require('lodash.merge')
    const fs = require('fs')
    
    // from https://www.viget.com/articles/run-multiple-webpack-configs-sequentially/
    class WaitPlugin extends WebpackBeforeBuildPlugin {
    constructor(file, interval = 100, timeout = 60e3) {
     super(function(stats, callback) {
       const start = Date.now()
    
       function poll() {
         if (fs.existsSync(file)) {
           callback()
         } else if (Date.now() - start > timeout) {
           throw Error(`Couldn't access ${file} within ${timeout}s`)
         } else {
           setTimeout(poll, interval)
         }
       }
       poll()
     })
    }
    }
    
    const swOutputName = 'custom-service-worker.js'
    const workerSource = path.resolve(__dirname, 'src', swOutputName)
    
    module.exports = {
    webpack: (config, env) => {
     // we need 2 webpack configurations:
     // 1- for the service worker file.
     //    it needs to be processed by webpack (to include 3rd party modules), and the output must be a
     //    plain, single file, not injected in the HTML page
     const swConfig = merge({}, config, {
       name: 'service worker',
       entry: workerSource,
       output: {
         filename: swOutputName
       },
       optimization: {
         splitChunks: false,
         runtimeChunk: false
       }
     })
     delete swConfig.plugins
    
     // 2- for the main application.
     //    we'll reuse configuration from create-react-app, without a specific Workbox configuration,
     //    so it could inject workbox-precache module and the computed manifest into the BUILT service-worker.js file.
     //    this require to WAIT for the first configuration to be finished
     if (env === 'production') {
       const builtWorkerPath = path.resolve(config.output.path, swOutputName)
       config.name = 'main-application'
       config.plugins.push(
         new WorkboxWebpackPlugin.InjectManifest({
           swSrc: builtWorkerPath,
           swDest: swOutputName
         }),
         new WaitPlugin(builtWorkerPath)
       )
     }
    
     // remove Workbox service-worker.js generator
     const removed = config.plugins.findIndex(
       ({ constructor: { name } }) => name === 'GenerateSW'
     )
     if (removed !== -1) {
       config.plugins.splice(removed, 1)
     } 
    
     const result = [swConfig, config]
     // compatibility hack for CRA's build script to support multiple configurations
     // https://github.com/facebook/create-react-app/blob/master/packages/react-scripts/scripts/build.js#L119
     result.output = { publicPath: config.output.publicPath }
     return result
    }
    }
    

专业人士 :您可以在service-worker文件中使用ES2016 / TypeScript代码。您仍然可以从Workbox网络缓存功能中受益,对其进行完全控制

缺点 :由于存在多种配置缺陷,因此复杂且脆弱。

我使用了最后一个解决方案,因为我既需要来自Workbox的缓存代码,也需要我的服务工作者文件中的某些import

react-app-rewire-workbox可能有助于简化Webpack的配置(用于主应用程序的配置)。要测试。

答案 1 :(得分:0)

创建React App 4-2020年10月发布

从Create React App 4开始,您可以完全控制 通过创建自己的服务,在此服务工作者中自定义逻辑 src / service-worker.js文件,或自定义由 cra-template-pwa(或cra-template-pwa-typescript)模板。您可以 使用Workbox项目中的其他模块,添加推送 通知库,或删除一些默认的缓存逻辑。

https://create-react-app.dev/docs/making-a-progressive-web-app/#customization