Skip to content

Commit b3e1801

Browse files
authored
for --template file-router --framework solid add --add-on start (#74)
1 parent 97752da commit b3e1801

File tree

11 files changed

+149
-2
lines changed

11 files changed

+149
-2
lines changed

templates/solid/add-on/form/assets/src/routes/demo.form.tsx.ejs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import type { JSX } from 'solid-js/jsx-runtime'
55
import type { ValidationError } from '@tanstack/solid-form'
66

77
<% if (codeRouter) { %>
8-
import type { RootRoute } from '@tanstack/react-router'
8+
import type { RootRoute } from '@tanstack/solid-router'
99
<% } else { %>
1010
export const Route = createFileRoute('/demo/form')({
1111
component: FormExample,
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { defineConfig } from '@tanstack/solid-start/config'
2+
import viteTsConfigPaths from 'vite-tsconfig-paths'
3+
4+
export default defineConfig({
5+
tsr: {
6+
appDirectory: 'src',
7+
},
8+
vite: {
9+
plugins: [
10+
// this is the plugin that enables path aliases
11+
viteTsConfigPaths({
12+
projects: ['./tsconfig.json'],
13+
}),
14+
],
15+
},
16+
})
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export default {
2+
plugins: {
3+
"@tailwindcss/postcss": {},
4+
},
5+
};
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import {
2+
createStartAPIHandler,
3+
defaultAPIFileRouteHandler,
4+
} from '@tanstack/solid-start/api'
5+
6+
export default createStartAPIHandler(defaultAPIFileRouteHandler)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { hydrate } from 'solid-js/web'
2+
import { StartClient } from '@tanstack/solid-start'
3+
import { createRouter } from './router'
4+
5+
const router = createRouter()
6+
7+
hydrate(() => <StartClient router={router} />, document.body)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { createRouter as createTanstackRouter } from '@tanstack/solid-router'
2+
3+
// Import the generated route tree
4+
import { routeTree } from './routeTree.gen'
5+
6+
import './styles.css'
7+
8+
// Create a new router instance
9+
export const createRouter = () => {
10+
const router = createTanstackRouter({
11+
routeTree,
12+
scrollRestoration: true,
13+
})
14+
return router
15+
}
16+
17+
const router = createRouter()
18+
19+
// Register the router instance for type safety
20+
declare module '@tanstack/solid-router' {
21+
interface Register {
22+
router: typeof router
23+
}
24+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import * as fs from 'fs'
2+
import { createFileRoute, useRouter } from '@tanstack/solid-router'
3+
import { createServerFn } from '@tanstack/solid-start'
4+
5+
const filePath = 'count.txt'
6+
7+
async function readCount() {
8+
return parseInt(
9+
await fs.promises.readFile(filePath, 'utf-8').catch(() => '0'),
10+
)
11+
}
12+
13+
const getCount = createServerFn({
14+
method: 'GET',
15+
}).handler(() => {
16+
return readCount()
17+
})
18+
19+
const updateCount = createServerFn({ method: 'POST' })
20+
.validator((d: number) => d)
21+
.handler(async ({ data }) => {
22+
const count = await readCount()
23+
await fs.promises.writeFile(filePath, `${count + data}`)
24+
})
25+
26+
export const Route = createFileRoute('/demo/start/server-funcs')({
27+
component: Home,
28+
loader: async () => await getCount(),
29+
})
30+
31+
function Home() {
32+
const router = useRouter()
33+
const state = Route.useLoaderData()
34+
35+
return (
36+
<div class="p-4">
37+
<button
38+
onClick={() => {
39+
updateCount({ data: 1 }).then(() => {
40+
router.invalidate()
41+
})
42+
}}
43+
claclassssName="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded"
44+
>
45+
Add 1 to {state}?
46+
</button>
47+
</div>
48+
)
49+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import {
2+
createStartHandler,
3+
defaultStreamHandler,
4+
} from '@tanstack/solid-start/server'
5+
import { getRouterManifest } from '@tanstack/solid-start/router-manifest'
6+
7+
import { createRouter } from './router'
8+
9+
export default createStartHandler({
10+
createRouter,
11+
getRouterManifest,
12+
})(defaultStreamHandler)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "Start",
3+
"phase": "setup",
4+
"description": "Add TanStack Start for SSR, API endpoints, and more.",
5+
"link": "https://tanstack.com/start/latest",
6+
"templates": ["file-router"],
7+
"warning": "TanStack Start is not yet at 1.0 and may change significantly or not be compatible with other add-ons.",
8+
"routes": [
9+
{
10+
"url": "/demo/start/server-funcs",
11+
"name": "Start - Server Functions"
12+
}
13+
]
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"scripts": {
3+
"dev": "vinxi dev",
4+
"build": "vinxi build",
5+
"start": "vinxi start"
6+
},
7+
"dependencies": {
8+
"@tailwindcss/postcss": "^4.0.7",
9+
"@tanstack/solid-start": "^1.114.3",
10+
"postcss": "^8.5.2",
11+
"vinxi": "^0.5.3",
12+
"vite-tsconfig-paths": "^5.1.4"
13+
}
14+
}

0 commit comments

Comments
 (0)