Skip to content

Commit

Permalink
fix: fixed the cache policy issues
Browse files Browse the repository at this point in the history
  • Loading branch information
meza committed Mar 10, 2023
1 parent 251485d commit db6aade
Showing 1 changed file with 23 additions and 9 deletions.
32 changes: 23 additions & 9 deletions deployment/lib/Constructs/RemixCDN.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
import { Duration } from 'aws-cdk-lib';
import { AllowedMethods,
import {
AllowedMethods,
CacheCookieBehavior,
CacheHeaderBehavior,
CachePolicy, CacheQueryStringBehavior, Distribution,
CachePolicy,
CacheQueryStringBehavior,
Distribution,
OriginRequestCookieBehavior,
OriginRequestHeaderBehavior,
OriginRequestPolicy,
OriginRequestQueryStringBehavior, PriceClass, ViewerProtocolPolicy
OriginRequestQueryStringBehavior,
PriceClass,
ViewerProtocolPolicy
} from 'aws-cdk-lib/aws-cloudfront';
import { HttpOrigin, S3Origin } from 'aws-cdk-lib/aws-cloudfront-origins';
import { Construct } from 'constructs';
import type { RemixApiGateway } from './RemixApiGateway';
import type { RemixDeployment } from './RemixDeployment';
import type { ICertificate } from 'aws-cdk-lib/aws-certificatemanager';
import type {
AddBehaviorOptions } from 'aws-cdk-lib/aws-cloudfront';
import type { AddBehaviorOptions } from 'aws-cdk-lib/aws-cloudfront';

export interface RemixCDNProps {
remixDeployment: RemixDeployment;
Expand All @@ -40,14 +44,23 @@ export class RemixCDN extends Construct {
});

const cachePolicy = new CachePolicy(this, 'CachePolicy', {
defaultTtl: Duration.seconds(86400),
defaultTtl: Duration.seconds(0),
minTtl: Duration.seconds(0),
maxTtl: Duration.seconds(31536000),
maxTtl: Duration.seconds(0),
cookieBehavior: CacheCookieBehavior.none(),
headerBehavior: CacheHeaderBehavior.allowList('Origin', 'Access-Control-Request-Method', 'Access-Control-Request-Headers', 'Referer', 'Accept-Language', 'Accept-Datetime'),
headerBehavior: CacheHeaderBehavior.none(),
queryStringBehavior: CacheQueryStringBehavior.none()
});

const assetsCachePolicy = new CachePolicy(this, 'AssetsCachePolicy', {
defaultTtl: Duration.seconds(86400),
minTtl: Duration.seconds(1),
maxTtl: Duration.seconds(31536000),
cookieBehavior: CacheCookieBehavior.none(),
headerBehavior: CacheHeaderBehavior.none(),
queryStringBehavior: CacheQueryStringBehavior.allowList('v')
});

const requestHandlerBehavior: AddBehaviorOptions = {
allowedMethods: AllowedMethods.ALLOW_ALL,
viewerProtocolPolicy: ViewerProtocolPolicy.REDIRECT_TO_HTTPS,
Expand All @@ -56,7 +69,8 @@ export class RemixCDN extends Construct {
};

const assetBehaviorOptions = {
viewerProtocolPolicy: ViewerProtocolPolicy.REDIRECT_TO_HTTPS
viewerProtocolPolicy: ViewerProtocolPolicy.REDIRECT_TO_HTTPS,
cachePolicy: assetsCachePolicy
};

this.cloudFrontDistribution = new Distribution(this, 'HUHDistribution', {
Expand Down

0 comments on commit db6aade

Please sign in to comment.