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
Server HTML: the button works once Solid hydrates.
/** @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> );}import type { LoadEvent } from "./$types";
/** * Runs on the server only. The count starts where the server says, and the * name of the framework comes with it. The same for everyone, so a browser * (or a cache in front) may keep it for five minutes. */export const load = ({ setHeaders }: LoadEvent) => { setHeaders({ "cache-control": "public, max-age=300" }); return { framework: "Solid", start: 3 };};Add it to a project
Section titled “Add it to a project”npx djs add counter --framework solidIt 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:
| Package | Installed as |
|---|---|
@dropletsjs/renderer-solid | a dev dependency, at exactly the version of @dropletsjs/droplets you have |
solid-js@^1.9.0 | a dependency |
By hand:
npm install solid-jsnpm install -D @dropletsjs/renderer-solidthen import { solid } from "@dropletsjs/renderer-solid" and solid() in renderers.
The files
Section titled “The files”| 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.
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} </> );}/** @jsxImportSource solid-js */import type { DropletErrorProps } from "./$types";
export default function HelloError(props: DropletErrorProps) { return ( <> <p class="oops">{props.error.message}</p> {props.children} </> );}What it supports
Section titled “What it supports”| 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
.tsxdroplet 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.
Specifics
Section titled “Specifics”- 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.childrenrenders the host’s content. - Styles: imported CSS goes to the droplet’s stylesheet. Attributes are
HTML’s:
class,for. - Forms:
const add = actionUrl("add")withactionUrlof$droplets/form, then<form method="post" action={add}>; the directive needsenhanceDropletFormin scope (void enhanceDropletForm;). - Options:
solid({ vitePlugin })passes options tovite-plugin-solid;ssris always on, andsolid.hydratablemust stay at its default. Withsolid({ plugin: false })you registervite-plugin-solidyourself, withssr: true. - Hot updates. Under
vite dev, withdev.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 newdatato the live component, its state kept, when the component readsprops.datawhere 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.
The example
Section titled “The example”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:
pnpm install && pnpm buildpnpm --filter @dropletsjs/example-solid dev # http://localhost:5173: the devtools pagepnpm --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.