Solid Start

SolidStart, the Solid app framework

README

Solid Docs


SolidStart


This is the home of the Solid app framework. This is still a work in progress. Many features are missing or incomplete. Experimental status does not even mean beta status. Patch releases will break everything.

- File-system based routing
- Supports all rendering modes:
  - Server-side rendering (SSR)
  - Streaming SSR
  - Client-side rendering (CSR)
  - Static Site Generation (SSG)
- Streaming
- Build optimizations with Code splitting, tree shaking and dead code elimination
- API Routes
- Built on Web standards like Fetch, Streams, and WebCrypto
- Adapters for deployment to all popular platforms
- CSS Modules, SASS/SCSS Support
- TypeScript-first

Getting started


  1. ``` sh
  2. mkdir my-app
  3. cd my-app
  4. npm init solid
  5. npm install
  6. npm run dev
  7. ```

Development


The monorepo uses pnpm as the package manager. To install pnpm, run the following command in your terminal.

  1. ``` sh
  2. npm install -g pnpm
  3. ```

Run pnpm install to install all the dependencies for the packages and examples in your monorepo.

Monorepo & project.json "workspace" support


If you are using Solid Start within a monorepo that takes advantage of the package.json "workspaces" property (e.g. yarn workspaces) with hoisted dependencies (the default for yarn), you must includesolid-start within the optional "nohoist" (for yarn v2 or higher, see further down for instructions) workspaces property.

- _In the following, "workspace root" refers to the root of your repository while "project root" refers to the root of a child package within your repository_

For example, if specifying "nohoist" options from the workspace root (i.e. for all packages):

  1. ``` jsonc
  2. // in workspace root
  3. {
  4.   "workspaces": {
  5.     "packages": [
  6.       /* ... */
  7.     ],
  8.     "nohoist": ["**/solid-start"]
  9.   }
  10. }
  11. ```

If specifying "nohoist" options for a specific package using solid-start:

  1. ``` jsonc
  2. // in project root of a workspace child
  3. {
  4.   "workspaces": {
  5.     "nohoist": ["solid-start"]
  6.   }
  7. }
  8. ```

Regardless of where you specify the nohoist option, you also need to include solid-start as a devDependency in the child package.json.

The reason why this is necessary is because solid-start creates an index.html file within your project which expects to load a script located in /node_modules/solid-start/runtime/entry.jsx (where / is the path of your project root). By default, if you hoist the solid-start dependency into the workspace root then that script will not be available within the package's node_modules folder.

Yarn v2 or higher

The nohoist option is no longer available in Yarn v2+. In this case, we can use the installConfig property in the package.json (either workspace package or a specific project package) to make sure our deps are not hoisted.

  1. ``` jsonc
  2. // in project root of a workspace child
  3. {
  4.   "installConfig": {
  5.     "hoistingLimits": "dependencies"
  6.   }
  7. }
  8. ```


Changelog


[0.1.6]


Renamed API Routes exports from lower case to upper case method names to match closely how people see those functions in the spec and in usage.

  1. ```diff
  2. - export function get() {
  3. + export function GET() {
  4.   return new Response();
  5. }

  6. - export function post() {
  7. + export function POST() {

  8.   return new Response();
  9. }

  10. - export function patch() {
  11. + export function PATCH() {
  12.   return new Response();
  13. }

  14. - export function del() {
  15. + export function DELETE() {
  16.   return new Response();
  17. }
  18. ```

[0.1.0-alpha.104]


Changed grouped routes from __name syntax to (name).

[0.1.0-alpha.103]


Changed special compiled functions like server, createServerData, createServerAction$, createServerMultiAction$. to have a postfix $ to indicate their special compiled (hoisted behavior).

Also moved the optional first argument of createServerData$ under key option. While this hides a very important option it makes the signatures more similar, so it is clear it is the main (first) function that is running on the server.

  1. ``` js
  2. const data = createServerData$(
  3.   async pathname => {
  4.     let mod = mods[`./docs${pathname}.mdx`] ?? mods[`./docs${pathname}.md`];
  5.     return mod.getHeadings().filter(h => h.depth > 1 && h.depth <= 3);
  6.   },
  7.   {
  8.     key: () => path.pathname
  9.   }
  10. );
  11. ```

[0.1.0-alpha.??] - Moving towards beta


vite.config.ts


  1. ```diff
  2. - import solid from 'solid-start';
  3. + import solid from 'solid-start/vite';
  4. import { defineConfig } from 'vite';

  5. export default defineConfig({
  6.   plugins: [solid()]
  7. })
  8. ```

Why?


We wanted to use the main entry point of solid-start for use within the app where you are spending most of your time. And for the vite config, we use the solid-start/vite entrypoint.

entry-server.tsx


  1. ```diff
  2. import { createHandler, renderAsync, StartServer } from "solid-start/entry-server";

  3. - export default createHandler(renderAsync(context => <StartServer context={context} />));
  4. + export default createHandler(renderAsync(event => <StartServer event={event} />));
  5. ```

Why?


The prop received by StartServer, and given to you by createHandler is called event instead of context. It represents a PageEvent which is a FetchEvent that the server decided should be rendered by our components as a Page. We adopted the event terminology to represent the input that our server handlers received.

For example, the input to our top-level server handler is a FetchEvent. It can then be routed to a server function and be passed as a ServerFunctionEvent or to an API Endpoint as an ApiEvent. This terminology is adopted from the ServiceWorker API and Cloudflare Workers API.

entry-client.tsx


If you were using SSR:

  1. ```diff
  2. - import { hydrate } from "solid-js";
  3. - import { StartClient } from "solid-start/entry-client";
  4. + import { mount, StartClient } from "solid-start/entry-client";

  5. - hydrate(() => <StartClient />, document);
  6. + mount(() => <StartClient />, document);
  7. ```

If you were not using SSR and rendering your app client-side:

  1. ```diff
  2. - import { render } from "solid-js";
  3. - import { StartClient } from "solid-start/entry-client";
  4. + import { mount, StartClient } from "solid-start/entry-client";

  5. - render(() => <StartClient />, document.body);
  6. + mount(() => <StartClient />, document);
  7. ```

Why?


Earlier, you called hydrate(document) or render(document.body) here based on what kind of rendering mode you had selected and whether you had SSR turned on. We felt this was slightly annoying to change if you wanted to switch between the modes and error prone if you are not careful and end up passing document to render instead.

We still wanted to expose entry-client.tsx to the user so that they can take over and do their own thing here if they want. We made a helper function called mount that embeds the logic for deciding how to interact with the app we get from the server, be it hydrate or render.

root.tsx


  1. ```diff
  2. // @refresh reload
  3. import { Suspense } from "solid-js";
  4. - import { Meta, Link, Routes, Scripts } from "solid-start/root";
  5. + import { FileRoutes, Scripts, Html, Head, Body, Routes, Meta, ErrorBoundary, A } from "solid-start";

  6. export default function Root() {
  7.   return (
  8. -    <html lang="en">
  9. +    <Html lang="en">
  10. -      <head>
  11. +      <Head>

  12. -        <meta charset="utf-8" />
  13. +        <Meta charset="utf-8" />
  14. -        <meta name="viewport" content="width=device-width, initial-scale=1" />
  15. +        <Meta name="viewport" content="width=device-width, initial-scale=1" />

  16. -        <Meta /> // already exists inside `Head`
  17. -        <Links /> // already exists inside `Head`

  18. -      </head>
  19. +      </Head>
  20. -      <body>
  21. +      <Body>
  22.          <Suspense>
  23.            <ErrorBoundary>
  24.              <A href="/">Index</A>
  25.              <A href="/about">About</A>
  26. -            <Routes />
  27. +            <Routes>
  28. +              <FileRoutes />
  29. +            </Routes>
  30.            </ErrorBoundary>
  31.          </Suspense>
  32.          <Scripts />
  33. -     </body>
  34. +     </Body>
  35. -   </html>
  36. +   </Html>
  37.   );
  38. }
  39. ```

Why?


We changed how we declare our routes to make it more flexible. Earlier we gave you a Routes component from solid-start that was equivalent to rendering a Routes from @solidjs/router (yeah we know its confusing, that's why we are changing it) and filling it with the routes from the file system. The opt-in to the file-system routing was all-in or nothing.

You didn't have an opportunity to add more Routes. We now export FileRoutes from solid-start that represents the route config based on the file-system. It is meant to be passed to the Routes component from solid-start or wherever you want to use the file-system routes config.

- You can use it together with other Route components.

  1. ```tsx
  2. <Routes>
  3.   <FileRoutes />
  4.   <Route path="/somewhere" component={SomeComponent} />
  5. </Routes>
  6. ```

- Also for quickly starting an app without creating a bunch of files, you can define your routes in a single file. We generally don't recommend this since it's a good idea to code split your app along your routes, but its a neat trick.

  1. ```tsx
  2. <Routes>
  3.   <Route path="/somewhere" component={SomeComponent} />
  4. </Routes>
  5. ```

For consistency between the SSR and client-side rendering modes, we needed to take more control of `root.tsx` specifically, we couldn't just take `` and `` tags and allow them to be part of the component tree since we can't client-side render the whole document.

We only really get to take over document.body. We needed to ship with special Html, Head, and Body components that you use in root.tsx instead of the lower-case counterparts. These document flow components know what to do whether you are in SSR mode on or off.

- We can avoid you having to include Meta and Links from solid-start/root in your head since we do it by default.
- We will always use the title-case variants of the tags used in head (eg. Link > link, Style > style, Meta > meta) for consistency throughout the app.
- solid-meta is renamed to @solidjs/meta.
- solid-app-router is renamed to @solidjs/router.
- solid-start exports all the components meant to be used in your app and these components work on the client and server. Sometimes they are the same on both, and other times they coordinate between the two.

Now, our root.tsx even more closely replicates how you would be writing your index.html. And this was intentionally done so that we could enable an SPA mode for you that used the same code as the SSR mode without changing anything.

How do we do this? At build time for SPA mode, we quickly run the vite server and make a request for your app's index. We tell our Body component not to render anything.

So, the index.html we get is the one you would have written. We then use that index.html as your entrypoint. You can still write your own index.html if you don't want to use this functionality.

createServerResource -> createServerData$


Renamed createServerResource to createServerData$, and createRouteResource to createRouteData.

  1. ```diff
  2. export function routeData() {
  3. -  return createServerResource(async (_, { request }) => {
  4. +  return createServerData$(async (_, { request }) => {
  5.     const user = await getUser(request);

  6.     if (!user) {
  7.       throw redirect("/login");
  8.     }

  9.     return user;
  10.   });
  11. }
  12. ```

Why?


We renamed createServerResource to createServerData$ because we were not using the createResource signature. That was confusing so we needed to indicate the function was compiled. We just return one single signal from createServerData$ instead of a tuple like createResource does. And we have moved the source into the options as key.

createServerAction$, createServerMultiAction$


  1. ```diff
  2. - const logoutAction = createServerAction(() => logout(server.request));
  3. + const [logginOut, logOut] = createServerAction$((_, { request }) => logout(request));
  4. ```

Why?


We pass in a ServerFunctionEvent which has a request field as the second argument to server actions. You can use this to access to the HTTP Request sent for your action and get the headers from it for things like auth.

We now return a tuple where the first argument is the current submission, and the second is the submit function it also has a progressive enhanceable form attached to it logout.Form.

🆕 HttpStatusCode, HttpHeader


  1. ```tsx
  2. export default function NotFound() {
  3.   return (
  4.     <div>
  5.       <HttpStatusCode code={404} />
  6.       <HttpHeader name="my-header" value="header-value" />
  7.     </div>
  8.   );
  9. }
  10. ```


Credits


All credit for the work on Forms and Sessions goes to the @remix-run team, MIT License, Copyright 2021 Remix Software Inc.