From e3023a7f24c3cc96074a03217734d0f994bcc736 Mon Sep 17 00:00:00 2001 From: Claudio Fuentes Date: Sun, 3 Aug 2025 22:24:46 -0400 Subject: [PATCH 01/10] chore: enhance build process for static asset management and Docker configuration - Updated buildspec.yml to include steps for uploading static and public assets to S3, improving asset management and resolving chunk drift issues. - Added validation for the STATIC_ASSETS_BUCKET environment variable to ensure proper configuration. - Modified next.config.ts to set assetPrefix for production, enabling S3 usage for static assets. - Streamlined Docker build process by copying necessary files to a container-build directory, ensuring a cleaner and more efficient build. --- apps/app/buildspec.yml | 68 +++++++++++++++++++++++++----------- apps/app/next.config.ts | 12 +++++-- apps/portal/buildspec.yml | 70 +++++++++++++++++++++++++++----------- apps/portal/next.config.ts | 12 +++++-- 4 files changed, 118 insertions(+), 44 deletions(-) diff --git a/apps/app/buildspec.yml b/apps/app/buildspec.yml index 39e165825..c48931644 100644 --- a/apps/app/buildspec.yml +++ b/apps/app/buildspec.yml @@ -16,6 +16,7 @@ phases: # Environment setup - export PATH="/root/.bun/bin:$PATH" - export PGSSLMODE=require + - export NODE_ENV=production - export NEXT_TELEMETRY_DISABLED=1 - export UV_THREADPOOL_SIZE=36 - export NODE_OPTIONS="--max-old-space-size=65536" @@ -35,6 +36,7 @@ phases: # Validate environment variables - echo "Validating environment variables..." - '[ -n "$NEXT_PUBLIC_PORTAL_URL" ] || { echo "❌ NEXT_PUBLIC_PORTAL_URL is not set"; exit 1; }' + - '[ -n "$STATIC_ASSETS_BUCKET" ] || { echo "❌ STATIC_ASSETS_BUCKET is not set"; exit 1; }' # Type check - echo "Type checking..." @@ -45,38 +47,66 @@ phases: - cd apps/$APP_NAME - NODE_TLS_REJECT_UNAUTHORIZED=0 bun run build - # Prepare standalone build + # Upload static assets to S3 (solves chunk drift issue) + - echo "Uploading static assets to S3..." + - | + if [ -d ".next/static" ]; then + echo "📦 Uploading .next/static/ files..." + aws s3 sync .next/static/ s3://$STATIC_ASSETS_BUCKET/_next/static/ \ + --cache-control "public, max-age=31536000, immutable" \ + --exclude "*.map" \ + --delete + echo "✅ Uploaded static assets to S3" + else + echo "⚠️ No .next/static directory found" + fi + + # Upload public assets + - | + if [ -d "public" ]; then + echo "📦 Uploading public/ files..." + aws s3 sync public/ s3://$STATIC_ASSETS_BUCKET/public/ \ + --cache-control "public, max-age=86400" \ + --exclude "*.map" + echo "✅ Uploaded public assets to S3" + else + echo "⚠️ No public directory found" + fi + + # Prepare standalone build for container - echo "Preparing standalone build..." - echo "DEBUG - Checking what Next.js built..." - ls -la .next/ - ls -la .next/standalone/ || echo "No standalone directory" - - echo "DEBUG - Checking if static files exist..." - - ls -la .next/static/ || echo "No static directory found" - - echo "DEBUG - Copying static files to standalone..." - - cp -r public .next/standalone/apps/$APP_NAME/ || echo "No public folder" - - cp -r .next/static .next/standalone/apps/$APP_NAME/.next/ || echo "No .next/static directory" - - echo "DEBUG - Final verification..." - - ls -la .next/standalone/ || echo "Standalone empty" - - find .next/standalone -name "*.css" | head -3 || echo "No CSS files found" - - find .next/standalone -name "*.js" | head -3 || echo "No JS files found" - - # Copy Prisma client + + # Create container build directory + - mkdir -p container-build + + # Copy standalone app (without static files) + - echo "DEBUG - Copying standalone files (excluding static assets)..." + - cp -r .next/standalone/apps/$APP_NAME/* container-build/ || echo "Standalone copy failed" + + # Copy package.json and other necessary files + - cp package.json container-build/ || echo "No package.json" + - cp Dockerfile container-build/ || echo "No Dockerfile" + + # Copy Prisma client to container - echo "Copying Prisma client..." - - mkdir -p .next/standalone/node_modules/.prisma .next/standalone/node_modules/@prisma + - mkdir -p container-build/node_modules/.prisma container-build/node_modules/@prisma - if [ -d "../../node_modules/.prisma/client" ]; then - cp -r ../../node_modules/.prisma/client .next/standalone/node_modules/.prisma/; + cp -r ../../node_modules/.prisma/client container-build/node_modules/.prisma/; elif [ -d "node_modules/.prisma/client" ]; then - cp -r node_modules/.prisma/client .next/standalone/node_modules/.prisma/; + cp -r node_modules/.prisma/client container-build/node_modules/.prisma/; fi - if [ -d "../../node_modules/@prisma/client" ]; then - cp -r "../../node_modules/@prisma/client" ".next/standalone/node_modules/@prisma/"; + cp -r "../../node_modules/@prisma/client" "container-build/node_modules/@prisma/"; elif [ -d "node_modules/@prisma/client" ]; then - cp -r "node_modules/@prisma/client" ".next/standalone/node_modules/@prisma/"; + cp -r "node_modules/@prisma/client" "container-build/node_modules/@prisma/"; fi - # Build Docker image + # Build Docker image (static assets now served from S3) - echo "Building Docker image..." - - docker build --build-arg BUILDKIT_INLINE_CACHE=1 -f ${DOCKERFILE_PATH:-Dockerfile} -t $ECR_REPOSITORY_URI:$IMAGE_TAG . + - docker build --build-arg BUILDKIT_INLINE_CACHE=1 -f container-build/Dockerfile -t $ECR_REPOSITORY_URI:$IMAGE_TAG container-build/ - docker tag $ECR_REPOSITORY_URI:$IMAGE_TAG $ECR_REPOSITORY_URI:latest post_build: diff --git a/apps/app/next.config.ts b/apps/app/next.config.ts index fb956adba..02047517d 100644 --- a/apps/app/next.config.ts +++ b/apps/app/next.config.ts @@ -1,7 +1,13 @@ import type { NextConfig } from 'next'; +import path from 'path'; import './src/env.mjs'; const config: NextConfig = { + // Use S3 bucket for static assets + assetPrefix: + process.env.NODE_ENV === 'production' + ? `https://${process.env.STATIC_ASSETS_BUCKET}.s3.amazonaws.com` + : '', poweredByHeader: false, reactStrictMode: true, transpilePackages: ['@trycompai/db'], @@ -29,6 +35,7 @@ const config: NextConfig = { }, authInterrupts: true, }, + outputFileTracingRoot: path.join(__dirname, '../../'), outputFileTracingIncludes: { '/api/**/*': ['./node_modules/.prisma/client/**/*'], }, @@ -99,8 +106,7 @@ const config: NextConfig = { skipTrailingSlashRedirect: true, }; -if (process.env.VERCEL !== '1') { - config.output = 'standalone'; -} +// Always use standalone output +config.output = 'standalone'; export default config; diff --git a/apps/portal/buildspec.yml b/apps/portal/buildspec.yml index c4587acf2..203357800 100644 --- a/apps/portal/buildspec.yml +++ b/apps/portal/buildspec.yml @@ -32,6 +32,10 @@ phases: - echo "Generating Prisma client..." - cd packages/db && bun x prisma generate && cd ../../ + # Validate environment variables + - echo "Validating environment variables..." + - '[ -n "$STATIC_ASSETS_BUCKET" ] || { echo "❌ STATIC_ASSETS_BUCKET is not set"; exit 1; }' + # Type check (optional - skip if script doesn't exist) - echo "Type checking..." - cd apps/$APP_NAME && (bun run typecheck || echo "No typecheck script found, skipping...") && cd ../../ @@ -41,38 +45,66 @@ phases: - cd apps/$APP_NAME - NODE_TLS_REJECT_UNAUTHORIZED=0 bun run build - # Prepare standalone build + # Upload static assets to S3 (solves chunk drift issue) + - echo "Uploading static assets to S3..." + - | + if [ -d ".next/static" ]; then + echo "📦 Uploading .next/static/ files..." + aws s3 sync .next/static/ s3://$STATIC_ASSETS_BUCKET/_next/static/ \ + --cache-control "public, max-age=31536000, immutable" \ + --exclude "*.map" \ + --delete + echo "✅ Uploaded static assets to S3" + else + echo "⚠️ No .next/static directory found" + fi + + # Upload public assets + - | + if [ -d "public" ]; then + echo "📦 Uploading public/ files..." + aws s3 sync public/ s3://$STATIC_ASSETS_BUCKET/public/ \ + --cache-control "public, max-age=86400" \ + --exclude "*.map" + echo "✅ Uploaded public assets to S3" + else + echo "⚠️ No public directory found" + fi + + # Prepare standalone build for container - echo "Preparing standalone build..." - echo "DEBUG - Checking what Next.js built..." - ls -la .next/ - ls -la .next/standalone/ || echo "No standalone directory" - - echo "DEBUG - Checking if static files exist..." - - ls -la .next/static/ || echo "No static directory found" - - echo "DEBUG - Copying static files to standalone..." - - cp -r public .next/standalone/apps/$APP_NAME/ || echo "No public folder" - - cp -r .next/static .next/standalone/apps/$APP_NAME/.next/ || echo "No .next/static directory" - - echo "DEBUG - Final verification..." - - ls -la .next/standalone/ || echo "Standalone empty" - - find .next/standalone -name "*.css" | head -3 || echo "No CSS files found" - - find .next/standalone -name "*.js" | head -3 || echo "No JS files found" - - # Copy Prisma client + + # Create container build directory + - mkdir -p container-build + + # Copy standalone app (without static files) + - echo "DEBUG - Copying standalone files (excluding static assets)..." + - cp -r .next/standalone/apps/$APP_NAME/* container-build/ || echo "Standalone copy failed" + + # Copy package.json and other necessary files + - cp package.json container-build/ || echo "No package.json" + - cp Dockerfile container-build/ || echo "No Dockerfile" + + # Copy Prisma client to container - echo "Copying Prisma client..." - - mkdir -p .next/standalone/node_modules/.prisma .next/standalone/node_modules/@prisma + - mkdir -p container-build/node_modules/.prisma container-build/node_modules/@prisma - if [ -d "../../node_modules/.prisma/client" ]; then - cp -r ../../node_modules/.prisma/client .next/standalone/node_modules/.prisma/; + cp -r ../../node_modules/.prisma/client container-build/node_modules/.prisma/; elif [ -d "node_modules/.prisma/client" ]; then - cp -r node_modules/.prisma/client .next/standalone/node_modules/.prisma/; + cp -r node_modules/.prisma/client container-build/node_modules/.prisma/; fi - if [ -d "../../node_modules/@prisma/client" ]; then - cp -r "../../node_modules/@prisma/client" ".next/standalone/node_modules/@prisma/"; + cp -r "../../node_modules/@prisma/client" "container-build/node_modules/@prisma/"; elif [ -d "node_modules/@prisma/client" ]; then - cp -r "node_modules/@prisma/client" ".next/standalone/node_modules/@prisma/"; + cp -r "node_modules/@prisma/client" "container-build/node_modules/@prisma/"; fi - # Build Docker image + # Build Docker image (static assets now served from S3) - echo "Building Docker image..." - - docker build --build-arg BUILDKIT_INLINE_CACHE=1 -f ${DOCKERFILE_PATH:-Dockerfile} -t $ECR_REPOSITORY_URI:$IMAGE_TAG . + - docker build --build-arg BUILDKIT_INLINE_CACHE=1 -f container-build/Dockerfile -t $ECR_REPOSITORY_URI:$IMAGE_TAG container-build/ - docker tag $ECR_REPOSITORY_URI:$IMAGE_TAG $ECR_REPOSITORY_URI:latest post_build: diff --git a/apps/portal/next.config.ts b/apps/portal/next.config.ts index 34fee5b04..555a155f3 100644 --- a/apps/portal/next.config.ts +++ b/apps/portal/next.config.ts @@ -1,8 +1,15 @@ import type { NextConfig } from 'next'; +import path from 'path'; import './src/env.mjs'; const config: NextConfig = { + // Use S3 bucket for static assets + assetPrefix: + process.env.NODE_ENV === 'production' + ? `https://${process.env.STATIC_ASSETS_BUCKET}.s3.amazonaws.com` + : '', transpilePackages: ['@trycompai/db'], + outputFileTracingRoot: path.join(__dirname, '../../'), outputFileTracingIncludes: { '/api/**/*': ['./prisma/**/*'], }, @@ -33,8 +40,7 @@ const config: NextConfig = { skipTrailingSlashRedirect: true, }; -if (process.env.VERCEL !== '1') { - config.output = 'standalone'; -} +// Always use standalone output +config.output = 'standalone'; export default config; From 1169bf76187e8a4d090e4b0ccc0a3331cf52fd1c Mon Sep 17 00:00:00 2001 From: Claudio Fuentes Date: Sun, 3 Aug 2025 22:41:09 -0400 Subject: [PATCH 02/10] chore: update Dockerfiles for streamlined build process and entry point configuration - Modified Dockerfiles for both app and portal to copy all necessary files from the current directory, simplifying the build context. - Updated the CMD instruction to directly run the server.js file, removing the previous conditional checks for entry points, enhancing clarity and efficiency. --- apps/app/Dockerfile | 8 ++++---- apps/portal/Dockerfile | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/apps/app/Dockerfile b/apps/app/Dockerfile index 0f4929ac8..97e83b6c6 100644 --- a/apps/app/Dockerfile +++ b/apps/app/Dockerfile @@ -13,8 +13,8 @@ ENV PORT=3000 ENV HOSTNAME="0.0.0.0" ENV NODE_TLS_REJECT_UNAUTHORIZED=0 -# Copy the complete standalone build from CodeBuild -COPY .next/standalone ./ +# Copy the standalone build files (already prepared in container-build/) +COPY . ./ # Create non-root user for security RUN addgroup -g 1001 -S nodejs && \ @@ -25,5 +25,5 @@ USER nextjs EXPOSE 3000 -# Use node to run the standalone server - handle monorepo structure -CMD ["sh", "-c", "if [ -f apps/app/server.js ]; then node apps/app/server.js; elif [ -f server.js ]; then node server.js; elif [ -f index.js ]; then node index.js; else echo 'No entry point found' && exit 1; fi"] \ No newline at end of file +# Use node to run the standalone server +CMD ["node", "server.js"] \ No newline at end of file diff --git a/apps/portal/Dockerfile b/apps/portal/Dockerfile index 02f8e2c1b..07e39e762 100644 --- a/apps/portal/Dockerfile +++ b/apps/portal/Dockerfile @@ -13,8 +13,8 @@ ENV PORT=3001 ENV HOSTNAME="0.0.0.0" ENV NODE_TLS_REJECT_UNAUTHORIZED=0 -# Copy the complete standalone build from CodeBuild -COPY .next/standalone ./ +# Copy the standalone build files (already prepared in container-build/) +COPY . ./ # Create non-root user for security RUN addgroup -g 1001 -S nodejs && \ @@ -25,5 +25,5 @@ USER nextjs EXPOSE 3001 -# Use node to run the standalone server - handle monorepo structure -CMD ["sh", "-c", "if [ -f apps/portal/server.js ]; then node apps/portal/server.js; elif [ -f server.js ]; then node server.js; elif [ -f index.js ]; then node index.js; else echo 'No entry point found' && exit 1; fi"] \ No newline at end of file +# Use node to run the standalone server +CMD ["node", "server.js"] \ No newline at end of file From 5555ece1fa559b5a6a1cd87ddf1a2a88132edf77 Mon Sep 17 00:00:00 2001 From: Claudio Fuentes Date: Sun, 3 Aug 2025 23:10:04 -0400 Subject: [PATCH 03/10] chore: enhance build process and asset management for app and portal - Updated buildspec.yml files to upload public assets directly to the S3 bucket root, improving asset management. - Added steps to copy standalone node_modules and ensure the availability of the Prisma client from both the monorepo root and app directory. - Enhanced next.config.ts to include production rewrites for redirecting public assets to S3, streamlining asset delivery. - Included debug statements to verify the contents of the container build directory, ensuring all necessary files are present. --- apps/app/buildspec.yml | 47 +++++++++++++++++++++++++++----------- apps/app/next.config.ts | 13 ++++++++++- apps/portal/buildspec.yml | 47 +++++++++++++++++++++++++++----------- apps/portal/next.config.ts | 13 ++++++++++- 4 files changed, 92 insertions(+), 28 deletions(-) diff --git a/apps/app/buildspec.yml b/apps/app/buildspec.yml index c48931644..614472dfb 100644 --- a/apps/app/buildspec.yml +++ b/apps/app/buildspec.yml @@ -61,14 +61,15 @@ phases: echo "⚠️ No .next/static directory found" fi - # Upload public assets + # Upload public assets to bucket root (not /public/ prefix) - | if [ -d "public" ]; then - echo "📦 Uploading public/ files..." - aws s3 sync public/ s3://$STATIC_ASSETS_BUCKET/public/ \ + echo "📦 Uploading public/ files to bucket root..." + aws s3 sync public/ s3://$STATIC_ASSETS_BUCKET/ \ --cache-control "public, max-age=86400" \ - --exclude "*.map" - echo "✅ Uploaded public assets to S3" + --exclude "*.map" \ + --exclude "_next/*" + echo "✅ Uploaded public assets to S3 root" else echo "⚠️ No public directory found" fi @@ -86,24 +87,44 @@ phases: - echo "DEBUG - Copying standalone files (excluding static assets)..." - cp -r .next/standalone/apps/$APP_NAME/* container-build/ || echo "Standalone copy failed" + # Copy node_modules from standalone build (contains minimal required dependencies) + - echo "Copying standalone node_modules..." + - cp -r .next/standalone/node_modules container-build/ || echo "No standalone node_modules" + # Copy package.json and other necessary files - cp package.json container-build/ || echo "No package.json" - cp Dockerfile container-build/ || echo "No Dockerfile" - # Copy Prisma client to container - - echo "Copying Prisma client..." + # Ensure Prisma client is available (override standalone if needed) + - echo "Ensuring Prisma client is available..." - mkdir -p container-build/node_modules/.prisma container-build/node_modules/@prisma - - if [ -d "../../node_modules/.prisma/client" ]; then - cp -r ../../node_modules/.prisma/client container-build/node_modules/.prisma/; + - | + if [ -d "../../node_modules/.prisma/client" ]; then + echo "Copying Prisma client from monorepo root..." + cp -r ../../node_modules/.prisma/client container-build/node_modules/.prisma/ elif [ -d "node_modules/.prisma/client" ]; then - cp -r node_modules/.prisma/client container-build/node_modules/.prisma/; + echo "Copying Prisma client from app directory..." + cp -r node_modules/.prisma/client container-build/node_modules/.prisma/ + else + echo "Warning: No Prisma client found" fi - - if [ -d "../../node_modules/@prisma/client" ]; then - cp -r "../../node_modules/@prisma/client" "container-build/node_modules/@prisma/"; + - | + if [ -d "../../node_modules/@prisma/client" ]; then + echo "Copying @prisma/client from monorepo root..." + cp -r "../../node_modules/@prisma/client" "container-build/node_modules/@prisma/" elif [ -d "node_modules/@prisma/client" ]; then - cp -r "node_modules/@prisma/client" "container-build/node_modules/@prisma/"; + echo "Copying @prisma/client from app directory..." + cp -r "node_modules/@prisma/client" "container-build/node_modules/@prisma/" + else + echo "Warning: No @prisma/client found" fi + # Debug: Verify container build contents + - echo "DEBUG - Verifying container build directory..." + - ls -la container-build/ + - ls -la container-build/node_modules/next/ || echo "❌ Next.js module not found" + - ls -la container-build/node_modules/.prisma/ || echo "❌ Prisma client not found" + # Build Docker image (static assets now served from S3) - echo "Building Docker image..." - docker build --build-arg BUILDKIT_INLINE_CACHE=1 -f container-build/Dockerfile -t $ECR_REPOSITORY_URI:$IMAGE_TAG container-build/ diff --git a/apps/app/next.config.ts b/apps/app/next.config.ts index 02047517d..56c3a7df7 100644 --- a/apps/app/next.config.ts +++ b/apps/app/next.config.ts @@ -88,7 +88,7 @@ const config: NextConfig = { ]; }, async rewrites() { - return [ + const rewrites = [ { source: '/ingest/static/:path*', destination: 'https://us-assets.i.posthog.com/static/:path*', @@ -102,6 +102,17 @@ const config: NextConfig = { destination: 'https://us.i.posthog.com/decide', }, ]; + + // In production, redirect public assets to S3 + if (process.env.NODE_ENV === 'production' && process.env.STATIC_ASSETS_BUCKET) { + rewrites.push({ + source: + '/:path((?!api|_next).*\\.(ico|png|jpg|jpeg|gif|svg|webp|woff|woff2|ttf|eot|otf|mp3|mp4|webm|pdf))', + destination: `https://${process.env.STATIC_ASSETS_BUCKET}.s3.amazonaws.com/:path`, + }); + } + + return rewrites; }, skipTrailingSlashRedirect: true, }; diff --git a/apps/portal/buildspec.yml b/apps/portal/buildspec.yml index 203357800..1c9f7813f 100644 --- a/apps/portal/buildspec.yml +++ b/apps/portal/buildspec.yml @@ -59,14 +59,15 @@ phases: echo "⚠️ No .next/static directory found" fi - # Upload public assets + # Upload public assets to bucket root (not /public/ prefix) - | if [ -d "public" ]; then - echo "📦 Uploading public/ files..." - aws s3 sync public/ s3://$STATIC_ASSETS_BUCKET/public/ \ + echo "📦 Uploading public/ files to bucket root..." + aws s3 sync public/ s3://$STATIC_ASSETS_BUCKET/ \ --cache-control "public, max-age=86400" \ - --exclude "*.map" - echo "✅ Uploaded public assets to S3" + --exclude "*.map" \ + --exclude "_next/*" + echo "✅ Uploaded public assets to S3 root" else echo "⚠️ No public directory found" fi @@ -84,24 +85,44 @@ phases: - echo "DEBUG - Copying standalone files (excluding static assets)..." - cp -r .next/standalone/apps/$APP_NAME/* container-build/ || echo "Standalone copy failed" + # Copy node_modules from standalone build (contains minimal required dependencies) + - echo "Copying standalone node_modules..." + - cp -r .next/standalone/node_modules container-build/ || echo "No standalone node_modules" + # Copy package.json and other necessary files - cp package.json container-build/ || echo "No package.json" - cp Dockerfile container-build/ || echo "No Dockerfile" - # Copy Prisma client to container - - echo "Copying Prisma client..." + # Ensure Prisma client is available (override standalone if needed) + - echo "Ensuring Prisma client is available..." - mkdir -p container-build/node_modules/.prisma container-build/node_modules/@prisma - - if [ -d "../../node_modules/.prisma/client" ]; then - cp -r ../../node_modules/.prisma/client container-build/node_modules/.prisma/; + - | + if [ -d "../../node_modules/.prisma/client" ]; then + echo "Copying Prisma client from monorepo root..." + cp -r ../../node_modules/.prisma/client container-build/node_modules/.prisma/ elif [ -d "node_modules/.prisma/client" ]; then - cp -r node_modules/.prisma/client container-build/node_modules/.prisma/; + echo "Copying Prisma client from app directory..." + cp -r node_modules/.prisma/client container-build/node_modules/.prisma/ + else + echo "Warning: No Prisma client found" fi - - if [ -d "../../node_modules/@prisma/client" ]; then - cp -r "../../node_modules/@prisma/client" "container-build/node_modules/@prisma/"; + - | + if [ -d "../../node_modules/@prisma/client" ]; then + echo "Copying @prisma/client from monorepo root..." + cp -r "../../node_modules/@prisma/client" "container-build/node_modules/@prisma/" elif [ -d "node_modules/@prisma/client" ]; then - cp -r "node_modules/@prisma/client" "container-build/node_modules/@prisma/"; + echo "Copying @prisma/client from app directory..." + cp -r "node_modules/@prisma/client" "container-build/node_modules/@prisma/" + else + echo "Warning: No @prisma/client found" fi + # Debug: Verify container build contents + - echo "DEBUG - Verifying container build directory..." + - ls -la container-build/ + - ls -la container-build/node_modules/next/ || echo "❌ Next.js module not found" + - ls -la container-build/node_modules/.prisma/ || echo "❌ Prisma client not found" + # Build Docker image (static assets now served from S3) - echo "Building Docker image..." - docker build --build-arg BUILDKIT_INLINE_CACHE=1 -f container-build/Dockerfile -t $ECR_REPOSITORY_URI:$IMAGE_TAG container-build/ diff --git a/apps/portal/next.config.ts b/apps/portal/next.config.ts index 555a155f3..55f11db30 100644 --- a/apps/portal/next.config.ts +++ b/apps/portal/next.config.ts @@ -22,7 +22,7 @@ const config: NextConfig = { ], }, async rewrites() { - return [ + const rewrites = [ { source: '/ingest/static/:path*', destination: 'https://us-assets.i.posthog.com/static/:path*', @@ -36,6 +36,17 @@ const config: NextConfig = { destination: 'https://us.i.posthog.com/decide', }, ]; + + // In production, redirect public assets to S3 + if (process.env.NODE_ENV === 'production' && process.env.STATIC_ASSETS_BUCKET) { + rewrites.push({ + source: + '/:path((?!api|_next).*\\.(ico|png|jpg|jpeg|gif|svg|webp|woff|woff2|ttf|eot|otf|mp3|mp4|webm|pdf))', + destination: `https://${process.env.STATIC_ASSETS_BUCKET}.s3.amazonaws.com/:path`, + }); + } + + return rewrites; }, skipTrailingSlashRedirect: true, }; From 5871433a94f120115bb9a06649a8a4166c40c467 Mon Sep 17 00:00:00 2001 From: Claudio Fuentes Date: Sun, 3 Aug 2025 23:15:48 -0400 Subject: [PATCH 04/10] chore: refactor asset rewrites in next.config.ts for app and portal - Updated next.config.ts files for both app and portal to enhance the handling of static assets in production. - Replaced the previous single rewrite rule with individual rewrites for common static file types, improving clarity and maintainability of asset management. --- apps/app/next.config.ts | 28 ++++++++++++++++++++++++---- apps/portal/next.config.ts | 28 ++++++++++++++++++++++++---- 2 files changed, 48 insertions(+), 8 deletions(-) diff --git a/apps/app/next.config.ts b/apps/app/next.config.ts index 56c3a7df7..d5c02c2a2 100644 --- a/apps/app/next.config.ts +++ b/apps/app/next.config.ts @@ -105,10 +105,30 @@ const config: NextConfig = { // In production, redirect public assets to S3 if (process.env.NODE_ENV === 'production' && process.env.STATIC_ASSETS_BUCKET) { - rewrites.push({ - source: - '/:path((?!api|_next).*\\.(ico|png|jpg|jpeg|gif|svg|webp|woff|woff2|ttf|eot|otf|mp3|mp4|webm|pdf))', - destination: `https://${process.env.STATIC_ASSETS_BUCKET}.s3.amazonaws.com/:path`, + // Add individual rewrites for common static file types + const staticExtensions = [ + 'ico', + 'png', + 'jpg', + 'jpeg', + 'gif', + 'svg', + 'webp', + 'woff', + 'woff2', + 'ttf', + 'eot', + 'otf', + 'mp3', + 'mp4', + 'webm', + 'pdf', + ]; + staticExtensions.forEach((ext) => { + rewrites.push({ + source: `/:path*.${ext}`, + destination: `https://${process.env.STATIC_ASSETS_BUCKET}.s3.amazonaws.com/:path*.${ext}`, + }); }); } diff --git a/apps/portal/next.config.ts b/apps/portal/next.config.ts index 55f11db30..da9e966da 100644 --- a/apps/portal/next.config.ts +++ b/apps/portal/next.config.ts @@ -39,10 +39,30 @@ const config: NextConfig = { // In production, redirect public assets to S3 if (process.env.NODE_ENV === 'production' && process.env.STATIC_ASSETS_BUCKET) { - rewrites.push({ - source: - '/:path((?!api|_next).*\\.(ico|png|jpg|jpeg|gif|svg|webp|woff|woff2|ttf|eot|otf|mp3|mp4|webm|pdf))', - destination: `https://${process.env.STATIC_ASSETS_BUCKET}.s3.amazonaws.com/:path`, + // Add individual rewrites for common static file types + const staticExtensions = [ + 'ico', + 'png', + 'jpg', + 'jpeg', + 'gif', + 'svg', + 'webp', + 'woff', + 'woff2', + 'ttf', + 'eot', + 'otf', + 'mp3', + 'mp4', + 'webm', + 'pdf', + ]; + staticExtensions.forEach((ext) => { + rewrites.push({ + source: `/:path*.${ext}`, + destination: `https://${process.env.STATIC_ASSETS_BUCKET}.s3.amazonaws.com/:path*.${ext}`, + }); }); } From 60ca18b3d14a6cfb372e020008c9bc8288a01c29 Mon Sep 17 00:00:00 2001 From: Claudio Fuentes Date: Sun, 3 Aug 2025 23:33:14 -0400 Subject: [PATCH 05/10] chore: update buildspec and next.config for improved asset management in app and portal - Enhanced buildspec.yml files to clarify the upload process for Next.js static and public assets to S3, improving CDN performance. - Refactored next.config.ts files to remove outdated production rewrites for static assets, streamlining asset handling. --- apps/app/buildspec.yml | 14 +++++++------- apps/app/next.config.ts | 33 +-------------------------------- apps/portal/buildspec.yml | 14 +++++++------- apps/portal/next.config.ts | 33 +-------------------------------- 4 files changed, 16 insertions(+), 78 deletions(-) diff --git a/apps/app/buildspec.yml b/apps/app/buildspec.yml index 614472dfb..8b74964f2 100644 --- a/apps/app/buildspec.yml +++ b/apps/app/buildspec.yml @@ -47,29 +47,29 @@ phases: - cd apps/$APP_NAME - NODE_TLS_REJECT_UNAUTHORIZED=0 bun run build - # Upload static assets to S3 (solves chunk drift issue) - - echo "Uploading static assets to S3..." + # Upload Next.js chunks and CSS to S3 for CDN performance + - echo "Uploading Next.js static assets to S3..." - | if [ -d ".next/static" ]; then - echo "📦 Uploading .next/static/ files..." + echo "📦 Uploading .next/static/ files to CDN..." aws s3 sync .next/static/ s3://$STATIC_ASSETS_BUCKET/_next/static/ \ --cache-control "public, max-age=31536000, immutable" \ --exclude "*.map" \ --delete - echo "✅ Uploaded static assets to S3" + echo "✅ Uploaded Next.js static assets to S3" else echo "⚠️ No .next/static directory found" fi - # Upload public assets to bucket root (not /public/ prefix) + # Upload public assets to S3 for CDN serving - | if [ -d "public" ]; then - echo "📦 Uploading public/ files to bucket root..." + echo "📦 Uploading public/ files to S3..." aws s3 sync public/ s3://$STATIC_ASSETS_BUCKET/ \ --cache-control "public, max-age=86400" \ --exclude "*.map" \ --exclude "_next/*" - echo "✅ Uploaded public assets to S3 root" + echo "✅ Uploaded public assets to S3" else echo "⚠️ No public directory found" fi diff --git a/apps/app/next.config.ts b/apps/app/next.config.ts index d5c02c2a2..02047517d 100644 --- a/apps/app/next.config.ts +++ b/apps/app/next.config.ts @@ -88,7 +88,7 @@ const config: NextConfig = { ]; }, async rewrites() { - const rewrites = [ + return [ { source: '/ingest/static/:path*', destination: 'https://us-assets.i.posthog.com/static/:path*', @@ -102,37 +102,6 @@ const config: NextConfig = { destination: 'https://us.i.posthog.com/decide', }, ]; - - // In production, redirect public assets to S3 - if (process.env.NODE_ENV === 'production' && process.env.STATIC_ASSETS_BUCKET) { - // Add individual rewrites for common static file types - const staticExtensions = [ - 'ico', - 'png', - 'jpg', - 'jpeg', - 'gif', - 'svg', - 'webp', - 'woff', - 'woff2', - 'ttf', - 'eot', - 'otf', - 'mp3', - 'mp4', - 'webm', - 'pdf', - ]; - staticExtensions.forEach((ext) => { - rewrites.push({ - source: `/:path*.${ext}`, - destination: `https://${process.env.STATIC_ASSETS_BUCKET}.s3.amazonaws.com/:path*.${ext}`, - }); - }); - } - - return rewrites; }, skipTrailingSlashRedirect: true, }; diff --git a/apps/portal/buildspec.yml b/apps/portal/buildspec.yml index 1c9f7813f..6cf1a39af 100644 --- a/apps/portal/buildspec.yml +++ b/apps/portal/buildspec.yml @@ -45,29 +45,29 @@ phases: - cd apps/$APP_NAME - NODE_TLS_REJECT_UNAUTHORIZED=0 bun run build - # Upload static assets to S3 (solves chunk drift issue) - - echo "Uploading static assets to S3..." + # Upload Next.js chunks and CSS to S3 for CDN performance + - echo "Uploading Next.js static assets to S3..." - | if [ -d ".next/static" ]; then - echo "📦 Uploading .next/static/ files..." + echo "📦 Uploading .next/static/ files to CDN..." aws s3 sync .next/static/ s3://$STATIC_ASSETS_BUCKET/_next/static/ \ --cache-control "public, max-age=31536000, immutable" \ --exclude "*.map" \ --delete - echo "✅ Uploaded static assets to S3" + echo "✅ Uploaded Next.js static assets to S3" else echo "⚠️ No .next/static directory found" fi - # Upload public assets to bucket root (not /public/ prefix) + # Upload public assets to S3 for CDN serving - | if [ -d "public" ]; then - echo "📦 Uploading public/ files to bucket root..." + echo "📦 Uploading public/ files to S3..." aws s3 sync public/ s3://$STATIC_ASSETS_BUCKET/ \ --cache-control "public, max-age=86400" \ --exclude "*.map" \ --exclude "_next/*" - echo "✅ Uploaded public assets to S3 root" + echo "✅ Uploaded public assets to S3" else echo "⚠️ No public directory found" fi diff --git a/apps/portal/next.config.ts b/apps/portal/next.config.ts index da9e966da..555a155f3 100644 --- a/apps/portal/next.config.ts +++ b/apps/portal/next.config.ts @@ -22,7 +22,7 @@ const config: NextConfig = { ], }, async rewrites() { - const rewrites = [ + return [ { source: '/ingest/static/:path*', destination: 'https://us-assets.i.posthog.com/static/:path*', @@ -36,37 +36,6 @@ const config: NextConfig = { destination: 'https://us.i.posthog.com/decide', }, ]; - - // In production, redirect public assets to S3 - if (process.env.NODE_ENV === 'production' && process.env.STATIC_ASSETS_BUCKET) { - // Add individual rewrites for common static file types - const staticExtensions = [ - 'ico', - 'png', - 'jpg', - 'jpeg', - 'gif', - 'svg', - 'webp', - 'woff', - 'woff2', - 'ttf', - 'eot', - 'otf', - 'mp3', - 'mp4', - 'webm', - 'pdf', - ]; - staticExtensions.forEach((ext) => { - rewrites.push({ - source: `/:path*.${ext}`, - destination: `https://${process.env.STATIC_ASSETS_BUCKET}.s3.amazonaws.com/:path*.${ext}`, - }); - }); - } - - return rewrites; }, skipTrailingSlashRedirect: true, }; From d454b3361e7c76366697f114cedee11b44abf1b7 Mon Sep 17 00:00:00 2001 From: Claudio Fuentes Date: Sun, 3 Aug 2025 23:40:38 -0400 Subject: [PATCH 06/10] chore: enhance buildspec for app and portal with app-specific standalone verification - Updated buildspec.yml files to include detailed debug statements for verifying app-specific standalone builds. - Added steps to ensure the presence of server.js, .next directory, and node_modules in the container build. - Improved clarity in the build process by organizing commands and enhancing error handling for file operations. --- apps/app/buildspec.yml | 35 ++++++++++++++++++++++++++--------- apps/portal/buildspec.yml | 32 +++++++++++++++++++++++--------- 2 files changed, 49 insertions(+), 18 deletions(-) diff --git a/apps/app/buildspec.yml b/apps/app/buildspec.yml index 8b74964f2..c551bdc54 100644 --- a/apps/app/buildspec.yml +++ b/apps/app/buildspec.yml @@ -79,20 +79,34 @@ phases: - echo "DEBUG - Checking what Next.js built..." - ls -la .next/ - ls -la .next/standalone/ || echo "No standalone directory" + - echo "DEBUG - Checking standalone structure..." + - find .next/standalone -name "server.js" -ls || echo "No server.js found" + - ls -la .next/standalone/apps/$APP_NAME/ || echo "No app directory" # Create container build directory - mkdir -p container-build - # Copy standalone app (without static files) - - echo "DEBUG - Copying standalone files (excluding static assets)..." - - cp -r .next/standalone/apps/$APP_NAME/* container-build/ || echo "Standalone copy failed" + # For monorepo standalone: copy only THIS app's server files + - echo "DEBUG - Creating app-specific standalone build..." + - test -f .next/standalone/apps/$APP_NAME/server.js && echo "✅ App-specific server.js found" || echo "❌ App-specific server.js missing" - # Copy node_modules from standalone build (contains minimal required dependencies) - - echo "Copying standalone node_modules..." - - cp -r .next/standalone/node_modules container-build/ || echo "No standalone node_modules" + # Copy the app-specific server and build artifacts + - cp -r .next/standalone/apps/$APP_NAME/* container-build/ || echo "App standalone copy failed" - # Copy package.json and other necessary files - - cp package.json container-build/ || echo "No package.json" + # Copy shared node_modules from monorepo standalone + - cp -r .next/standalone/node_modules container-build/ || echo "Shared node_modules copy failed" + + # Copy shared .next build artifacts + - cp -r .next/standalone/.next container-build/ || echo "Shared .next copy failed" + + # Verify the app-specific standalone build + - echo "DEBUG - Verifying app-specific standalone build..." + - ls -la container-build/ + - test -f container-build/server.js && echo "✅ App server.js exists" || echo "❌ App server.js missing" + - test -d container-build/.next && echo "✅ .next directory exists" || echo "❌ .next directory missing" + - test -d container-build/node_modules && echo "✅ node_modules exists" || echo "❌ node_modules missing" + + # Add Dockerfile to the standalone build - cp Dockerfile container-build/ || echo "No Dockerfile" # Ensure Prisma client is available (override standalone if needed) @@ -120,10 +134,13 @@ phases: fi # Debug: Verify container build contents - - echo "DEBUG - Verifying container build directory..." + - echo "DEBUG - Verifying app-specific container build..." - ls -la container-build/ + - ls -la container-build/.next/ || echo "❌ .next directory not found" - ls -la container-build/node_modules/next/ || echo "❌ Next.js module not found" - ls -la container-build/node_modules/.prisma/ || echo "❌ Prisma client not found" + - test -f container-build/server.js && echo "✅ App-specific server.js found" || echo "❌ App-specific server.js not found" + - head -10 container-build/server.js || echo "❌ Cannot read server.js" # Build Docker image (static assets now served from S3) - echo "Building Docker image..." diff --git a/apps/portal/buildspec.yml b/apps/portal/buildspec.yml index 6cf1a39af..8ba5d892f 100644 --- a/apps/portal/buildspec.yml +++ b/apps/portal/buildspec.yml @@ -81,16 +81,27 @@ phases: # Create container build directory - mkdir -p container-build - # Copy standalone app (without static files) - - echo "DEBUG - Copying standalone files (excluding static assets)..." - - cp -r .next/standalone/apps/$APP_NAME/* container-build/ || echo "Standalone copy failed" + # For monorepo standalone: copy only THIS app's server files + - echo "DEBUG - Creating app-specific standalone build..." + - test -f .next/standalone/apps/$APP_NAME/server.js && echo "✅ App-specific server.js found" || echo "❌ App-specific server.js missing" - # Copy node_modules from standalone build (contains minimal required dependencies) - - echo "Copying standalone node_modules..." - - cp -r .next/standalone/node_modules container-build/ || echo "No standalone node_modules" + # Copy the app-specific server and build artifacts + - cp -r .next/standalone/apps/$APP_NAME/* container-build/ || echo "App standalone copy failed" - # Copy package.json and other necessary files - - cp package.json container-build/ || echo "No package.json" + # Copy shared node_modules from monorepo standalone + - cp -r .next/standalone/node_modules container-build/ || echo "Shared node_modules copy failed" + + # Copy shared .next build artifacts + - cp -r .next/standalone/.next container-build/ || echo "Shared .next copy failed" + + # Verify the app-specific standalone build + - echo "DEBUG - Verifying app-specific standalone build..." + - ls -la container-build/ + - test -f container-build/server.js && echo "✅ App server.js exists" || echo "❌ App server.js missing" + - test -d container-build/.next && echo "✅ .next directory exists" || echo "❌ .next directory missing" + - test -d container-build/node_modules && echo "✅ node_modules exists" || echo "❌ node_modules missing" + + # Add Dockerfile to the standalone build - cp Dockerfile container-build/ || echo "No Dockerfile" # Ensure Prisma client is available (override standalone if needed) @@ -118,10 +129,13 @@ phases: fi # Debug: Verify container build contents - - echo "DEBUG - Verifying container build directory..." + - echo "DEBUG - Verifying app-specific container build..." - ls -la container-build/ + - ls -la container-build/.next/ || echo "❌ .next directory not found" - ls -la container-build/node_modules/next/ || echo "❌ Next.js module not found" - ls -la container-build/node_modules/.prisma/ || echo "❌ Prisma client not found" + - test -f container-build/server.js && echo "✅ App-specific server.js found" || echo "❌ App-specific server.js not found" + - head -10 container-build/server.js || echo "❌ Cannot read server.js" # Build Docker image (static assets now served from S3) - echo "Building Docker image..." From 5d8e714edda79f778a4574ad0057dbffc4e2285a Mon Sep 17 00:00:00 2001 From: Claudio Fuentes Date: Sun, 3 Aug 2025 23:44:39 -0400 Subject: [PATCH 07/10] chore: enhance buildspec for app and portal with improved standalone build verification - Updated buildspec.yml files to include additional debug statements for verifying the app's own .next build structure and server.js presence. - Refactored steps to copy the app's built files and standalone node_modules, ensuring a more robust container build process. - Improved error handling and clarity in the build process by organizing commands and enhancing feedback for file operations. --- apps/app/buildspec.yml | 51 ++++++++++++++++++++++++++++------- apps/portal/buildspec.yml | 56 ++++++++++++++++++++++++++++++--------- 2 files changed, 86 insertions(+), 21 deletions(-) diff --git a/apps/app/buildspec.yml b/apps/app/buildspec.yml index c551bdc54..757a620ff 100644 --- a/apps/app/buildspec.yml +++ b/apps/app/buildspec.yml @@ -82,22 +82,55 @@ phases: - echo "DEBUG - Checking standalone structure..." - find .next/standalone -name "server.js" -ls || echo "No server.js found" - ls -la .next/standalone/apps/$APP_NAME/ || echo "No app directory" + - echo "DEBUG - Checking app's own .next build structure..." + - ls -la .next/server/ || echo "No .next/server directory" + - ls -la .next/standalone/ || echo "No .next/standalone directory" + - echo "DEBUG - Checking for server.js in various locations..." + - test -f .next/standalone/server.js && echo "✅ Standalone server.js exists" || echo "❌ No standalone server.js" + - find .next -name "server.js" | head -5 || echo "No server.js found anywhere" # Create container build directory - mkdir -p container-build - # For monorepo standalone: copy only THIS app's server files - - echo "DEBUG - Creating app-specific standalone build..." - - test -f .next/standalone/apps/$APP_NAME/server.js && echo "✅ App-specific server.js found" || echo "❌ App-specific server.js missing" + # Use the standalone build properly: copy from .next/standalone + app's own build + - echo "DEBUG - Building container from standalone + app build..." - # Copy the app-specific server and build artifacts - - cp -r .next/standalone/apps/$APP_NAME/* container-build/ || echo "App standalone copy failed" + # Copy the app's own built files (server.js, etc.) + - echo "Copying app's own .next build..." + - cp -r .next container-build/ || echo "App .next copy failed" - # Copy shared node_modules from monorepo standalone - - cp -r .next/standalone/node_modules container-build/ || echo "Shared node_modules copy failed" + # Copy shared node_modules from standalone (minimal runtime deps) + - echo "Copying standalone node_modules (runtime dependencies)..." + - cp -r .next/standalone/node_modules container-build/ || echo "Standalone node_modules copy failed" - # Copy shared .next build artifacts - - cp -r .next/standalone/.next container-build/ || echo "Shared .next copy failed" + # Copy or create server.js for standalone + - | + if [ -f ".next/standalone/server.js" ]; then + echo "Using standalone server.js..." + cp .next/standalone/server.js container-build/ + else + echo "Creating minimal standalone server.js..." + cat > container-build/server.js << 'EOF' + const { createServer } = require('http') + const next = require('next') + + const dev = false + const hostname = process.env.HOSTNAME || '0.0.0.0' + const port = process.env.PORT || 3000 + + // Use the current directory as the Next.js app + const app = next({ dev, hostname, port, dir: __dirname }) + const handle = app.getRequestHandler() + + app.prepare().then(() => { + createServer(async (req, res) => { + await handle(req, res) + }).listen(port, hostname, () => { + console.log(`> Ready on http://${hostname}:${port}`) + }) + }) + EOF + fi # Verify the app-specific standalone build - echo "DEBUG - Verifying app-specific standalone build..." diff --git a/apps/portal/buildspec.yml b/apps/portal/buildspec.yml index 8ba5d892f..fb8440628 100644 --- a/apps/portal/buildspec.yml +++ b/apps/portal/buildspec.yml @@ -77,27 +77,59 @@ phases: - echo "DEBUG - Checking what Next.js built..." - ls -la .next/ - ls -la .next/standalone/ || echo "No standalone directory" + - echo "DEBUG - Checking app's own .next build structure..." + - ls -la .next/server/ || echo "No .next/server directory" + - echo "DEBUG - Checking for server.js in various locations..." + - test -f .next/standalone/server.js && echo "✅ Standalone server.js exists" || echo "❌ No standalone server.js" + - find .next -name "server.js" | head -5 || echo "No server.js found anywhere" # Create container build directory - mkdir -p container-build - # For monorepo standalone: copy only THIS app's server files - - echo "DEBUG - Creating app-specific standalone build..." - - test -f .next/standalone/apps/$APP_NAME/server.js && echo "✅ App-specific server.js found" || echo "❌ App-specific server.js missing" + # Use the standalone build properly: copy from .next/standalone + app's own build + - echo "DEBUG - Building container from standalone + app build..." - # Copy the app-specific server and build artifacts - - cp -r .next/standalone/apps/$APP_NAME/* container-build/ || echo "App standalone copy failed" + # Copy the app's own built files (server.js, etc.) + - echo "Copying app's own .next build..." + - cp -r .next container-build/ || echo "App .next copy failed" - # Copy shared node_modules from monorepo standalone - - cp -r .next/standalone/node_modules container-build/ || echo "Shared node_modules copy failed" + # Copy shared node_modules from standalone (minimal runtime deps) + - echo "Copying standalone node_modules (runtime dependencies)..." + - cp -r .next/standalone/node_modules container-build/ || echo "Standalone node_modules copy failed" - # Copy shared .next build artifacts - - cp -r .next/standalone/.next container-build/ || echo "Shared .next copy failed" + # Copy or create server.js for standalone + - | + if [ -f ".next/standalone/server.js" ]; then + echo "Using standalone server.js..." + cp .next/standalone/server.js container-build/ + else + echo "Creating minimal standalone server.js..." + cat > container-build/server.js << 'EOF' + const { createServer } = require('http') + const next = require('next') + + const dev = false + const hostname = process.env.HOSTNAME || '0.0.0.0' + const port = process.env.PORT || 3000 + + // Use the current directory as the Next.js app + const app = next({ dev, hostname, port, dir: __dirname }) + const handle = app.getRequestHandler() + + app.prepare().then(() => { + createServer(async (req, res) => { + await handle(req, res) + }).listen(port, hostname, () => { + console.log(`> Ready on http://${hostname}:${port}`) + }) + }) + EOF + fi - # Verify the app-specific standalone build - - echo "DEBUG - Verifying app-specific standalone build..." + # Verify the standalone build + - echo "DEBUG - Verifying standalone build..." - ls -la container-build/ - - test -f container-build/server.js && echo "✅ App server.js exists" || echo "❌ App server.js missing" + - test -f container-build/server.js && echo "✅ Server.js exists" || echo "❌ Server.js missing" - test -d container-build/.next && echo "✅ .next directory exists" || echo "❌ .next directory missing" - test -d container-build/node_modules && echo "✅ node_modules exists" || echo "❌ node_modules missing" From b5ba377d579b85054662780cd435ff0fd2d89c13 Mon Sep 17 00:00:00 2001 From: Claudio Fuentes Date: Sun, 3 Aug 2025 23:53:32 -0400 Subject: [PATCH 08/10] chore: update buildspec for app and portal to support app-specific standalone server.js - Modified buildspec.yml files to check for app-specific server.js files in the .next/standalone/apps directory, enhancing the build process for individual applications. - Added conditional logic to handle both app-specific and global server.js files, improving flexibility and clarity in the build output. --- apps/app/buildspec.yml | 7 +++++-- apps/portal/buildspec.yml | 7 +++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/apps/app/buildspec.yml b/apps/app/buildspec.yml index 757a620ff..1d447eb1d 100644 --- a/apps/app/buildspec.yml +++ b/apps/app/buildspec.yml @@ -105,8 +105,11 @@ phases: # Copy or create server.js for standalone - | - if [ -f ".next/standalone/server.js" ]; then - echo "Using standalone server.js..." + if [ -f ".next/standalone/apps/$APP_NAME/server.js" ]; then + echo "Using app-specific standalone server.js..." + cp .next/standalone/apps/$APP_NAME/server.js container-build/ + elif [ -f ".next/standalone/server.js" ]; then + echo "Using global standalone server.js..." cp .next/standalone/server.js container-build/ else echo "Creating minimal standalone server.js..." diff --git a/apps/portal/buildspec.yml b/apps/portal/buildspec.yml index fb8440628..90f105d5d 100644 --- a/apps/portal/buildspec.yml +++ b/apps/portal/buildspec.yml @@ -99,8 +99,11 @@ phases: # Copy or create server.js for standalone - | - if [ -f ".next/standalone/server.js" ]; then - echo "Using standalone server.js..." + if [ -f ".next/standalone/apps/$APP_NAME/server.js" ]; then + echo "Using app-specific standalone server.js..." + cp .next/standalone/apps/$APP_NAME/server.js container-build/ + elif [ -f ".next/standalone/server.js" ]; then + echo "Using global standalone server.js..." cp .next/standalone/server.js container-build/ else echo "Creating minimal standalone server.js..." From 3e203dee9920dc16016c4acab40210bdde6b99b5 Mon Sep 17 00:00:00 2001 From: Claudio Fuentes Date: Mon, 4 Aug 2025 00:05:35 -0400 Subject: [PATCH 09/10] chore: update next.config for app and portal to use STATIC_ASSETS_URL for assetPrefix - Refactored next.config.ts files to replace the hardcoded S3 asset URL with a dynamic STATIC_ASSETS_URL environment variable for production, enhancing flexibility in asset management. --- apps/app/next.config.ts | 5 +---- apps/portal/next.config.ts | 5 +---- 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/apps/app/next.config.ts b/apps/app/next.config.ts index 02047517d..57fae7015 100644 --- a/apps/app/next.config.ts +++ b/apps/app/next.config.ts @@ -4,10 +4,7 @@ import './src/env.mjs'; const config: NextConfig = { // Use S3 bucket for static assets - assetPrefix: - process.env.NODE_ENV === 'production' - ? `https://${process.env.STATIC_ASSETS_BUCKET}.s3.amazonaws.com` - : '', + assetPrefix: process.env.NODE_ENV === 'production' ? process.env.STATIC_ASSETS_URL : '', poweredByHeader: false, reactStrictMode: true, transpilePackages: ['@trycompai/db'], diff --git a/apps/portal/next.config.ts b/apps/portal/next.config.ts index 555a155f3..cc5d394c1 100644 --- a/apps/portal/next.config.ts +++ b/apps/portal/next.config.ts @@ -4,10 +4,7 @@ import './src/env.mjs'; const config: NextConfig = { // Use S3 bucket for static assets - assetPrefix: - process.env.NODE_ENV === 'production' - ? `https://${process.env.STATIC_ASSETS_BUCKET}.s3.amazonaws.com` - : '', + assetPrefix: process.env.NODE_ENV === 'production' ? process.env.STATIC_ASSETS_URL : '', transpilePackages: ['@trycompai/db'], outputFileTracingRoot: path.join(__dirname, '../../'), outputFileTracingIncludes: { From 64d4b2cd0b1df570537309202e24f5cb140352ff Mon Sep 17 00:00:00 2001 From: Claudio Fuentes Date: Mon, 4 Aug 2025 00:21:21 -0400 Subject: [PATCH 10/10] chore: update buildspec for app and portal to exclude .map files during S3 upload - Modified buildspec.yml files to remove the --delete option from the AWS S3 sync command, ensuring that only .map files are excluded from uploads, enhancing the asset management process. --- apps/app/buildspec.yml | 3 +-- apps/portal/buildspec.yml | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/apps/app/buildspec.yml b/apps/app/buildspec.yml index 1d447eb1d..b6afee6a4 100644 --- a/apps/app/buildspec.yml +++ b/apps/app/buildspec.yml @@ -54,8 +54,7 @@ phases: echo "📦 Uploading .next/static/ files to CDN..." aws s3 sync .next/static/ s3://$STATIC_ASSETS_BUCKET/_next/static/ \ --cache-control "public, max-age=31536000, immutable" \ - --exclude "*.map" \ - --delete + --exclude "*.map" echo "✅ Uploaded Next.js static assets to S3" else echo "⚠️ No .next/static directory found" diff --git a/apps/portal/buildspec.yml b/apps/portal/buildspec.yml index 90f105d5d..63c47dc8a 100644 --- a/apps/portal/buildspec.yml +++ b/apps/portal/buildspec.yml @@ -52,8 +52,7 @@ phases: echo "📦 Uploading .next/static/ files to CDN..." aws s3 sync .next/static/ s3://$STATIC_ASSETS_BUCKET/_next/static/ \ --cache-control "public, max-age=31536000, immutable" \ - --exclude "*.map" \ - --delete + --exclude "*.map" echo "✅ Uploaded Next.js static assets to S3" else echo "⚠️ No .next/static directory found"