Answers.Body: <p>The simplest way to update the title is to include a title element anywhere in the response.</p>
  <pre><code>&lt;title&gt;New title&lt;/title&gt;
  </code></pre>
  <p>I’m not sure if the same is true for meta tags. You can ask I in the <a href="https://htmx.org/discord" rel="nofollow noreferrer">htmx discord server</a>, since swap functionality is handled by htmx.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-10-15T19:55:37.273Z
Answers.Text: Works great! Thanks! I thought just tags with an id would get replaced.
Answers.UserDisplayName: Matthias Redl-Mann
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-10-15T09:38:05.583Z
Answers.LastActivityDate: 2024-10-15T14:51:26.133Z
Answers.LastEditDate: 2024-10-15T14:51:26.133Z
Answers.UserDisplayName: Ben Croker
Body: <p>We are currently working on a website using the Sprig Plugin to show and reactively filter event overviews.</p>
  <p>One of the options is to browse through monthly views, showing you all information for one month at a time. In this context we also update the URL the <code>s-push-url</code> attribute and we also want to update <code>&lt;title&gt;</code> and maybe some meta tags.</p>
  <p>Our way to do it is by listening to the htmx:afterOnLoad event and fetching title information from a certain header:</p>
  <pre><code>&lt;script&gt;
      document.body.addEventListener('htmx:afterOnLoad', function(event) {
          // Check if the response includes a custom header or data with the new title
          var newTitle = event.detail.xhr.getResponseHeader('X-Page-Title');
          if (newTitle) {
              document.title = newTitle;
          }
      });
  &lt;/script&gt;
  </code></pre>
  <p>This works but I do not really like it. Are there any other solutions for this requirement?</p>
  <p>Thanks!</p>
  
Tags: <meta><sprig><head>
Title: SPRIG: best way to update head information like title, meta,
ThreadText: # SPRIG: best way to update head information like title, meta,
  
  (Asked by: username_0 on 2024-10-15)
  
  We are currently working on a website using the Sprig Plugin to show and reactively filter event overviews.
  
  One of the options is to browse through monthly views, showing you all information for one month at a time. In this context we also update the URL the `s-push-url` attribute and we also want to update `<title>` and maybe some meta tags.
  
  Our way to do it is by listening to the htmx:afterOnLoad event and fetching title information from a certain header:
  
  ```
  <script>
      document.body.addEventListener('htmx:afterOnLoad', function(event) {
          // Check if the response includes a custom header or data with the new title
          var newTitle = event.detail.xhr.getResponseHeader('X-Page-Title');
          if (newTitle) {
              document.title = newTitle;
          }
      });
  </script>
  
  ```
  
  This works but I do not really like it. Are there any other solutions for this requirement?
  
  Thanks!
  
  **Tags:** meta, sprig, head
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2024-10-15. Score: 2 | Accepted answer
  
  The simplest way to update the title is to include a title element anywhere in the response.
  
  ```
  <title>New title</title>
  
  ```
  
  I’m not sure if the same is true for meta tags. You can ask I in the [htmx discord server](https://htmx.org/discord), since swap functionality is handled by htmx.
  
  > **Comments:**
  > **username_0**: Works great! Thanks! I thought just tags with an id would get replaced.  
  
  ---
  
  