Skip to content

Commit cb13afd

Browse files
committed
Host is no longer mandatory
It may not have ever been necessary, I think I just forgot to pass it over the encoded URL field like I had been for url and shouldPrerender (because the origin request does not get the host val from the viewer request)
1 parent 1ed7f9c commit cb13afd

File tree

7 files changed

+20
-29
lines changed

7 files changed

+20
-29
lines changed

handler.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,10 @@ const resetPrerenderCloud = () => {
3737
// example use case: if your origin is http only
3838
// prerendercloud.set("protocol", "http");
3939

40-
// 3. host (mandatory)
41-
// Set this to your custom domain (something like example.com)
42-
// or if you don't have one, then the CloudFront distribution URL (something like d1pxreml448ujs.cloudfront.net)
43-
// This is what service.prerender.cloud will prerender, and if we didn't
44-
// set it, the only info we'd have access to during Lambda@Edge runtime is the host of the origin (S3)
45-
// which would require additional configuration to make it publicly accessible (and it just makes things more confusing).
40+
// 3. host (optional, will infer from host header if not set here)
41+
// If having issues, try setting this to your custom domain (something like example.com)
42+
// or if you don't have one, then the CloudFront distribution URL (something like d1pxreml448ujs.cloudfront.net).
43+
// Note, setting this config option shouldn't be necessary
4644
// example value: example.com or d1pxreml448ujs.cloudfront.net (don't include the protocol)
4745
// prerendercloud.set("host", "");
4846

lib/OriginRequestInterface.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,21 @@ module.exports = class OriginRequestInterface {
1717

1818
createReq() {
1919
console.log("about to parse URI", this.cloudFrontRequest.uri);
20-
const { uri, shouldPrerender } = util.parseUriField(
20+
const { uri, shouldPrerender, host } = util.parseUriField(
2121
this.cloudFrontRequest.uri
2222
);
2323

2424
this.originalUri = uri;
2525

26-
console.log("parsed URI", { uri, shouldPrerender });
26+
console.log("parsed URI", { uri, shouldPrerender, host });
2727

2828
const req = {
2929
connection: { encrypted: true },
3030
method: this.cloudFrontRequest.method,
3131
originalUrl: uri,
3232
url: uri,
3333
headers: {
34-
host: util.getHeader(this.cloudFrontRequest, "host"),
34+
host: host,
3535
"user-agent": util.getHeader(
3636
this.cloudFrontRequest,
3737
util.USER_AGENT_PLACEHOLDER

lib/ViewerRequestInterface.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ module.exports = class ViewerRequestInterface {
6565
// specifically for this Lambda lib - it's the requested path after applying the whitelistQueryParams
6666
this.cloudFrontRequest.uri = util.createUri(
6767
res.prerender.url.requestedPath,
68-
true
68+
true,
69+
util.getHeader(this.cloudFrontRequest, "host")
6970
);
7071

7172
console.log({

lib/util.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ const getHeader = (cloudFrontRequest, name) =>
88

99
const toBase64 = str => Buffer.from(str).toString("base64");
1010
const fromBase64 = str => Buffer.from(str, "base64").toString("utf8");
11-
const createUri = (uri, shouldPrerender) =>
12-
"/" + toBase64(JSON.stringify({ uri, shouldPrerender }));
11+
const createUri = (uri, shouldPrerender, host) =>
12+
"/" + toBase64(JSON.stringify({ uri, shouldPrerender, host }));
1313

1414
const parseUriField = uri => {
1515
// uri has leading slash

spec/originRequestSpec.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ describe("originRequest", function() {
7171
it("returns original CloudFront request with normal path", function() {
7272
expect(this.cb).toHaveBeenCalledWith(null, {
7373
headers: {
74-
host: [{ value: "d123.cf.net", key: "Host" }],
74+
host: [{ value: "origin-request-gets-s3-origin.s3.amazonaws.com", key: "Host" }],
7575
"user-agent": [{ value: "CloudFront", key: "User-Agent" }]
7676
},
7777
clientIp: "2001:cdba::3257:9652",
@@ -104,7 +104,7 @@ describe("originRequest", function() {
104104
headers: {
105105
host: [
106106
{
107-
value: "d123.cf.net",
107+
value: "origin-request-gets-s3-origin.s3.amazonaws.com",
108108
key: "Host"
109109
}
110110
],
@@ -130,7 +130,8 @@ describe("originRequest", function() {
130130
beforeEach(function() {
131131
this.event.Records[0].cf.request.uri = util.createUri(
132132
uri,
133-
shouldPrerender
133+
shouldPrerender,
134+
"d123.cf.net"
134135
);
135136
this.event.Records[0].cf.request.headers[
136137
"prerendercloud-lambda-edge-original-user-agent"

spec/viewerRequestSpec.js

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

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

6-
const createUriShouldPrerender = (uri, querystring) =>
7-
util.createUri(uri + (querystring ? `?${querystring}` : ""), true);
8-
const createUriShouldNotPrerender = uri => util.createUri(uri, false);
6+
const createUriShouldPrerender = (uri, querystring, host = "d123.cf.net") =>
7+
util.createUri(uri + (querystring ? `?${querystring}` : ""), true, host);
8+
const createUriShouldNotPrerender = (uri) =>
9+
util.createUri(uri, false);
910

1011
describe("viewerRequest", function() {
1112
beforeEach(function() {
@@ -202,10 +203,7 @@ describe("viewerRequest", function() {
202203
prerendercloud.set("blacklistPaths", req => ["/signin/*"])
203204
);
204205
});
205-
withUserAgentAndUri(
206-
"prerendercloud random-suffix",
207-
"/signin/oauth"
208-
);
206+
withUserAgentAndUri("prerendercloud random-suffix", "/signin/oauth");
209207
runHandlerWithViewerRequestEvent();
210208

211209
itDoesNotPrerender("prerendercloud random-suffix", "/signin/oauth");

validate.js

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,8 @@ monkeyPatchPrerenderCloud(prerendercloud, options);
1212
const handler = require("./handler");
1313
handler.resetPrerenderCloud();
1414

15-
if (!options["host"]) {
16-
throw new Error(
17-
"host was not set, edit handler.js and set host to your CloudFront distribution URL or aliased domain"
18-
);
19-
}
20-
2115
if (!options["prerenderToken"]) {
2216
console.log(
2317
"warning, prerenderToken was not set, requests will be rate limited"
2418
);
2519
}
26-

0 commit comments

Comments
 (0)