Skip to content

Commit

Permalink
DO-1394: introduce optional ENABLE_PRERENDER_HEADER flag to handle X-…
Browse files Browse the repository at this point in the history
…Prerender header in the headless chrome request
  • Loading branch information
Chris Park committed Jan 24, 2024
1 parent 22f0ea5 commit 88d1321
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
6 changes: 6 additions & 0 deletions packages/prerender-fargate/lib/prerender-fargate-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ export interface PrerenderFargateOptions {
* @default - true
*/
enableRecache?: boolean;
/**
* Let Headless Chrome send "x-prerender" header in the request.
* Ensure CORS allows the header.
* @default - true
*/
enablePrerenderHeader?: boolean;
}

/**
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 @@ -89,6 +89,7 @@ export class PrerenderFargate extends Construct {
prerenderFargateScalingOptions,
prerenderFargateRecachingOptions,
enableRecache,
enablePrerenderHeader,
} = props;

// Create bucket for prerender storage
Expand Down Expand Up @@ -133,6 +134,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_PRERENDER_HEADER: enablePrerenderHeader?.toString() || "true",
};

const secrets = {
Expand Down
10 changes: 7 additions & 3 deletions packages/prerender-fargate/lib/prerender/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,13 @@ server.use({

server.use(prerender.blacklist());

// Let headless chrome send 'X-Prerender: 1' in the request for any specicial handling such as disabling geo-redirection.
// Ensure that the "access-control-allow-headers" header of any backend systems allows "x-prerender" if CORS is configured.
server.use(prerender.sendPrerenderHeader());
if (process.env.ENABLE_PRERENDER_HEADER.toLowerCase() === 'true'){

// Let headless chrome send 'X-Prerender: 1' in the request for any specicial handling such as disabling geo-redirection.
// Ensure that the "access-control-allow-headers" header of any backend systems allows "x-prerender" if CORS is configured.

server.use(prerender.sendPrerenderHeader());
}

if (process.env.ENABLE_REDIRECT_CACHE.toLowerCase() === 'true'){
var he = require('he');
Expand Down

0 comments on commit 88d1321

Please sign in to comment.