Skip to content

Commit 29c82e5

Browse files
authored
Merge pull request #1258 from trycompai/claudio/fix-file-upload
[dev] [claudfuen] claudio/fix-file-upload
2 parents 4defae3 + e1e3152 commit 29c82e5

File tree

2 files changed

+77
-7
lines changed

2 files changed

+77
-7
lines changed

apps/portal/buildspec.yml

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,34 @@ phases:
4343
# Build Next.js portal app
4444
- echo "Building Next.js portal application..."
4545
- cd apps/$APP_NAME
46+
- rm -rf .next/cache/
4647
- NODE_TLS_REJECT_UNAUTHORIZED=0 bun run build
4748

4849
# Upload Next.js chunks and CSS to S3 for CDN performance
4950
- echo "Uploading Next.js static assets to S3..."
5051
- |
5152
if [ -d ".next/static" ]; then
5253
echo "📦 Uploading .next/static/ files to CDN..."
54+
LOCAL_CHUNK_COUNT=$(find .next/static -name "*.js" | wc -l)
55+
echo "Local chunks: $LOCAL_CHUNK_COUNT"
5356
aws s3 sync .next/static/ s3://$STATIC_ASSETS_BUCKET/portal/_next/static/ \
5457
--cache-control "public, max-age=31536000, immutable" \
5558
--exclude "*.map"
5659
echo "✅ Uploaded Next.js static assets to S3"
60+
61+
# Verify upload
62+
S3_CHUNK_COUNT=$(aws s3 ls s3://$STATIC_ASSETS_BUCKET/portal/_next/static/ --recursive | grep ".js$" | wc -l)
63+
echo "S3 chunks: $S3_CHUNK_COUNT"
64+
if [ "$S3_CHUNK_COUNT" -lt "$LOCAL_CHUNK_COUNT" ]; then
65+
echo "❌ S3 upload verification failed: Missing chunks"
66+
exit 1
67+
fi
68+
69+
# Invalidate CloudFront cache for static assets
70+
if [ -n "$CLOUDFRONT_DISTRIBUTION_ID" ]; then
71+
echo "Invalidating CloudFront cache..."
72+
aws cloudfront create-invalidation --distribution-id $CLOUDFRONT_DISTRIBUTION_ID --paths "/portal/_next/static/*"
73+
fi
5774
else
5875
echo "⚠️ No .next/static directory found"
5976
fi
@@ -88,14 +105,14 @@ phases:
88105
# Use the standalone build properly: copy from .next/standalone + app's own build
89106
- echo "DEBUG - Building container from standalone + app build..."
90107

91-
# Copy the app's own built files (server.js, etc.)
108+
# Copy standalone build first (includes server actions)
109+
- echo "Copying standalone build..."
110+
- cp -r .next/standalone/* container-build/ || echo "Standalone copy failed"
111+
112+
# Then copy app's own .next build to ensure server actions are included
92113
- echo "Copying app's own .next build..."
93114
- cp -r .next container-build/ || echo "App .next copy failed"
94115

95-
# Copy shared node_modules from standalone (minimal runtime deps)
96-
- echo "Copying standalone node_modules (runtime dependencies)..."
97-
- cp -r .next/standalone/node_modules container-build/ || echo "Standalone node_modules copy failed"
98-
99116
# Copy or create server.js for standalone
100117
- |
101118
if [ -f ".next/standalone/apps/$APP_NAME/server.js" ]; then
@@ -134,6 +151,19 @@ phases:
134151
- test -f container-build/server.js && echo "✅ Server.js exists" || echo "❌ Server.js missing"
135152
- test -d container-build/.next && echo "✅ .next directory exists" || echo "❌ .next directory missing"
136153
- test -d container-build/node_modules && echo "✅ node_modules exists" || echo "❌ node_modules missing"
154+
155+
# Critical: Verify static files and server files exist
156+
- |
157+
if [ ! -d "container-build/.next/static" ]; then
158+
echo "❌ Missing .next/static directory in container build"
159+
exit 1
160+
fi
161+
- |
162+
if [ ! -d "container-build/.next/server" ]; then
163+
echo "❌ Missing .next/server directory in container build"
164+
exit 1
165+
fi
166+
- echo "✅ Container build verification passed"
137167

138168
# Add Dockerfile to the standalone build
139169
- cp Dockerfile container-build/ || echo "No Dockerfile"
@@ -173,8 +203,13 @@ phases:
173203

174204
# Build Docker image (static assets now served from S3)
175205
- echo "Building Docker image..."
206+
- sleep 5
176207
- docker build --build-arg BUILDKIT_INLINE_CACHE=1 -f container-build/Dockerfile -t $ECR_REPOSITORY_URI:$IMAGE_TAG container-build/
177208
- docker tag $ECR_REPOSITORY_URI:$IMAGE_TAG $ECR_REPOSITORY_URI:latest
209+
210+
# Test container startup
211+
- echo "Testing container startup..."
212+
- timeout 30 docker run --rm -e NODE_ENV=production $ECR_REPOSITORY_URI:$IMAGE_TAG node --version || echo "Container test completed"
178213

179214
post_build:
180215
commands:
@@ -190,7 +225,6 @@ cache:
190225
- 'node_modules/**/*'
191226
- 'packages/db/node_modules/**/*'
192227
- '/root/.bun/install/cache/**/*'
193-
- '.next/cache/**/*'
194228
- 'bun.lock'
195229

196230
artifacts:

apps/portal/next.config.ts

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,16 @@ const config: NextConfig = {
99
transpilePackages: ['@trycompai/db'],
1010
outputFileTracingRoot: path.join(__dirname, '../../'),
1111
outputFileTracingIncludes: {
12-
'/api/**/*': ['./prisma/**/*'],
12+
'/**/*': ['./src/actions/**/*', './node_modules/.prisma/client/**/*'],
13+
},
14+
generateEtags: false,
15+
experimental: {
16+
serverActions: {
17+
allowedOrigins: process.env.NODE_ENV === 'production'
18+
? [process.env.NEXT_PUBLIC_BETTER_AUTH_URL].filter(Boolean) as string[]
19+
: undefined,
20+
},
21+
optimizePackageImports: ['@trycompai/db'],
1322
},
1423
images: {
1524
remotePatterns: [
@@ -62,6 +71,33 @@ const config: NextConfig = {
6271
},
6372
];
6473
},
74+
async headers() {
75+
return [
76+
{
77+
source: '/:path*',
78+
has: [
79+
{
80+
type: 'header',
81+
key: 'Next-Action',
82+
},
83+
],
84+
headers: [
85+
{
86+
key: 'Cache-Control',
87+
value: 'no-cache, no-store, must-revalidate',
88+
},
89+
{
90+
key: 'Pragma',
91+
value: 'no-cache',
92+
},
93+
{
94+
key: 'Expires',
95+
value: '0',
96+
},
97+
],
98+
},
99+
];
100+
},
65101
skipTrailingSlashRedirect: true,
66102
};
67103

0 commit comments

Comments
 (0)