# Migrating from SolidStart

These tables map SolidStart to Hamur: its files, its routing, its data and server functions, its API routes, its middleware, its environment variables and its config. The tables on pages, data and the server in [Migrating from Nuxt](/migrating-from-nuxt) apply to any app. The `Status` column says how close the Hamur way is: `same` (the name and the behaviour carry over), `different` (Hamur has the feature under another name or shape; the row says how) and `none` (Hamur has no equivalent today).

There is no `hamur migrate` command for SolidStart: the move is by hand, route by route, with the tables below. Keep `solid-js`. The components stay Solid components; what changes is where the data and the server code live.

The shape is one thing in Hamur: a [handler](/handlers), a typed function that runs on the server, exported from `server/index.ts`. It replaces `"use server"`, `query`, `action` and an API route at once. A `load` calls it, a component calls it (`useAction(fn)`, `useAsyncData(fn, input)`), a form posts to it (`Form action={fn}`), and with a `route` a URL answers it. It is a public endpoint that must check who calls it.

## Step by step

1. Install Hamur: `bun remove @solidjs/start @solidjs/router vinxi`, then `bun add hamur @hamur/adapter-solid`. Keep `solid-js`.
2. Write `hamur.config.ts` with `defineConfig({ framework: "solid" })` from `app.config.ts` ([config keys](#config-keys)).
3. Move `src/routes/` to `app/pages/`, `src/components/` to `app/components/` and `src/app.tsx` to `app/app.tsx` ([Files and folders](#files-and-folders)).
4. Turn each `query` and `action` into a handler, and each `createAsync` into a `load` ([Data and server functions](#data-and-server-functions)).
5. Move `src/routes/api/` to handlers with a `route`, and `src/middleware/` to `defineServer` ([API routes](#api-routes), [Middleware](#middleware)).
6. Run `hamur dev`, open each page, then `hamur build`.

## Files and folders

| SolidStart                                               | Hamur                                               | Status    | Differences                                                                                                                                                   |
| -------------------------------------------------------- | --------------------------------------------------- | --------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `app.config.ts`                                          | `hamur.config.ts`                                   | different | with `defineConfig` of `hamur` and `framework: "solid"`; the keys are [below](#config-keys)                                                                   |
| `@solidjs/start` and `@solidjs/router` in `package.json` | `hamur` and `@hamur/adapter-solid`                  | different | `bun remove @solidjs/start @solidjs/router vinxi`, then `bun add hamur @hamur/adapter-solid`; see [Adapters](/adapters)                                       |
| `src/app.tsx`                                            | `app/app.tsx`                                       | different | Hamur creates the router: the root renders `Outlet` where `Router` and `FileRoutes` were. See [Root component](/api-reference/routing-details#root-component) |
| `src/entry-server.tsx`                                   | none                                                | none      | Hamur writes the document; `meta.document` picks a template of `app/documents/` for a page. See [Page meta](/api-reference/routing-details#page-meta)         |
| `src/entry-client.tsx`                                   | an app plugin                                       | different | `app/plugins/*.client.ts` runs when the app starts in the browser. See [Plugins and hooks](/plugins-and-hooks#app-plugins)                                    |
| `src/routes/**`                                          | `app/pages/**`                                      | same      | one file per route; see [Files and routes](/api-reference/routing-details#files-and-routes)                                                                   |
| `src/routes/api/**`                                      | handlers with a `route`, in `server/`               | different | see [API routes](#api-routes)                                                                                                                                 |
| `src/middleware/index.ts`                                | `defineServer({ middleware })` in `server/index.ts` | different | see [Middleware](#middleware)                                                                                                                                 |
| `src/components/**`                                      | `app/components/**`                                 | same      | import a component by its path. See [Co-located components](/api-reference/routing-details#co-located-components)                                             |
| `src/lib/**`                                             | `app/lib/**` and `server/utils/**`                  | different | code the browser needs under `app/`, server code under `server/utils/`                                                                                        |
| `public/`                                                | `public/`                                           | same      | copied into the build as it is. See [Directory structure](/directory-structure)                                                                               |
| `.env`, `.env.local`                                     | `.env`, `.env.local`, `.env.<mode>`                 | different | the values are the variables of `app/env.ts`; see [Environment variables](#environment-variables) and [Modes](/environment#modes)                             |

## Routing, heading by heading

Each heading of the SolidStart routing page has a row, in its order. `{path}` is the folders under `app/pages/`.

| SolidStart                                           | Hamur                                                              | Status    | Differences                                                                                                                                                  |
| ---------------------------------------------------- | ------------------------------------------------------------------ | --------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| creating a route: `routes/about.tsx`                 | `app/pages/about.tsx`                                              | same      | `index.tsx` is `/`. See [Files and routes](/api-reference/routing-details#files-and-routes)                                                                  |
| `routes/blog/index.tsx`                              | `app/pages/blog/index.tsx`                                         | same      |                                                                                                                                                              |
| dynamic routes: `routes/users/[id].tsx`              | `app/pages/users/[id].tsx`                                         | same      | the param is `useRoute().params.id` where it was `useParams()`                                                                                               |
| optional params: `routes/users/[[id]].tsx`           | `app/pages/users/[[id]].tsx`                                       | same      |                                                                                                                                                              |
| catch-all routes: `routes/[...rest].tsx`             | `app/pages/[...rest].tsx`                                          | different | the param is a `string[]`, and `[...rest]` never matches an empty path; `[[...rest]]` does                                                                   |
| the not-found page `[...404].tsx`                    | `app/pages/[...slug].tsx`, or `app/pages/_not-found.tsx`           | different | it answers 200 unless the page throws `createError({ status: 404 })`; see [A URL no page matches](/errors#a-url-no-page-matches)                             |
| `<HttpStatusCode code={404} />`                      | `HttpStatusCode` from `@hamur/adapter-solid`                       | same      | renders nothing and sets the status during the server render                                                                                                 |
| nested routes: `routes/blog.tsx` with `routes/blog/` | `app/pages/blog.tsx` beside `app/pages/blog/`                      | different | the parent renders `Outlet` where it rendered `props.children`. See [Nested routes](/api-reference/routing-details#nested-routes)                            |
| renaming index: a file named for the folder          | `app/pages/{path}.tsx` beside the folder                           | different | a folder's own page is `index.tsx` or the parent page                                                                                                        |
| route groups: `routes/(auth)/login.tsx`              | `app/pages/(auth)/login.tsx`                                       | same      | a group adds nothing to the path. See [Pathless group layouts](/api-reference/routing-details#pathless-group-layouts)                                        |
| escaping special characters                          | `[x+HH]`, `[u+HHHH]` and `[.]` in a file name                      | different | see [Escapes](/api-reference/routing-details#escapes)                                                                                                        |
| route config: `export const route`                   | `export const meta`                                                | different | its keys are in the table below                                                                                                                              |
| layouts: a parent route with `props.children`        | `app/layouts/default.tsx`                                          | different | a layout renders `children`; a page picks another with `meta.layout`. See [Layouts](/api-reference/routing-details#layouts)                                  |
| the `A` component                                    | `Link` from `@hamur/adapter-solid`                                 | different | `to` is the path; the active class is `router-link-active`                                                                                                   |
| `useParams()`                                        | `useRoute().params`                                                | different | `useRoute` from `@hamur/adapter-solid`                                                                                                                       |
| `useLocation()`                                      | `useRoute()`                                                       | different | `path`, `params` and `query`                                                                                                                                 |
| `useNavigate()`                                      | `useRouter().navigate(to, { replace })`                            | different | `useRouter` from `@hamur/adapter-solid`; `navigateTo(to)` also works                                                                                         |
| `useSearchParams()`                                  | `useSearch()` from `@hamur/adapter-solid`                          | different | a page declares `export const search`, a Standard Schema, and reads it with `useSearch()`. See [Search params](/api-reference/routing-details#search-params) |
| `useIsRouting()`                                     | `useNavigation().state`                                            | different | `idle` or `loading`                                                                                                                                          |
| `useBeforeLeave()`                                   | `onBeforeRouteLeave(fn)` from `@hamur/adapter-solid`               | different | see [Leaving a page](/api-reference/navigation-details#leaving-a-page)                                                                                       |
| `<Navigate href>`                                    | `Navigate` from `@hamur/adapter-solid`                             | same      |                                                                                                                                                              |
| `redirect("/x")` from `@solidjs/router`              | `redirect("/x")` from `hamur/app`                                  | same      | return or throw it from a `load`, route middleware, a request step or a handler                                                                              |
| `useMatch()`                                         | `useMatches()` from `@hamur/adapter-solid`                         | different |                                                                                                                                                              |
| `clientOnly(() => import("./x"))`                    | `ClientOnly` from `@hamur/adapter-solid`, or a `x.client.tsx` file | different | see [`ClientOnly`](/api-reference/adapters#clientonly)                                                                                                       |
| `<Title>`, `<Meta>`, `<Link>` of `@solidjs/meta`     | `useHead` from `@hamur/adapter-solid`                              | different | one call for the whole head; see [Head](/head)                                                                                                               |

The keys of `export const route` become the keys of `meta`:

| SolidStart                                 | Hamur                             | Status    | Differences                                                                                                                                  |
| ------------------------------------------ | --------------------------------- | --------- | -------------------------------------------------------------------------------------------------------------------------------------------- |
| `route.preload`                            | `load` in `x.load.ts`             | different | it runs before the page chunk arrives, and `Link` runs it on hover and focus. See [Data](/data#xloadts)                                      |
| `route.matchFilters`                       | `app/params/{name}.ts`            | different | a param is `[id=integer]` and the matcher is a file with `match(param)`. See [Param matchers](/api-reference/routing-details#param-matchers) |
| `export const prerender = true` of a route | `meta.rules: { prerender: true }` | different | a route rule for this page's path                                                                                                            |
| the layout of a route                      | `meta.layout`                     | different | a name, or `false` for none                                                                                                                  |

## Data and server functions

A server function is a handler. There is no `"use server"` directive or compiler split: a handler is a module-level `defineHandler` export of `server/index.ts`, and the build writes the browser stub.

````ts [migrate-solidstart/server/posts.ts]
import { createError, defineHandler } from "hamur/server"
import { schema } from "../../migrate-next/server/schema.ts"

const posts = new Map([["1", { id: "1", title: "Hello" }]])

const idInput = schema<{ id: string }>(
  { type: "object", properties: { id: { type: "string" } }, required: ["id"] },
  (value) => {
    const id = (value as { id?: unknown } | undefined)?.id
    return typeof id === "string" && id
      ? { value: { id } }
      : { issues: [{ message: "An id is required", path: ["id"] }] }
  },
)

const titleInput = schema<{ title: string }>(
  { type: "object", properties: { title: { type: "string" } }, required: ["title"] },
  (value) => {
    const title = (value as { title?: unknown } | undefined)?.title
    return typeof title === "string" && title.trim()
      ? { value: { title: title.trim() } }
      : { issues: [{ message: "A title is required", path: ["title"] }] }
  },
)

export const getPost = defineHandler({
  method: "GET",
  input: idInput,
  handler({ id }) {
    const post = posts.get(id)
    if (!post) throw createError({ status: 404, message: "Post not found" })
    return post
  },
})

export const addPost = defineHandler({
  input: titleInput,
  handler({ title }, { event }) {
    if (!event.context.user) throw createError({ status: 401, message: "Log in first" })
    const post = { id: String(posts.size + 1), title }
    posts.set(post.id, post)
    return post
  },
})

export const health = defineHandler({
  route: "/api/health",
  method: "GET",
  raw: true,
  handler: () => Response.json({ ok: true }),
})
````

`createAsync(() => getPost(id))` is a `load` that calls the handler. A page reads the value with `useData()`, which is an accessor in Solid.

````ts [migrate-solidstart/app/pages/posts/[id].load.ts]
import type { Load } from "hamur/app"
import { getPost } from "../../../server/index.ts"

export const load = (async ({ params }) => ({ post: await getPost({ id: params.id as string }) })) satisfies Load
````

| SolidStart                                                         | Hamur                                                                       | Status    | Differences                                                                                                                         |
| ------------------------------------------------------------------ | --------------------------------------------------------------------------- | --------- | ----------------------------------------------------------------------------------------------------------------------------------- |
| `"use server"` in a function                                       | `defineHandler({ handler })` from `hamur/server`                            | different | exported from `server/index.ts`; it is a public endpoint. See [Defining a handler](/handlers#defining-a-handler)                    |
| `query(fn, "posts")`                                               | `defineHandler({ method: "GET", input, handler })`                          | different | a `GET` handler is cacheable; add `cache: { maxAge }`. See [Cache and batches](/handlers#cache-and-batches)                         |
| `createAsync(() => getPost(id))`                                   | `useData()` of the page's `load`, or `useAsyncData(fn, input)`              | different | both from `@hamur/adapter-solid`; the value is an accessor                                                                          |
| `preload` that calls a query                                       | `load` that calls the handler                                               | different | the handler is the same function the component calls                                                                                |
| `action(fn, "addPost")`                                            | `defineHandler({ input, handler })`                                         | different | a `POST`; the name is the export                                                                                                    |
| `useAction(addPost)`                                               | `useAction(fn)` from `@hamur/adapter-solid`                                 | different | `state`, `failure`, `error`, `pending` and `run(input)`                                                                             |
| `useSubmission(addPost)`, `useSubmissions`                         | `useAction(fn)`, `useFormStatus()`                                          | different | `pending` and `result` of `useAction`; `useFormStatus` is for a child of a form                                                     |
| `<form action={addPost} method="post">`                            | `<Form action={fn}>` from `@hamur/adapter-solid`                            | different | works without JavaScript. See [Forms and login](/forms-and-login)                                                                   |
| `revalidate` keys of an action                                     | `refresh(event, getPost, input)` in the handler, or `useRouter().refresh()` | different | the answer carries the value of the read, and the client's next read sends nothing                                                  |
| a thrown `redirect()` in a server function                         | `return redirect(to)` from `hamur/app`                                      | same      | see [Errors and redirects on the client](/handlers#errors-and-redirects-on-the-client)                                              |
| `getRequestEvent()`                                                | `useEvent()` of `hamur/server`, or `ctx.event`                              | different | `useEvent()` works where no `event` is passed down; see [The request, anywhere](/api-reference/server-details#the-request-anywhere) |
| `getCookie`, `setCookie`, `getHeader`, `setHeader` of `vinxi/http` | `getCookie`, `setCookie`, `getRequestHeader`, `setHeader` of `hamur/server` | different | each takes the `event`                                                                                                              |
| `useSession` of `vinxi/http`                                       | `getUserSession` and `setUserSession` of `@hamur/auth`                      | different | see [Auth](/auth) and [Sessions and security](/sessions-and-security)                                                               |
| `cache`, `createResource`                                          | `useAsyncData(fn, input)`                                                   | different | the client cache is `meta.staleTime`. See [The client data cache](/data#the-client-data-cache)                                      |
| a `Suspense` boundary around `createAsync`                         | `Await` and `useDeferred` from `@hamur/adapter-solid`                       | different | see [Deferred values](/islands-and-streaming#deferred-values)                                                                       |

## API routes

An API route is a file of `src/routes/api/` that exports `GET`, `POST` and the other methods. A handler with a `route` and `raw: true` answers a URL: one handler for each method, exported from `server/index.ts`. The `APIEvent` is `event`, and the answer is a `Response`.

| SolidStart                             | Hamur                                                         | Status    | Differences                                                                                   |
| -------------------------------------- | ------------------------------------------------------------- | --------- | --------------------------------------------------------------------------------------------- |
| `export function GET(event: APIEvent)` | `defineHandler({ route, method: "GET", raw: true, handler })` | different | the second argument has `event`. See [A handler with a route](/server#a-handler-with-a-route) |
| `event.params.id`                      | `event.params.id`                                             | same      | the `route` uses `:id` where the file used `[id]`                                             |
| `event.request`                        | `event.request`                                               | same      |                                                                                               |
| `json(data)` from `@solidjs/router`    | `Response.json(data)`                                         | different | a plain `Response`                                                                            |
| `export const POST` with a body        | `await event.request.json()` or `formData()` in a raw handler | different | or `input` of a plain handler, validated                                                      |
| `export const ALL`                     | none                                                          | none      | write a handler for each method                                                               |
| `event.locals`                         | `event.context`                                               | different | see [Middleware](#middleware)                                                                 |

## Middleware

`createMiddleware({ onRequest, onBeforeResponse })` becomes the `middleware` list of `defineServer`. The list runs before every request, in the order written. `onBeforeResponse` is the code after `await next()`.

````ts [migrate-solidstart/server/middleware.ts]
import { defineMiddleware, getCookie } from "hamur/server"

export const onRequest = defineMiddleware(({ event }) => {
  event.context.user = getCookie(event, "session") ?? null
})

export const onBeforeResponse = defineMiddleware(async ({ event, next }) => {
  const answer = await next()
  event.response.headers.set("x-served-by", "hamur")
  return answer
})
````

````ts [migrate-solidstart/server/index.ts]
import { defineServer } from "hamur/server"
import { onBeforeResponse, onRequest } from "./middleware.ts"

export { addPost, getPost, health } from "./posts.ts"

export default defineServer({
  middleware: [onRequest, onBeforeResponse],
})
````

| SolidStart                        | Hamur                                               | Status    | Differences                                                                                                                |
| --------------------------------- | --------------------------------------------------- | --------- | -------------------------------------------------------------------------------------------------------------------------- |
| `createMiddleware({ onRequest })` | a step of `middleware` in `defineServer`            | different | `defineMiddleware(({ event, next }) => …)` from `hamur/server`. See [Middleware](/server#middleware)                       |
| `onBeforeResponse`                | the code after `await next()`                       | different | it sees the answer, sets `event.response.headers` and may return a `Response`                                              |
| `onRequest: [a, b]`               | `middleware: [a, b]`                                | same      | outermost first                                                                                                            |
| `event.locals.user = …`           | `event.context.user = …`                            | different | a handler and a `load` read it; type the keys with `HamurEventContext`                                                     |
| a check on the path               | `{ route: "/admin/**", handler }`                   | different | there is no `matcher`; a list entry with a `route` runs only under it                                                      |
| `return redirect("/login")`       | `throw redirect("/login")` from `hamur/app`         | different | a thrown or returned `redirect()` redirects                                                                                |
| a guard of a server function      | `handlerMiddleware`, or `middleware` of the handler | different | a request step guards the document; the handler's list guards every call. See [Handlers: middleware](/handlers#middleware) |

## Environment variables

| SolidStart                                    | Hamur                                          | Status    | Differences                                                                                   |
| --------------------------------------------- | ---------------------------------------------- | --------- | --------------------------------------------------------------------------------------------- |
| `process.env.API_KEY` on the server           | `import { API_KEY } from "#hamur/env/private"` | different | an entry of `defineEnv` in `app/env.ts`; typed, validated at start, never sent to the browser |
| `import.meta.env.VITE_GA_ID`                  | `import { GA_ID } from "#hamur/env/public"`    | different | the entry has `public: true`; there is no `VITE_` prefix. See [Environment](/environment)     |
| `import.meta.env.MODE`, `import.meta.env.DEV` | `import.meta.env.MODE`, `import.meta.dev`      | different | constants of the build                                                                        |
| `import.meta.env.SSR`                         | `import.meta.server`                           | different | `import.meta.client` is the other side                                                        |

## Config keys

| SolidStart                          | Hamur                                                       | Status    | Differences                                                                                |
| ----------------------------------- | ----------------------------------------------------------- | --------- | ------------------------------------------------------------------------------------------ |
| `server.preset`                     | `preset`                                                    | different | the build target is a preset: `bun`, `node`, `cloudflare`, `vercel`, `netlify` or `static` |
| `server.prerender.routes`           | `prerender.routes`                                          | same      | see [Configuration](/configuration)                                                        |
| `ssr: false`                        | a route rule `"/**": { ssr: false }`, or `meta.ssr`         | different | a single-page app renders in the browser only. See [Rendering modes](/rendering-modes)     |
| `middleware: "./src/middleware.ts"` | `server/index.ts`                                           | different | the default export is `defineServer({ middleware })`                                       |
| `vite`                              | `modules` with `fromVite(plugin)` from `@hamur/vite-compat` | different | Hamur builds with Bun; a Vite plugin is wrapped. See [Vite plugins](/vite-plugins)         |
| `server.routeRules`                 | `routeRules`                                                | same      | see [Route rules](/route-rules)                                                            |
| `extensions`                        | none                                                        | none      | a page is a file of the adapter's extension, `.tsx` for Solid                              |

## What Hamur lacks

- `entry-server.tsx` and `entry-client.tsx`: the document and the mount are Hamur's. A page that needs its own document uses `app/documents/<name>.html`; code that runs at start goes in an app plugin.
- `export const ALL` in an API route, and a `matcher` of a middleware list entry: write a handler for each method and use `{ route, handler }`.
- `createResource` and `cache` as separate primitives: `useAsyncData` and a `load` cover them.
- Files that mix a route and its server code in one module: Hamur keeps the page, its `load` and the handler in separate files, so the build can drop the server code from the browser bundle.
