Skip to content

Commit

Permalink
feat: Cache-control max-age (#20)
Browse files Browse the repository at this point in the history
* Cache-control max-age

* chore: self mutation

Signed-off-by: github-actions <[email protected]>

* fix: Fix hosted zone lookup (#22)

* mark aws-sdk as external when building the lambda server

* fix precendence ordering of image path (#29)

* Update README.md

* Update README.md

* Update README.md

* Discord link

* fix typing for cdk.lambda

* fix: update quickstart doc to address non monorepo (#32)

* docs: update quickstart doc to address non monorepo

* Update README.md

Co-authored-by: Mischa Spiegelmock <[email protected]>

* dont make static assets bucket public

* update docs

* fix: Forward all headers to SSR pages (#31)

* Forward all headers to SSR pages

* optional access customHeaders

* strip cookie headers from requests to the S3 origin

* update SST example with needed stageName

* Fix public asset access
Simplify OAI policy for S3, fix Host header for S3 in fallback origin

* update API doc

* fix: fix broken middleware (#33)

* fix: prevent crash if nextjs app does not contain public folder (#35)

* allow 0 max age

Signed-off-by: github-actions <[email protected]>
Co-authored-by: github-actions <[email protected]>
Co-authored-by: khuezy <[email protected]>
  • Loading branch information
3 people authored Nov 25, 2022
1 parent ed7494e commit b15a89a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
13 changes: 13 additions & 0 deletions API.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions src/Nextjs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { dirname } from 'path';
import { App, Duration, Fn, RemovalPolicy } from 'aws-cdk-lib';
import * as acm from 'aws-cdk-lib/aws-certificatemanager';
import * as cloudfront from 'aws-cdk-lib/aws-cloudfront';
import { ResponseHeadersPolicy } from 'aws-cdk-lib/aws-cloudfront';
import * as origins from 'aws-cdk-lib/aws-cloudfront-origins';
import { ServicePrincipal } from 'aws-cdk-lib/aws-iam';
import * as lambda from 'aws-cdk-lib/aws-lambda';
Expand Down Expand Up @@ -35,6 +36,11 @@ export interface NextjsCachePolicyProps {
readonly staticCachePolicy?: cloudfront.ICachePolicy;
readonly lambdaCachePolicy?: cloudfront.ICachePolicy;
readonly imageCachePolicy?: cloudfront.ICachePolicy;

/**
* Cache-control max-age default for static assets (/_next/*) in seconds.
*/
readonly staticClientMaxAgeDefault?: number;
}

/**
Expand Down Expand Up @@ -405,13 +411,32 @@ export class Nextjs extends Construct {
const lambdaCachePolicy = cdk?.cachePolicies?.lambdaCachePolicy ?? this.createCloudFrontLambdaCachePolicy();

// requests for static objects
const defaultStaticMaxAge = cdk?.cachePolicies?.staticClientMaxAgeDefault;
const staticResponseHeadersPolicy =
typeof defaultStaticMaxAge !== 'undefined'
? new ResponseHeadersPolicy(this, 'StaticResponseHeadersPolicy', {
// add default header for static assets
customHeadersBehavior: {
customHeaders: [
{
header: 'cache-control',
override: false,
// by default tell browser to cache static files for this long
// this is separate from the origin cache policy
value: `public, max-age=${defaultStaticMaxAge}, immutable`,
},
],
},
})
: undefined;
const staticBehavior: cloudfront.BehaviorOptions = {
viewerProtocolPolicy,
origin: s3Origin,
allowedMethods: cloudfront.AllowedMethods.ALLOW_GET_HEAD_OPTIONS,
cachedMethods: cloudfront.CachedMethods.CACHE_GET_HEAD_OPTIONS,
compress: true,
cachePolicy: staticCachePolicy,
responseHeadersPolicy: staticResponseHeadersPolicy,
};

// requests going to lambda (api, etc)
Expand Down

0 comments on commit b15a89a

Please sign in to comment.