Astro client directives defer JavaScript but not component markup
0 reputation · 13 Dec 2021, 22:46 UTC
0 reputation · 13 Dec 2021, 22:46 UTC
I am evaluating whether Astro hydration directives can prevent a component's HTML markup from reaching the browser when JavaScript delivery is deferred.
Astro renders components on the server and ships zero component JavaScript unless a directive such as client:visible, client:idle, or client:load is present. The directives control when JavaScript loads and executes, but documentation indicates the component markup remains in the initial HTML response. Behavior may also differ between static site output and SSR adapters, particularly for timing-based directives like client:idle.
I need to confirm whether any directive or build setting excludes markup from the response, or whether markup exposure is unavoidable for server-rendered Astro components.
Does any Astro client directive prevent component HTML markup from appearing in the initial server response? Is markup exposure identical between static output and SSR adapter output for deferred hydration?
37775 reputation · 14 Dec 2021, 06:04 UTC
No Astro deferral directive keeps component HTML out of the initial server response. client:load, client:idle, and client:visible only delay when the component's JavaScript is requested and executed; the server-rendered markup is still included in the HTML the browser receives. The sole directive that excludes server markup is client:only, but it is not a deferral directive—it renders nothing on the server and delivers a client-only component instead.
Markup exposure is effectively the same for output: 'static' and SSR adapter output when using deferral directives: the component HTML appears in the initial response in both cases. Runtime behavior can differ—for example, client:idle scheduling or HTML streaming may vary between static and SSR—but that changes when hydration happens, not whether the markup is delivered.
<span data-astro-marker>.client:visible, client:idle, or client:load.astro build and search the output with grep:astro build
grep -R "data-astro-marker" dist/
If the marker appears, the markup is in the response. Repeat the check against your SSR adapter's preview or deployed URL, then repeat once with client:only to confirm the server HTML omits the component markup while loading the client component script.
This assumes current Astro behavior; directive semantics and SSR streaming can vary across major versions, so check your installed version before relying on streaming-specific details. The static/SSR equivalence is likely but should be confirmed in your deployment because adapter streaming is not guaranteed by directive behavior alone.
Use comments to ask for clarification. Post a solution as an answer.
37,775 reputation · 13 Dec 2021, 23:49 UTC
No deferral directive—client:load, client:idle, client:visible, or client:media—removes server-rendered markup from the initial HTML. Those directives only delay JavaScript delivery and hydration. The exception is client:only, which skips server rendering for that component entirely, so its markup is absent from the first response but requires a UI framework and ships no server HTML. This is consistent across static output and SSR adapters; the adapter changes when HTML is generated, not whether deferred-component markup appears. Verify with astro build and dist/index.html, or by fetching the SSR endpoint and inspecting the initial document.