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

Do not render cache after preloaded response #458

Merged
merged 10 commits into from
Nov 22, 2021

Conversation

tleish
Copy link
Contributor

@tleish tleish commented Nov 19, 2021

Fix a race condition where BrowserAdapter#visitStarted renders cache after preloaded content is rendered.

The BrowserAdapter has the following:

export class BrowserAdapter implements Adapter {
  //...
  visitStarted(visit) {
    visit.issueRequest() //=> Async send request
    visit.changeHistory()
    visit.goToSamePageAnchor()
    visit.loadCachedSnapshot() //=> render cached snapshot while waiting for async request
  }

When visit.issueRequest() takes longer than 1ms, then visit.loadCachedSnapshot() renders the cache first. When it renders immediately (as in a preloaded response) then visit.loadCachedSnapshot() renders last.

Fixes #447

@tleish
Copy link
Contributor Author

tleish commented Nov 19, 2021

@seanpdoyle - can you review this PR and let me know if I need to change anything?

async "test cache does not override response after redirect"() {
await this.remote.execute(() => {
const cachedElement = document.createElement("some-cached-element")
document.body.appendChild(cachedElement)
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it significant that this is an appended element? Could an <input> with text typed into it suit this test as state to preserve?

Copy link
Contributor Author

@tleish tleish Nov 19, 2021

Choose a reason for hiding this comment

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

In my experience, the browser stores <input> text in memory, not in the DOM. Since turbo only caches DOM, testing cache through text input would not work.

Alternatively, I could add (or remove) a attribute to an existing DOM item (e.g. data-cached="true")

Essentially, I have to mock a scenario where the same URL is different from the first cached visit, to the follow-up new visit. Something like a <div id="flash">One Item Flash Message</div>.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@seanpdoyle - thumbs up means you are good with the explanation or you want an update to the test with one of the suggestions?

Copy link
Contributor

@seanpdoyle seanpdoyle Nov 20, 2021

Choose a reason for hiding this comment

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

The current version works for me. Thank you for uncovering and digging into this!

@dhh dhh merged commit b88bb24 into hotwired:main Nov 22, 2021
@ghiculescu
Copy link

@tleish do you use the iOS adapter by any chance?

I'm trying to work out if this function has the same problem that this PR fixed: https://github.com/hotwired/turbo-ios/blob/fadb1f3888427a839f1e102ec5f38125f96172ba/Source/WebView/turbo.js#L116

@tleish
Copy link
Contributor Author

tleish commented Feb 17, 2022

@ghiculescu Yes, we do use the iOS adapter. What issue are you experiencing?

@ghiculescu
Copy link

It was a screen flicker, but it ended up being unrelated and due to us not creating a new view controller on each visit - apparently yiu really need to do that. So not this issue at all.

That said... does that code I linked look like it could cause the issue this PR addresses?

@tleish
Copy link
Contributor Author

tleish commented Feb 17, 2022

That said... does that code I linked look like it could cause the issue this PR addresses?

I haven't tested this particular bug on mobile, but it looks to follow the same older pattern and quite possibly has the same bug. Are you going to submit a PR to suggest an update to follow the same pattern as this MR?

@ghiculescu
Copy link

I can, but I'm not confident it's an issue yet. Do you have a test page I can use to replicate it?

@tleish
Copy link
Contributor Author

tleish commented Feb 17, 2022

@ghiculescu - See the steps to reproduce and demo app linked in the original #447 issue

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.

Bug: Turbo.visit Render sequence race condition for Cache and Request
4 participants