Hidden prop SSR exposure with Box component
0 reputation · 01 Jun 2021, 09:16 UTC
0 reputation · 01 Jun 2021, 09:16 UTC
Goal is to prevent accidental public access of UI sections that should not appear in server-rendered markup when using Material UI conditional visibility.
The hidden prop removes a component from the DOM on the client but the component can still be present in the initial server-rendered HTML. The NoSSR prop is documented to suppress server-side rendering for its children, which changes hydration. Client-side hiding alone does not provide data security and server-side access controls remain necessary. Behavior may differ between MUI v5 and v6.
What conditions cause hidden to leave markup in the initial HTML? How does NoSSR interact with hidden to avoid server-side exposure? What hydration trade-offs arise when combining these props for components like Box?
35275 reputation · 01 Jun 2021, 11:18 UTC
The hidden prop leaves markup in the initial HTML whenever the server renders the component — which is always, because the server has no access to the client viewport or breakpoint context that hidden depends on. NoSSR (or <NoSsr>) prevents that exposure by omitting the children entirely from the server render, emitting only a placeholder comment node. When combined, NoSSR wins on the server (no markup), then the client hydrates the placeholder and evaluates hidden afterward. The trade-off is a hydration boundary: the subtree mounts later, which can cause layout shift and delays interactive readiness.
display: none (or breakpoint-specific styles). Markup and any child data remain in the HTML payload.<!-- -->) or custom fallback. Children mount only after client hydration.hidden logic (media query / breakpoint check) to decide visibility.hidden uses withWidth() HOC or useMediaQuery. In v6, hidden is deprecated in favor of sx={{ display: { xs: 'none', md: 'block' } }}. Both produce identical SSR semantics — markup present with styles.<div> (or component prop) and inherits system props.hidden={false} on client), it can push layout. Provide a fallback prop with matching dimensions (e.g., a skeleton) to avoid CLS.NoSSR subtree are unavailable until hydration completes. For above-the-fold content, this degrades perceived performance.NoSSR subtree waits, causing a brief flash of missing content.useEffect runs.hidden is visual only. Props, children, and any serialized data in the component tree remain in the server HTML. NoSSR removes markup from the SSR payload but does not replace server-side authorization. Sensitive data must be gated at the data-fetching layer (e.g., getServerSideProps, loaders, API checks).
npm list @mui/material. If v6+, migrate hidden to sx breakpoint syntax.<NoSsr> (or NoSSR prop on Box) rather than relying on hidden.fallback prop to <NoSsr> with a skeleton or fixed-height placeholder matching the expected client dimensions.curl -s localhost:3000) — the sensitive markup should be absent.What MUI version is in package.json? The migration path for hidden differs between v5 and v6, and the recommended replacement (sx breakpoints) only applies to v6+.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.