-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #382 from aligent/feature/prerender_cf_cache_control
Prerender CloudFront cache control
- Loading branch information
Showing
8 changed files
with
622 additions
and
4,608 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,26 @@ | ||
# Prerender Proxy | ||
This library provides two function constructs and a construct that creates two Lambda@Edge functions to use prerender.io as a Cloudfront Origin for site indexers (Google, Bing, etc). | ||
|
||
This library provides two function constructs and a construct that creates two Lambda@Edge functions to use prerender.io as a Cloudfront Origin for site indexers (Google, Bing, etc). | ||
|
||
The `prerender-check` is a `viewer-request` function that will check if a requester is from a indexer and if it is adds a header so that the second function `prerender` (`origin-request`) will alter the origin to prerender. | ||
|
||
The `prerender` will function also make a HEAD request to a nominated backend to detect 301 and 302 redirects and if so forward them on to the frontend. This ensures that your SEO rankings are not penalized by having multiple pages at the same URL. | ||
|
||
These functions are intended to be added to an existing Cloudfront | ||
|
||
## Cache Control | ||
|
||
The intention of `cache-control` function is to avoid/control CloudFront Caches for `prerender` bot. | ||
This function to be associated with CloudFront's `origin response` | ||
|
||
## Props | ||
`redirectBackendOrigin`: The backend origin to make the HEAD request to. | ||
`redirectFrontendHost`: This hostname is used to replace the backend host for any redirects that contain the backend host. | ||
`prerenderToken`: Your prerender.io authentication token | ||
`prerenderUrl`: The URL of your Prerender service (optional: defaults to prerender.io) | ||
`pathPrefix`: A prefix path (optional) | ||
|
||
`redirectBackendOrigin`: The backend origin to make the HEAD request to | ||
`redirectFrontendHost`: This hostname is used to replace the backend host for any redirects that contain the backend host | ||
`prerenderToken`: Your prerender.io authentication token | ||
`prerenderUrl`: The URL of your Prerender service (optional: defaults to prerender.io) | ||
`pathPrefix`: A prefix path (optional) | ||
`cacheControlProps.cacheKey`: An optional parameter, which is to set `PRERENDER_CACHE_KEY` to be used in *[prerender CloudFront cache control function]* | ||
`cacheControlProps.maxAge`: An optional parameter, which is to set `PRERENDER_CACHE_MAX_AGE` to be used in *[prerender CloudFront cache control function]* | ||
|
||
[prerender CloudFront cache control function]:lib/handlers/cache-control.ts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import 'source-map-support/register'; | ||
import { CloudFrontResponseEvent, CloudFrontResponse } from 'aws-lambda'; | ||
/* | ||
Prerender cache control function | ||
Consider associate this function as *origin response* function | ||
*/ | ||
export const handler = async (event: CloudFrontResponseEvent): Promise<CloudFrontResponse> => { | ||
const cacheKey = process.env.PRERENDER_CACHE_KEY || 'x-prerender-requestid'; | ||
const cacheMaxAge = process.env.PRERENDER_CACHE_MAX_AGE || '0'; | ||
let response = event.Records[0].cf.response; | ||
|
||
if (response.headers[`${cacheKey}`]) { | ||
response.headers['Cache-Control'] = [ | ||
{ | ||
key: 'Cache-Control', | ||
value: `max-age=${cacheMaxAge}` | ||
} | ||
] | ||
} | ||
return response; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
packages/prerender-proxy/lib/prerender-cf-cache-control-construct.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { Construct } from "@aws-cdk/core"; | ||
import { Bundling } from '@aws-cdk/aws-lambda-nodejs/lib/bundling'; | ||
import { Runtime } from '@aws-cdk/aws-lambda'; | ||
import { experimental } from '@aws-cdk/aws-cloudfront'; | ||
import { EdgeFunction } from "@aws-cdk/aws-cloudfront/lib/experimental"; | ||
|
||
export interface CloudFrontCacheControlOptions { | ||
cacheKey?: string, | ||
maxAge?: Number, | ||
} | ||
|
||
export class CloudFrontCacheControl extends Construct { | ||
readonly edgeFunction: EdgeFunction; | ||
|
||
constructor(scope: Construct, id: string, options?: CloudFrontCacheControlOptions) { | ||
super(scope, id); | ||
|
||
this.edgeFunction = new experimental.EdgeFunction( | ||
this, | ||
'PrerenderCloudFrontCacheControl', | ||
{ | ||
code: Bundling.bundle({ | ||
entry: `${__dirname}/handlers/cache-control.ts`, | ||
runtime: Runtime.NODEJS_14_X, | ||
sourceMap: true, | ||
projectRoot: `${__dirname}/handlers/`, | ||
depsLockFilePath: `${__dirname}/handlers/package-lock.json`, | ||
// Define options replace values at build time so we can use environment variables to test locally | ||
// and replace during build/deploy with static values. This gets around the lambda@edge limitation | ||
// of no environment variables at runtime. | ||
define: { | ||
'process.env.PRERENDER_CACHE_KEY': JSON.stringify(options?.cacheKey ?? 'x-prerender-requestid'), | ||
'process.env.PRERENDER_CACHE_MAX_AGE': JSON.stringify(String(options?.maxAge) ?? '0'), | ||
} | ||
}), | ||
runtime: Runtime.NODEJS_14_X, | ||
handler: 'index.handler', | ||
} | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters