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

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

Open
rawbeen72 opened this issue Jun 15, 2024 · 3 comments

Comments

@rawbeen72
Copy link

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
Copy link
Contributor

1cg commented Jun 18, 2024

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

@rawbeen72
Copy link
Author

rawbeen72 commented Jun 19, 2024

@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
Copy link
Author

rawbeen72 commented Aug 9, 2024

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

`<script>
window.addEventListener("beforeunload", function (e)
{
sessionStorage.tabId = window.tabId;
return null;
});

window.addEventListener("load", function (_e)
{
    if (sessionStorage.tabId)
    {
        window.tabId = sessionStorage.tabId;
        window.sessionStorage.removeItem("tabId");
    }
    else
    {
        window.tabId = Math.floor(Math.random() * 1000000);
        window.location.reload()
    }

    return null;
});

</script>`

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

2 participants