E
E
Evgeny Khripunov2018-10-04 13:41:48
Laravel
Evgeny Khripunov, 2018-10-04 13:41:48

How to setup laravel SPA workbox?

SPA application on laravel + vue.js
Trying to "move" from sw-precache to workbox .
I'm using the workbox-webpack-plugin plugin.

const WorkboxPlugin = require('workbox-webpack-plugin');
mix.webpackConfig({
    plugins: [        
        new WorkboxPlugin.InjectManifest({            
            swDest: 'sw.js',
            swSrc: 'public/sw-offline.js',
            importsDirectory: 'service-worker', 
        }),
    ],
    output: {
        chunkFilename: path.join('js', '[name].[hash].js'),
        publicPath: '/'       
    }
});

On the advice from here , the sw- offline.js file looks like this:
workbox.precaching.precacheAndRoute([]);
workbox.routing.registerNavigationRoute('/index.php');

The precache-manifest file when built with webpack looks like this:
self.__precacheManifest = [
  {
    "revision": "853b1f0f734e11279f53",
    "url": "js/UserPage.853b1f.js"
  },
  {
    "revision": "b8b4df614607b69f622e",
    "url": "js/Main.b8b4df.js"
  },
  {
    "revision": "41476c1f9be506f01c9a",
    "url": "js/Chat.41476c.js"
  },
  {
    "revision": "c03182970c9216b11ad7",
    "url": "js/Auth.c03182.js"
  },
  {
    "revision": "bde9bd3ca6ed292caccc",
    "url": "js/ArticleCreate.bde9bd.js"
  },
  {
    "revision": "5e5dfde110cf70b0e157",
    "url": "js/Article.5e5dfd.js"
  },
  {
    "revision": "8184ad7012b57796f25e",
    "url": "/js/app.8184ad.js"
  },
  {
    "revision": "8184ad7012b57796f25e",
    "url": "/css/app.a33cbb.css"
  }
];

When loading the site, the service worker for some reason does not want to process the files specified in the precache-manifest.
5bb5ed8541406673269835.png
When offline, it refuses to work at all:
5bb5ee139cb93718810774.png
Previously, I used sw-precache and everything worked fine with this config:
new SWPrecacheWebpackPlugin({
            cacheId: 'pwa',
            filename: 'sw.js',
            staticFileGlobs: ['public/**/*.{css,eot,svg,ttf,woff,woff2,js,html,mp3}'],
            minify: true,
            stripPrefix: 'public/',
            handleFetch: true,
            dynamicUrlToDependencies: {
                '/': ['resources/views/spa.blade.php'],
            },
            staticFileGlobsIgnorePatterns: [/\.map$/, /mix-manifest\.json$/, /manifest\.json$/, /sw\.js$/],
            navigateFallback: '/',
            runtimeCaching: [
                {
                    urlPattern: /^https:\/\/fonts\.googleapis\.com\//,
                    handler: 'cacheFirst'
                },
                {
                    urlPattern: /^https:\/\/www\.thecocktaildb\.com\/images\/media\/drink\/(\w+)\.jpg/,
                    handler: 'cacheFirst'
                }
            ],            
        }),

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question