Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/do 1484 enable cache for redirects #1069

Merged

Conversation

crispy101
Copy link
Contributor

@crispy101 crispy101 commented Sep 15, 2023

Description of the proposed changes

  • Let Prerender be able to cache 301 and 302 responses in the S3 bucket when explicitly enabled by enableRedirectCache flag

Screenshots (if applicable)

  • cached object meta-tags for this purpose
    image

Other solutions considered (if any)

  • N/A

Notes to PR author

⚠️ Please make sure the changes adhere to the guidelines mentioned here

Notes to reviewers
Incorporated the below existing plug-ins

🛈 When you've finished leaving feedback, please add a final comment to the PR tagging the author, letting them know that you have finished leaving feedback

Copy link
Contributor

@TheOrangePuff TheOrangePuff left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quite a few comments here 😅 Happy to chat anything through of you have any questions. Logic is good just mostly stylistic comments 🙂

server.use(s3Cache);

if (process.env.ENABLE_REDIRECT_CACHE.toLowerCase() === 'true'){
server.use(prerender.removeScriptTags());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is used in both conditions so it can be pulled out

server.use({
requestReceived: function(req, res, next) {
if(req.method !== 'GET' && req.method !== 'HEAD') {
console.log("skipping requestReceived from S3 Cache... ")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor point but I would remove this console log. It could be useful during local dev but in a prod system it wouldn't be very helpful (e.g. no logging of which request is being skipped).

}, function (err, result) {

if (!err && result) {
console.log(result.Metadata);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This console log is also questionable in terms of debugging usefulness, I'd probably remove it too

headerMatch = /<meta[^<>]*(?:name=['"]prerender-header['"][^<>]*content=['"]([^'"]*?): ?([^'"]*?)['"]|content=['"]([^'"]*?): ?([^'"]*?)['"][^<>]*name=['"]prerender-header['"])[^<>]*>/gi,
head = req.prerender.content.toString().split('</head>', 1).pop(),
// statusCode = 200,
match;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just for clarity I'd put a let on each line rather than using the comma syntax. I can be a bit hard to read especially with the long regexes 😅

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also if a variable isn't going to change you should use the const keyword rather than let

console.log("metaTagStatusCode: " + metaTagStatusCode);
}

while (match = headerMatch.exec(head)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this in a while loop while the one above is an if statement? 🤔


// 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.");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably not a useful log either. Also fun JS syntax fact, it's generally preferred to use string literals rather than +: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals

This would become:

console.log(`metaTagStatusCode ${metaTagStatusCode} is not in the cachable code list. Returning without caching.`);


// 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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the purpose of the metaTagStatusCode variable, why not just assign to req.prerender.statusCode directly?

// 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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be worth stating which object is being cached (url key maybe?)

httpreturncode: req.prerender.statusCode.toString()
}

if (location) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like this could be done earlier on, it feels a bit out of place referencing the location variable here. I reckon you could change this so location is scoped inside an existing if statement above rather than being scoped to the function

Copy link
Contributor

@TheOrangePuff TheOrangePuff left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good! One minor comment

// 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();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this slash should be here 🤔

Copy link
Contributor

@TheOrangePuff TheOrangePuff left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 🎉

@crispy101 crispy101 merged commit 3adbbbc into feature/DO-1285-upgrade-cdk Sep 22, 2023
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants