Skip to content

Solid

Solid components, rendered on the server and hydrated with Solid’s own hydration, keyed by each render. The droplet below is a Solid component served by the demo server: its load sets the start, Solid renders it to HTML, and the button works once the bundle hydrates it.

counter · Solid

3

Server HTML: the button works once Solid hydrates.

Live: rendered by the demo server, hydrated in this page.
src/droplets/counter-solid/+droplet.tsx
/** @jsxImportSource solid-js */
import { createResource, createSignal, onMount } from "solid-js";
import type { DropletProps } from "./$types";
export const ssr = true;
export const hydrate = true;
export default function Counter(props: DropletProps) {
// What the server load returned. A value when hydrating, a promise when
// the element mounts it fresh: a resource of it suspends.
const data = props.data;
const value = data instanceof Promise ? createResource(() => data)[0] : () => data;
const [added, setAdded] = createSignal(0);
// False in the server's HTML; true once this component runs in the browser.
const [hydrated, setHydrated] = createSignal(false);
onMount(() => setHydrated(true));
const framework = () => value()?.framework;
return (
<section class="demo-card" data-hydrated={String(hydrated())}>
<p class="demo-row">
<span class="demo-tag">counter · {framework()}</span>
</p>
<p class="demo-row">
<output class="demo-title" aria-live="polite">
{(value()?.start ?? 0) + added()}
</output>
<button type="button" class="demo-button" onClick={() => setAdded((n) => n + 1)}>
Add one
</button>
</p>
<p class="demo-muted">
{hydrated()
? `Hydrated: ${framework()} runs the button in this tab.`
: `Server HTML: the button works once ${framework()} hydrates.`}
</p>
</section>
);
}
Terminal window
npx djs add counter --framework solid

It installs what the project lacks and adds solid() to renderers invite.config.ts, asking first, then writes src/droplets/counter/+droplet.tsx.

As the project's first .tsx framework, it also sets"jsx": "react-jsx", "jsxImportSource": "solid-js" in tsconfig.json.

Every step it takes is in Add a renderer. What it installs for Solid:

PackageInstalled as
@dropletsjs/renderer-solida dev dependency, at exactly the version of @dropletsjs/droplets you have
solid-js@^1.9.0a dependency

By hand:

Terminal window
npm install solid-js
npm install -D @dropletsjs/renderer-solid

then import { solid } from "@dropletsjs/renderer-solid" and solid() in renderers.

File What it is
+droplet.tsx, +droplet.jsx The component, the file’s default export. Its flags are named exports.
+error.tsx, +error.jsx The error page, optional.
+droplet.ts, +droplet.server.ts The loads, as for every framework: Writing droplets.

Which .tsx files are Solid’s. When Solid is the only .tsx framework in renderers, all of them. Next to React, Preact or Hono JSX, a droplet file is Solid’s when it imports solid-js or @dropletsjs/renderer-solid, a subpath included (solid-js/store), or carries a /** @jsxImportSource solid-js */ comment (djs add writes one at the top of every Solid component), and names no other .tsx framework. See Which renderer a .tsx file belongs to.

src/droplets/hello/+droplet.tsx
import { createResource, createSignal } from "solid-js";
import type { DropletProps } from "./$types";
export const hydrate = true;
export default function Hello(props: DropletProps) {
const [clicks, setClicks] = createSignal(0);
// A value when hydrating, a promise when mounted from scratch.
const data = props.data;
const value = data instanceof Promise ? createResource(() => data)[0] : () => data;
return (
<>
<h1>{value()?.greeting}</h1>
<button onClick={() => setClicks(clicks() + 1)}>{clicks()}</button>
{props.children}
</>
);
}
src/droplets/hello/+error.tsx
/** @jsxImportSource solid-js */
import type { DropletErrorProps } from "./$types";
export default function HelloError(props: DropletErrorProps) {
return (
<>
<p class="oops">{props.error.message}</p>
{props.children}
</>
);
}
Server rendering yes: ssr = true, the default; ssr = false mounts it in the browser only
hydrate true (the default), "visible", "interactive", false
Prerendering yes: prerender = true with entries
Forms the directive use:enhanceDropletForm of @dropletsjs/renderer-solid/form

Two mistakes stop vite build, and the vite dev server reports them too, each with a message that says what to change:

  • a .tsx droplet that names no framework, or two, when several share the extension:

    droplets: counter/+droplet.tsx: react, solid, preact and hono-jsx all handle this file and it imports none of them; import from react, solid-js, preact or hono/jsx, or add a /** @jsxImportSource <package> */ comment
  • importing another framework, even through a neutral file:

    droplets: a solid droplet imports preact code: src/droplets/counter/+droplet.tsx -> src/droplets/counter/lib.ts -> preact/hooks/dist/hooks.mjs. A droplet uses its own framework's modules and neutral code only.

Flags are read from +droplet.tsx alone. A flag that is not a literal (export const hydrate = isMobile) is ignored, with a warning under vite dev and vite build; a flag exported from +droplet.ts or +droplet.server.ts is ignored.

  • Data as a promise: a resource of it suspends; one that rejects reaches the renderer’s error boundary, which shows +error.tsx. The crash droplet on Errors is a Solid droplet with an error page.
  • Hydration is keyed by the droplet’s render id, so several Solid droplets on one page hydrate independently. The renderer sets up Solid’s hydration state itself; hosts need no hydration script.
  • Children: props.children renders the host’s content.
  • Styles: imported CSS goes to the droplet’s stylesheet. Attributes are HTML’s: class, for.
  • Forms: const add = actionUrl("add") with actionUrl of $droplets/form, then <form method="post" action={add}>; the directive needs enhanceDropletForm in scope (void enhanceDropletForm;).
  • Options: solid({ vitePlugin }) passes options to vite-plugin-solid; ssr is always on, and solid.hydratable must stay at its default. With solid({ plugin: false }) you register vite-plugin-solid yourself, with ssr: true.
  • Hot updates. Under vite dev, with dev.strategy: "hmr" (the default), an edit to a component goes through solid-refresh, which swaps the component in place and creates it again: its signals start over. An edit to the load brings new data to the live component, its state kept, when the component reads props.data where Solid tracks it (in JSX, a memo). A component that reads it once at setup, or wraps the first promise in a resource, keeps the old data until its next edit. See Hot updates.

examples/frameworks/solid has one droplet per mode (client, ssr-hydrate with an error page, ssr-nohydrate), with loads, a props schema, /api endpoints, env variables and a handle hook. From a clone of the repository:

Terminal window
pnpm install && pnpm build
pnpm --filter @dropletsjs/example-solid dev # http://localhost:5173: the devtools page

pnpm --filter @dropletsjs/example-solid build then start serves it on http://localhost:8000; /droplets/ssr-hydrate?who=Ana is the fragment a host pastes into its page.