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

Response from HX-Location should respect HX-Push-Url header #3115

Open
devashishdxt opened this issue Jan 9, 2025 · 5 comments
Open

Response from HX-Location should respect HX-Push-Url header #3115

devashishdxt opened this issue Jan 9, 2025 · 5 comments

Comments

@devashishdxt
Copy link

I have a scenario where I set the HX-Location header to redirect the client to other page (assume /x). In some cases, the response (of /x) could contain HX-Push-Url header (assume /y) which is not being respected, i.e., the page url is still same as the url in HX-Location header (/x).

Is there a way to push the url in HX-Push-Url (/y) header of response in browser history after the redirect using HX-Location header?

@Telroshan
Copy link
Collaborator

From what I see in the sources:

htmx/src/htmx.js

Lines 4692 to 4694 in e0905ff

if (hasHeader(xhr, /HX-Location:/i)) {
saveCurrentPageToHistory()
let redirectPath = xhr.getResponseHeader('HX-Location')

htmx/src/htmx.js

Lines 4703 to 4705 in e0905ff

ajaxHelper('get', redirectPath, redirectSwapSpec).then(function() {
pushUrlIntoHistory(redirectPath)
})

I don't see any reason HX-Push-Url would not be applied from the second endpoint's response, however it looks like htmx ignores whether the redirected endpoint submitted any instruction about pushing a URL or not, and uses pushUrlIntoHistory after the request is made to push the redirected URL anyway

So, I would expect your history to actually contain the URL sent back by your second endpoint's HX-Push-Url header as you would expect, but likely one step backwards in the history (which would be an issue of course).

Could you confirm if this is actually what is happening? Do you see the desired URL when going back in the history by 1 step? If so, we can likely add a fix for that

@MichaelWest22
Copy link
Contributor

also see #3045 for more detail on the cause

@MichaelWest22
Copy link
Contributor

The issue is the ajaxHelper function is waits to complete fully as a promise which will push the hx-push-url to history and 'then' it will push the hx-location URL every time. We could add

      ajaxHelper('get', redirectPath, redirectSwapSpec).then(function() {
        if (!redirectSwapSpec?.noPushUrl) pushUrlIntoHistory(redirectPath)
      })

to add a new optional key to hx-location data spec that if set true would block the post request push if required.

Maybe a better way would be this

      const oldPath = currentPathForHistory
      ajaxHelper('get', redirectPath, redirectSwapSpec).then(function() {
        if (oldPath === currentPathForHistory) pushUrlIntoHistory(redirectPath)
      })

@devashishdxt
Copy link
Author

Could you confirm if this is actually what is happening? Do you see the desired URL when going back in the history by 1 step? If so, we can likely add a fix for that

Yes. I can see the desired URL if I go one step back.

@MichaelWest22
Copy link
Contributor

could another solution be to add a pushUrl optional boolean to HtmxAjaxHelperContext and HtmxAjaxEtc and HtmxResponseInfoand then allow ajaxHelper to pass this through with the request config and then set this true for all hx-location requests. Then get the ajax request to push history at the end if this is true by using this boolean in the determineHistoryUpdates function. This new option would also resolve #3114 as there would then be a proper ajax api to push url.

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

3 participants