From a86b5a941d276770f5b5e6e6e6c1eaf335c5ad25 Mon Sep 17 00:00:00 2001 From: Chris Park Date: Wed, 13 Sep 2023 15:18:46 +0930 Subject: [PATCH 01/10] enable s3 caching for non-200 responses --- .../prerender-fargate/lib/prerender/server.js | 112 +++++++++++++++++- 1 file changed, 109 insertions(+), 3 deletions(-) diff --git a/packages/prerender-fargate/lib/prerender/server.js b/packages/prerender-fargate/lib/prerender/server.js index 8f82fe44..c744d794 100644 --- a/packages/prerender-fargate/lib/prerender/server.js +++ b/packages/prerender-fargate/lib/prerender/server.js @@ -36,8 +36,114 @@ server.use({ }); server.use(prerender.blacklist()); -server.use(prerender.httpHeaders()); -server.use(prerender.removeScriptTags()); -server.use(s3Cache); + +if (process.env.ENABLE_CACHE_FOR_REDIRECTION){ + server.use(prerender.removeScriptTags()); + var he = require('he'); + var s3 = new (require('aws-sdk')).S3({params:{Bucket: process.env.S3_BUCKET_NAME}}); + server.use({ + requestReceived: function(req, res, next) { + if(req.method !== 'GET' && req.method !== 'HEAD') { + console.log("skipping requestReceived from S3 Cache... ") + return next(); + } + + var key = req.prerender.url; + + if (process.env.S3_PREFIX_KEY) { + key = process.env.S3_PREFIX_KEY + '/' + key; + } + + s3.getObject({ + Key: key + }, function (err, result) { + + if (!err && result) { + console.log(result.Metadata); + console.log("Found cached object: " + key); + if (result.Metadata.location){ + res.setHeader('Location', result.Metadata.location); + } + return res.send(result.Metadata.httpreturncode, result.Body); + } else { + console.error(err); + } + + next(); + }); + }, + + pageLoaded: function(req, res, next) { + const statusCodesToCache = ['200', '301', '302']; + let metaTagStatusCode = 200; + let location = ''; + + if (req.prerender.content && req.prerender.renderType == 'html') { + let statusMatch = /]*(?:name=['"]prerender-status-code['"][^<>]*content=['"]([0-9]{3})['"]|content=['"]([0-9]{3})['"][^<>]*name=['"]prerender-status-code['"])[^<>]*>/i, + headerMatch = /]*(?:name=['"]prerender-header['"][^<>]*content=['"]([^'"]*?): ?([^'"]*?)['"]|content=['"]([^'"]*?): ?([^'"]*?)['"][^<>]*name=['"]prerender-header['"])[^<>]*>/gi, + head = req.prerender.content.toString().split('', 1).pop(), + // statusCode = 200, + match; + + if (match = statusMatch.exec(head)) { + metaTagStatusCode = match[1] || match[2]; + req.prerender.content = req.prerender.content.toString().replace(match[0], ''); + console.log("metaTagStatusCode: " + metaTagStatusCode); + } + + while (match = headerMatch.exec(head)) { + location = he.decode(match[2] || match[4]); + res.setHeader(match[1] || match[3], location); + req.prerender.content = req.prerender.content.toString().replace(match[0], ''); + } + + // Skip caching for the http response codes not in the list, such as 404 + if ( ! statusCodesToCache.includes(metaTagStatusCode.toString()) ) { + console.log("metaTagStatusCode " + metaTagStatusCode + " is not in the cachable code list. Returning without caching."); + return res.send(metaTagStatusCode, req.prerender.content); + } + } + + if(req.prerender.statusCode !== 200) { + return next(); + } + + // Override req.prerender.statusCode with the StatusCode returned via the meta tag. + // If metaTagStatusCode is not in the statusCodesToCache array or req.prerender.statusCode is not 200, then this line wouldn't be reached. Therefore no if condition for this overriding is needed. + req.prerender.statusCode = metaTagStatusCode; + console.log("Caching the object with statusCode " + req.prerender.statusCode); + + var key = req.prerender.url; + var s3Metadata = { + httpreturncode: req.prerender.statusCode.toString() + } + + if (location) { + s3Metadata.location = location; + } + + if (process.env.S3_PREFIX_KEY) { + key = process.env.S3_PREFIX_KEY + '/' + key; + } + + s3.putObject({ + Key: key, + ContentType: 'text/html;charset=UTF-8', + StorageClass: 'REDUCED_REDUNDANCY', + Body: req.prerender.content, + Metadata: s3Metadata + }, function(err, result) { + console.log(result); + if (err) console.error(err); + + next(); + }); + } + }); +} else { + server.use(prerender.httpHeaders()); + server.use(prerender.removeScriptTags()); + server.use(s3Cache); +} server.start(); From 670a8e1feefa86d646b6cc8ba2b5d4c04a0e5ae0 Mon Sep 17 00:00:00 2001 From: Chris Park Date: Fri, 15 Sep 2023 14:30:21 +0930 Subject: [PATCH 02/10] introduce flag for enabling redirect cache --- packages/prerender-fargate/README.md | 27 ++++++++++--------- .../lib/prerender-fargate.ts | 2 ++ .../prerender-fargate/lib/prerender/server.js | 2 +- 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/packages/prerender-fargate/README.md b/packages/prerender-fargate/README.md index f8a325f6..97c9cae6 100644 --- a/packages/prerender-fargate/README.md +++ b/packages/prerender-fargate/README.md @@ -1,15 +1,18 @@ # Prerender in Fargate -A construct to host [Prerender](https://github.com/prerender/prerender) in Fargate. + +A construct to host [Prerender](https://github.com/prerender/prerender) in Fargate. ## Props - - `prerenderName`: Name of the Prerender service - - `domainName`: Domain name for Prerender - - `vpcId`: VPC to host Prerender in - - `bucketName`: Optional S3 bucket name - - `expirationDays`: Optional days until items expire in bucket (default to 7 days) - - `tokenList`: List of tokens to accept as authentication - - `certificateArn`: Certificate arn to match the domain - - `desiredInstanceCount`: Number of Prerender instances to run (default 1) - - `maxInstanceCount`: Maximum number of Prerender instances to run (default 2) - - `instanceCPU`: CPU to allocate to each instance (default 512) - - `instanceMemory`: Amount of memory to allocate to each instance (default 1024) + +- `prerenderName`: Name of the Prerender service +- `domainName`: Domain name for Prerender +- `vpcId`: VPC to host Prerender in +- `bucketName`: Optional S3 bucket name +- `expirationDays`: Optional days until items expire in bucket (default to 7 days) +- `tokenList`: List of tokens to accept as authentication +- `certificateArn`: Certificate arn to match the domain +- `desiredInstanceCount`: Number of Prerender instances to run (default 1) +- `maxInstanceCount`: Maximum number of Prerender instances to run (default 2) +- `instanceCPU`: CPU to allocate to each instance (default 512) +- `instanceMemory`: Amount of memory to allocate to each instance (default 1024) +- `enableRedirectCache`: Cache 301 and 302 responses, too (default false) diff --git a/packages/prerender-fargate/lib/prerender-fargate.ts b/packages/prerender-fargate/lib/prerender-fargate.ts index b1d86a92..07e33b91 100644 --- a/packages/prerender-fargate/lib/prerender-fargate.ts +++ b/packages/prerender-fargate/lib/prerender-fargate.ts @@ -22,6 +22,7 @@ export interface PrerenderOptions { maxInstanceCount?: number, instanceCPU?: number, instanceMemory?: number + enableRedirectCache?: string } export class PrerenderFargate extends Construct { @@ -80,6 +81,7 @@ export class PrerenderFargate extends Construct { AWS_ACCESS_KEY_ID: accessKey.accessKeyId, AWS_SECRET_ACCESS_KEY: accessKey.secretAccessKey.toString(), AWS_REGION: Stack.of(this).region, + ENABLE_REDIRECT_CACHE: props.enableRedirectCache || "false", TOKEN_LIST: props.tokenList.toString() } }, diff --git a/packages/prerender-fargate/lib/prerender/server.js b/packages/prerender-fargate/lib/prerender/server.js index c744d794..215ad2cc 100644 --- a/packages/prerender-fargate/lib/prerender/server.js +++ b/packages/prerender-fargate/lib/prerender/server.js @@ -37,7 +37,7 @@ server.use({ server.use(prerender.blacklist()); -if (process.env.ENABLE_CACHE_FOR_REDIRECTION){ +if (process.env.ENABLE_REDIRECT_CACHE.toLowerCase() === 'true'){ server.use(prerender.removeScriptTags()); var he = require('he'); var s3 = new (require('aws-sdk')).S3({params:{Bucket: process.env.S3_BUCKET_NAME}}); From b6cd3b2929bad73db12e09451e31419d5a9edf42 Mon Sep 17 00:00:00 2001 From: Chris Park Date: Fri, 15 Sep 2023 14:48:15 +0930 Subject: [PATCH 03/10] remove extra whitespaces --- .../prerender-fargate/lib/prerender/server.js | 34 +++++++++---------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/packages/prerender-fargate/lib/prerender/server.js b/packages/prerender-fargate/lib/prerender/server.js index 215ad2cc..d88a044b 100644 --- a/packages/prerender-fargate/lib/prerender/server.js +++ b/packages/prerender-fargate/lib/prerender/server.js @@ -47,17 +47,17 @@ if (process.env.ENABLE_REDIRECT_CACHE.toLowerCase() === 'true'){ console.log("skipping requestReceived from S3 Cache... ") return next(); } - + var key = req.prerender.url; - + if (process.env.S3_PREFIX_KEY) { key = process.env.S3_PREFIX_KEY + '/' + key; } - + s3.getObject({ Key: key }, function (err, result) { - + if (!err && result) { console.log(result.Metadata); console.log("Found cached object: " + key); @@ -68,64 +68,64 @@ if (process.env.ENABLE_REDIRECT_CACHE.toLowerCase() === 'true'){ } else { console.error(err); } - + next(); }); }, - + pageLoaded: function(req, res, next) { const statusCodesToCache = ['200', '301', '302']; let metaTagStatusCode = 200; let location = ''; - + if (req.prerender.content && req.prerender.renderType == 'html') { let statusMatch = /]*(?:name=['"]prerender-status-code['"][^<>]*content=['"]([0-9]{3})['"]|content=['"]([0-9]{3})['"][^<>]*name=['"]prerender-status-code['"])[^<>]*>/i, headerMatch = /]*(?:name=['"]prerender-header['"][^<>]*content=['"]([^'"]*?): ?([^'"]*?)['"]|content=['"]([^'"]*?): ?([^'"]*?)['"][^<>]*name=['"]prerender-header['"])[^<>]*>/gi, head = req.prerender.content.toString().split('', 1).pop(), // statusCode = 200, match; - + if (match = statusMatch.exec(head)) { metaTagStatusCode = match[1] || match[2]; req.prerender.content = req.prerender.content.toString().replace(match[0], ''); console.log("metaTagStatusCode: " + metaTagStatusCode); } - + while (match = headerMatch.exec(head)) { location = he.decode(match[2] || match[4]); res.setHeader(match[1] || match[3], location); req.prerender.content = req.prerender.content.toString().replace(match[0], ''); } - + // Skip caching for the http response codes not in the list, such as 404 if ( ! statusCodesToCache.includes(metaTagStatusCode.toString()) ) { console.log("metaTagStatusCode " + metaTagStatusCode + " is not in the cachable code list. Returning without caching."); return res.send(metaTagStatusCode, req.prerender.content); } } - + if(req.prerender.statusCode !== 200) { return next(); } - + // Override req.prerender.statusCode with the StatusCode returned via the meta tag. // If metaTagStatusCode is not in the statusCodesToCache array or req.prerender.statusCode is not 200, then this line wouldn't be reached. Therefore no if condition for this overriding is needed. req.prerender.statusCode = metaTagStatusCode; console.log("Caching the object with statusCode " + req.prerender.statusCode); - + var key = req.prerender.url; var s3Metadata = { httpreturncode: req.prerender.statusCode.toString() } - + if (location) { s3Metadata.location = location; } - + if (process.env.S3_PREFIX_KEY) { key = process.env.S3_PREFIX_KEY + '/' + key; } - + s3.putObject({ Key: key, ContentType: 'text/html;charset=UTF-8', @@ -135,7 +135,7 @@ if (process.env.ENABLE_REDIRECT_CACHE.toLowerCase() === 'true'){ }, function(err, result) { console.log(result); if (err) console.error(err); - + next(); }); } From 7c7b9c7cf5aa20d84220add9e267a0cbc374963a Mon Sep 17 00:00:00 2001 From: Chris Park Date: Mon, 18 Sep 2023 14:43:04 +0930 Subject: [PATCH 04/10] DO-1484: re-order removeScriptTags middleware to ensure it is called after httpHeader is run --- packages/prerender-fargate/lib/prerender/server.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/prerender-fargate/lib/prerender/server.js b/packages/prerender-fargate/lib/prerender/server.js index d88a044b..6a087640 100644 --- a/packages/prerender-fargate/lib/prerender/server.js +++ b/packages/prerender-fargate/lib/prerender/server.js @@ -38,7 +38,6 @@ server.use({ server.use(prerender.blacklist()); if (process.env.ENABLE_REDIRECT_CACHE.toLowerCase() === 'true'){ - server.use(prerender.removeScriptTags()); var he = require('he'); var s3 = new (require('aws-sdk')).S3({params:{Bucket: process.env.S3_BUCKET_NAME}}); server.use({ @@ -138,8 +137,9 @@ if (process.env.ENABLE_REDIRECT_CACHE.toLowerCase() === 'true'){ next(); }); - } - }); + } + }); + server.use(prerender.removeScriptTags()); } else { server.use(prerender.httpHeaders()); server.use(prerender.removeScriptTags()); From 1cf99dbfe8c90d863ed71bffb035334829b18c0d Mon Sep 17 00:00:00 2001 From: Chris Park Date: Mon, 18 Sep 2023 14:44:44 +0930 Subject: [PATCH 05/10] DO-1484: make the code more readable --- .../prerender-fargate/lib/prerender/server.js | 40 +++++++++---------- 1 file changed, 18 insertions(+), 22 deletions(-) diff --git a/packages/prerender-fargate/lib/prerender/server.js b/packages/prerender-fargate/lib/prerender/server.js index 6a087640..bd4a7b03 100644 --- a/packages/prerender-fargate/lib/prerender/server.js +++ b/packages/prerender-fargate/lib/prerender/server.js @@ -71,29 +71,32 @@ if (process.env.ENABLE_REDIRECT_CACHE.toLowerCase() === 'true'){ next(); }); }, + // The pageLoaded function is a modified version of https://github.com/prerender/prerender/blob/478fa6d0a5196ea29c88c69e64e72eb5507b6d2c/lib/plugins/httpHeaders.js pageLoaded: function(req, res, next) { const statusCodesToCache = ['200', '301', '302']; let metaTagStatusCode = 200; - let location = ''; + var s3Metadata = { + httpreturncode: req.prerender.statusCode.toString() + } if (req.prerender.content && req.prerender.renderType == 'html') { - let statusMatch = /]*(?:name=['"]prerender-status-code['"][^<>]*content=['"]([0-9]{3})['"]|content=['"]([0-9]{3})['"][^<>]*name=['"]prerender-status-code['"])[^<>]*>/i, - headerMatch = /]*(?:name=['"]prerender-header['"][^<>]*content=['"]([^'"]*?): ?([^'"]*?)['"]|content=['"]([^'"]*?): ?([^'"]*?)['"][^<>]*name=['"]prerender-header['"])[^<>]*>/gi, - head = req.prerender.content.toString().split('', 1).pop(), - // statusCode = 200, - match; - - if (match = statusMatch.exec(head)) { - metaTagStatusCode = match[1] || match[2]; - req.prerender.content = req.prerender.content.toString().replace(match[0], ''); - console.log("metaTagStatusCode: " + metaTagStatusCode); + const statusMatchRegex = /]*(?:name=['"]prerender-status-code['"][^<>]*content=['"]([0-9]{3})['"]|content=['"]([0-9]{3})['"][^<>]*name=['"]prerender-status-code['"])[^<>]*>/i; + const headerMatchRegex = /]*(?:name=['"]prerender-header['"][^<>]*content=['"]([^'"]*?): ?([^'"]*?)['"]|content=['"]([^'"]*?): ?([^'"]*?)['"][^<>]*name=['"]prerender-header['"])[^<>]*>/gi + const head = req.prerender.content.toString().split('', 1).pop() + + const statusMatch = statusMatchRegex.exec(head) + if (statusMatch) { + metaTagStatusCode = statusMatch[1] || statusMatch[2]; + req.prerender.content = req.prerender.content.toString().replace(statusMatch[0], ''); } - while (match = headerMatch.exec(head)) { - location = he.decode(match[2] || match[4]); - res.setHeader(match[1] || match[3], location); - req.prerender.content = req.prerender.content.toString().replace(match[0], ''); + let headerMatch = headerMatchRegex.exec(head) + while (headerMatch) { + s3Metadata.location = he.decode(headerMatch[2] || headerMatch[4]); + res.setHeader(headerMatch[1] || headerMatch[3], s3Metadata.location); + req.prerender.content = req.prerender.content.toString().replace(headerMatch[0], ''); + headerMatch = headerMatchRegex.exec(head) } // Skip caching for the http response codes not in the list, such as 404 @@ -113,13 +116,6 @@ if (process.env.ENABLE_REDIRECT_CACHE.toLowerCase() === 'true'){ console.log("Caching the object with statusCode " + req.prerender.statusCode); var key = req.prerender.url; - var s3Metadata = { - httpreturncode: req.prerender.statusCode.toString() - } - - if (location) { - s3Metadata.location = location; - } if (process.env.S3_PREFIX_KEY) { key = process.env.S3_PREFIX_KEY + '/' + key; From b57031b8438ca8fa961c3765940df88fde1e221d Mon Sep 17 00:00:00 2001 From: Chris Park Date: Mon, 18 Sep 2023 14:45:38 +0930 Subject: [PATCH 06/10] DO-1484: make log more useful --- packages/prerender-fargate/lib/prerender/server.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/prerender-fargate/lib/prerender/server.js b/packages/prerender-fargate/lib/prerender/server.js index bd4a7b03..af571bbc 100644 --- a/packages/prerender-fargate/lib/prerender/server.js +++ b/packages/prerender-fargate/lib/prerender/server.js @@ -101,7 +101,7 @@ if (process.env.ENABLE_REDIRECT_CACHE.toLowerCase() === 'true'){ // Skip caching for the http response codes not in the list, such as 404 if ( ! statusCodesToCache.includes(metaTagStatusCode.toString()) ) { - console.log("metaTagStatusCode " + metaTagStatusCode + " is not in the cachable code list. Returning without caching."); + console.log(`metaTagStatusCode ${metaTagStatusCode} for ${req.prerender.url} is not in the cachable code list. Returning without caching the result.`); return res.send(metaTagStatusCode, req.prerender.content); } } @@ -113,8 +113,7 @@ if (process.env.ENABLE_REDIRECT_CACHE.toLowerCase() === 'true'){ // Override req.prerender.statusCode with the StatusCode returned via the meta tag. // If metaTagStatusCode is not in the statusCodesToCache array or req.prerender.statusCode is not 200, then this line wouldn't be reached. Therefore no if condition for this overriding is needed. req.prerender.statusCode = metaTagStatusCode; - console.log("Caching the object with statusCode " + req.prerender.statusCode); - + console.log(`Caching the object ${req.prerender.url} with statusCode ${metaTagStatusCode}`); var key = req.prerender.url; if (process.env.S3_PREFIX_KEY) { From 2c2093cad18efc8cfc68eb09d8fba98f8e0e70d1 Mon Sep 17 00:00:00 2001 From: Chris Park Date: Mon, 18 Sep 2023 14:46:22 +0930 Subject: [PATCH 07/10] DO-1484: clean the code --- packages/prerender-fargate/lib/prerender/server.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/packages/prerender-fargate/lib/prerender/server.js b/packages/prerender-fargate/lib/prerender/server.js index af571bbc..28db7618 100644 --- a/packages/prerender-fargate/lib/prerender/server.js +++ b/packages/prerender-fargate/lib/prerender/server.js @@ -41,7 +41,7 @@ if (process.env.ENABLE_REDIRECT_CACHE.toLowerCase() === 'true'){ var he = require('he'); var s3 = new (require('aws-sdk')).S3({params:{Bucket: process.env.S3_BUCKET_NAME}}); server.use({ - requestReceived: function(req, res, next) { + requestReceived: function(req, res, next) { if(req.method !== 'GET' && req.method !== 'HEAD') { console.log("skipping requestReceived from S3 Cache... ") return next(); @@ -54,11 +54,10 @@ if (process.env.ENABLE_REDIRECT_CACHE.toLowerCase() === 'true'){ } s3.getObject({ - Key: key + Key: key }, function (err, result) { if (!err && result) { - console.log(result.Metadata); console.log("Found cached object: " + key); if (result.Metadata.location){ res.setHeader('Location', result.Metadata.location); @@ -72,7 +71,6 @@ if (process.env.ENABLE_REDIRECT_CACHE.toLowerCase() === 'true'){ }); }, // The pageLoaded function is a modified version of https://github.com/prerender/prerender/blob/478fa6d0a5196ea29c88c69e64e72eb5507b6d2c/lib/plugins/httpHeaders.js - pageLoaded: function(req, res, next) { const statusCodesToCache = ['200', '301', '302']; let metaTagStatusCode = 200; From 257c3c9d4b81a93eca1290f8465d5f7774d5f981 Mon Sep 17 00:00:00 2001 From: Chris Park Date: Tue, 19 Sep 2023 11:46:46 +0930 Subject: [PATCH 08/10] refactor code - remove unnecessary variables and fix indentation --- .../prerender-fargate/lib/prerender/server.js | 102 +++++++++--------- 1 file changed, 49 insertions(+), 53 deletions(-) diff --git a/packages/prerender-fargate/lib/prerender/server.js b/packages/prerender-fargate/lib/prerender/server.js index 28db7618..49d95b2f 100644 --- a/packages/prerender-fargate/lib/prerender/server.js +++ b/packages/prerender-fargate/lib/prerender/server.js @@ -41,6 +41,9 @@ if (process.env.ENABLE_REDIRECT_CACHE.toLowerCase() === 'true'){ var he = require('he'); var s3 = new (require('aws-sdk')).S3({params:{Bucket: process.env.S3_BUCKET_NAME}}); server.use({ + // The requestReceived and pageLoaded functions are a modified version of + // httpHeader plugin - https://github.com/prerender/prerender/blob/478fa6d0a5196ea29c88c69e64e72eb5507b6d2c/lib/plugins/httpHeaders.js combined with + // s3cache plugin - https://github.com/prerender/prerender-aws-s3-cache/blob/98707fa0f787de83aa41583682cd2c2d330a9cca/index.js requestReceived: function(req, res, next) { if(req.method !== 'GET' && req.method !== 'HEAD') { console.log("skipping requestReceived from S3 Cache... ") @@ -62,7 +65,8 @@ if (process.env.ENABLE_REDIRECT_CACHE.toLowerCase() === 'true'){ if (result.Metadata.location){ res.setHeader('Location', result.Metadata.location); } - return res.send(result.Metadata.httpreturncode, result.Body); + // default 200 for legacy objects that do not have Metadata.httpreturncode defined + return res.send(result.Metadata.httpreturncode || 200, result.Body); } else { console.error(err); } @@ -70,69 +74,61 @@ if (process.env.ENABLE_REDIRECT_CACHE.toLowerCase() === 'true'){ next(); }); }, - // The pageLoaded function is a modified version of https://github.com/prerender/prerender/blob/478fa6d0a5196ea29c88c69e64e72eb5507b6d2c/lib/plugins/httpHeaders.js - pageLoaded: function(req, res, next) { - const statusCodesToCache = ['200', '301', '302']; - let metaTagStatusCode = 200; - var s3Metadata = { - httpreturncode: req.prerender.statusCode.toString() - } - - if (req.prerender.content && req.prerender.renderType == 'html') { - const statusMatchRegex = /]*(?:name=['"]prerender-status-code['"][^<>]*content=['"]([0-9]{3})['"]|content=['"]([0-9]{3})['"][^<>]*name=['"]prerender-status-code['"])[^<>]*>/i; - const headerMatchRegex = /]*(?:name=['"]prerender-header['"][^<>]*content=['"]([^'"]*?): ?([^'"]*?)['"]|content=['"]([^'"]*?): ?([^'"]*?)['"][^<>]*name=['"]prerender-header['"])[^<>]*>/gi - const head = req.prerender.content.toString().split('', 1).pop() - const statusMatch = statusMatchRegex.exec(head) - if (statusMatch) { - metaTagStatusCode = statusMatch[1] || statusMatch[2]; - req.prerender.content = req.prerender.content.toString().replace(statusMatch[0], ''); - } + pageLoaded: function(req, res, next) { + const statusCodesToCache = ['200', '301', '302']; + var s3Metadata = {} - let headerMatch = headerMatchRegex.exec(head) - while (headerMatch) { - s3Metadata.location = he.decode(headerMatch[2] || headerMatch[4]); - res.setHeader(headerMatch[1] || headerMatch[3], s3Metadata.location); - req.prerender.content = req.prerender.content.toString().replace(headerMatch[0], ''); - headerMatch = headerMatchRegex.exec(head) - } + // Inspect prerender meta tags and update response accordingly + if (req.prerender.content && req.prerender.renderType == 'html') { + const statusMatchRegex = /]*(?:name=['"]prerender-status-code['"][^<>]*content=['"]([0-9]{3})['"]|content=['"]([0-9]{3})['"][^<>]*name=['"]prerender-status-code['"])[^<>]*>/i; + const headerMatchRegex = /]*(?:name=['"]prerender-header['"][^<>]*content=['"]([^'"]*?): ?([^'"]*?)['"]|content=['"]([^'"]*?): ?([^'"]*?)['"][^<>]*name=['"]prerender-header['"])[^<>]*>/gi + const head = req.prerender.content.toString().split('', 1).pop() - // Skip caching for the http response codes not in the list, such as 404 - if ( ! statusCodesToCache.includes(metaTagStatusCode.toString()) ) { - console.log(`metaTagStatusCode ${metaTagStatusCode} for ${req.prerender.url} is not in the cachable code list. Returning without caching the result.`); - return res.send(metaTagStatusCode, req.prerender.content); - } + const statusMatch = statusMatchRegex.exec(head) + if (statusMatch) { + req.prerender.statusCode = statusMatch[1] || statusMatch[2]; + req.prerender.content = req.prerender.content.toString().replace(statusMatch[0], ''); } - if(req.prerender.statusCode !== 200) { - return next(); + let headerMatch = headerMatchRegex.exec(head) + while (headerMatch) { + s3Metadata.location = he.decode(headerMatch[2] || headerMatch[4]); + res.setHeader(headerMatch[1] || headerMatch[3], s3Metadata.location); + req.prerender.content = req.prerender.content.toString().replace(headerMatch[0], ''); + headerMatch = headerMatchRegex.exec(head) } - // Override req.prerender.statusCode with the StatusCode returned via the meta tag. - // If metaTagStatusCode is not in the statusCodesToCache array or req.prerender.statusCode is not 200, then this line wouldn't be reached. Therefore no if condition for this overriding is needed. - req.prerender.statusCode = metaTagStatusCode; - console.log(`Caching the object ${req.prerender.url} with statusCode ${metaTagStatusCode}`); - var key = req.prerender.url; - - if (process.env.S3_PREFIX_KEY) { - key = process.env.S3_PREFIX_KEY + '/' + key; + // Skip caching for the http response codes not in the list, such as 404 + if ( ! statusCodesToCache.includes(req.prerender.statusCode.toString()) ) { + console.log(`StatusCode ${req.prerender.statusCode} for ${req.prerender.url} is not in the cachable code list. Returning without caching the result.`); + return res.send(req.prerender.statusCode, req.prerender.content); } + } + s3Metadata.httpreturncode = req.prerender.statusCode.toString() - s3.putObject({ - Key: key, - ContentType: 'text/html;charset=UTF-8', - StorageClass: 'REDUCED_REDUNDANCY', - Body: req.prerender.content, - Metadata: s3Metadata - }, function(err, result) { - console.log(result); - if (err) console.error(err); + console.log(`Caching the object ${req.prerender.url} with statusCode ${req.prerender.statusCode}`); + var key = req.prerender.url; - next(); - }); + if (process.env.S3_PREFIX_KEY) { + key = process.env.S3_PREFIX_KEY + '/' + key; } - }); - server.use(prerender.removeScriptTags()); + + s3.putObject({ + Key: key, + ContentType: 'text/html;charset=UTF-8', + StorageClass: 'REDUCED_REDUNDANCY', + Body: req.prerender.content, + Metadata: s3Metadata + }, function(err, result) { + console.log(result); + if (err) console.error(err); + + next(); + }); + } + }); + server.use(prerender.removeScriptTags()); } else { server.use(prerender.httpHeaders()); server.use(prerender.removeScriptTags()); From 994fa044157219c4920d289e2a7466dc87283205 Mon Sep 17 00:00:00 2001 From: Chris Park Date: Tue, 19 Sep 2023 11:51:44 +0930 Subject: [PATCH 09/10] remove unnecessary logging --- packages/prerender-fargate/lib/prerender/server.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/prerender-fargate/lib/prerender/server.js b/packages/prerender-fargate/lib/prerender/server.js index 49d95b2f..4abdbfdb 100644 --- a/packages/prerender-fargate/lib/prerender/server.js +++ b/packages/prerender-fargate/lib/prerender/server.js @@ -46,8 +46,7 @@ if (process.env.ENABLE_REDIRECT_CACHE.toLowerCase() === 'true'){ // s3cache plugin - https://github.com/prerender/prerender-aws-s3-cache/blob/98707fa0f787de83aa41583682cd2c2d330a9cca/index.js requestReceived: function(req, res, next) { if(req.method !== 'GET' && req.method !== 'HEAD') { - console.log("skipping requestReceived from S3 Cache... ") - return next(); +\ return next(); } var key = req.prerender.url; From c2008a339fbbd549a927a34d6370894669a29edc Mon Sep 17 00:00:00 2001 From: Chris Park Date: Thu, 21 Sep 2023 08:29:07 +0930 Subject: [PATCH 10/10] fix typo --- packages/prerender-fargate/lib/prerender/server.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/prerender-fargate/lib/prerender/server.js b/packages/prerender-fargate/lib/prerender/server.js index 4abdbfdb..e248cc76 100644 --- a/packages/prerender-fargate/lib/prerender/server.js +++ b/packages/prerender-fargate/lib/prerender/server.js @@ -46,7 +46,7 @@ if (process.env.ENABLE_REDIRECT_CACHE.toLowerCase() === 'true'){ // s3cache plugin - https://github.com/prerender/prerender-aws-s3-cache/blob/98707fa0f787de83aa41583682cd2c2d330a9cca/index.js requestReceived: function(req, res, next) { if(req.method !== 'GET' && req.method !== 'HEAD') { -\ return next(); + return next(); } var key = req.prerender.url;