Skip to content

Commit 5472015

Browse files
committed
Add whitelistQueryParams option
1 parent 3ae6c3c commit 5472015

File tree

4 files changed

+48
-27
lines changed

4 files changed

+48
-27
lines changed

handler.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,26 @@ const resetPrerenderCloud = () => {
4242
// that is, you treat /docs/ differently than /docs (trailing slash) which is rare
4343
// prerendercloud.set("removeTrailingSlash", true);
4444

45-
// 5. botsOnly
45+
// 5. whitelistQueryParams (recommended)
46+
// improves cache hit rate by dropping query params not in the whitelist
47+
// must be a function that returns null or array
48+
// * default (null) preserves all query params
49+
// * empty array drops all query params
50+
// prerendercloud.set("whitelistQueryParams", req => ["page"]);
51+
52+
// 6. botsOnly
4653
// generally not recommended due to potential google SEO cloaking penalties no one fully understands
4754
// prerendercloud.set("botsOnly", true);
4855

49-
// 6. removeScriptsTag (not recommended)
56+
// 7. removeScriptsTag (not recommended)
5057
// Removes all scripts/JS, useful if:
5158
// - trying to get under 1MB Lambda@Edge limit
5259
// - having problems with your JS app taking over from the pre-rendered content
5360
// Huge caveat: this also means your app will no longer be a "single-page app" since
5461
// all of the JavaScript will be gone
5562
// prerendercloud.set("removeScriptTags", true);
5663

57-
// 7. disableServerCache
64+
// 8. disableServerCache
5865
// Disable the cache on prerender.cloud (default is enabled with 5 minute duration).
5966
// It probably makes sense to disable the prerender.cloud server cache
6067
// since CloudFront is caching things for you.
@@ -67,7 +74,7 @@ const resetPrerenderCloud = () => {
6774
// things down unnecessarily)
6875
// prerendercloud.set('disableServerCache', true);
6976

70-
// 8. see all configuration options here: https://github.com/sanfrancesco/prerendercloud-nodejs
77+
// 9. see all configuration options here: https://github.com/sanfrancesco/prerendercloud-nodejs
7178

7279
// for tests
7380
if (prerenderCloudOption) prerenderCloudOption(prerendercloud);

lib/ViewerRequestInterface.js

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ module.exports = class ViewerRequestInterface {
2323
createReq() {
2424
const req = {
2525
method: this.cloudFrontRequest.method,
26-
originalUrl: this.cloudFrontRequest.uri,
26+
originalUrl: this.originalUrl,
2727
url: this.originalUrl,
2828
headers: {
2929
host: util.getHeader(this.cloudFrontRequest, "host"),
@@ -42,27 +42,41 @@ module.exports = class ViewerRequestInterface {
4242
setHeader: (key, val) => {
4343
this.headers[key] = val;
4444
},
45-
end: body => {
46-
// since the user-agent header will be overwritten with CloudFront
47-
// we use this to hint at the real one, but:
48-
// 1. it will not affect the cache-key
49-
// 2. prerender.cloud will only see it once (after that, the req will be cached in CloudFront)
50-
// 3. we don't need this for anything other than the potential for user stats/analytics in prerender.cloud
51-
// (i.e. the user can see the user-agent of the request that triggered the first CloudFront request)
52-
this.cloudFrontRequest.headers[util.USER_AGENT_PLACEHOLDER] = [
53-
{
54-
key: util.USER_AGENT_PLACEHOLDER,
55-
value: util.getHeader(this.cloudFrontRequest, "user-agent")
56-
}
57-
];
45+
writeHead(_status, _headers) {}
46+
};
5847

59-
this.cloudFrontRequest.uri = util.createUri(this.originalUrl, true);
48+
res.end = body => {
49+
// since the user-agent header will be overwritten with CloudFront
50+
// we use this to hint at the real one, but:
51+
// 1. it will not affect the cache-key
52+
// 2. prerender.cloud will only see it once (after that, the req will be cached in CloudFront)
53+
// 3. we don't need this for anything other than the potential for user stats/analytics in prerender.cloud
54+
// (i.e. the user can see the user-agent of the request that triggered the first CloudFront request)
55+
this.cloudFrontRequest.headers[util.USER_AGENT_PLACEHOLDER] = [
56+
{
57+
key: util.USER_AGENT_PLACEHOLDER,
58+
value: util.getHeader(this.cloudFrontRequest, "user-agent")
59+
}
60+
];
6061

61-
console.log({ shouldPrerender: true, uri: this.originalUrl });
62+
const origCloudFrontUri = this.cloudFrontRequest.uri;
6263

63-
this.callback(null, this.cloudFrontRequest);
64-
},
65-
writeHead(_status, _headers) {}
64+
// res.prerender.url.requestedPath is set by https://github.com/sanfrancesco/prerendercloud-nodejs
65+
// specifically for this Lambda lib - it's the requested path after applying the whitelistQueryParams
66+
this.cloudFrontRequest.uri = util.createUri(
67+
res.prerender.url.requestedPath,
68+
true
69+
);
70+
71+
console.log({
72+
shouldPrerender: true,
73+
cloudFrontUriAfterEncode: this.cloudFrontRequest.uri,
74+
requestedUriAfterWhitelist: res.prerender.url.requestedPath,
75+
originalCloudFrontUri: origCloudFrontUri,
76+
originalCloudFrontQuerystring: this.cloudFrontRequest.querystring
77+
});
78+
79+
this.callback(null, this.cloudFrontRequest);
6680
};
6781

6882
res.headers = {};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
"serverless": "1.24.1"
77
},
88
"dependencies": {
9-
"prerendercloud": "1.27.0"
9+
"prerendercloud": "1.30.1"
1010
}
1111
}

yarn.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1300,9 +1300,9 @@ prepend-http@^2.0.0:
13001300
version "2.0.0"
13011301
resolved "https://registry.yarnpkg.com/prepend-http/-/prepend-http-2.0.0.tgz#e92434bfa5ea8c19f41cdfd401d741a3c819d897"
13021302

1303-
prerendercloud@1.27.0:
1304-
version "1.27.0"
1305-
resolved "https://registry.yarnpkg.com/prerendercloud/-/prerendercloud-1.27.0.tgz#992c64a1420aeb20c02b2c19dd740d64a5ad8cff"
1303+
prerendercloud@1.30.1:
1304+
version "1.30.1"
1305+
resolved "https://registry.yarnpkg.com/prerendercloud/-/prerendercloud-1.30.1.tgz#2e960a9b3865a588623f49007833be704aa9187d"
13061306
dependencies:
13071307
debug "^2.2.0"
13081308
got-lite "^8.0.1"

0 commit comments

Comments
 (0)