-
Notifications
You must be signed in to change notification settings - Fork 429
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
Form submit not clearing cache #193
Comments
@seanpdoyle I apologize for pinging you directly on this issue but I have seen you show up in a lot on issues I have been watching and your support has been very valuable. I can imagine you are terribly busy and there must be an overwhelming amount of issues to address with Hotwire. For now I would be more than satisfied with just a few words on whether I have named something known or not and whether anything is likely to be changed in Hotwire to address this problem. At the moment I am just turning off caching with the no-cache meta tag for the whole application to avoid these undesirable caching effects but that does not seem ideal long term if there are other options. I would be happy to dig in and work on a solution if this seems possible and something that others would value. |
Thank you for raising this, @jonsgreen.
To help narrow down the surface area a bit, a minimal example that reproduces the behavior would be valuable? If you're able to add one, a failing functional test case would be a valuable alternative. There are lines of code in the Navigator to clear the snapshot cache on a form submission success and a form submission failure. In the meantime, a |
[Form submit not clearing cache #193](hotwired/turbo#193)
[Form submit not clearing cache #193](hotwired/turbo#193)
[Form submit not clearing cache #193](hotwired/turbo#193)
@seanpdoyle Thanks for responding. I have created an MVCE here. Hopefully it illustrates the problem and will help you answer whether there is a problem with our code, whether there is some simple change we can make to correct the behavior or whether there is something at fault in turbo. I put some notes right in the README. I look forward to your response. |
@seanpdoyle I decided to dig into the code a bit to see if I could understand a bit more about what is happening. It is quite the maze for someone from the outside who doesn't know very much about the internals of Turbo. However, from what I can tell, as you mentioned above the cache does get cleared with the form submission success. However, it seem like a few lines down a visit is proposed and that seems to reestablish the snapshot cache. Not sure if that helps but thought I would point it out. |
I just confirmed this by fiddling around the console. Submitting a form clears the cache and then caches the current page (with the form that we don’t want cached) when navigating away. |
Thanks @javan for confirming this issue. I am curious if in the scope of things whether this would be considered a bug and if so how significant. Is there anyway that I can help in moving this issue forward? |
Hi there! I just hit this also - and I'm also trying to think of whether this is expected or a bug... and if a bug how to fix. The behavior also happens form submitting a form without success: A) Navigate to a page with a form (e.g. On (D), the filled form with validation errors will show up briefly (the snapshot) before being replaced with a fresh form. This is... sort of expected: when you navigate on (C), like any other navigation, this takes a snapshot of the page (with the filled form and validation errors). This seems tricky to fix. The only fix I can think of is to: As a policy, disable snapshots on pages with forms. This would need to be done by the user with We could try to be smarter... and cache the page only in its "initial" (unsubmitted) form... and then not cache future snapshots. This would fix the "preview" problem (showing the filled out form as a preview when you click back to the page) but it would have the negative effect of showing the blank form on restoration visits (e.g. when I hit back, now the form would be blank, whereas currently, the form contains the filled-fields & errors from my submit). |
i'm running into this as well. in our case we're on an "edit" page and disable the button when submitting the form ( |
To clarify, I think there are 2 possible bugs - and fixing one is pretty clear: A) On a successful POST, Turbo already clears the cache... but then re-caches the page that was just submitted - #193 (comment) - and that seems like a clear mistake. B) On unsuccessful POST, Turbo does not clear the cache... which causes problems... but the solution is not clear (unless Turbolinks already has a perfect solution) - #193 (comment) Item (A) is probably a "lowish hanging fruit" - I didn't want my introduction of item (B) to cloud things. Cheers! |
I'm also running into this issue. Not only when navigating back with the browsers' back button but also when navigating back to the form by clicking a link. The form retains the filled in values and the button status seems to be disabled. @jonsgreen how did you get around this? I've currently disabled turbo on my form with |
Regarding the compatibility between Rails ujs (button being disabled) and Turbo: Please correct me if I'm wrong but my understanding is that In practice, this means that This is where Stimulus comes in. In a Stimulus controller the Bottom line is: I don't think we have an answer to Rails UJS Also, there is a library called mrujs (Modern Rails UJS) which I believe serves as a modern alternative to the classic rails ujs and is compatible with Turbo. |
In my case I've actually removed Rails ujs. I also don't mind adding something like Still, the fact that all data is shown in the form seems like a problem. I haven't found any way to just disable the cache. |
There's an ongoing discussion about a replacement for There's also an ongoing discussion about backwards compatible support for UJS: hotwired/turbo-rails#257 As far as the snapshot cache is concerned, I wonder if it'd be worth introducing a |
@pinzonjulian yes Mrujs currently accounts for data-disable-with on turbo:submit-start and turbo:submit-end I plan to move these to the MrujsTurbo plugin, but currently they're in the core of Mrujs. I would like to do more work in this regard in reference to @seanpdoyle 's best practices by using buttons. There is also some compatibility issues to be solved with mrujs in relation to TurboFrames, but perhaps that's out of the scope of Mrujs which will add https://github.com/ParamagicDev/mrujs/blob/main/src/remoteWatcher.ts |
You shouldn't need to do |
I was getting stale cache when using a turbo frame. In this case, added a reusable Stimulus controller to clear it up, as suggested using // turbo_clear_cache_controller.js
// Add `data-controller="turbo-clear-cache"` to any form within a turbo frame
// that you'd like to clear the cache for
import { Controller } from 'stimulus'
import { clearCache } from '@hotwired/turbo'
export default class extends Controller {
initialize() {
this.afterRequest = this.afterRequest.bind(this)
}
connect() {
this.element.addEventListener(
'turbo:before-fetch-response',
this.afterRequest
)
}
disconnect() {
this.element.removeEventListener(
'turbo:before-fetch-response',
this.afterRequest
)
}
afterRequest({ detail }) {
if (detail.fetchResponse.succeeded) {
clearCache()
}
}
} |
Does adding the following solve the issue? const FetchMethod = { get: 0 };
document.addEventListener('turbo:submit-end', async ({ detail }) => {
const nonGetFetch = detail.formSubmission.fetchRequest.method !== FetchMethod.get;
const responseHTML = await detail.fetchResponse.responseHTML;
if (detail.success && nonGetFetch && responseHTML) {
Turbo.clearCache();
}
}); |
I had to add a timeout in order to make sure that the cache clearing happened after the errant "recache the page" behavior kicked in. Maybe const FetchMethod = { get: 0 };
document.addEventListener('turbo:submit-end', async ({ detail }) => {
const nonGetFetch = detail.formSubmission.fetchRequest.method !== FetchMethod.get;
const responseHTML = await detail.fetchResponse.responseHTML;
if (detail.success && nonGetFetch && responseHTML) {
setTimeout(() => {
Turbo.clearCache();
}, "1000");
}
}); |
I have a form that posts to an action that changes the state of an object and then redirects to a different page. If I then hit the back button I am given an old snapshot of the page and I need to refresh to get the correct view based on the updated state.
I would have expected Turbo to clear the cache in such cases but that appears to not be happening. I am wondering if this is expected behavior? If so then is there something simple I should be doing to get the desired behavior? If not then is this a bug or is there something I might be doing wrong?
I am happy to create an MVCE but thought I should check first before wasting time on something that is already clearly known.
The text was updated successfully, but these errors were encountered: