From 56c2682c9eb458419b60497d3e851a9ed84b80cf Mon Sep 17 00:00:00 2001 From: keyan Date: Fri, 15 Mar 2024 21:54:29 -0500 Subject: [PATCH] fix service worker env imports --- docker/s3/init-s3.sh | 3 ++- next.config.js | 14 +++++++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/docker/s3/init-s3.sh b/docker/s3/init-s3.sh index 9e6ed148..e9d859be 100755 --- a/docker/s3/init-s3.sh +++ b/docker/s3/init-s3.sh @@ -2,4 +2,5 @@ pip3 install --upgrade virtualenv awscli awscli-local requests awslocal s3 mb s3://uploads -awslocal s3api put-bucket-cors --bucket uploads --cors-configuration file:///etc/localstack/init/ready.d/cors.json \ No newline at end of file +awslocal s3api put-bucket-cors --bucket uploads --cors-configuration file:///etc/localstack/init/ready.d/cors.json +echo "Localstack S3 bucket 'uploads' created and CORS configuration added" \ No newline at end of file diff --git a/next.config.js b/next.config.js index f5b3085d..68aab201 100644 --- a/next.config.js +++ b/next.config.js @@ -1,6 +1,7 @@ const { withPlausibleProxy } = require('next-plausible') const { InjectManifest } = require('workbox-webpack-plugin') const { generatePrecacheManifest } = require('./sw/build.js') +const webpack = require('webpack') let isProd = process.env.NODE_ENV === 'production' const corsHeaders = [ @@ -197,7 +198,18 @@ module.exports = withPlausibleProxy()({ exclude: [/.*/], // by default, webpack saves service worker at .next/server/ swDest: '../../public/sw.js', - swSrc: './sw/index.js' + swSrc: './sw/index.js', + webpackCompilationPlugins: [ + // this is need to allow the service worker to access these environment variables + // from lib/constants.js + new webpack.DefinePlugin({ + 'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV), + 'process.env.MEDIA_URL_DOCKER': JSON.stringify(process.env.MEDIA_URL_DOCKER), + 'process.env.NEXT_PUBLIC_MEDIA_URL': JSON.stringify(process.env.NEXT_PUBLIC_MEDIA_URL), + 'process.env.NEXT_PUBLIC_MEDIA_DOMAIN': JSON.stringify(process.env.NEXT_PUBLIC_MEDIA_DOMAIN), + 'process.env.NEXT_IS_EXPORT_WORKER': 'true' + }) + ] }) if (dev) { // Suppress the "InjectManifest has been called multiple times" warning by reaching into