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

Clearing history cache after certain requests? #3193

Open
WattsUp opened this issue Feb 16, 2025 · 2 comments
Open

Clearing history cache after certain requests? #3193

WattsUp opened this issue Feb 16, 2025 · 2 comments

Comments

@WattsUp
Copy link

WattsUp commented Feb 16, 2025

The Problem

In multiple project I have a page of items and a page to edit them. When pressing back after an edit the page of items should be invalid and require a server fetch. This ensures the updates to the item are reflected on the page of all items.

List of actions and expected history cache behavior

  • Visit page of items GET /fruit
  • Visit page of an item GET /fruit/banana
  • Back arrow to items /fruit: expect to use history cache. This works
  • Forward arrow to item /fruit/banana: expect to use history cache. This works
  • Edit item PUT /fruit/banana, also updates item page with OOB swap
  • Back arrow to items /fruit: expect to not use history cache and issue new GET /fruit. This does not work
  • Forward arrow to item /fruit/banana: expect to use history cache. This works
  • Back arrow to items /fruit: expect to use history cache. This works

My Current Solution

In the response to a successful item edit, I return a script calling localStorage.removeItem('htmx-history-cache'). This implements the behavior I expect. However this is hacky since it isn't a public API of HTMX.

Question

Is there a better & existing method of solving this problem? I looked into what htmx says about caching and just Googling caching, back and forth history, etc.

Proposed Solution

If there isn't a better way of solving this then I propose a response header be added that will trigger a history clear.

HX-Cache-Clear: true This would do localStorage.removeItem('htmx-history-cache') or similar

@MichaelWest22
Copy link
Contributor

https://htmx.org/attributes/hx-history/ There is an attribute you can place on an element in the problem pages and this will prevent the problem page being placed into history.

This is also an ideal candidate for a simple htmx extension which could for example perform removeItem during edit requests that have a hx-clear-history="/fruit" attribute set on them.

@Said-Ait-Driss
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants