Skip to content

Commit

Permalink
Ditch prerender.io
Browse files Browse the repository at this point in the history
  • Loading branch information
amits97 committed Aug 24, 2024
1 parent a175e69 commit 8783ac4
Show file tree
Hide file tree
Showing 13 changed files with 27,174 additions and 89 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "amitsn-blog-client",
"homepage": "https://www.amitsn.com/",
"version": "0.17.9",
"version": "0.18.1",
"private": true,
"dependencies": {
"@aws-sdk/shared-ini-file-loader": "^3.54.1",
Expand Down
46 changes: 29 additions & 17 deletions prerender-cloudfront.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
Parameters:
PrerenderToken:
Type: String
S3BucketName:
Type: String
Resources:
Expand Down Expand Up @@ -54,7 +52,7 @@ Resources:
- "logs:CreateLogGroup"
- "logs:CreateLogStream"
- "logs:PutLogEvents"
SetPrerenderHeader:
SetCustomHeaders:
Type: "AWS::Lambda::Function"
Properties:
Handler: "index.handler"
Expand All @@ -72,24 +70,23 @@ Resources:
const user_agent = headers['user-agent'];
const host = headers['host'];
if (user_agent && host) {
var prerender = /googlebot|bingbot|yandex|baiduspider|Facebot|facebookexternalhit|twitterbot|rogerbot|linkedinbot|embedly|quora link preview|showyoubot|outbrain|pinterest|slackbot|vkShare|W3C_Validator/i.test(user_agent[0].value);
var prerender = /googlebot|Mediapartners-Google|AdsBot-Google-Mobile|AdsBot-Google-Mobile|AdsBot-Google|Googlebot-Image|Googlebot-News|FeedFetcher-Google|Google-Read-Aloud|Google-Structured-Data-Testing-Tool|Google Page Speed Insights|Chrome-Lighthouse|bingbot|yandex|baiduspider|Facebot|facebookexternalhit|twitterbot|rogerbot|linkedinbot|embedly|quora link preview|showyoubot|outbrain|pinterest|slackbot|vkShare|W3C_Validator/i.test(user_agent[0].value);
prerender = prerender || /_escaped_fragment_/.test(request.querystring);
prerender = prerender && ! /\.(js|css|xml|less|png|jpg|jpeg|gif|pdf|doc|txt|ico|rss|zip|mp3|rar|exe|wmv|doc|avi|ppt|mpg|mpeg|tif|wav|mov|psd|ai|xls|mp4|m4a|swf|dat|dmg|iso|flv|m4v|torrent|ttf|woff|svg|eot)$/i.test(request.uri);
prerender = prerender && ! /\.(js|css|xml|less|png|jpg|jpeg|json|gif|pdf|doc|txt|ico|rss|zip|mp3|rar|exe|wmv|doc|avi|ppt|mpg|mpeg|tif|wav|mov|psd|ai|xls|mp4|m4a|swf|dat|dmg|iso|flv|m4v|torrent|ttf|woff|svg|eot)$/i.test(request.uri);
if (prerender) {
headers['x-prerender-token'] = [{ key: 'X-Prerender-Token', value: '${PrerenderToken}'}];
headers['x-prerender-host'] = [{ key: 'X-Prerender-Host', value: host[0].value}];
headers['x-prerender-cachebuster'] = [{ key: 'X-Prerender-Cachebuster', value: Date.now().toString()}];
}
}
callback(null, request);
};
Runtime: "nodejs18.x"
SetPrerenderHeaderVersion3:
SetCustomHeadersVersion4:
Type: "AWS::Lambda::Version"
Properties:
FunctionName:
Ref: "SetPrerenderHeader"
Description: "SetPrerenderHeader"
Ref: "SetCustomHeaders"
Description: "SetCustomHeaders"
RedirectToPrerender:
Type: "AWS::Lambda::Function"
Properties:
Expand All @@ -104,24 +101,25 @@ Resources:
/* change the version number below whenever this code is modified */
exports.handler = (event, context, callback) => {
const request = event.Records[0].cf.request;
if (request.headers['x-prerender-token'] && request.headers['x-prerender-host']) {
if (request.headers['x-prerender-host']) {
request.origin = {
custom: {
domainName: 'service.prerender.io',
domainName: 'prerender.amitsn.com',
port: 443,
protocol: 'https',
readTimeout: 20,
keepaliveTimeout: 5,
customHeaders: {},
sslProtocols: ['TLSv1', 'TLSv1.1'],
path: '/https%3A%2F%2F' + request.headers['x-prerender-host'][0].value
path: '/prerender?url=https%3A%2F%2F' + request.headers['x-prerender-host'][0].value
}
};
request.headers['host'] = [{ key: 'host', value: 'prerender.amitsn.com'}];
}
callback(null, request);
};
Runtime: "nodejs18.x"
RedirectToPrerenderVersion1:
RedirectToPrerenderVersion2:
Type: "AWS::Lambda::Version"
Properties:
FunctionName:
Expand All @@ -135,26 +133,34 @@ Resources:
Compress: true
# NOTE: we let cloudfront cache heavily the resurces of the SPA. Your deploy
# step will need to include an invalidation of the cloudfromt cache.
# The requests to prerender.io are NOT cached thanks to the X-Prerender-Cachebuster
# The requests to prerender.amitsn.com are NOT cached thanks to the X-Prerender-Cachebuster
# header.
MinTTL: 31536000
DefaultTTL: 31536000
ForwardedValues:
QueryString: false
QueryString: true
Headers:
- "X-Prerender-Token"
- "X-Prerender-Host"
- "X-Prerender-Cachebuster"
TargetOriginId: origin
ViewerProtocolPolicy: redirect-to-https
LambdaFunctionAssociations:
- EventType: viewer-request
LambdaFunctionARN:
!Join [
":",
[
!GetAtt [SetCustomHeaders, Arn],
!GetAtt [SetCustomHeadersVersion4, Version],
],
]
- EventType: origin-request
LambdaFunctionARN:
!Join [
":",
[
!GetAtt [RedirectToPrerender, Arn],
!GetAtt [RedirectToPrerenderVersion1, Version],
!GetAtt [RedirectToPrerenderVersion2, Version],
],
]
Enabled: true
Expand All @@ -165,6 +171,12 @@ Resources:
- ErrorCode: 501
ResponseCode: 404
ResponsePagePath: /index.html
Aliases:
- "www.amitsn.com"
ViewerCertificate:
AcmCertificateArn: "arn:aws:acm:us-east-1:608318364761:certificate/699d33b0-a601-4743-9c73-d481f6cf5e09"
MinimumProtocolVersion: "TLSv1.1_2016"
SslSupportMethod: "sni-only"
HttpVersion: http2
Origins:
- CustomOriginConfig:
Expand Down
108 changes: 108 additions & 0 deletions prerender/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
### Node ###
.serverless

# Reports
*.report.html
*.report.csv
*.report.json

# Dependency directories
/**/node_modules

# dotenv environment variables file(s)
.env
.env.*

# Build generated
dist/
public/
build/
.next

# Compressed archives
*.7zip
*.rar
*.zip

### VisualStudioCode ###
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json

### Vim ###
*.sw[a-p]

# Windows thumbnail cache files
Thumbs.db
ehthumbs.db
ehthumbs_vista.db

# Folder config file
Desktop.ini

# Recycle Bin used on file shares
$RECYCLE.BIN/

### macOS ###
*.DS_Store
.AppleDouble
.LSOverride

# Icon must end with two \r
Icon

# Thumbnails
._*

# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent

# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk

### Node ###
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity
7 changes: 7 additions & 0 deletions prerender/libs/dynamodb-lib.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import AWS from "aws-sdk";

export function call(action, params) {
const dynamoDb = new AWS.DynamoDB.DocumentClient();

return dynamoDb[action](params).promise();
}
33 changes: 33 additions & 0 deletions prerender/libs/response-lib.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
export function success(body) {
return buildResponse(200, body);
}

export function failure(body) {
return buildResponse(400, body);
}

export function redirect(headers) {
let location = headers.replace("Location: ", "");
return {
statusCode: 301,
headers: {
"Location": location
}
};
}

export function custom(status, body) {
return buildResponse(status, body);
}

function buildResponse(statusCode, body) {
return {
statusCode: statusCode,
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Credentials": true,
"Content-Type": "text/html"
},
body: body
};
}
5 changes: 5 additions & 0 deletions prerender/mocks/prerender-event.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"queryStringParameters": {
"url": "https://www.amitsn.com/blog/aws-cognito-set-up-both-password-and-passwordless-login"
}
}
Loading

0 comments on commit 8783ac4

Please sign in to comment.