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

Handle flash attributes on htmx redirects #137

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

xhaggi
Copy link
Contributor

@xhaggi xhaggi commented Oct 25, 2024

This adds handling for flash attributes via RedirectAttributes. Now you can perform client-side redirects with htmx and the flash attributes work as with normal redirects.

Fixes #96

@xhaggi
Copy link
Contributor Author

xhaggi commented Oct 25, 2024

@wimdeblauwe there is a limitation, because the flash attribute processing does not work if HtmxResponse is set as model attribute (PR #128). I'm not sure if it was a good idea to support such a case and to be honest, I don't like it.

@xhaggi xhaggi force-pushed the feature/handle-flash-attributes branch from aac224b to 8097ea9 Compare October 25, 2024 12:09
@xhaggi xhaggi force-pushed the feature/handle-flash-attributes branch from 8097ea9 to a15d7c2 Compare October 25, 2024 12:10
@xhaggi
Copy link
Contributor Author

xhaggi commented Oct 25, 2024

BTW @wimdeblauwe there is more magic done by RedirectView

  • Treat the redirect URL as a URI template (default: true)
  • Model attributes can be exposed as HTTP query parameters (default: true)
  • Propagate the query params of the current URL. (default: false)

We could support all of them to behave as similarly as possible to Spring. Let me know what you think about it.

@checketts
Copy link
Collaborator

@wimdeblauwe there is a limitation, because the flash attribute processing does not work if HtmxResponse is set as model attribute (PR #128). I'm not sure if it was a good idea to support such a case and to be honest, I don't like it.

I couldn't find an alternative approach that was compatible with using JTE Models as the return. I'm happy to try another approach if you have any suggestions.

@xhaggi
Copy link
Contributor Author

xhaggi commented Oct 25, 2024

@checketts let's move the discussion about an alternative approach to the initial issue #127.

}
}

private void buildAndRender(HtmxResponse htmxResponse, ModelAndView mav, HttpServletRequest request, HttpServletResponse response) {
View v = htmxResponseHandlerMethodReturnValueHandler.toView(htmxResponse);
try {
v.render(mav.getModel(), request, response);
htmxResponseHandlerMethodReturnValueHandler.addHxHeaders(htmxResponse, response);
// ModelAndViewContainer is not available here, so flash attributes won't work
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure when exactly the flash attributes will not work? Maybe we should document this in the README?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's what I pointed out in my comment above.

@wimdeblauwe
Copy link
Owner

BTW @wimdeblauwe there is more magic done by RedirectView

  • Treat the redirect URL as a URI template (default: true)
  • Model attributes can be exposed as HTTP query parameters (default: true)
  • Propagate the query params of the current URL. (default: false)

We could support all of them to behave as similarly as possible to Spring. Let me know what you think about it.

If you are willing to implement that, it would be great if we align as much as possible with RedirectView I think.

@xhaggi
Copy link
Contributor Author

xhaggi commented Oct 28, 2024

If you are willing to implement that, it would be great if we align as much as possible with RedirectView I think.

Sure, but let's merge this before and I'll create a separate PR for it. Okay?

@wimdeblauwe
Copy link
Owner

wimdeblauwe commented Oct 30, 2024

ok, but can we already merge this? Don't we need a solution to #127 first?

@xhaggi
Copy link
Contributor Author

xhaggi commented Oct 30, 2024

I'm not sure because I need more information from @checketts on how he uses it in his projects. But if you ask me, we should revert #127 in favor of a better solution to his issue. It's definitely the wrong approach to render views in HtmxHandlerInterceptor#postHandle which was introduced by #127. And as far as I know, using JteModel as a return type is not a supported feature of Jte itself.

See https://jte.gg/spring-boot-starter-3/ or https://github.com/casid/jte/tree/main/jte-spring-boot-starter-3/src/main/java/gg/jte/springframework/boot/autoconfigure for more information about integration with Spring Boot.

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.

[feature request]: RedirectAttributes with .redirect() ?
3 participants