Skip to content

Commit 3b70866

Browse files
authored
Include url in turbo:before-fetch-request event (#289)
* Avoid race between visit tests TurboDriveTestCase maintains a eventLogChannel that acts as a cursor for the page's window.eventLogs array. Before every test the eventLogChannel is drained by reading all events and its internal index (cursor) is updated accordingly. The race was happening when we changed the page location and not blocking on it: the eventLogChannel was draining the previous page bumping its internal index. After the page was reload that index was out of sync since the new page had an new, empty eventLogs array. Order of events * async location change * draining eventLogChannel on the previous page * actual location change * eventLogChannel is out-of-sync This is was fixed as part of #289 * Include url in `turbo:before-fetch-request` event
1 parent 9dfca8f commit 3b70866

2 files changed

Lines changed: 9 additions & 1 deletion

File tree

src/http/fetch_request.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ export class FetchRequest {
132132

133133
private async allowRequestToBeIntercepted(fetchOptions: RequestInit) {
134134
const requestInterception = new Promise(resolve => this.resolveRequestPromise = resolve)
135-
const event = dispatch("turbo:before-fetch-request", { cancelable: true, detail: { fetchOptions, resume: this.resolveRequestPromise } })
135+
const event = dispatch("turbo:before-fetch-request", { cancelable: true, detail: { fetchOptions, url: this.url.href, resume: this.resolveRequestPromise } })
136136
if (event.defaultPrevented) await requestInterception
137137
}
138138
}

src/tests/functional/visit_tests.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,14 @@ export class VisitTests extends TurboDriveTestCase {
7171
this.assert(await this.changedBody)
7272
}
7373

74+
async "test turbo:before-fetch-request event.detail"() {
75+
await this.clickSelector("#same-origin-link")
76+
const { url, fetchOptions } = await this.nextEventNamed("turbo:before-fetch-request")
77+
78+
this.assert.equal(fetchOptions.method, "GET")
79+
this.assert.include(url, "/src/tests/fixtures/one.html")
80+
}
81+
7482
async visitLocation(location: string) {
7583
this.remote.execute((location: string) => window.Turbo.visit(location), [location])
7684
}

0 commit comments

Comments
 (0)