【问题标题】:How to precache static assets with laravel-mix-workbox?如何使用 laravel-mix-workbox 预缓存静态资产?
【发布时间】:2020-08-05 03:55:30
【问题描述】:

我正在尝试使用 Laravel 和 Vue.js 构建具有离线支持的 PWA。我正在使用 laravel-mix-workbox 插件来设置我的服务工作者,但是我在尝试完成应该是一项简单的任务时遇到了很多麻烦。我有一些静态资产(图像、XML 文件等)从我的应用程序中提供,我无法让工作箱将它们添加到预缓存文件列表中。

我尝试将资产移动到 /resources/img 并添加对 copyDirectory 的调用以尝试将它们包含在内,此外,我尝试了 webpack-copy-plugin,但只包含编译后的资产(js、css ,字体等)。这是我的 webpack.mix.js 文件:

const mix = require('laravel-mix');

//mp035 add workbox plugin and copy-webpack-plugin
require('laravel-mix-workbox');
const CopyPlugin = require('copy-webpack-plugin');

/*
 |--------------------------------------------------------------------------
 | Mix Asset Management
 |--------------------------------------------------------------------------
 |
 | Mix provides a clean, fluent API for defining some Webpack build steps
 | for your Laravel application. By default, we are compiling the Sass
 | file for the application as well as bundling up all the JS files.
 |
 */

//mp035 fix issue with laravel-mix outputting bad urls in precache manifest for app.js (//js/app.js) and app.css
// and copy assets into place (so they are in the build tree)
mix.webpackConfig({
    output: {
      publicPath: ''
    },
    plugins: [
        new CopyPlugin([
          { from: 'resources/img/*', to: 'public/img', flatten:true },
          { from: 'resources/root/*', to: 'public', flatten:true },
        ]),
      ],
  })


.js('resources/js/app.js', 'public/js')
   .sass('resources/sass/app.scss', 'public/css')
   .sourceMaps().version()

// mp035 add inject manifest plugin to inject workbox manifest into the service worker.
.injectManifest({
    swSrc: './resources/pwa/service-worker.js',
    maximumFileSizeToCacheInBytes: 20000000, // ******************************DEBUG ONLY!!!
});

有谁知道如何将我的 /resources/img(或 /public/img)中的所有文件包含在预缓存文件列表中?

【问题讨论】:

    标签: laravel webpack laravel-mix workbox workbox-webpack-plugin


    【解决方案1】:

    好的,看来这是 laravel-mix-workbox 的问题。删除它并使用通用工作箱 webpack 插件解决了这个问题。对于任何发现这个的人,这里是更新的 webpack.mix.js:

    const mix = require('laravel-mix');
    
    //mp035 add workbox plugin and copy-webpack-plugin
    const CopyPlugin = require('copy-webpack-plugin');
    const {InjectManifest} = require('workbox-webpack-plugin');
    
    /*
     |--------------------------------------------------------------------------
     | Mix Asset Management
     |--------------------------------------------------------------------------
     |
     | Mix provides a clean, fluent API for defining some Webpack build steps
     | for your Laravel application. By default, we are compiling the Sass
     | file for the application as well as bundling up all the JS files.
     |
     */
    
    //mp035 fix issue with laravel-mix outputting bad urls in precache manifest for app.js (//js/app.js) and app.css
    // and copy assets into place (so they are in the build tree)
    mix.webpackConfig({
        output: {
          publicPath: ''
        },
        plugins: [
            new CopyPlugin([
              { from: 'resources/img/*', to: 'public/img', flatten:true },
              { from: 'resources/root/*', to: 'public', flatten:true },
            ]),
            new InjectManifest({
                swSrc: './resources/pwa/service-worker.js',
                maximumFileSizeToCacheInBytes: 20000000, // ******************************DEBUG ONLY!!!
            }),
          ],
      })
    
    
    .js('resources/js/app.js', 'public/js')
       .sass('resources/sass/app.scss', 'public/css')
       .sourceMaps().version();
    

    总而言之,在 laravel-mix 中使用 workbox 是一个非常痛苦的过程,因为 laravel-mix 所做的所有“小”调整都会破坏 workbox 插件。如果可能的话,我建议坚持使用普通的 webpack。

    【讨论】:

    • 完全同意——事实证明,混合和工作箱极具挑战性!
    猜你喜欢
    • 2019-12-14
    • 1970-01-01
    • 2015-08-19
    • 2023-03-04
    • 2018-02-06
    • 2023-03-26
    • 2018-05-19
    • 2012-09-02
    • 1970-01-01
    相关资源
    最近更新 更多