solid-optimizer rewrites JSX before Solid's JSX transform lowers it. It inlines components, removes context providers, inlines memos, and resolves control flow whose outcome is known at build time. More markup lands in fewer templates, so the browser clones fewer of them, the server sends fewer hydration keys, and fewer component calls run.
import { defineConfig } from 'vite';
import solid from 'solid-optimizer/vite';
export default defineConfig({
plugins: [solid({ ssr: true })],
});Before you start#
- A Vite project that builds with
@solidjs/vite-plugin. - SolidJS 2.0 and
@solidjs/compiler. Solid 1.x projects stay onsolid-optimizer@0.
You can try the optimizer on your own components in the REPL first.
Install#
Install the package as a development dependency.
npm i -D solid-optimizerpnpm add -D solid-optimizeryarn add -D solid-optimizer@solidjs/vite-plugin and vite are optional peer dependencies. Only solid-optimizer/vite needs them.
Set up Vite#
solid-optimizer/vite replaces @solidjs/vite-plugin. It takes the same options and wraps the official plugin, so SSR, server functions, and HMR keep working. Change the import and keep the options.
import { defineConfig } from 'vite';
import solid from 'solid-optimizer/vite';
export default defineConfig({
// Replaces @solidjs/vite-plugin, with the same options.
plugins: [solid({ ssr: true })],
});Build#
Run your usual build. The optimizer runs on builds and is off while serving.
npx vite buildSee what changed#
Turn on the report to see what the optimizer left as written, and why.
import { defineConfig } from 'vite';
import solid from 'solid-optimizer/vite';
export default defineConfig({
plugins: [
solid({
ssr: true,
optimizer: {
// Print what stays as written in each build, and why.
report: true,
},
}),
],
});Build again. Each build prints a report like this one:
solid-optimizer: what stays as written in client
inline lazy 4 About, Chart, Dashboard, Settings
inline copy-no-host 1 Home (./pages/Home)
inline no-host 1 HomeEach line is a pass, a reason, how many times it happened, and the names it happened to. A component that is not listed was inlined, or never could be. Here About and the other lazy() components stay components, because a lazy component loads at run time.
Next steps#
- Set up the Vite plugin for the options you are likely to change.
- Find out why a component stays when one you expected to inline stays a component.
- How it works for the model behind the output.
- Hydration before you ship a server-rendered app.