From 40ac9bece987568735a3a5759275d123d1ae9bbd Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Tue, 5 Apr 2022 14:43:29 -0500 Subject: [PATCH] Enable the ServiceWorker --- app/soapbox/main.js | 12 +-- webpack/production.js | 192 +++++++++++++++++++++--------------------- 2 files changed, 102 insertions(+), 102 deletions(-) diff --git a/app/soapbox/main.js b/app/soapbox/main.js index f2318d008..dfac430e0 100644 --- a/app/soapbox/main.js +++ b/app/soapbox/main.js @@ -1,11 +1,11 @@ 'use strict'; import './precheck'; -// import * as OfflinePluginRuntime from '@lcdp/offline-plugin/runtime'; +import * as OfflinePluginRuntime from '@lcdp/offline-plugin/runtime'; import React from 'react'; import ReactDOM from 'react-dom'; -// import { NODE_ENV } from 'soapbox/build_config'; +import { NODE_ENV } from 'soapbox/build_config'; import { default as Soapbox } from './containers/soapbox'; import * as monitoring from './monitoring'; @@ -23,10 +23,10 @@ function main() { ReactDOM.render(, mountNode); - // if (NODE_ENV === 'production') { - // // avoid offline in dev mode because it's harder to debug - // OfflinePluginRuntime.install(); - // } + if (NODE_ENV === 'production') { + // avoid offline in dev mode because it's harder to debug + OfflinePluginRuntime.install(); + } perf.stop('main()'); }); } diff --git a/webpack/production.js b/webpack/production.js index 521ad6245..ce47d647b 100644 --- a/webpack/production.js +++ b/webpack/production.js @@ -1,16 +1,16 @@ // Note: You must restart bin/webpack-dev-server for changes to take effect console.log('Running in production mode'); // eslint-disable-line no-console -// const { join } = require('path'); +const { join } = require('path'); -// const OfflinePlugin = require('@lcdp/offline-plugin'); +const OfflinePlugin = require('@lcdp/offline-plugin'); const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer'); const { merge } = require('webpack-merge'); const sharedConfig = require('./shared'); -// const { FE_SUBDIRECTORY } = require(join(__dirname, '..', 'app', 'soapbox', 'build_config')); -// const joinPublicPath = (...paths) => join(FE_SUBDIRECTORY, ...paths); +const { FE_SUBDIRECTORY } = require(join(__dirname, '..', 'app', 'soapbox', 'build_config')); +const joinPublicPath = (...paths) => join(FE_SUBDIRECTORY, ...paths); module.exports = merge(sharedConfig, { mode: 'production', @@ -28,97 +28,97 @@ module.exports = merge(sharedConfig, { openAnalyzer: false, logLevel: 'silent', }), - // new OfflinePlugin({ - // caches: { - // main: [':rest:'], - // additional: [ - // ':externals:', - // 'packs/images/32-*.png', // used in emoji-mart - // 'packs/icons/*.svg', - // ], - // optional: [ - // '**/locale_*.js', // don't fetch every locale; the user only needs one - // '**/*_polyfills-*.js', // the user may not need polyfills - // '**/*.chunk.js', // only cache chunks when needed - // '**/*.chunk.css', - // '**/*.woff2', // the user may have system-fonts enabled - // // images can be cached on-demand - // '**/*.png', - // '**/*.svg', - // ], - // }, - // externals: [ - // joinPublicPath('packs/emoji/1f602.svg'), // used for emoji picker dropdown - // - // // Default emoji reacts - // joinPublicPath('packs/emoji/1f44d.svg'), // Thumbs up - // joinPublicPath('packs/emoji/2764.svg'), // Heart - // joinPublicPath('packs/emoji/1f606.svg'), // Laughing - // joinPublicPath('packs/emoji/1f62e.svg'), // Surprised - // joinPublicPath('packs/emoji/1f622.svg'), // Crying - // joinPublicPath('packs/emoji/1f629.svg'), // Weary - // joinPublicPath('packs/emoji/1f621.svg'), // Angry (Spinster) - // ], - // excludes: [ - // '**/*.gz', - // '**/*.map', - // '**/*.LICENSE.txt', - // 'stats.json', - // 'report.html', - // 'instance/**/*', - // // any browser that supports ServiceWorker will support woff2 - // '**/*.eot', - // '**/*.ttf', - // '**/*-webfont-*.svg', - // '**/*.woff', - // // Sounds return a 206 causing sw.js to crash - // // https://stackoverflow.com/a/66335638 - // '**/*.ogg', - // '**/*.oga', - // '**/*.mp3', - // '404.html', - // 'assets-manifest.json', - // // It would be nice to serve these, but they bloat up sw.js - // 'packs/images/crypto/**/*', - // 'packs/emoji/**/*', - // ], - // ServiceWorker: { - // cacheName: 'soapbox', - // entry: join(__dirname, '../app/soapbox/service_worker/entry.js'), - // minify: true, - // }, - // cacheMaps: [{ - // match: requestUrl => { - // const backendRoutes = [ - // '/api', - // '/pleroma', - // '/nodeinfo', - // '/socket', - // '/oauth', - // '/.well-known/webfinger', - // '/static', - // '/instance', - // '/main/ostatus', - // '/ostatus_subscribe', - // '/pghero', - // '/sidekiq', - // '/open-source', - // ]; - // - // const isBackendRoute = ({ pathname }) => { - // if (pathname) { - // return backendRoutes.some(pathname.startsWith); - // } else { - // return false; - // } - // }; - // - // return isBackendRoute(requestUrl) && requestUrl; - // }, - // requestTypes: ['navigate'], - // }], - // safeToUseOptionalCaches: true, - // appShell: join(FE_SUBDIRECTORY, '/'), - // }), + new OfflinePlugin({ + caches: { + main: [':rest:'], + additional: [ + ':externals:', + 'packs/images/32-*.png', // used in emoji-mart + 'packs/icons/*.svg', + ], + optional: [ + '**/locale_*.js', // don't fetch every locale; the user only needs one + '**/*_polyfills-*.js', // the user may not need polyfills + '**/*.chunk.js', // only cache chunks when needed + '**/*.chunk.css', + '**/*.woff2', // the user may have system-fonts enabled + // images can be cached on-demand + '**/*.png', + '**/*.svg', + ], + }, + externals: [ + joinPublicPath('packs/emoji/1f602.svg'), // used for emoji picker dropdown + + // Default emoji reacts + joinPublicPath('packs/emoji/1f44d.svg'), // Thumbs up + joinPublicPath('packs/emoji/2764.svg'), // Heart + joinPublicPath('packs/emoji/1f606.svg'), // Laughing + joinPublicPath('packs/emoji/1f62e.svg'), // Surprised + joinPublicPath('packs/emoji/1f622.svg'), // Crying + joinPublicPath('packs/emoji/1f629.svg'), // Weary + joinPublicPath('packs/emoji/1f621.svg'), // Angry (Spinster) + ], + excludes: [ + '**/*.gz', + '**/*.map', + '**/*.LICENSE.txt', + 'stats.json', + 'report.html', + 'instance/**/*', + // any browser that supports ServiceWorker will support woff2 + '**/*.eot', + '**/*.ttf', + '**/*-webfont-*.svg', + '**/*.woff', + // Sounds return a 206 causing sw.js to crash + // https://stackoverflow.com/a/66335638 + '**/*.ogg', + '**/*.oga', + '**/*.mp3', + '404.html', + 'assets-manifest.json', + // It would be nice to serve these, but they bloat up sw.js + 'packs/images/crypto/**/*', + 'packs/emoji/**/*', + ], + ServiceWorker: { + cacheName: 'soapbox', + entry: join(__dirname, '../app/soapbox/service_worker/entry.js'), + minify: true, + }, + cacheMaps: [{ + match: requestUrl => { + const backendRoutes = [ + '/api', + '/pleroma', + '/nodeinfo', + '/socket', + '/oauth', + '/.well-known/webfinger', + '/static', + '/instance', + '/main/ostatus', + '/ostatus_subscribe', + '/pghero', + '/sidekiq', + '/open-source', + ]; + + const isBackendRoute = ({ pathname }) => { + if (pathname) { + return backendRoutes.some(pathname.startsWith); + } else { + return false; + } + }; + + return isBackendRoute(requestUrl) && requestUrl; + }, + requestTypes: ['navigate'], + }], + safeToUseOptionalCaches: true, + appShell: join(FE_SUBDIRECTORY, '/'), + }), ], });