Skip to content

Commit

Permalink
Merge pull request #1392 from aligent/feature/DO-1683-cache-404s
Browse files Browse the repository at this point in the history
feat: cache 404s
  • Loading branch information
TheOrangePuff authored Jul 11, 2024
2 parents 107edda + 26f388f commit 234c0bf
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
5 changes: 5 additions & 0 deletions packages/prerender-fargate/lib/prerender-fargate-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ export interface PrerenderFargateOptions {
* @default - false
*/
enableRedirectCache?: boolean;
/**
* Whether to enable caching of HTTP 404s.
* @default - false
*/
enableNotFoundCache?: boolean;
/**
* Whether to enable the S3 endpoint for the VPC.
* @default - false
Expand Down
2 changes: 2 additions & 0 deletions packages/prerender-fargate/lib/prerender-fargate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ export class PrerenderFargate extends Construct {
expirationDays,
enableS3Endpoint,
enableRedirectCache,
enableNotFoundCache,
desiredInstanceCount,
bucketName,
domainName,
Expand Down Expand Up @@ -142,6 +143,7 @@ export class PrerenderFargate extends Construct {
S3_BUCKET_NAME: this.bucket.bucketName,
AWS_REGION: Stack.of(this).region,
ENABLE_REDIRECT_CACHE: enableRedirectCache?.toString() || "false",
ENABLE_NOTFOUND_CACHE: enableNotFoundCache?.toString() || "false",
ENABLE_PRERENDER_HEADER: enablePrerenderHeader?.toString() || "true",
};

Expand Down
11 changes: 10 additions & 1 deletion packages/prerender-fargate/lib/prerender/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,16 @@ server.use(prerender.removeScriptTags());

server.use({
pageLoaded: function(req, res, next) {
const statusCodesToCache = process.env.ENABLE_REDIRECT_CACHE.toLowerCase() === 'true' ? ['200', '301', '302', '308'] : ['200'];
const statusCodesToCache = ['200'];

if (process.env.ENABLE_REDIRECT_CACHE.toLowerCase() === 'true') {
statusCodesToCache.push('301', '302', '308');
}

if (process.env.ENABLE_NOTFOUND_CACHE.toLowerCase() === 'true') {
statusCodesToCache.push('404');
}

var s3Metadata = {}
const cacheObject = function (err, result) {
if (!err && result) {
Expand Down

0 comments on commit 234c0bf

Please sign in to comment.