about summary refs log tree commit diff
path: root/config/webpack
diff options
context:
space:
mode:
authorAkihiko Odaki <akihiko.odaki.4i@stu.hosei.ac.jp>2018-05-29 07:43:47 +0900
committerEugen Rochko <eugen@zeonfederated.com>2018-05-29 00:43:47 +0200
commitd95642f6d913a99fc44f0ac0695d53534afb7962 (patch)
treeeb40f058b8b96b9cc5a0495908c5ca7ff8d32818 /config/webpack
parent03f4c214b459a241a1aab08095a986475bd6f8f0 (diff)
Cache attachments on external host with service worker (#7493)
Diffstat (limited to 'config/webpack')
-rw-r--r--config/webpack/production.js20
1 files changed, 18 insertions, 2 deletions
diff --git a/config/webpack/production.js b/config/webpack/production.js
index a82330791..408c56930 100644
--- a/config/webpack/production.js
+++ b/config/webpack/production.js
@@ -6,8 +6,9 @@ const CompressionPlugin = require('compression-webpack-plugin');
 const sharedConfig = require('./shared.js');
 const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
 const OfflinePlugin = require('offline-plugin');
-const { env, publicPath } = require('./configuration.js');
+const { publicPath } = require('./configuration.js');
 const path = require('path');
+const { URL } = require('url');
 
 let compressionAlgorithm;
 try {
@@ -19,6 +20,21 @@ try {
   compressionAlgorithm = 'gzip';
 }
 
+let attachmentHost;
+
+if (process.env.S3_ENABLED === 'true') {
+  if (process.env.S3_CLOUDFRONT_HOST) {
+    attachmentHost = process.env.S3_CLOUDFRONT_HOST;
+  } else {
+    attachmentHost = process.env.S3_HOSTNAME || `s3-${process.env.S3_REGION || 'us-east-1'}.amazonaws.com`;
+  }
+} else if (process.env.SWIFT_ENABLED === 'true') {
+  const { host } = new URL(process.env.SWIFT_OBJECT_URL);
+  attachmentHost = host;
+} else {
+  attachmentHost = null;
+}
+
 module.exports = merge(sharedConfig, {
   output: {
     filename: '[name]-[chunkhash].js',
@@ -90,7 +106,7 @@ module.exports = merge(sharedConfig, {
         '**/*.woff',
       ],
       ServiceWorker: {
-        entry: `imports-loader?process.env=>${encodeURIComponent(JSON.stringify(env))}!${encodeURI(path.join(__dirname, '../../app/javascript/mastodon/service_worker/entry.js'))}`,
+        entry: `imports-loader?ATTACHMENT_HOST=>${encodeURIComponent(JSON.stringify(attachmentHost))}!${encodeURI(path.join(__dirname, '../../app/javascript/mastodon/service_worker/entry.js'))}`,
         cacheName: 'mastodon',
         output: '../assets/sw.js',
         publicPath: '/sw.js',