`@effect/ui` (Effect-TS/ui): `HtmlServer` writes no attribute for `.prop("hidden", true)` and other reflected properties, so hidden content shows until hydration
I maintainer di solito rispondono entro 1 giorno
Valutazione
Questa issue non è ancora stata valutata.
Descrizione
[!NOTE]
Please note that Claude Opus 5.5 at high effort assisted the author in preparing this content.
What version of Effect is running?
| Package | Version | Commit |
|---|---|---|
@effect/ui (Effect-TS/ui) |
0.0.0 (unreleased) | dec1ca9 |
effect |
4.1.6 |
What steps can reproduce the bug?
TL;DR: HtmlServer writes no attribute for a property such as .prop("hidden", true) on a built-in element, and hydration drops some reflected attributes too, so one table of the HTML standard's reflected properties, shared by the server and hydration, should make both produce what the browser host builds.
The browser host writes .prop(name, value) as element[name] = value, and for most properties of built-in elements the browser reflects that into a content attribute, so a mounted page has hidden, inert, title, tabindex or class. The server writes only value, checked, selected and open. Content hidden with .prop("hidden", true) therefore shows from first paint until hydration, and for good if the script never runs, while .attr("hidden", "").prop("hidden", false) does the reverse and serves hidden content the client shows. Controls disabled or made read-only through .prop are served enabled and editable, and crawlers and readers without JavaScript get a different page. After hydration, className, htmlFor and tabIndex: -1 also leave the element without the attribute a fresh mount has. Related work in the repository includes:
- Effect-TS/effect#11040: the same
.prop("hidden", true)reachesHtmlLocatorrole queries onTestHostand scene trees, through a separate cause in the testing helpers. - Effect-TS/ui#277: the server already treats the
href,src,action,formActionanddataproperties as reflected, removing their attribute for a blocked URL, but writes nothing for any other value.
Save this as packages/ui/test/html/ServerProperty.test.ts in Effect-TS/ui and run pnpm vitest run packages/ui/test/html/ServerProperty.test.ts --silent=false:
// @vitest-environment happy-dom
import { Html, HtmlHydrate, HtmlServer } from "@effect/ui/html"
import * as BrowserHost from "@effect/ui/html/BrowserHost"
import { it } from "@effect/vitest"
import { Effect, Schema } from "effect"
const views = {
hidden: (h: Html.Tags<never>) => h.div.prop("hidden", true).children("Secret"),
shown: (h: Html.Tags<never>) => h.div.attr("hidden", "").prop("hidden", false).children("Shown"),
title: (h: Html.Tags<never>) => h.div.prop("title", "Tip"),
tabIndex: (h: Html.Tags<never>) => h.div.prop("tabIndex", -1),
className: (h: Html.Tags<never>) => h.div.prop("className", "note")
}
const attributes = (app: Element) =>
app.firstElementChild!.getAttributeNames()
.map((name) => `${name}="${app.firstElementChild!.getAttribute(name)}"`).join(" ")
const container = (html: string) =>
Effect.acquireRelease(
Effect.sync(() => {
const app = document.body.appendChild(document.createElement("div"))
app.innerHTML = html
return app
}),
(app) => Effect.sync(() => app.remove())
)
it.effect("server, hydrated and mounted attributes", () =>
Effect.gen(function*() {
for (const [label, view] of Object.entries(views)) {
const page = Html.component({ name: "Page", model: Schema.Null, message: Schema.Never })
.make({ init: () => null, update: ($) => $.model, view })
const { html, state } = yield* HtmlServer.renderToHtml(page, {})
const served = yield* container(html)
const server = attributes(served)
yield* HtmlHydrate.hydrateEffect(page, {}, served, state)
const mounted = yield* container("")
yield* BrowserHost.mountEffect(page, {}, mounted, { key: "mounted" })
console.log(
`${label}: server [${server}], hydrated [${attributes(served)}], ` +
`mounted [${attributes(mounted)}]`
)
}
}).pipe(Effect.scoped))
What is the expected behavior?
The server's HTML, and the element after hydration, have the attributes of the browser host's own mount:
hidden: server [hidden=""], hydrated [hidden=""], mounted [hidden=""]
shown: server [], hydrated [], mounted []
title: server [title="Tip"], hydrated [title="Tip"], mounted [title="Tip"]
tabIndex: server [tabindex="-1"], hydrated [tabindex="-1"], mounted [tabindex="-1"]
className: server [class="note"], hydrated [class="note"], mounted [class="note"]
The HTML standard reflects these IDL attributes into content attributes, so setting the property sets the attribute. The hidden setter removes the attribute for false, "", null, 0 and NaN, writes hidden="until-found" for "until-found", and hidden="" for any other value.
What do you see instead?
hidden: server [], hydrated [hidden=""], mounted [hidden=""]
shown: server [hidden=""], hydrated [], mounted []
title: server [], hydrated [title="Tip"], mounted [title="Tip"]
tabIndex: server [], hydrated [], mounted [tabindex="-1"]
className: server [], hydrated [], mounted [class="note"]
The server leaves out each property's attribute and keeps the hidden attribute the property removes. After hydration, tabIndex and className have no attribute at all, and neither does htmlFor on a label, unlike a fresh mount. inert, disabled and readOnly behave like hidden and title.
Additional information
Root cause. The HTML renderer has no mapping from a property of a built-in element to the content attribute it reflects, while the browser host writes the property and lets the browser reflect it:
- The server's
elementAttributeswrites the controlledvalue,checked,selectedandopenand, for any other property, only removes a blocked URL's attribute, leaving the rest to the client's adoption. - Hydration's
setProprecords a property's served attribute asname.toLowerCase()(classname,htmlfor) and skips a property whose getter already equals the value (adiv'stabIndexis-1). When it finishes the element, it removes the served attributes it didn't record,classandforamong them, although the property has just written them.
Proposed resolution. Give the HTML renderer one table of the properties of built-in elements that reflect a content attribute, following the HTML standard's reflection rules: for each property, its attribute name (tabIndex → tabindex, className → class, htmlFor → for, readOnly → readonly) and its kind (boolean, string, integer, enumerated, URL, or the hidden setter's own rule). The server writes each property in the table as the attribute its setter sets, after the element's attributes so that the property wins, as it does in the browser, where attributes are written first; URL properties keep the blocked-URL check. Hydration records the table's attribute name and compares the served attribute rather than the property, so it keeps and writes what a fresh mount has. Both sides need the table: with the server alone, hydration would still remove a served class written from className.
- Server HTML matches the browser host's DOM for reflected properties, so content hidden with
.prop("hidden", true)is hidden from first paint and without JavaScript. The public API doesn't change. - Properties without a content attribute, such as
indeterminate,scrollTopor event handler properties likeonclick, stay client-only, as a modal dialog and an open popover already are. The table is an allow-list, so no such value is ever serialized. The docs ofpropwould say which properties the server renders. - Server HTML and hydration snapshots change for elements with such properties.
Nothing needs to be settled before the fix; when and how it lands is for the maintainers to decide.
- Lingua principale
- TypeScript
- Stelle
- 16.7k
- Fork
- 808
- Merge medio
- 21h 22m
- PR unite (30g)
- 486
Preparare l'ambiente
Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di Effect-TS/effect
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
I maintainer di solito rispondono entro 1 giorno
-
`@effect/ui-native` (Effect-TS/ui): `kit/VirtualList` `list` part widens `R` to `any`, poisoning enclosing componentsForse già presa @effect-bot l’ha presa oggi. Aperta
Effect-TS/effect#11097 · 1 assegnatario ·
I maintainer di solito rispondono entro 1 giorno
-
`@effect/ui-native` (Effect-TS/ui): Controlled `input` rejected-edit restore never calls `host.commit()`Forse già presa @effect-bot l’ha presa oggi. Aperta
Effect-TS/effect#11096 · 1 assegnatario ·
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di Effect-TS/effect
Issue simili
-
check:failed streams:edit
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
iptv-org/iptv#60585 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 67/100
I maintainer di solito rispondono entro 1 giorno
-
[Bug]: Critical path skips a child span that ends exactly when the next one startsForse già presa @Piyush4801 l’ha presa oggi. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
jaegertracing/jaeger-ui#4865 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 90/100
anthropics/claude-code#112972 ·
-
Type: Improvement
Difficoltà 2/5 1-3 ore Idoneità per principianti 85/100
I maintainer di solito rispondono entro 1 giorno