Skip to content

Conversation

@scriptogre
Copy link

Description

This PR adds history management functions from htmx core:

  1. saveCurrentPageToHistory
  2. setCurrentPathForHistory
  3. pushUrlIntoHistory
  4. replaceUrlInHistory

to the internalAPI const, making them accessible via extensions using api.pushUrlIntoHistory, etc.

I'm currently working on a new extension that needs to reuse this history logic. I'm currently duplicating 200+ lines of code from core library just to be able to ensure consistent history behavior 😢 I would love to see this merged.

Testing

All tests pass.

Checklist

  • I have read the contribution guidelines
  • I have targeted this PR against the correct branch (master for website changes, dev for
    source changes)
  • I ran the test suite locally (npm run test) and verified that it succeeded

This allows extensions to manage browser history the same way htmx does, instead of having to reimplement these functions.
@scriptogre scriptogre changed the title Enable extension access to history functions Enable access to history functions via extensions Sep 4, 2025
@scriptogre scriptogre changed the title Enable access to history functions via extensions Enable access to history functions in extensions Sep 4, 2025
@scriptogre
Copy link
Author

scriptogre commented Sep 4, 2025

@1cg hey big boss, can you merge when available? thanks 🫡

@MichaelWest22
Copy link
Collaborator

Hi, i have a PR up #3404 that adds history control to the htmx.ajax() api. this change would add push and replace optional context inputs for the api so it can be used by javascript calls to perform the push and replace as this is a commonly asked for feature. Just wondering if this might also help resolve your limitation with not being able to access history functions? The only issue is this does need to do a full ajax request that could do a cached or full network request before it will trigger the htmx history update.

If you needed to never do real ajax network requests but still push the url you could maybe use

htmx.ajax('GET', 'data:text/html,', {
  swap: 'none',
  push: '/your-new-url'  // or replace: '/your-new-url'
});

data url's are a bit different in that they don't do real network requests but you need to allow data: urls past the default selfRequestOnly config block by using htmx:validateUrl event listener i think.

Or you can just do a rubbish or likely to be cached real network request to allow the full htmx ajax flow.

What does your extension need to do?

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

Successfully merging this pull request may close these issues.

2 participants