-
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.
DO-1541: upgrade prerender proxy construct
- Loading branch information
Showing
14 changed files
with
630 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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 |
---|---|---|
@@ -0,0 +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). | ||
|
||
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) | ||
`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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { | ||
PrerenderLambda, | ||
PrerenderLambdaProps, | ||
} from "./lib/prerender-lambda-construct"; | ||
import { | ||
PrerenderFunction, | ||
PrerenderFunctionOptions, | ||
} from "./lib/prerender-construct"; | ||
import { PrerenderCheckFunction } from "./lib/prerender-check-construct"; | ||
import { | ||
ErrorResponseFunction, | ||
ErrorResponseFunctionOptions, | ||
} from "./lib/error-response-construct"; | ||
import { | ||
CloudFrontCacheControl, | ||
CloudFrontCacheControlOptions, | ||
} from "./lib/prerender-cf-cache-control-construct"; | ||
|
||
export { | ||
PrerenderLambda, | ||
PrerenderFunction, | ||
PrerenderCheckFunction, | ||
ErrorResponseFunction, | ||
CloudFrontCacheControl, | ||
CloudFrontCacheControlOptions, | ||
ErrorResponseFunctionOptions, | ||
PrerenderFunctionOptions, | ||
PrerenderLambdaProps, | ||
}; |
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,54 @@ | ||
import { AssetHashType, DockerImage } from "aws-cdk-lib"; | ||
import { EdgeFunction } from "aws-cdk-lib/aws-cloudfront/lib/experimental"; | ||
import { Code, IVersion, Runtime, Version } from "aws-cdk-lib/aws-lambda"; | ||
import { Construct } from "constructs"; | ||
import { join } from "path"; | ||
import { Esbuild } from "@aligent/esbuild"; | ||
|
||
export interface ErrorResponseFunctionOptions { | ||
pathPrefix?: string; | ||
} | ||
|
||
export class ErrorResponseFunction extends Construct { | ||
readonly edgeFunction: EdgeFunction; | ||
|
||
constructor( | ||
scope: Construct, | ||
id: string, | ||
options: ErrorResponseFunctionOptions | ||
) { | ||
super(scope, id); | ||
|
||
const command = [ | ||
"sh", | ||
"-c", | ||
'echo "Docker build not supported. Please install esbuild."', | ||
]; | ||
|
||
this.edgeFunction = new EdgeFunction(this, `${id}-error-response-fn`, { | ||
code: Code.fromAsset(join(__dirname, "handlers"), { | ||
assetHashType: AssetHashType.OUTPUT, | ||
bundling: { | ||
command, | ||
image: DockerImage.fromRegistry("busybox"), | ||
local: new Esbuild({ | ||
entryPoints: [join(__dirname, "handlers/error-response.ts")], | ||
define: { | ||
"process.env.PATH_PREFIX": options.pathPrefix ?? "", | ||
}, | ||
}), | ||
}, | ||
}), | ||
runtime: Runtime.NODEJS_18_X, | ||
handler: "error-response.handler", | ||
}); | ||
} | ||
|
||
public getFunctionVersion(): IVersion { | ||
return Version.fromVersionArn( | ||
this, | ||
"error-response-fn-version", | ||
this.edgeFunction.currentVersion.edgeArn | ||
); | ||
} | ||
} |
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,20 @@ | ||
import "source-map-support/register"; | ||
import { CloudFrontResponseEvent, CloudFrontResponse } from "aws-lambda"; | ||
|
||
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"; | ||
const 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import "source-map-support/register"; | ||
import { | ||
CloudFrontRequest, | ||
CloudFrontResponseEvent, | ||
CloudFrontResponse, | ||
} from "aws-lambda"; | ||
import axios from "axios"; | ||
import * as https from "https"; | ||
|
||
const FRONTEND_HOST = process.env.FRONTEND_HOST; | ||
const PATH_PREFIX = process.env.PATH_PREFIX; | ||
|
||
// Create axios client outside of lambda function for re-use between calls | ||
const instance = axios.create({ | ||
timeout: 1000, | ||
// Don't follow redirects | ||
maxRedirects: 0, | ||
// Only valid response codes are 200 | ||
validateStatus: function (status) { | ||
return status == 200; | ||
}, | ||
// keep connection alive so we don't constantly do SSL negotiation | ||
httpsAgent: new https.Agent({ keepAlive: true }), | ||
}); | ||
|
||
export const handler = ( | ||
event: CloudFrontResponseEvent | ||
): Promise<CloudFrontResponse | CloudFrontRequest> => { | ||
const response = event.Records[0].cf.response; | ||
const request = event.Records[0].cf.request; | ||
|
||
if ( | ||
response.status != "200" && | ||
!request.headers["x-request-prerender"] && | ||
request.uri != `${PATH_PREFIX}/index.html` | ||
) { | ||
// Fetch default page and return body | ||
return instance | ||
.get(`https://${FRONTEND_HOST}${PATH_PREFIX}/index.html`) | ||
.then(res => { | ||
response.body = res.data; | ||
|
||
response.headers["content-type"] = [ | ||
{ | ||
key: "Content-Type", | ||
value: "text/html", | ||
}, | ||
]; | ||
|
||
// Remove content-length if set as this may be the value from the origin. | ||
delete response.headers["content-length"]; | ||
|
||
return response; | ||
}) | ||
.catch(err => { | ||
return response; | ||
}); | ||
} | ||
|
||
return Promise.resolve(response); | ||
}; |
Oops, something went wrong.