Skip to content

Preserving Web Component state with hx-preserve on browser navigation #2531

Closed
@jasontheiler

Description

@jasontheiler

Hi everyone,

I'm trying to create a website with HTMX and its hx-boost feature. Since I want to avoid using another frontend framework, I'm trying to use Web Components for elements that have some logic and state associated with them. With the hx-preserve attribute and an ID I can preserve a Web Component's state across page navigations. However, if one uses the browser's built-in back and forward navigation, the Web Components lose all of their state and they get newly instantiated.

I have searched the internet and the HTMX issues for a solution, but I have trouble finding useful information about this specific use case.

I've created a simple reproduction on CodeSandbox with a simple counter as a Web Component that keeps its count across page navigations using the links at the top in the header. As soon as you use the browser's back and forward navigation though, the counter resets back to 0.

I'd be really grateful for any help on the matter!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions