forked from cybrespace/mastodon
		
	Cache attachments on external host with service worker (#7493)
This commit is contained in:
		
							parent
							
								
									03f4c214b4
								
							
						
					
					
						commit
						d95642f6d9
					
				
					 4 changed files with 30 additions and 3 deletions
				
			
		| 
						 | 
					@ -88,6 +88,10 @@ SMTP_FROM_ADDRESS=notifications@example.com
 | 
				
			||||||
# CDN_HOST=https://assets.example.com
 | 
					# CDN_HOST=https://assets.example.com
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# S3 (optional)
 | 
					# S3 (optional)
 | 
				
			||||||
 | 
					# The attachment host must allow cross origin request from WEB_DOMAIN or
 | 
				
			||||||
 | 
					# LOCAL_DOMAIN if WEB_DOMAIN is not set. For example, the server may have the
 | 
				
			||||||
 | 
					# following header field:
 | 
				
			||||||
 | 
					# Access-Control-Allow-Origin: https://192.168.1.123:9000/
 | 
				
			||||||
# S3_ENABLED=true
 | 
					# S3_ENABLED=true
 | 
				
			||||||
# S3_BUCKET=
 | 
					# S3_BUCKET=
 | 
				
			||||||
# AWS_ACCESS_KEY_ID=
 | 
					# AWS_ACCESS_KEY_ID=
 | 
				
			||||||
| 
						 | 
					@ -97,6 +101,8 @@ SMTP_FROM_ADDRESS=notifications@example.com
 | 
				
			||||||
# S3_HOSTNAME=192.168.1.123:9000
 | 
					# S3_HOSTNAME=192.168.1.123:9000
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# S3 (Minio Config (optional) Please check Minio instance for details)
 | 
					# S3 (Minio Config (optional) Please check Minio instance for details)
 | 
				
			||||||
 | 
					# The attachment host must allow cross origin request - see the description
 | 
				
			||||||
 | 
					# above.
 | 
				
			||||||
# S3_ENABLED=true
 | 
					# S3_ENABLED=true
 | 
				
			||||||
# S3_BUCKET=
 | 
					# S3_BUCKET=
 | 
				
			||||||
# AWS_ACCESS_KEY_ID=
 | 
					# AWS_ACCESS_KEY_ID=
 | 
				
			||||||
| 
						 | 
					@ -108,6 +114,8 @@ SMTP_FROM_ADDRESS=notifications@example.com
 | 
				
			||||||
# S3_SIGNATURE_VERSION=
 | 
					# S3_SIGNATURE_VERSION=
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# Swift (optional)
 | 
					# Swift (optional)
 | 
				
			||||||
 | 
					# The attachment host must allow cross origin request - see the description
 | 
				
			||||||
 | 
					# above.
 | 
				
			||||||
# SWIFT_ENABLED=true
 | 
					# SWIFT_ENABLED=true
 | 
				
			||||||
# SWIFT_USERNAME=
 | 
					# SWIFT_USERNAME=
 | 
				
			||||||
# For Keystone V3, the value for SWIFT_TENANT should be the project name
 | 
					# For Keystone V3, the value for SWIFT_TENANT should be the project name
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -7,6 +7,9 @@ env:
 | 
				
			||||||
  es6: true
 | 
					  es6: true
 | 
				
			||||||
  jest: true
 | 
					  jest: true
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					globals:
 | 
				
			||||||
 | 
					  ATTACHMENT_HOST: false
 | 
				
			||||||
 | 
					
 | 
				
			||||||
parser: babel-eslint
 | 
					parser: babel-eslint
 | 
				
			||||||
 | 
					
 | 
				
			||||||
plugins:
 | 
					plugins:
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -49,7 +49,7 @@ self.addEventListener('fetch', function(event) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      return response;
 | 
					      return response;
 | 
				
			||||||
    }));
 | 
					    }));
 | 
				
			||||||
  } else if (storageFreeable && process.env.CDN_HOST ? url.host === process.env.CDN_HOST : url.pathname.startsWith('/system/')) {
 | 
					  } else if (storageFreeable && (ATTACHMENT_HOST ? url.host === ATTACHMENT_HOST : url.pathname.startsWith('/system/'))) {
 | 
				
			||||||
    event.respondWith(openSystemCache().then(cache => {
 | 
					    event.respondWith(openSystemCache().then(cache => {
 | 
				
			||||||
      return cache.match(event.request.url).then(cached => {
 | 
					      return cache.match(event.request.url).then(cached => {
 | 
				
			||||||
        if (cached === undefined) {
 | 
					        if (cached === undefined) {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -6,8 +6,9 @@ const CompressionPlugin = require('compression-webpack-plugin');
 | 
				
			||||||
const sharedConfig = require('./shared.js');
 | 
					const sharedConfig = require('./shared.js');
 | 
				
			||||||
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
 | 
					const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
 | 
				
			||||||
const OfflinePlugin = require('offline-plugin');
 | 
					const OfflinePlugin = require('offline-plugin');
 | 
				
			||||||
const { env, publicPath } = require('./configuration.js');
 | 
					const { publicPath } = require('./configuration.js');
 | 
				
			||||||
const path = require('path');
 | 
					const path = require('path');
 | 
				
			||||||
 | 
					const { URL } = require('url');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
let compressionAlgorithm;
 | 
					let compressionAlgorithm;
 | 
				
			||||||
try {
 | 
					try {
 | 
				
			||||||
| 
						 | 
					@ -19,6 +20,21 @@ try {
 | 
				
			||||||
  compressionAlgorithm = 'gzip';
 | 
					  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, {
 | 
					module.exports = merge(sharedConfig, {
 | 
				
			||||||
  output: {
 | 
					  output: {
 | 
				
			||||||
    filename: '[name]-[chunkhash].js',
 | 
					    filename: '[name]-[chunkhash].js',
 | 
				
			||||||
| 
						 | 
					@ -90,7 +106,7 @@ module.exports = merge(sharedConfig, {
 | 
				
			||||||
        '**/*.woff',
 | 
					        '**/*.woff',
 | 
				
			||||||
      ],
 | 
					      ],
 | 
				
			||||||
      ServiceWorker: {
 | 
					      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',
 | 
					        cacheName: 'mastodon',
 | 
				
			||||||
        output: '../assets/sw.js',
 | 
					        output: '../assets/sw.js',
 | 
				
			||||||
        publicPath: '/sw.js',
 | 
					        publicPath: '/sw.js',
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		
		Reference in a new issue