Skip to content

Server rendering without a flash

A server-rendered page shows its HTML before the scripts run. If a script hides the content, the content shows, then hides, then animates. The React, Vue and Astro packages hide the waiting content in the server HTML, so it does not flash.

Each waiting element has the data-awesome-reveal attribute in the server HTML:

  • Reveal and the other reveal APIs set data-awesome-reveal="hidden" on the element.
  • Cascade and the other cascade APIs set data-awesome-reveal="cascade" on the container.

The reveal.css stylesheet of the package gives opacity: 0 to each hidden element and to the direct children of each cascade container. The element keeps its size and its place, so the layout does not move at the reveal. At the reveal, the package removes the attribute, and the entrance starts.

The modules of each package import the stylesheet, so your bundler or Astro adds it to the page. Three setups need a step:

The stylesheet does not hide a waiting element with a static rule. It plays the awesome-reveal-hold CSS animation, which holds opacity: 0 for 5 seconds. When the script of the package attaches the element, it adds the data-awesome-reveal-attached attribute. A static opacity: 0 rule then hides the element until the reveal.

If no script attaches the element in 5 seconds, the hold ends, and the content shows. Thus the content does not stay hidden when a script fails to load or throws.

While focus is in the element or in the cascade container, the content shows. Thus a keyboard user does not focus invisible content before the script attaches it. After a blur, the hold continues until its 5 seconds end.

If the script attaches the element after 5 seconds, the content shows, then hides again until it reveals. Lazy hydration often attaches the element that late. Hydrate the reveal content eagerly.

The stylesheet hides content only for the screen media type, and only with prefers-reduced-motion: no-preference. Thus the content shows immediately in these cases:

  • The user asks for reduced motion.
  • The user prints the page.
  • JavaScript is off. A scripting: none rule stops the hold. In browsers without the scripting media feature, such as Safari 16, the content shows after 5 seconds.
  • The hold replaces your own animation until the reveal. Put your own animation on an inner element. The hold also fires animationstart and animationend events, and shows in getAnimations().
  • !important styles can show the element too early. An inline opacity or your own animation with !important can show the element before the reveal.
  • The packages reserve two attributes. Do not set data-awesome-reveal or data-awesome-reveal-attached yourself.
  • The stylesheet does not reach a shadow root. It cannot hide an element in a shadow root.

@awesome-reveal/core has no stylesheet, and it does not hide content before your script calls reveal() or cascade(). Server-rendered content can show, then animate. For server-rendered pages, use the React, Vue or Astro package.

You can import core on the server. Call reveal() and cascade() only in the browser.

The content is in the server HTML. The stylesheet changes only the opacity of the content, not the text or the layout. Thus a search engine or a tool that reads the HTML gets all the content.