Skip to content

Commit 3ae6c3c

Browse files
committed
Add support for query strings
1 parent a3f9fde commit 3ae6c3c

File tree

3 files changed

+46
-17
lines changed

3 files changed

+46
-17
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,6 @@ You can also sign into AWS and go to CloudFormation and manually remove things.
134134
* We're waiting for the Lambda@Edge to add a feature to address this
135135
3. Redirects (301/302 status codes)
136136
* if you use `<meta name="prerender-status-code" content="301">` to initiate a redirect, your CloudFront TTL must be zero, otherwise CloudFront will cache the body/response and return status code 200 with the body from the redirected path
137-
4. No support for query strings yet
138137

139138
## Troubleshooting
140139

lib/ViewerRequestInterface.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ module.exports = class ViewerRequestInterface {
1212
}
1313
constructor(cloudFrontRequest, callback) {
1414
this.cloudFrontRequest = cloudFrontRequest;
15+
const querystring = this.cloudFrontRequest.querystring
16+
? `?${this.cloudFrontRequest.querystring}`
17+
: "";
18+
this.originalUrl = this.cloudFrontRequest.uri + querystring;
1519
this.callback = callback;
1620
this.headers = {};
1721
}
@@ -20,7 +24,7 @@ module.exports = class ViewerRequestInterface {
2024
const req = {
2125
method: this.cloudFrontRequest.method,
2226
originalUrl: this.cloudFrontRequest.uri,
23-
url: this.cloudFrontRequest.uri,
27+
url: this.originalUrl,
2428
headers: {
2529
host: util.getHeader(this.cloudFrontRequest, "host"),
2630
"user-agent": util.getHeader(this.cloudFrontRequest, "user-agent")
@@ -52,12 +56,9 @@ module.exports = class ViewerRequestInterface {
5256
}
5357
];
5458

55-
this.cloudFrontRequest.uri = util.createUri(
56-
this.cloudFrontRequest.uri,
57-
true
58-
);
59+
this.cloudFrontRequest.uri = util.createUri(this.originalUrl, true);
5960

60-
console.log({shouldPrerender: true, uri: this.cloudFrontRequest.uri});
61+
console.log({ shouldPrerender: true, uri: this.originalUrl });
6162

6263
this.callback(null, this.cloudFrontRequest);
6364
},
@@ -70,18 +71,20 @@ module.exports = class ViewerRequestInterface {
7071
createNext() {
7172
return () => {
7273
if (util.isHtml(this.cloudFrontRequest.uri)) {
73-
console.log({rewriteToIndexHtml: true});
74+
console.log({ rewriteToIndexHtml: true });
7475
this.cloudFrontRequest.uri = "/index.html";
7576
} else {
76-
console.log({rewriteToIndexHtml: false});
77+
console.log({ rewriteToIndexHtml: false });
7778
}
7879

80+
// the URI will not include query string when not pre-rendering
81+
// (because if not pre-rendering, we don't want to mutate the URI field)
7982
this.cloudFrontRequest.uri = util.createUri(
8083
this.cloudFrontRequest.uri,
8184
false
8285
);
8386

84-
console.log({shouldPrerender: false, uri: this.cloudFrontRequest.uri});
87+
console.log({ shouldPrerender: false, uri: this.cloudFrontRequest.uri });
8588
this.callback(null, this.cloudFrontRequest);
8689
};
8790
}

spec/viewerRequestSpec.js

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ const nock = require("nock");
33

44
const util = require("../lib/util");
55

6-
const createUriShouldPrerender = uri => util.createUri(uri, true);
6+
const createUriShouldPrerender = (uri, querystring) =>
7+
util.createUri(uri + (querystring ? `?${querystring}` : ""), true);
78
const createUriShouldNotPrerender = uri => util.createUri(uri, false);
89

910
describe("viewerRequest", function() {
@@ -20,16 +21,19 @@ describe("viewerRequest", function() {
2021
});
2122
}
2223

23-
function withUserAgentAndUri(userAgent, uri) {
24+
function withUserAgentAndUri(userAgent, uri, querystring) {
2425
beforeEach(function() {
2526
this.event.Records[0].cf.request.uri = uri;
2627
this.event.Records[0].cf.request.headers[
2728
"user-agent"
2829
][0].value = userAgent;
30+
if (querystring) {
31+
this.event.Records[0].cf.request.querystring = querystring;
32+
}
2933
});
3034
}
3135

32-
function itPrerenders(userAgent, uri) {
36+
function itPrerenders(userAgent, uri, querystring) {
3337
it("modifies request object with base64 encoded JSON string that has path and user-agent", function() {
3438
expect(this.cb).toHaveBeenCalledWith(null, {
3539
headers: {
@@ -40,21 +44,23 @@ describe("viewerRequest", function() {
4044
]
4145
},
4246
clientIp: "2001:cdba::3257:9652",
43-
uri: createUriShouldPrerender(uri),
47+
uri: createUriShouldPrerender(uri, querystring),
48+
querystring: querystring || "",
4449
method: "GET"
4550
});
4651
});
4752
}
4853

49-
function itDoesNotPrerender(userAgent, uri) {
54+
function itDoesNotPrerender(userAgent, uri, querystring) {
5055
it("modifies request object with base64 encoded JSON string that has path and user-agent", function() {
5156
expect(this.cb).toHaveBeenCalledWith(null, {
5257
headers: {
5358
host: [{ value: "d123.cf.net", key: "Host" }],
5459
"user-agent": [{ value: userAgent, key: "User-Agent" }]
5560
},
5661
clientIp: "2001:cdba::3257:9652",
57-
uri: createUriShouldNotPrerender(uri),
62+
uri: createUriShouldNotPrerender(uri), // the URI will not include query string when not pre-rendering
63+
querystring: querystring || "",
5864
method: "GET"
5965
});
6066
});
@@ -83,7 +89,8 @@ describe("viewerRequest", function() {
8389
},
8490
clientIp: "2001:cdba::3257:9652",
8591
uri: "/index.html",
86-
method: "GET"
92+
method: "GET",
93+
querystring: ""
8794
}
8895
}
8996
}
@@ -113,6 +120,12 @@ describe("viewerRequest", function() {
113120

114121
itPrerenders("curl", "/index/");
115122
});
123+
describe("with query string", function() {
124+
withUserAgentAndUri("curl", "/index.html", "a=b&c=d");
125+
runHandlerWithViewerRequestEvent();
126+
127+
itPrerenders("curl", "/index.html", "a=b&c=d");
128+
});
116129
});
117130
describe("non html files", function() {
118131
withUserAgentAndUri("curl", "/app.js");
@@ -143,6 +156,20 @@ describe("viewerRequest", function() {
143156

144157
itDoesNotPrerender("prerendercloud random-suffix", "/index.html");
145158
});
159+
describe("with query string", function() {
160+
withUserAgentAndUri(
161+
"prerendercloud random-suffix",
162+
"/index/",
163+
"a=b&c=d"
164+
);
165+
runHandlerWithViewerRequestEvent();
166+
167+
itDoesNotPrerender(
168+
"prerendercloud random-suffix",
169+
"/index.html",
170+
"a=b&c=d"
171+
);
172+
});
146173
});
147174

148175
// even though shouldPrerender is false, the uri is not HTML so it preserves uri for cache-key

0 commit comments

Comments
 (0)