Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include url in turbo:before-fetch-request event #289

Merged
merged 2 commits into from
Jul 14, 2021

Conversation

ctrochalakis
Copy link
Contributor

Hello all and thank you for Turbo.

Although the turbo:before-fetch-request event includes the request's fetchOptions (method, headers, etc), it doesn't include the URL itself. With this patch applied you can access it with event.detail.url.

If it gets accepted I can send a PR to add a reference to the events documentation in https://github.com/hotwired/turbo-site/.

@@ -71,6 +71,14 @@ export class VisitTests extends TurboDriveTestCase {
this.resetRequestInterceptor()
}

async "test turbo:before-fetch-request event.detail"() {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am getting a timeout error when I run the whole suite, but everything seems to work if I run this specific test in isolation (by adding intern.configure({ grep: /turbo:before/ }) in runner.js).

It's strange since it's basically identical to the request interceptor test.

× firefox on linux 5.10.0-0.bpo.5-amd64 - VisitTests - turbo:before-fetch-request event.detail (2.5s)
    TimeoutError: Timeout reached on firefox on linux 5.10.0-0.bpo.5-amd64 - VisitTests - turbo:before-fetch-request event.detail#
      at Timeout._onTimeout @ src/lib/Test.ts:226:24
      at ontimeout @ timers.js:436:11
      at tryOnTimeout @ timers.js:300:5
      at listOnTimeout @ timers.js:263:5
      at Timer.processTimers @ timers.js:223:10


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are seeing the same on #299. Only for Firefox. Odd. Please do investigate!

@kirillplatonov
Copy link
Contributor

@ctrochalakis could you please resolve merge conflicts? The FetchRequest class has been recently updated.

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 hotwired#289
@ctrochalakis
Copy link
Contributor Author

I looked into the test failure yesterday, it was a race between the page reload and the events tracking setup. I've opened #310 with a fix.

I rebased the PR on top of the main branch (the #310 fix is also included here).

@kirillplatonov
Copy link
Contributor

@ctrochalakis nice catch with visits tests race condition! 👍

@ctrochalakis ctrochalakis force-pushed the include-url-in-turbo-before-fetch branch from 33170b3 to 09b846a Compare July 14, 2021 06:40
dhh pushed a commit that referenced this pull request Jul 14, 2021
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
@dhh dhh merged commit 3b70866 into hotwired:main Jul 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants