Skip to content

Fix: document title no longer updated for responses suppressed by noSwap - #4118

Open
alizamani1616 wants to merge 1 commit into
bigskysoftware:four-devfrom
alizamani1616:fix-4113-title
Open

alizamani1616 wants to merge 1 commit into
bigskysoftware:four-devfrom
alizamani1616:fix-4113-title

Conversation

@alizamani1616

Copy link
Copy Markdown

Description

When a response status matches htmx.config.noSwap — the default [204, 304], or "4xx"/"5xx" pushed by the htmx-2-compat extension — htmx correctly skips swapping the response content, but if the response body contained a <title>, the document title was still updated:

<meta name="htmx-config" content='{"noSwap":[204,304,"4xx","5xx"]}'>

A 404/500 response with <title>Error</title> in the body silently changed the page title even though nothing was swapped.

htmx 2 never touched the title for a response that was not swapped, and noSwap is the documented way to restore htmx 2 behavior, so it should not update the title either.

Fixes #4113

Root cause

__handleStatusCodes marks the response as non-swappable by setting ctx.swap = "none", and swap() still runs (events, indicators, OOB handling). The title update at the end of swap() only checked ignoreTitle, so the none swap style coming from noSwap still updated document.title.

Fix

  • __handleStatusCodes sets ctx.noSwap = true alongside ctx.swap = "none" when the status matches the noSwap config.
  • swap() skips the title update when ctx.noSwap is set.

Explicit hx-swap="none" and hx-status:*="swap:none" overrides still update the title, matching htmx 2 behavior, since they represent a deliberate swap specification rather than status-based suppression.

Testing

  • Added unit tests for the noSwap flag in test/tests/unit/__handleStatusCodes.js (set for exact and wildcard patterns, not set for swappable statuses or hx-status overrides).
  • Added title unit tests in test/tests/unit/swap.js: no title update with noSwap: true, title still updates for plain swap: "none" without the flag.
  • Verified in a real browser: with noSwap: ["4xx","5xx"], a 404 response containing <title>Error</title> no longer changes document.title; before the fix it changed it to "Error" while the content stayed unswapped, exactly as reported.

Full suite passes: 1742 tests, 0 failures, 100% coverage of src/htmx.js.

When a response status matches htmx.config.noSwap (e.g. the default
[204, 304], or '4xx'/'5xx' pushed by the htmx-2-compat extension),
content swapping is correctly skipped but a <title> in the response
body still updated document.title. htmx 2 never touched the title for
non-swapped responses, and noSwap exists to restore that behavior.

__handleStatusCodes now sets ctx.noSwap = true alongside
ctx.swap = 'none', and swap() skips the title update when it is set.
Explicit hx-swap='none' and hx-status:*='swap:none' keep updating the
title, matching htmx 2.

Fixes bigskysoftware#4113
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.

1 participant