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

Introduce htmx and use it to avoid full page load on Subscribe and Follow #28908

Merged
merged 16 commits into from
Jan 30, 2024

Conversation

yardenshoham
Copy link
Member

@yardenshoham yardenshoham commented Jan 23, 2024

This change introduces htmx with the hope we could use it to make Gitea more reactive while keeping our "HTML rendered on the server" approach.

  • Add htmx.js that imports htmx.org and initializes error toasts
  • Place hx-headers='{"x-csrf-token": "{{.CsrfToken}}"}' on the <body> tag so every request that htmx sends is authenticated
  • Place hx-swap="outerHTML" on the <body> tag so the response of each htmx request replaces the tag it targets (as opposed to its inner content)
  • Place hx-push-url="false" on the <body> tag so no changes to the URL happen in <form> tags
  • Add the is-loading class during request

Error toasts in action

errors

Don't do a full page load when clicking the subscribe button

  • Refactor the form around the subscribe button into its own template
  • Use htmx to perform the form submission
    • hx-boost="true" to prevent the default form submission behavior of a full page load
    • hx-sync="this:replace" to replace the current request (in case the button is clicked again before the response is returned)
    • hx-target="this" to replace the form tag with the new form tag
  • Change the backend response to return a <form> tag instead of a redirect to the issue page

Before

subscribe_before

After

subscribe_after

Don't do a full page load when clicking the follow button

  • Use htmx to perform the button request
    • hx-post="{{.ContextUser.HomeLink}}?action=follow" to send a POST request to follow the user
    • hx-target="#profile-avatar-card" to target the card div for replacement
    • hx-indicator="#profile-avatar-card" to place the loading indicator on the card
  • Change the backend response to return a <div> tag (the card) instead of a redirect to the user page

Before

follow_before

After

follow_after

…`Follow`

This change introduces htmx with the hope we could use it to make Gitea
more reactive while keeping our "HTML rendered on the server" approach.

- Add `htmx.js` that imports `htmx.org` and initializes error toasts
- Place `hx-headers='{"x-csrf-token": "{{.CsrfToken}}"}'` on the `<body>` tag so every request that htmx sends is authenticated

### Error toasts in action

![errors](https://github.com/go-gitea/gitea/assets/20454870/181a1beb-1cb8-4858-abe8-fa1fc3f5b8f3)

## Don't do a full page load when clicking the subscribe button
- Refactor the form around the subscribe button into its own template
- Use htmx to perform the form submission
  - `hx-boost="true"` to prevent the default form submission behavior of a
full page load
  - `hx-sync="this:replace"` to replace the current request (in case the
button is clicked again before the response is returned)
  - `hx-target="this"` to replace the form tag with the new form tag
  - `hx-push-url="false"` to disable a change to the URL
  - `hx-swap="show:no-scroll"` to preserve the scroll position
- Change the backend response to return a `<form>` tag instead of a
redirect to the issue page

### Before

![subscribe_before](https://github.com/go-gitea/gitea/assets/20454870/cb2439a2-c3c0-425c-8d3c-5d646b1cdc28)

### After

Also shows a small request loading indicator (the indicator should be worked on further in a future change)

![subscribe_after](https://github.com/go-gitea/gitea/assets/20454870/5e71a43e-9500-42af-8118-9d6441e8dc42)

## Don't do a full page load when clicking the follow button
- Use htmx to perform the button request
  - `hx-post="{{.ContextUser.HomeLink}}?action=follow"` to send a POST
request to follow the user
  - `hx-target="#profile-avatar-card"` to target the card div for
replacement
  - `hx-swap="outerHTML"` to replace the card (as opposed to its inner
content) with the new card that shows the new follower count and button
color
- Change the backend response to return a `<div>` tag (the card) instead
of a redirect to the user page

### Before

![follow_before](https://github.com/go-gitea/gitea/assets/20454870/a210b643-6e74-4ff9-8e61-d658c62edf1f)

### After

![follow_after](https://github.com/go-gitea/gitea/assets/20454870/5df04537-0dd9-4e26-a5f4-73ae11e6d976)

Signed-off-by: Yarden Shoham <git@yardenshoham.com>
@GiteaBot GiteaBot added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Jan 23, 2024
@pull-request-size pull-request-size bot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Jan 23, 2024
Copy link
Member

@jolheiser jolheiser left a comment

Choose a reason for hiding this comment

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

(Requesting changes only so we can "block" while we gather feedback, overall I like it I think)


I like that the htmx-specific templates are also split out, it seems easier to reason about; similar to a component.

templates/repo/issue/view_content/watching.tmpl Outdated Show resolved Hide resolved
@GiteaBot GiteaBot added lgtm/blocked A maintainer has reservations with the PR and thus it cannot be merged and removed lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. labels Jan 23, 2024
Signed-off-by: Yarden Shoham <git@yardenshoham.com>
Signed-off-by: Yarden Shoham <git@yardenshoham.com>
@yardenshoham yardenshoham added the topic/ui-interaction Change the process how users use Gitea instead of the visual appearance label Jan 23, 2024
@lunny
Copy link
Member

lunny commented Jan 24, 2024

How to update multiple divs on one htmx request?

@yardenshoham
Copy link
Member Author

How to update multiple divs on one htmx request?

https://htmx.org/attributes/hx-sync/

Signed-off-by: Yarden Shoham <git@yardenshoham.com>
@yardenshoham
Copy link
Member Author

@jolheiser is anything else needed to remove your block?

@jolheiser jolheiser dismissed their stale review January 28, 2024 19:48

Feedback indicated a general interest in htmx

@GiteaBot GiteaBot added lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. and removed lgtm/blocked A maintainer has reservations with the PR and thus it cannot be merged labels Jan 28, 2024
@yardenshoham yardenshoham added the reviewed/wait-merge This pull request is part of the merge queue. It will be merged soon. label Jan 28, 2024
web_src/js/htmx.js Outdated Show resolved Hide resolved
@6543
Copy link
Member

6543 commented Jan 30, 2024

( #28908 (comment) )

Signed-off-by: Yarden Shoham <git@yardenshoham.com>
@6543 6543 merged commit 3e84141 into go-gitea:main Jan 30, 2024
25 checks passed
@GiteaBot GiteaBot added this to the 1.22.0 milestone Jan 30, 2024
@GiteaBot GiteaBot removed the reviewed/wait-merge This pull request is part of the merge queue. It will be merged soon. label Jan 30, 2024
@yardenshoham yardenshoham deleted the htmx branch January 30, 2024 14:52
henrygoodman pushed a commit to henrygoodman/gitea that referenced this pull request Jan 31, 2024
…`Follow` (go-gitea#28908)

- Closes go-gitea#28880

This change introduces htmx with the hope we could use it to make Gitea
more reactive while keeping our "HTML rendered on the server" approach.

- Add `htmx.js` that imports `htmx.org` and initializes error toasts
- Place `hx-headers='{"x-csrf-token": "{{.CsrfToken}}"}'` on the
`<body>` tag so every request that htmx sends is authenticated
- Place `hx-swap="outerHTML"` on the `<body>` tag so the response of
each htmx request replaces the tag it targets (as opposed to its inner
content)
- Place `hx-push-url="false"` on the `<body>` tag so no changes to the
URL happen in `<form>` tags
- Add the `is-loading` class during request

### Error toasts in action


![errors](https://github.com/go-gitea/gitea/assets/20454870/181a1beb-1cb8-4858-abe8-fa1fc3f5b8f3)

## Don't do a full page load when clicking the subscribe button
- Refactor the form around the subscribe button into its own template
- Use htmx to perform the form submission
- `hx-boost="true"` to prevent the default form submission behavior of a
full page load
- `hx-sync="this:replace"` to replace the current request (in case the
button is clicked again before the response is returned)
  - `hx-target="this"` to replace the form tag with the new form tag
- Change the backend response to return a `<form>` tag instead of a
redirect to the issue page

### Before


![subscribe_before](https://github.com/go-gitea/gitea/assets/20454870/cb2439a2-c3c0-425c-8d3c-5d646b1cdc28)

### After


![subscribe_after](https://github.com/go-gitea/gitea/assets/20454870/6fcd77d8-7b11-40b0-af4f-b152aaad787c)

## Don't do a full page load when clicking the follow button
- Use htmx to perform the button request
- `hx-post="{{.ContextUser.HomeLink}}?action=follow"` to send a POST
request to follow the user
- `hx-target="#profile-avatar-card"` to target the card div for
replacement
- `hx-indicator="#profile-avatar-card"` to place the loading indicator
on the card
- Change the backend response to return a `<div>` tag (the card) instead
of a redirect to the user page

### Before


![follow_before](https://github.com/go-gitea/gitea/assets/20454870/a210b643-6e74-4ff9-8e61-d658c62edf1f)

### After


![follow_after](https://github.com/go-gitea/gitea/assets/20454870/5bb19ae9-0d59-4ae3-b538-4c83334e4722)

---------

Signed-off-by: Yarden Shoham <git@yardenshoham.com>
Co-authored-by: 6543 <m.huber@kithara.com>
Co-authored-by: Giteabot <teabot@gitea.io>
silverwind added a commit to silverwind/gitea that referenced this pull request Feb 1, 2024
* origin/main:
  [skip ci] Updated translations via Crowdin
  Fix UI Spacing Errors in mirror settings (go-gitea#28990)
  Add htmx guidelines (go-gitea#28993)
  Some refactor for git http (go-gitea#28995)
  Fix an actions schedule bug (go-gitea#28942)
  Fix doc img path in profile readme (go-gitea#28994)
  Introduce htmx and use it to avoid full page load on `Subscribe` and `Follow` (go-gitea#28908)
  Fix joins in `db.Find(AndCount)` (go-gitea#28978)
  Update golang links to use https (go-gitea#28980)
  Fix google logo in security page (go-gitea#28982)
zjjhot added a commit to zjjhot/gitea that referenced this pull request Feb 1, 2024
* giteaofficial/main:
  Revert "Speed up loading the dashboard on mysql/mariadb (go-gitea#28546)" (go-gitea#29006)
  Update dorny/paths-filter action (go-gitea#29003)
  [skip ci] Updated translations via Crowdin
  Fix UI Spacing Errors in mirror settings (go-gitea#28990)
  Add htmx guidelines (go-gitea#28993)
  Some refactor for git http (go-gitea#28995)
  Fix an actions schedule bug (go-gitea#28942)
  Fix doc img path in profile readme (go-gitea#28994)
  Introduce htmx and use it to avoid full page load on `Subscribe` and `Follow` (go-gitea#28908)
  Fix joins in `db.Find(AndCount)` (go-gitea#28978)
  Update golang links to use https (go-gitea#28980)
  Fix google logo in security page (go-gitea#28982)
  Also match weakly validated ETags (go-gitea#28957)
lunny pushed a commit that referenced this pull request Feb 17, 2024
- following organization is broken from #28908
- add login check for the follow button in organization profile page
silverwind pushed a commit to silverwind/gitea that referenced this pull request Feb 20, 2024
…`Follow` (go-gitea#28908)

- Closes go-gitea#28880

This change introduces htmx with the hope we could use it to make Gitea
more reactive while keeping our "HTML rendered on the server" approach.

- Add `htmx.js` that imports `htmx.org` and initializes error toasts
- Place `hx-headers='{"x-csrf-token": "{{.CsrfToken}}"}'` on the
`<body>` tag so every request that htmx sends is authenticated
- Place `hx-swap="outerHTML"` on the `<body>` tag so the response of
each htmx request replaces the tag it targets (as opposed to its inner
content)
- Place `hx-push-url="false"` on the `<body>` tag so no changes to the
URL happen in `<form>` tags
- Add the `is-loading` class during request

### Error toasts in action


![errors](https://github.com/go-gitea/gitea/assets/20454870/181a1beb-1cb8-4858-abe8-fa1fc3f5b8f3)

## Don't do a full page load when clicking the subscribe button
- Refactor the form around the subscribe button into its own template
- Use htmx to perform the form submission
- `hx-boost="true"` to prevent the default form submission behavior of a
full page load
- `hx-sync="this:replace"` to replace the current request (in case the
button is clicked again before the response is returned)
  - `hx-target="this"` to replace the form tag with the new form tag
- Change the backend response to return a `<form>` tag instead of a
redirect to the issue page

### Before


![subscribe_before](https://github.com/go-gitea/gitea/assets/20454870/cb2439a2-c3c0-425c-8d3c-5d646b1cdc28)

### After


![subscribe_after](https://github.com/go-gitea/gitea/assets/20454870/6fcd77d8-7b11-40b0-af4f-b152aaad787c)

## Don't do a full page load when clicking the follow button
- Use htmx to perform the button request
- `hx-post="{{.ContextUser.HomeLink}}?action=follow"` to send a POST
request to follow the user
- `hx-target="#profile-avatar-card"` to target the card div for
replacement
- `hx-indicator="#profile-avatar-card"` to place the loading indicator
on the card
- Change the backend response to return a `<div>` tag (the card) instead
of a redirect to the user page

### Before


![follow_before](https://github.com/go-gitea/gitea/assets/20454870/a210b643-6e74-4ff9-8e61-d658c62edf1f)

### After


![follow_after](https://github.com/go-gitea/gitea/assets/20454870/5bb19ae9-0d59-4ae3-b538-4c83334e4722)

---------

Signed-off-by: Yarden Shoham <git@yardenshoham.com>
Co-authored-by: 6543 <m.huber@kithara.com>
Co-authored-by: Giteabot <teabot@gitea.io>
silverwind pushed a commit to silverwind/gitea that referenced this pull request Feb 20, 2024
- following organization is broken from go-gitea#28908
- add login check for the follow button in organization profile page
6543 pushed a commit to 6543-forks/gitea that referenced this pull request Feb 26, 2024
- following organization is broken from go-gitea#28908
- add login check for the follow button in organization profile page

(cherry picked from commit 6822799)
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 4, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. modifies/internal size/M Denotes a PR that changes 30-99 lines, ignoring generated files. topic/ui-interaction Change the process how users use Gitea instead of the visual appearance
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Decide to use htmx or not
9 participants