【问题标题】:How do I create a App shell that is cached for a Progressive Web app in a React App?如何在 React 应用程序中创建为 Progressive Web 应用程序缓存的应用程序外壳?
【发布时间】:2016-08-07 09:37:34
【问题描述】:

目前,我使用 webpack 构建一个代表我的应用程序的 JS 文件。如何将我的 React 应用程序 UI 外壳与应用程序逻辑的其余部分分离,以便我可以让服务工作者缓存它?

我的 webpackpack 配置文件如下所示,生成一个 index_bundle.js 文件(没有 css 文件):

import webpack from 'webpack'
import path from 'path'
import HtmlWebpackPlugin from 'html-webpack-plugin'
import autoprefixer from 'autoprefixer'

const LAUNCH_COMMAND = process.env.npm_lifecycle_event

const isProduction = LAUNCH_COMMAND === 'production'
process.env.BABEL_ENV = LAUNCH_COMMAND

const PATHS = {
  root: path.join(__dirname),
  app: path.join(__dirname, 'app'),
  build: path.join(__dirname, 'dist')
}

const HTMLWebpackPluginConfig = new HtmlWebpackPlugin({
  template: PATHS.app + '/index.html',
  filename: 'index.html',
  inject: 'body'
})

const productionPlugin = new webpack.DefinePlugin({
  'process.env': {
    NODE_ENV: JSON.stringify('production')
  }
})

const productionPlugin2 = new webpack.optimize.UglifyJsPlugin({
  compressor: {
    warnings: false
  }
})

const base = {
  entry: [
    'babel-polyfill',
    PATHS.app
  ],
  output: {
    path: PATHS.build,
    filename: 'index_bundle.js'
  },
  module: {
    loaders: [
      {test: /\.js$/, exclude: /node_modules/, loader: 'babel-loader'},
      {test: /\.css$/, loader: 'style!css?sourceMap&modules&localIdentName=[name]__[local]___[hash:base64:5]&importLoader=1!postcss'}
    ]
  },
  postcss: [ autoprefixer({ browsers: ['last 2 versions'] }) ],
  resolve: {
    root: path.resolve('./app')
  }
}

const developmentConfig = {
  devtool: 'cheap-module-inline-source-map',
  devServer: {
    contentBase: PATHS.build,
    historyApiFallback: true,
    hot: true,
    inline: true,
    progress: true
  },
  plugins: [HTMLWebpackPluginConfig, new webpack.HotModuleReplacementPlugin()]
}

const productionConfig = {
  devtool: 'cheap-module-source-map',
  plugins: [HTMLWebpackPluginConfig, productionPlugin, productionPlugin2]
}

export default Object.assign({}, base, isProduction === true ? productionConfig : developmentConfig)

【问题讨论】:

    标签: user-interface reactjs service-worker progressive-web-apps


    【解决方案1】:

    我在 2015 年 Chrome 开发者峰会上的“Instant Loading with Service Workers”演讲涵盖了使用由 React 提供支持的 App Shell + 动态模型创建 PWA。

    它的代码示例是 sw-precache 库的 repo 的一部分:https://github.com/GoogleChrome/sw-precache/tree/master/app-shell-demo

    (它不一定是世界上最惯用的 React 代码,但一般概念,尤其是涉及到 Service Worker 实现时,应该成立。)

    【讨论】:

    • 我正在使用 webpack 进行构建过程,目前只生成一个 bundle.js 文件,即使在观看了您的视频并浏览了 github 存储库之后,我也不清楚如何生成单独的应用程序外壳我可以用 sw-precache 缓存的文件
    • 嘿@jasan,你是怎么做到的?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-05
    • 2012-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-14
    • 1970-01-01
    相关资源
    最近更新 更多