Skip to content
kaskaid

Server-Side Rendering

Ship pre-positioned items in the server HTML

Rendering happens client-side by default: the server emits an empty grid and items appear once the browser has measured them. Passing the ssr option instead lays the grid out on the server, so the first paint already carries positioned items.

estimateSize becomes required — the server has no DOM to measure, so it is the only height source. Without it every item collapses to 0.

With the component

Pass ssr alongside estimateSize:

import {  } from 'kaskaid';
 
const  = [200, 600, 200, 400, 100, 100, 50, 200];
 
function () {
  return (
    <
      ={}
      ={{ : ., : 4 }}
      ={() => []}
      ={({ ,  }) => < ={{ :  }}>{}</>}
    />
  );
}

With the hook

ssr is a plain useMasonry option, so a custom element structure gets the same server-rendered layout. Before mount, items holds the first ssr.itemCount entries from the measurements cache; after mount it becomes the real visible window.

import {  } from 'kaskaid';
 
const  = [200, 600, 200, 400, 100, 100, 50, 200];
 
function () {
  const { , ,  } = ({
    : ,
    : { : ., : 4 },
    : () => [],
  });
 
  return (
    < {...}>
      {.(() => (
        < ={.} {...()}>
          < ={{ : [.] }}>{.}</>
        </>
      ))}
    </>
  );
}

Choosing the config

lanes (default 4) is the lane count used for the server layout and the first paint, before CSS resolves --lanes on the client. Pick the value your most common viewport lands on; see Styling & Responsive Lanes.

scrollMargin (default 0) is the pixel distance from the document top to the grid. The client measures this itself after mount, but the server cannot — so if anything renders above the grid (a header, a hero), pass its height here or the pre-rendered items will shift once the client corrects the offset.

Reference