bigskysoftware / htmx

</> htmx - high power tools for HTML
https://htmx.org
Other
37.14k stars 1.25k forks source link

Handling Browser Tab Duplication Behavior in HTMX Requests with Conditional Layouts (SPA Scenario) #2617

Open rawbeen72 opened 2 months ago

rawbeen72 commented 2 months ago

In Single Page Applications (SPA) that utilize HTMX for dynamic content loading, we face challenges related to browser tab duplication. This issue particularly impacts applications where layout components—such as sidebars and top bars—are conditionally rendered based on the context of the request. Specifically, the application distinguishes between normal browser GET requests and HTMX requests (identified by the presence of the HX-Request header).

The problem arises when a user duplicates a tab using the browser's "Duplicate Tab" feature (e.g., right-clicking on the tab and selecting "Duplicate"). This action inherits the state information from the original tab, including headers like HX-Request, which can lead to unintended consequences. These consequences include incorrect handling of HTMX requests and inconsistencies in the rendering of layout components.

In our SPA setup, the server-side rendering of layout components, such as the sidebar and top bar, depends on the presence of the HX-Request header. This dependency ensures that these components are selectively included in responses to HTMX requests to optimize performance and reduce unnecessary data transmission.

However, when a tab is duplicated, the new tab inherits the HX-Request header from the original tab. This can cause issues because the duplicated tab's request context may not align with the intended state for layout rendering, leading to scenarios where the sidebar and top bar are not displayed.

1cg commented 2 months ago

is there any way to hook into the duplicate tab creation logic?

rawbeen72 commented 2 months ago

@1cg not sure if there is any way to do so but currently this is the one downside/limitation of htmx as far as i know. Is there any solution to this problem that we can try?

rawbeen72 commented 1 month ago

@1cg the current workaround for the problem is achieved by triggering reload when tab is duplicated.

``