htmx-禁用hx-push-url = true的DOM的快照
根据htmx
hx-push-url < /代码>
:
HX-PUSH-url属性使您可以将URL推入浏览器位置历史记录。这将创建一个新的历史记录条目,并允许使用浏览器的背部和前方按钮导航。 HTMX快照当前DOM并将其保存到其历史缓存中,并在导航上从此缓存中恢复。
这会导致以前的页面仅进行还原而没有重新加载,如果我删除hx-push-url
,导航与用户的期望不符。我的几乎所有页面导航都带有htmx
,有时我希望页面重新加载回到返回时。有没有办法使用HTMX实现这一目标?还是我可能会滥用HTMX?
According to Htmx
doc for hx-push-url
:
The hx-push-url attribute allows you to push a URL into the browser location history. This creates a new history entry, allowing navigation with the browser’s back and forward buttons. htmx snapshots the current DOM and saves it into its history cache, and restores from this cache on navigation.
This causes previous pages to just get restored and not reloaded and If I remove hx-push-url
, navigation doesn't match with user's expectation. Almost all of my page navigations are with Htmx
and I sometimes want pages to reload on navigating back. Is there a way achieve this with Htmx? Or could be that I'm misusing Htmx?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
HTMX还没有提供预防这种行为的官方方法。但是,通过一些手动工作,我们可以强迫HTMX进行整页刷新。 HTMX将页面存储在
localstorage
下的htmx-History-cache
键。因此,我们可以使用htmx:PushedIntoHistory
事件(〜该页面已保存到历史记录)来完全删除此密钥,从而迫使HTMX向服务器提出新请求。但是,默认情况下,HTMX将提供AJAX请求,而不是全页重新加载。要强制完整的重新加载,我们还需要将
RefreshonhistoryMiss
设置为true
:HTMX does not provide an official way to prevent this behavior, yet. However with a little manual work we can force HTMX to make a full page refresh. HTMX stores the page in the
localStorage
underhtmx-history-cache
key. So we can use thehtmx:pushedIntoHistory
event (~ the page has been saved to the history) to delete this key completely, forcing HTMX to make a new request to the server.However, by default HTMX will make an AJAX request instead a full page reload. To force a full reload, we also need to set
refreshOnHistoryMiss
totrue
:查看以下内容:
来源: docs
Check out this:
Source: docs