<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: TimEngleSF</title>
    <description>The latest articles on DEV Community by TimEngleSF (@timenglesf).</description>
    <link>https://dev.to/timenglesf</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1042410%2F6b8a9928-dd5a-4bca-8e50-828ec8344f09.png</url>
      <title>DEV Community: TimEngleSF</title>
      <link>https://dev.to/timenglesf</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/timenglesf"/>
    <language>en</language>
    <item>
      <title>Redirecting the browser using HTMX</title>
      <dc:creator>TimEngleSF</dc:creator>
      <pubDate>Thu, 01 Aug 2024 18:40:40 +0000</pubDate>
      <link>https://dev.to/timenglesf/redirecting-the-browser-using-htmx-nan</link>
      <guid>https://dev.to/timenglesf/redirecting-the-browser-using-htmx-nan</guid>
      <description>&lt;p&gt;I am currently working on creating my own blogging platform using Go, Templ, and HTMX that I will be hosting myself. I decided this would be a fun and useful project to tackle that will also help me gain knowledge of this stack. In this blog post, I will share my experience and solutions to a challenge I faced with handling redirects using HTMX.&lt;/p&gt;

&lt;h2&gt;
  
  
  Redirecting the browser
&lt;/h2&gt;

&lt;p&gt;While wrapping up the functionality for the admin, I decided it was time to finally start implementing HTMX into the project. I began with a button element that would send a POST request to my logout handler. Typically, I would accomplish this task using a form element, but as I mentioned, it's time to start implementing HTMX into the project. The issue is I want this POST action to still behave like a form and have the response from the server be a redirect back to the &lt;code&gt;/&lt;/code&gt; endpoint.&lt;/p&gt;

&lt;h3&gt;
  
  
  Issue: handling redirect with &lt;code&gt;http.Redirect&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Here's the button element I used:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;button&lt;/span&gt; &lt;span class="na"&gt;hx-post=&lt;/span&gt;&lt;span class="s"&gt;"/logout"&lt;/span&gt; &lt;span class="na"&gt;hx-trigger=&lt;/span&gt;&lt;span class="s"&gt;"click"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Logout&lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The issue I encountered was that the response for the redirect is still HTML, and HTMX swaps this content for the logout button element.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Handling the redirect with http.Redirect&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;app&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;application&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;handleLogoutPost&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ResponseWriter&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c"&gt;// handle business logic...&lt;/span&gt;
    &lt;span class="c"&gt;//...&lt;/span&gt;

    &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Redirect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"/"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;StatusSeeOther&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Image: The content has been swapped with the Logout button&lt;/em&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5g2eeh4yytc3plkxdb36.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5g2eeh4yytc3plkxdb36.png" alt="The content has been swapped with the Logout button" width="800" height="455"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Solution: using &lt;code&gt;HX-Redirect&lt;/code&gt; header
&lt;/h3&gt;

&lt;p&gt;The swapping of content can be prevented by replacing the &lt;code&gt;http.Redirect&lt;/code&gt; with an &lt;code&gt;HX-Redirect&lt;/code&gt; header in the response and the target location as its value.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;app&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;application&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;handleLogoutPost&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ResponseWriter&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c"&gt;// handle business logic...&lt;/span&gt;
    &lt;span class="c"&gt;//...&lt;/span&gt;

    &lt;span class="c"&gt;// Write our HX-Redirect header with location and redirect&lt;/span&gt;
    &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Header&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"HX-Redirect"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"/"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;WriteHeader&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;StatusNoContent&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Image: The browser has been redirected to "/" note the url.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0t304vu995eyb81q38ds.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0t304vu995eyb81q38ds.png" alt="The browser has been redirected to  raw `/` endraw  note the url." width="800" height="455"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;The process of building my own blogging platform with Go, Templ, and HTMX has been a rewarding experience so far. By integrating HTMX, the site will be able to still have many of interactivity features if a modern website without having to write and serve extra javascript code. Handling redirects with the &lt;code&gt;HX-Redirect&lt;/code&gt; header was a simple and effective solution. I hope this post helps anyone with their projects and encourages you to explore the potential of HTMX in your web applications.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>htmx</category>
      <category>go</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
