Skip to content

Commit 2014f2c

Browse files
committed
Fix rewrites to /index.html for non-root paths after origin 4xx/5xx
reverts a change made in e929eca which itself was a revert in a long chain of bugs (see git blame)
1 parent e03cdc6 commit 2014f2c

File tree

5 files changed

+58
-41
lines changed

5 files changed

+58
-41
lines changed

handler.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ module.exports.originRequest = (event, context, callback) => {
161161
console.log("originRequest", JSON.stringify(cloudFrontRequest));
162162

163163
const { req, res, next, shouldPrerender } = OriginRequestInterface.create(
164+
cachedOptions,
164165
cloudFrontRequest,
165166
callback
166167
);

lib/OriginRequestInterface.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
const util = require("./util");
22

33
module.exports = class OriginRequestInterface {
4-
static create(cloudFrontRequest, callback) {
4+
static create(cachedOptions, cloudFrontRequest, callback) {
55
const ori = new this(cloudFrontRequest, callback);
66
const { req, shouldPrerender } = ori.createReq();
7-
const next = ori.createNext();
7+
const next = ori.createNext(req, cachedOptions);
88
const res = ori.createRes(next);
99

1010
return { req, res, next, shouldPrerender };
@@ -97,16 +97,22 @@ module.exports = class OriginRequestInterface {
9797

9898
return res;
9999
}
100-
createNext() {
100+
createNext(req, cachedOptions) {
101101
// this flow will be called for prerender.cloud LAST_RESORT_TIMEOUT
102102
// aka "prerendercloud middleware SKIPPED: server error: Request timed out"
103103
// it will merely fall back to non-prerendered content
104104
return () => {
105105
delete this.cloudFrontRequest.headers[util.USER_AGENT_PLACEHOLDER];
106106

107-
if (this.originalUri === "/") {
107+
if (util.shouldRewriteToIndexHtml(req, cachedOptions, this.originalUri)) {
108+
console.log("OriginRequestInterface.next", {
109+
rewriteToIndexHtml: true
110+
});
108111
this.cloudFrontRequest.uri = "/index.html";
109112
} else {
113+
console.log("OriginRequestInterface.next", {
114+
rewriteToIndexHtml: false
115+
});
110116
this.cloudFrontRequest.uri = this.originalUri;
111117
}
112118

lib/ViewerRequestInterface.js

Lines changed: 11 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,5 @@
11
const util = require("./util");
22

3-
// this function exists in the npm lib: prerendercloud
4-
// but must also exist here since we use it in our 404->/index.html
5-
// functionality when the file has no extension or .html extension
6-
const pathIsBlacklisted = (blacklistedPaths, cloudfrontUri) => {
7-
const paths = blacklistedPaths;
8-
9-
if (paths && Array.isArray(paths)) {
10-
return paths.some(path => {
11-
if (path === cloudfrontUri) return true;
12-
13-
if (path.endsWith("*")) {
14-
const starIndex = path.indexOf("*");
15-
const pathSlice = path.slice(0, starIndex);
16-
17-
if (cloudfrontUri.startsWith(pathSlice)) return true;
18-
}
19-
20-
return false;
21-
});
22-
}
23-
24-
return false;
25-
};
26-
273
module.exports = class ViewerRequestInterface {
284
static create(cachedOptions, cloudFrontRequest, callback) {
295
const vri = new this(cloudFrontRequest, callback);
@@ -109,18 +85,20 @@ module.exports = class ViewerRequestInterface {
10985
createNext(req, cachedOptions) {
11086
return () => {
11187
if (
112-
util.isHtml(this.cloudFrontRequest.uri) &&
113-
(!cachedOptions.blacklistPaths ||
114-
(cachedOptions.blacklistPaths &&
115-
!pathIsBlacklisted(
116-
cachedOptions.blacklistPaths(req),
117-
this.cloudFrontRequest.uri
118-
)))
88+
util.shouldRewriteToIndexHtml(
89+
req,
90+
cachedOptions,
91+
this.cloudFrontRequest.uri
92+
)
11993
) {
120-
console.log({ rewriteToIndexHtml: true });
94+
console.log("ViewerRequestInterface.next", {
95+
rewriteToIndexHtml: true
96+
});
12197
this.cloudFrontRequest.uri = "/index.html";
12298
} else {
123-
console.log({ rewriteToIndexHtml: false });
99+
console.log("ViewerRequestInterface.next", {
100+
rewriteToIndexHtml: false
101+
});
124102
}
125103

126104
// the URI will not include query string when not pre-rendering

lib/util.js

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,46 @@ const parseUriField = uri => {
1818

1919
const isHtml = require("prerendercloud").util.urlPathIsHtml;
2020

21+
// this function exists in the npm lib: prerendercloud
22+
// but must also exist here since we use it in our 404->/index.html
23+
// functionality when the file has no extension or .html extension
24+
const pathIsBlacklisted = (blacklistedPaths, cloudfrontUri) => {
25+
const paths = blacklistedPaths;
26+
27+
if (paths && Array.isArray(paths)) {
28+
return paths.some(path => {
29+
if (path === cloudfrontUri) return true;
30+
31+
if (path.endsWith("*")) {
32+
const starIndex = path.indexOf("*");
33+
const pathSlice = path.slice(0, starIndex);
34+
35+
if (cloudfrontUri.startsWith(pathSlice)) return true;
36+
}
37+
38+
return false;
39+
});
40+
}
41+
42+
return false;
43+
};
44+
45+
const shouldRewriteToIndexHtml = (req, cachedOptions, uri) => {
46+
return (
47+
isHtml(uri) &&
48+
(!cachedOptions.blacklistPaths ||
49+
(cachedOptions.blacklistPaths &&
50+
!pathIsBlacklisted(cachedOptions.blacklistPaths(req), uri)))
51+
);
52+
};
53+
2154
module.exports = {
2255
USER_AGENT_PLACEHOLDER,
2356
toBase64,
2457
fromBase64,
2558
createUri,
2659
getHeader,
2760
parseUriField,
28-
isHtml
61+
isHtml,
62+
shouldRewriteToIndexHtml
2963
};

spec/originRequestSpec.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,9 +214,7 @@ describe("originRequest", function() {
214214
withInputs("prerendercloud", "/some/path", true);
215215
runHandlerWithOriginRequestEvent();
216216

217-
// N.B. /some/path does NOT get rewritten to index.html in origin-request
218-
// because it should have already been rewritten in viewer-request
219-
itReturnsOriginalCloudFrontRequestWithNormalPath("/some/path");
217+
itReturnsOriginalCloudFrontRequestWithNormalPath("/index.html");
220218
});
221219

222220
describe("when a file with an extension", function() {

0 commit comments

Comments
 (0)