Skip to content

Commit 351f6fb

Browse files
committed
improved redirect-on-new-page handling: if page open event results in a redirect, create an implicit redirect record instead of re-fetching in browser
async fetch: allow adding synthetic redirect response instead of trying in browser, if all that's needed is the redirect record itself should fix #137
1 parent ce5611b commit 351f6fb

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

src/recorder.js

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,7 @@ class Recorder {
626626

627627
handleWindowOpen(url, sessions) {
628628
const headers = {"Referer": this.pageInfo.url};
629-
this.doAsyncFetch({url, headers}, sessions);
629+
this.doAsyncFetch({url, headers, redirectOnly: true}, sessions);
630630
}
631631

632632
isPagePDF() {
@@ -1217,6 +1217,23 @@ class Recorder {
12171217
}
12181218
}
12191219

1220+
async attemptFetchRedirect(request, resp) {
1221+
if (request.redirectOnly && resp.type === "opaqueredirect") {
1222+
const abort = new AbortController();
1223+
resp = await fetch(request.url, {abort});
1224+
abort.abort();
1225+
1226+
if (resp.redirected) {
1227+
console.warn(`Adding synthetic redirect ${request.url} -> ${resp.url}`);
1228+
return Response.redirect(resp.url, 302);
1229+
}
1230+
}
1231+
1232+
console.warn(`async fetch error ${resp.status}, opaque due to redirect, retrying in browser`);
1233+
await this.doAsyncFetchInBrowser(request, request.sessions, true);
1234+
return null;
1235+
}
1236+
12201237
async doAsyncFetchInBrowser(request, sessions) {
12211238
this._fetchUrls.add(request.url);
12221239

@@ -1283,9 +1300,10 @@ class Recorder {
12831300

12841301
let resp = await fetch(request.url, opts);
12851302
if (resp.status === 0) {
1286-
console.warn(`async fetch error ${resp.status}, opaque due to redirect, retrying in browser`);
1287-
await this.doAsyncFetchInBrowser(request, request.sessions, true);
1288-
return;
1303+
resp = await this.attemptFetchRedirect(request, resp);
1304+
if (!resp) {
1305+
return;
1306+
}
12891307
} else if (resp.status >= 400) {
12901308
console.warn(`async fetch error ${resp.status}, retrying without headers`);
12911309
resp = await fetch(request.url, this.defaultFetchOpts);

0 commit comments

Comments
 (0)