From 6065d72fa2f78c33323280401918dfc7ee77cb5d Mon Sep 17 00:00:00 2001 From: Roman Bobrovskyi Date: Fri, 4 Apr 2025 16:36:40 +0200 Subject: [PATCH] feat: added healtched and added locales to regexs --- src/build/next.ts | 8 +++++- src/cdk/constructs/CloudFrontDistribution.ts | 2 +- .../constructs/RenderServerDistribution.ts | 25 ++++++++++++++++--- src/cdk/stacks/NextRenderServerStack.ts | 7 ++++-- src/commands/deploy.ts | 1 + src/types/index.ts | 1 + 6 files changed, 37 insertions(+), 7 deletions(-) diff --git a/src/build/next.ts b/src/build/next.ts index 5a25227..d78a403 100644 --- a/src/build/next.ts +++ b/src/build/next.ts @@ -98,10 +98,16 @@ export const getNextCachedRoutesConfig = async ( const prerenderManifest = JSON.parse(prerenderManifestJSON) as PrerenderManifest const routesManifest = JSON.parse(routesManifestJSON) as RoutesManifest + const locales = routesManifest.i18n?.locales ?? [] + const cachedRoutesMatchers = [...routesManifest.dynamicRoutes, ...routesManifest.staticRoutes].reduce( (prev, route) => { if (prerenderManifest.routes?.[route.page] || prerenderManifest.dynamicRoutes?.[route.page]) { - prev.push(route.regex) + if (locales.length) { + prev.push(...locales.map((locale) => route.regex.replace('^', `^/${locale}`))) + } else { + prev.push(route.regex) + } } return prev diff --git a/src/cdk/constructs/CloudFrontDistribution.ts b/src/cdk/constructs/CloudFrontDistribution.ts index 86b1af1..3e11309 100644 --- a/src/cdk/constructs/CloudFrontDistribution.ts +++ b/src/cdk/constructs/CloudFrontDistribution.ts @@ -24,7 +24,7 @@ const OneMonthCache = Duration.days(30) const NoCache = Duration.seconds(0) const defaultNextQueries = ['_rsc'] -const defaultNextHeaders = ['Cache-Control', 'Next-Router-State-Tree', 'Next-Url', 'Rsc', 'Next-Router-Prefetch'] +const defaultNextHeaders = ['Next-Router-State-Tree', 'Next-Url', 'Rsc', 'Next-Router-Prefetch'] const imageQueries = ['w', 'h', 'url', 'q'] export class CloudFrontDistribution extends Construct { public readonly cf: cloudfront.Distribution diff --git a/src/cdk/constructs/RenderServerDistribution.ts b/src/cdk/constructs/RenderServerDistribution.ts index 3096b97..6e17b62 100644 --- a/src/cdk/constructs/RenderServerDistribution.ts +++ b/src/cdk/constructs/RenderServerDistribution.ts @@ -17,6 +17,7 @@ interface RenderServerDistributionProps { minInstances?: number maxInstances?: number dynamoDBCacheTable: string + healthCheckPath?: string } const NodeJSEnvironmentMapping: Record = { @@ -46,7 +47,8 @@ export class RenderServerDistribution extends Construct { instanceType = 't2.micro', minInstances = 1, maxInstances = 2, - dynamoDBCacheTable + dynamoDBCacheTable, + healthCheckPath = '/' } = props this.vpc = new Vpc(this, 'BeanstalkVPC', { @@ -82,6 +84,13 @@ export class RenderServerDistribution extends Construct { managedPolicies: [iam.ManagedPolicy.fromAwsManagedPolicyName('AWSElasticBeanstalkWebTier')] }) + this.ebInstanceProfileRole.addToPolicy( + new iam.PolicyStatement({ + actions: ['logs:PutLogEvents', 'logs:CreateLogStream', 'logs:DescribeLogGroups', 'logs:DescribeLogStreams'], + resources: ['*'] + }) + ) + this.ebInstanceProfileRole.addToPolicy( new iam.PolicyStatement({ actions: ['s3:Get*', 's3:Put*', 's3:Delete*', 's3:ListBucket'], @@ -131,6 +140,11 @@ export class RenderServerDistribution extends Construct { optionName: 'LoadBalancerType', value: 'application' }, + { + namespace: 'aws:elasticbeanstalk:environment:process:default', + optionName: 'HealthCheckPath', + value: healthCheckPath + }, { namespace: 'aws:autoscaling:launchconfiguration', optionName: 'InstanceType', @@ -174,12 +188,12 @@ export class RenderServerDistribution extends Construct { { namespace: 'aws:autoscaling:trigger', optionName: 'UpperThreshold', - value: '75' + value: '60' }, { namespace: 'aws:autoscaling:trigger', optionName: 'LowerThreshold', - value: '40' + value: '30' }, { namespace: 'aws:ec2:vpc', @@ -195,6 +209,11 @@ export class RenderServerDistribution extends Construct { namespace: 'aws:ec2:vpc', optionName: 'ELBSubnets', value: publicSubnets.join(',') + }, + { + namespace: 'aws:elasticbeanstalk:cloudwatch:logs', + optionName: 'StreamLogs', + value: 'true' } ] }) diff --git a/src/cdk/stacks/NextRenderServerStack.ts b/src/cdk/stacks/NextRenderServerStack.ts index 0f9a3a5..970f771 100644 --- a/src/cdk/stacks/NextRenderServerStack.ts +++ b/src/cdk/stacks/NextRenderServerStack.ts @@ -16,6 +16,7 @@ export interface NextRenderServerStackProps extends cdk.StackProps { renderServerInstanceType?: string renderServerMinInstances?: number renderServerMaxInstances?: number + healthCheckPath?: string } export class NextRenderServerStack extends cdk.Stack { @@ -35,7 +36,8 @@ export class NextRenderServerStack extends cdk.Stack { region, renderServerInstanceType, renderServerMinInstances, - renderServerMaxInstances + renderServerMaxInstances, + healthCheckPath } = props this.staticBucketName = `${id}-static` @@ -68,7 +70,8 @@ export class NextRenderServerStack extends cdk.Stack { instanceType: renderServerInstanceType, minInstances: renderServerMinInstances, maxInstances: renderServerMaxInstances, - dynamoDBCacheTable: this.dynamoDB.table.tableName + dynamoDBCacheTable: this.dynamoDB.table.tableName, + healthCheckPath }) this.renderWorker = new RenderWorkerDistribution(this, `${id}-renderWorker`, { diff --git a/src/commands/deploy.ts b/src/commands/deploy.ts index 815b996..4e64ce9 100644 --- a/src/commands/deploy.ts +++ b/src/commands/deploy.ts @@ -133,6 +133,7 @@ export const deploy = async (config: DeployConfig) => { renderServerInstanceType, renderServerMaxInstances, renderServerMinInstances, + healthCheckPath: deployConfig.healthCheckPath, env: { region } diff --git a/src/types/index.ts b/src/types/index.ts index 0b7779c..83332cf 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -10,6 +10,7 @@ export interface CacheConfig { export interface DeployConfig { cache: CacheConfig + healthCheckPath?: string publicAssets?: { prefix: string ttl?: number