Skip to content

Commit

Permalink
move stories to cloudfront
Browse files Browse the repository at this point in the history
  • Loading branch information
mtrenker committed Dec 22, 2023
1 parent d70c8ba commit dc4514e
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions packages/cdk-next-app/next-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,16 +153,6 @@ export class NextApp extends Construct {
new ARecord(this, 'ARecord', recordProps);
new AaaaRecord(this, 'AaaaRecord', recordProps);

// stories config

const storiesRecordProps: ARecordProps = {
recordName: ['stories', domainName].join('.'),
zone: hostedZone,
target: RecordTarget.fromAlias(new BucketWebsiteTarget(this.storiesBucket)),
};
new ARecord(this, 'StoriesARecord', storiesRecordProps);
new AaaaRecord(this, 'StoriesAaaaRecord', storiesRecordProps);

// queue config
this.revalidationQueue.grantSendMessages(this.serverFunction);
this.revalidationQueue.grantConsumeMessages(this.revalidationFunction);
Expand Down Expand Up @@ -200,11 +190,8 @@ export class NextApp extends Construct {

private prepareStoriesBucket(): Bucket {
return new Bucket(this, 'StoriesBucket', {
bucketName: ['stories', this.domainName].join('.'),
removalPolicy: RemovalPolicy.DESTROY,
autoDeleteObjects: true,
publicReadAccess: true,
websiteIndexDocument: 'index.html',
});
}

Expand Down Expand Up @@ -297,6 +284,7 @@ export class NextApp extends Construct {
const serverOrigin = new HttpOrigin(Fn.parseDomainName(serverFunctionUrl.url));
const imageOrigin = new HttpOrigin(Fn.parseDomainName(imageFunctionUrl.url));
const staticOrigin = new S3Origin(this.staticBucket);
const storiesOrigin = new S3Origin(this.storiesBucket);

const fallbackGroup = new OriginGroup({
primaryOrigin: serverOrigin,
Expand Down Expand Up @@ -343,6 +331,23 @@ export class NextApp extends Construct {
'/': lambdaBehavior,
'api/*': lambdaBehavior,
'_next/data/*': lambdaBehavior,
'stories/*': {
viewerProtocolPolicy: ViewerProtocolPolicy.REDIRECT_TO_HTTPS,
origin: storiesOrigin,
allowedMethods: AllowedMethods.ALLOW_GET_HEAD_OPTIONS,
cachedMethods: CachedMethods.CACHE_GET_HEAD_OPTIONS,
compress: true,
cachePolicy: new CachePolicy(this, 'StoriesCachePolicy', {
queryStringBehavior: CacheQueryStringBehavior.none(),
headerBehavior: CacheHeaderBehavior.none(),
cookieBehavior: CacheCookieBehavior.none(),
defaultTtl: Duration.days(30),
maxTtl: Duration.days(60),
minTtl: Duration.days(30),
enableAcceptEncodingBrotli: true,
enableAcceptEncodingGzip: true,
}),
},
'_next/image*': {
viewerProtocolPolicy: ViewerProtocolPolicy.REDIRECT_TO_HTTPS,
origin: imageOrigin,
Expand Down

0 comments on commit dc4514e

Please sign in to comment.