Skip to Content
solid-trees
Guides

Shape tree data for fast rendering

Prepare tree input outside the UI so the client skips repeated shape and sort work.

Shape the tree before it reaches the UI when the dataset is large. Then this out-of-UI preparation is worth the effort. solid-trees treats prepared input as a first-class public concept. It is not an internal optimization trick.

Start with server-prepared input for scalable trees

prepareFileTreeInput(...) lets the client skip repeated tree-shape work. Do this preparation on the server, a loader, or another non-UI boundary. That boundary already has the full path list.

load-project-tree-input.ts
import { prepareFileTreeInput } from 'solid-trees';

export async function loadProjectTreeInput(projectId: string) {
  const paths = await fetchProjectPaths(projectId);

  return prepareFileTreeInput(paths, {
    flattenEmptyDirectories: true,
  });
}

The client still uses the same path-first model. Only the expensive preparation step moves earlier.

Pass preparedInput into the model

createFileTree(...) accepts the prepared payload in place of raw paths.

project-tree.tsx
import { createFileTree, FileTree } from 'solid-trees';
import type { FileTreePreparedInput } from 'solid-trees';

export function ProjectTree(props: { preparedInput: FileTreePreparedInput }) {
  const tree = createFileTree({ preparedInput: props.preparedInput });
  return <FileTree model={tree} style={{ height: '320px' }} />;
}

Use simple paths input only for small trees

Raw paths is still the right choice for small demos, tests, and very small static trees.

small-paths.ts
const tree = createFileTree({
  paths: ['README.md', 'src/index.ts', 'src/components/Button.tsx'],
});

This is the easy start path, not the scale-oriented default. Move the preparation work out of the client when the tree grows.

Presorted input is the highest-performance prepared-input path

Use preparePresortedFileTreeInput(...) when your server or indexer already knows the final order. It skips the tree-shape work. It also skips the extra sort work that a normal prepared-input pass applies.

presorted.ts
import { preparePresortedFileTreeInput } from 'solid-trees';

const preparedInput = preparePresortedFileTreeInput([
  'README.md',
  'src/index.ts',
  'src/components/Button.tsx',
]);

Use this path when the backend, build step, or cached index already owns the sort order. Do not write the default order again in the client only to reach the presorted path.

Prepare on the server, render on the client

Use this simple split:

  1. Load canonical paths outside the UI.
  2. Prepare the input one time.
  3. Pass preparedInput into createFileTree(...), or into resetPaths(...) when the tree already exists.

This split reduces client CPU work. It makes the startup cost more predictable. It also lets one prepared payload feed every tree that shows the same paths.

Keep client-side sorting and preparation secondary

Sometimes the data exists only in the browser. Sometimes custom order must run there. solid-trees supports both cases, but they are the exception path. Start with prepared input when the app can move the work out of the UI. Use client-side shaping only as a fallback.

To learn the shared contract behind these inputs, read Shared concepts. For the scale-oriented version of this guidance, continue with Handle large trees efficiently.