create-thexjs-app 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +1026 -0
- package/dist/index.js +224 -0
- package/package.json +30 -0
- package/templates/basic/components.json +21 -0
- package/templates/basic/content/blog/hello.md +8 -0
- package/templates/basic/public/favicon.ico +0 -0
- package/templates/basic/src/actions/dashboard/greet.ts +3 -0
- package/templates/basic/src/api/auth/login.ts +30 -0
- package/templates/basic/src/api/auth/logout.ts +12 -0
- package/templates/basic/src/api/placeholder/comments.ts +17 -0
- package/templates/basic/src/api/placeholder/posts.ts +20 -0
- package/templates/basic/src/api/placeholder/todos.ts +21 -0
- package/templates/basic/src/api/placeholder/users.ts +12 -0
- package/templates/basic/src/components/Head.tsx +21 -0
- package/templates/basic/src/components/Image.tsx +27 -0
- package/templates/basic/src/components/Link.tsx +16 -0
- package/templates/basic/src/components/ui/badge.tsx +49 -0
- package/templates/basic/src/components/ui/button.tsx +58 -0
- package/templates/basic/src/components/ui/card.tsx +88 -0
- package/templates/basic/src/components/ui/input.tsx +20 -0
- package/templates/basic/src/layouts/main.tsx +38 -0
- package/templates/basic/src/lib/auth.ts +70 -0
- package/templates/basic/src/lib/migrations/001_create_sessions.sql +9 -0
- package/templates/basic/src/lib/placeholder-data.ts +176 -0
- package/templates/basic/src/lib/utils.ts +6 -0
- package/templates/basic/src/pages/_404.tsx +17 -0
- package/templates/basic/src/pages/about.tsx +22 -0
- package/templates/basic/src/pages/blog.tsx +41 -0
- package/templates/basic/src/pages/dashboard/_middleware.ts +16 -0
- package/templates/basic/src/pages/dashboard/index.tsx +84 -0
- package/templates/basic/src/pages/index.tsx +54 -0
- package/templates/basic/src/pages/login.tsx +85 -0
- package/templates/basic/src/pages/placeholder.tsx +194 -0
- package/templates/basic/src/pages/posts/[id].tsx +17 -0
- package/templates/basic/src/styles/globals.css +90 -0
- package/templates/basic/src/x-routes.ts +29 -0
- package/templates/basic/tsconfig.json +11 -0
- package/templates/basic/x.config.ts +10 -0
- package/templates/blog/components.json +20 -0
- package/templates/blog/content/posts/getting-started.md +38 -0
- package/templates/blog/content/posts/hello-world.md +21 -0
- package/templates/blog/content/posts/tailwind-v4.md +31 -0
- package/templates/blog/public/favicon.ico +0 -0
- package/templates/blog/src/components/ui/avatar.tsx +91 -0
- package/templates/blog/src/components/ui/badge.tsx +49 -0
- package/templates/blog/src/components/ui/button.tsx +58 -0
- package/templates/blog/src/components/ui/card.tsx +88 -0
- package/templates/blog/src/components/ui/input.tsx +20 -0
- package/templates/blog/src/layouts/blog.tsx +46 -0
- package/templates/blog/src/layouts/main.tsx +32 -0
- package/templates/blog/src/lib/utils.ts +5 -0
- package/templates/blog/src/pages/about.tsx +36 -0
- package/templates/blog/src/pages/blog/[slug].tsx +56 -0
- package/templates/blog/src/pages/blog/index.tsx +70 -0
- package/templates/blog/src/pages/index.tsx +70 -0
- package/templates/blog/src/styles/globals.css +90 -0
- package/templates/blog/src/x-routes.ts +20 -0
- package/templates/blog/tsconfig.json +5 -0
- package/templates/blog/x.config.ts +6 -0
- package/templates/landing/components.json +20 -0
- package/templates/landing/public/favicon.ico +0 -0
- package/templates/landing/src/components/code-block.tsx +24 -0
- package/templates/landing/src/components/ui/avatar.tsx +91 -0
- package/templates/landing/src/components/ui/badge.tsx +49 -0
- package/templates/landing/src/components/ui/button.tsx +58 -0
- package/templates/landing/src/components/ui/card.tsx +88 -0
- package/templates/landing/src/components/ui/input.tsx +20 -0
- package/templates/landing/src/layouts/main.tsx +48 -0
- package/templates/landing/src/lib/syntax.tsx +366 -0
- package/templates/landing/src/lib/utils.ts +5 -0
- package/templates/landing/src/pages/contact.tsx +95 -0
- package/templates/landing/src/pages/docs/_layout.tsx +109 -0
- package/templates/landing/src/pages/docs/api-routes.tsx +113 -0
- package/templates/landing/src/pages/docs/build-deploy.tsx +124 -0
- package/templates/landing/src/pages/docs/configuration.tsx +121 -0
- package/templates/landing/src/pages/docs/content-collections.tsx +130 -0
- package/templates/landing/src/pages/docs/data-layer.tsx +142 -0
- package/templates/landing/src/pages/docs/getting-started.tsx +119 -0
- package/templates/landing/src/pages/docs/index.tsx +123 -0
- package/templates/landing/src/pages/docs/layouts.tsx +114 -0
- package/templates/landing/src/pages/docs/middleware.tsx +124 -0
- package/templates/landing/src/pages/docs/pages.tsx +135 -0
- package/templates/landing/src/pages/docs/routing.tsx +123 -0
- package/templates/landing/src/pages/docs/server-functions.tsx +123 -0
- package/templates/landing/src/pages/features.tsx +129 -0
- package/templates/landing/src/pages/index.tsx +175 -0
- package/templates/landing/src/styles/globals.css +100 -0
- package/templates/landing/src/x-routes.ts +31 -0
- package/templates/landing/tsconfig.json +5 -0
- package/templates/landing/x.config.ts +2 -0
- package/templates/saas/components.json +20 -0
- package/templates/saas/public/favicon.ico +0 -0
- package/templates/saas/src/actions/dashboard.ts +3 -0
- package/templates/saas/src/components/ui/avatar.tsx +91 -0
- package/templates/saas/src/components/ui/badge.tsx +49 -0
- package/templates/saas/src/components/ui/button.tsx +58 -0
- package/templates/saas/src/components/ui/card.tsx +88 -0
- package/templates/saas/src/components/ui/input.tsx +20 -0
- package/templates/saas/src/layouts/dashboard.tsx +67 -0
- package/templates/saas/src/layouts/main.tsx +38 -0
- package/templates/saas/src/lib/auth.ts +70 -0
- package/templates/saas/src/lib/migrations/001_create_sessions.sql +9 -0
- package/templates/saas/src/lib/utils.ts +5 -0
- package/templates/saas/src/pages/dashboard/_middleware.ts +9 -0
- package/templates/saas/src/pages/dashboard/analytics.tsx +68 -0
- package/templates/saas/src/pages/dashboard/index.tsx +86 -0
- package/templates/saas/src/pages/dashboard/settings.tsx +105 -0
- package/templates/saas/src/pages/index.tsx +110 -0
- package/templates/saas/src/pages/login.tsx +67 -0
- package/templates/saas/src/pages/pricing.tsx +121 -0
- package/templates/saas/src/styles/globals.css +90 -0
- package/templates/saas/src/x-routes.ts +22 -0
- package/templates/saas/tsconfig.json +5 -0
- package/templates/saas/x.config.ts +7 -0
package/README.md
ADDED
|
@@ -0,0 +1,1026 @@
|
|
|
1
|
+
<p align="center">
|
|
2
|
+
<a href="#">
|
|
3
|
+
<img src="./examples/landing/public/favicon.ico" alt="x framework logo" width="120" height="120">
|
|
4
|
+
</a>
|
|
5
|
+
</p>
|
|
6
|
+
|
|
7
|
+
<p align="center">
|
|
8
|
+
|
|
9
|
+
<strong>The ultra-fast, full-stack React framework built natively for Bun.</strong>
|
|
10
|
+
Static sites, SSR, file-based API routes, and server functions — running in a single process.
|
|
11
|
+
|
|
12
|
+
</p>
|
|
13
|
+
|
|
14
|
+
<p align="center">
|
|
15
|
+
<a href="#quick-start"><img src="https://img.shields.io/badge/Bun-1.0+-black?style=flat-square&logo=bun" alt="Bun Ready"></a>
|
|
16
|
+
<a href="#features"><img src="https://img.shields.io/badge/React-19-blue?style=flat-square&logo=react" alt="React 19"></a>
|
|
17
|
+
<a href="#features"><img src="https://img.shields.io/badge/TypeScript-Strict-blue?style=flat-square&logo=typescript" alt="TypeScript"></a>
|
|
18
|
+
<a href="./TASKS.md"><img src="https://img.shields.io/badge/Status-Active_Development-brightgreen?style=flat-square" alt="Status"></a>
|
|
19
|
+
</p>
|
|
20
|
+
|
|
21
|
+
<p align="center">
|
|
22
|
+
<a href="#getting-started">Getting Started</a> •
|
|
23
|
+
<a href="#routing">Routing</a> •
|
|
24
|
+
<a href="#pages--loaders">Pages</a> •
|
|
25
|
+
<a href="#layouts">Layouts</a> •
|
|
26
|
+
<a href="#api-routes">API Routes</a> •
|
|
27
|
+
<a href="#server-functions">Server Functions</a> •
|
|
28
|
+
<a href="#content-collections">Content</a> •
|
|
29
|
+
<a href="#middleware">Middleware</a> •
|
|
30
|
+
<a href="#data-layer">Data</a> •
|
|
31
|
+
<a href="#build--deploy">Deploy</a>
|
|
32
|
+
</p>
|
|
33
|
+
|
|
34
|
+
## Layout
|
|
35
|
+
|
|
36
|
+
```
|
|
37
|
+
packages/core framework runtime: file-based router, SSR renderer, middleware
|
|
38
|
+
packages/cli x dev / x build / x start
|
|
39
|
+
packages/env type-safe environment variable validation
|
|
40
|
+
examples/basic basic app exercising pages, API routes, server functions
|
|
41
|
+
examples/landing full marketing site with docs, blog (x's own docs site)
|
|
42
|
+
examples/blog blog example with content collections
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Getting Started
|
|
46
|
+
|
|
47
|
+
Create a new x project, learn the project structure, and build your first page.
|
|
48
|
+
|
|
49
|
+
### Installation
|
|
50
|
+
|
|
51
|
+
The fastest way to start is with the create-x CLI. Make sure you have [Bun](https://bun.sh) installed, then run:
|
|
52
|
+
|
|
53
|
+
```bash
|
|
54
|
+
bun create x-app@latest my-app
|
|
55
|
+
cd my-app
|
|
56
|
+
x dev
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
Your app will be running at `http://localhost:3000`.
|
|
60
|
+
|
|
61
|
+
### Manual setup
|
|
62
|
+
|
|
63
|
+
If you prefer to set up manually, create a directory and add x:
|
|
64
|
+
|
|
65
|
+
```bash
|
|
66
|
+
mkdir my-app && cd my-app
|
|
67
|
+
bun init -y
|
|
68
|
+
bun add @thexjs/core
|
|
69
|
+
cat << EOF > x.config.ts
|
|
70
|
+
import { defineConfig } from "@thexjs/core";
|
|
71
|
+
export default defineConfig({
|
|
72
|
+
pagesDir: "src/pages",
|
|
73
|
+
});
|
|
74
|
+
EOF
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
### Project structure
|
|
78
|
+
|
|
79
|
+
A typical x project looks like this:
|
|
80
|
+
|
|
81
|
+
```
|
|
82
|
+
my-app/
|
|
83
|
+
x.config.ts
|
|
84
|
+
src/
|
|
85
|
+
pages/ # File-based routes
|
|
86
|
+
index.tsx
|
|
87
|
+
about.tsx
|
|
88
|
+
_404.tsx
|
|
89
|
+
blog/
|
|
90
|
+
[slug].tsx
|
|
91
|
+
layouts/ # Nested layouts
|
|
92
|
+
main.tsx
|
|
93
|
+
api/ # API routes
|
|
94
|
+
hello.ts
|
|
95
|
+
actions/ # Server functions
|
|
96
|
+
greet.ts
|
|
97
|
+
content/ # Markdown content
|
|
98
|
+
posts/
|
|
99
|
+
hello-world.md
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
### Your first page
|
|
103
|
+
|
|
104
|
+
Create `src/pages/index.tsx` with a simple component:
|
|
105
|
+
|
|
106
|
+
```tsx
|
|
107
|
+
export default function Home() {
|
|
108
|
+
return (
|
|
109
|
+
<div>
|
|
110
|
+
<h1 className="text-4xl font-bold">Hello x!</h1>
|
|
111
|
+
<p className="text-muted-foreground">Welcome to your new app.</p>
|
|
112
|
+
</div>
|
|
113
|
+
);
|
|
114
|
+
}
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
### Running the dev server
|
|
118
|
+
|
|
119
|
+
Start the development server with hot reload:
|
|
120
|
+
|
|
121
|
+
```bash
|
|
122
|
+
x dev
|
|
123
|
+
[x] resolving routes...
|
|
124
|
+
[x] found 3 routes in 12ms
|
|
125
|
+
[x] dev server running at http://localhost:3000
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
The dev server watches your `src/` directory and automatically reloads when files change.
|
|
129
|
+
|
|
130
|
+
---
|
|
131
|
+
|
|
132
|
+
## Routing
|
|
133
|
+
|
|
134
|
+
x uses the file system as your route table. Drop a file in `src/pages/`, get a route.
|
|
135
|
+
|
|
136
|
+
### How it works
|
|
137
|
+
|
|
138
|
+
Every `.tsx` file in your pages directory becomes a route. The file path determines the URL pattern.
|
|
139
|
+
|
|
140
|
+
```
|
|
141
|
+
pages/index.tsx -> /
|
|
142
|
+
pages/about.tsx -> /about
|
|
143
|
+
pages/contact.tsx -> /contact
|
|
144
|
+
pages/blog/index.tsx -> /blog
|
|
145
|
+
pages/blog/[slug].tsx -> /blog/:slug
|
|
146
|
+
pages/dashboard/
|
|
147
|
+
settings.tsx -> /dashboard/settings
|
|
148
|
+
profile.tsx -> /dashboard/profile
|
|
149
|
+
pages/_404.tsx -> catch-all 404
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
### Static routes
|
|
153
|
+
|
|
154
|
+
Simple files map to exact URL paths. `pages/about.tsx` becomes `/about`.
|
|
155
|
+
|
|
156
|
+
```tsx
|
|
157
|
+
export default function About() {
|
|
158
|
+
return <h1 className="text-3xl font-bold">About us</h1>;
|
|
159
|
+
}
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
### Dynamic segments
|
|
163
|
+
|
|
164
|
+
Wrap a filename in square brackets to create a dynamic segment. The value is available via the `params` object in loaders.
|
|
165
|
+
|
|
166
|
+
```tsx
|
|
167
|
+
import type { RouteProps, LoaderArgs } from "@thexjs/core";
|
|
168
|
+
|
|
169
|
+
export async function loader({ params }: LoaderArgs) {
|
|
170
|
+
const post = await getPost(params.slug);
|
|
171
|
+
return { title: post.title, content: post.content };
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
export default function BlogPost({ loaderData }: RouteProps<typeof loader>) {
|
|
175
|
+
return (
|
|
176
|
+
<article>
|
|
177
|
+
<h1 className="text-3xl font-bold">{loaderData.title}</h1>
|
|
178
|
+
<div>{loaderData.content}</div>
|
|
179
|
+
</article>
|
|
180
|
+
);
|
|
181
|
+
}
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
Multiple dynamic segments work too: `pages/product/[category]/[id].tsx` → `/product/:category/:id`.
|
|
185
|
+
|
|
186
|
+
### Nested routes with folders
|
|
187
|
+
|
|
188
|
+
Organize routes in folders for nested URL structures. Each folder can have its own `index.tsx`.
|
|
189
|
+
|
|
190
|
+
```
|
|
191
|
+
pages/dashboard/
|
|
192
|
+
index.tsx -> /dashboard
|
|
193
|
+
settings.tsx -> /dashboard/settings
|
|
194
|
+
profile.tsx -> /dashboard/profile
|
|
195
|
+
billing/
|
|
196
|
+
index.tsx -> /dashboard/billing
|
|
197
|
+
history.tsx -> /dashboard/billing/history
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
### Catch-all 404 page
|
|
201
|
+
|
|
202
|
+
Create `pages/_404.tsx` to show a custom not-found page for unmatched routes.
|
|
203
|
+
|
|
204
|
+
```tsx
|
|
205
|
+
export default function NotFound() {
|
|
206
|
+
return (
|
|
207
|
+
<div className="text-center py-20">
|
|
208
|
+
<h1 className="text-6xl font-bold text-muted-foreground">404</h1>
|
|
209
|
+
<p className="mt-4 text-lg text-muted-foreground">Page not found</p>
|
|
210
|
+
<a href="/" className="mt-6 inline-block text-primary hover:underline">
|
|
211
|
+
Go home
|
|
212
|
+
</a>
|
|
213
|
+
</div>
|
|
214
|
+
);
|
|
215
|
+
}
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
---
|
|
219
|
+
|
|
220
|
+
## Pages & Loaders
|
|
221
|
+
|
|
222
|
+
x supports two page modes — static prerendering and server-side rendering — both powered by loaders.
|
|
223
|
+
|
|
224
|
+
### Page modes
|
|
225
|
+
|
|
226
|
+
By default, pages are server-rendered (SSR). Export `mode = "static"` to prerender at build time.
|
|
227
|
+
|
|
228
|
+
### Static pages
|
|
229
|
+
|
|
230
|
+
Static pages are rendered at build time and exported as HTML. Use this for marketing pages, blog posts, or any content that doesn't need per-request rendering.
|
|
231
|
+
|
|
232
|
+
```tsx
|
|
233
|
+
import type { RouteProps } from "@thexjs/core";
|
|
234
|
+
|
|
235
|
+
export const mode = "static";
|
|
236
|
+
|
|
237
|
+
export default function About({}: RouteProps) {
|
|
238
|
+
return (
|
|
239
|
+
<div className="max-w-2xl mx-auto py-12">
|
|
240
|
+
<h1 className="text-4xl font-bold">About</h1>
|
|
241
|
+
<p className="mt-4 text-muted-foreground">
|
|
242
|
+
This page is prerendered at build time.
|
|
243
|
+
</p>
|
|
244
|
+
</div>
|
|
245
|
+
);
|
|
246
|
+
}
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
### Server pages with loaders
|
|
250
|
+
|
|
251
|
+
Server pages (the default) run a `loader` function on every request. The loader can fetch data, query a database, or call an external API.
|
|
252
|
+
|
|
253
|
+
```tsx
|
|
254
|
+
import type { RouteProps, LoaderArgs } from "@thexjs/core";
|
|
255
|
+
|
|
256
|
+
export async function loader({ request }: LoaderArgs) {
|
|
257
|
+
const res = await fetch("https://api.example.com/products");
|
|
258
|
+
const products = await res.json();
|
|
259
|
+
return { products };
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
export default function Products({ loaderData }: RouteProps<typeof loader>) {
|
|
263
|
+
return (
|
|
264
|
+
<div>
|
|
265
|
+
<h1 className="text-3xl font-bold">Products</h1>
|
|
266
|
+
<ul className="mt-6 space-y-4">
|
|
267
|
+
{loaderData.products.map((p: any) => (
|
|
268
|
+
<li key={p.id} className="rounded-xl border border-border bg-card p-4">
|
|
269
|
+
<h2 className="font-semibold">{p.name}</h2>
|
|
270
|
+
<p className="text-sm text-muted-foreground">{p.price}</p>
|
|
271
|
+
</li>
|
|
272
|
+
))}
|
|
273
|
+
</ul>
|
|
274
|
+
</div>
|
|
275
|
+
);
|
|
276
|
+
}
|
|
277
|
+
```
|
|
278
|
+
|
|
279
|
+
### Loader with dynamic params
|
|
280
|
+
|
|
281
|
+
Combined with dynamic routing, loaders receive `params` parsed from the URL path.
|
|
282
|
+
|
|
283
|
+
```tsx
|
|
284
|
+
import type { RouteProps, LoaderArgs } from "@thexjs/core";
|
|
285
|
+
|
|
286
|
+
export async function loader({ params }: LoaderArgs) {
|
|
287
|
+
const product = await db.query(
|
|
288
|
+
"SELECT * FROM products WHERE id = ?", [params.id]
|
|
289
|
+
);
|
|
290
|
+
if (!product) throw new Response(null, { status: 404 });
|
|
291
|
+
return { product };
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
export default function ProductDetail({ loaderData }: RouteProps<typeof loader>) {
|
|
295
|
+
const { product } = loaderData;
|
|
296
|
+
return (
|
|
297
|
+
<div>
|
|
298
|
+
<h1 className="text-3xl font-bold">{product.name}</h1>
|
|
299
|
+
<p className="mt-2 text-muted-foreground">{product.description}</p>
|
|
300
|
+
<p className="mt-4 text-2xl font-bold text-primary">${product.price}</p>
|
|
301
|
+
</div>
|
|
302
|
+
);
|
|
303
|
+
}
|
|
304
|
+
```
|
|
305
|
+
|
|
306
|
+
### RouteProps type
|
|
307
|
+
|
|
308
|
+
The `RouteProps` type provides typed access to `loaderData`, `params`, and `request`. Pass your loader function as the type parameter for full type safety.
|
|
309
|
+
|
|
310
|
+
```tsx
|
|
311
|
+
import type { RouteProps } from "@thexjs/core";
|
|
312
|
+
|
|
313
|
+
export default function Page({ loaderData, params, request }: RouteProps<typeof loader>) {
|
|
314
|
+
// loaderData has the return type of loader()
|
|
315
|
+
// params has the dynamic segment types
|
|
316
|
+
// request is the standard Request object
|
|
317
|
+
}
|
|
318
|
+
```
|
|
319
|
+
|
|
320
|
+
---
|
|
321
|
+
|
|
322
|
+
## Layouts
|
|
323
|
+
|
|
324
|
+
Layouts wrap your pages with shared UI. x supports nested layouts via a dedicated layouts directory and the `_layout.tsx` convention.
|
|
325
|
+
|
|
326
|
+
### Layouts directory
|
|
327
|
+
|
|
328
|
+
Configure a layouts directory in `x.config.ts`. Layouts follow the same file-tree hierarchy as pages.
|
|
329
|
+
|
|
330
|
+
```tsx
|
|
331
|
+
import { defineConfig } from "@thexjs/core";
|
|
332
|
+
|
|
333
|
+
export default defineConfig({
|
|
334
|
+
pagesDir: "src/pages",
|
|
335
|
+
layoutsDir: "src/layouts",
|
|
336
|
+
});
|
|
337
|
+
```
|
|
338
|
+
|
|
339
|
+
### Root layout
|
|
340
|
+
|
|
341
|
+
The root layout wraps every page in your app. Create `src/layouts/main.tsx` to add a header, footer, or global styling.
|
|
342
|
+
|
|
343
|
+
```tsx
|
|
344
|
+
import type { ReactNode } from "react";
|
|
345
|
+
|
|
346
|
+
export default function RootLayout({ children }: { children: ReactNode }) {
|
|
347
|
+
return (
|
|
348
|
+
<div className="flex min-h-screen flex-col bg-background">
|
|
349
|
+
<header className="border-b border-border p-4">
|
|
350
|
+
<a href="/" className="text-lg font-bold">My App</a>
|
|
351
|
+
</header>
|
|
352
|
+
<main className="flex-1">{children}</main>
|
|
353
|
+
<footer className="border-t border-border p-4 text-center text-sm text-muted-foreground">
|
|
354
|
+
© 2026 My App
|
|
355
|
+
</footer>
|
|
356
|
+
</div>
|
|
357
|
+
);
|
|
358
|
+
}
|
|
359
|
+
```
|
|
360
|
+
|
|
361
|
+
### Nested layouts with _layout.tsx
|
|
362
|
+
|
|
363
|
+
Place a `_layout.tsx` file inside a pages folder to create a nested layout. All pages in that folder (and subfolders) inherit it.
|
|
364
|
+
|
|
365
|
+
```
|
|
366
|
+
pages/
|
|
367
|
+
_layout.tsx -> root layout
|
|
368
|
+
index.tsx
|
|
369
|
+
blog/
|
|
370
|
+
_layout.tsx -> nested layout for /blog/*
|
|
371
|
+
index.tsx
|
|
372
|
+
[slug].tsx
|
|
373
|
+
```
|
|
374
|
+
|
|
375
|
+
A nested layout can add a sidebar, breadcrumbs, or section-specific navigation.
|
|
376
|
+
|
|
377
|
+
```tsx
|
|
378
|
+
import type { ReactNode } from "react";
|
|
379
|
+
|
|
380
|
+
export default function BlogLayout({ children }: { children: ReactNode }) {
|
|
381
|
+
return (
|
|
382
|
+
<div className="flex gap-8">
|
|
383
|
+
<aside className="w-64 shrink-0">
|
|
384
|
+
<nav className="space-y-2">
|
|
385
|
+
<a href="/blog" className="block font-semibold">All posts</a>
|
|
386
|
+
<a href="/blog/category/react" className="block text-muted-foreground hover:text-foreground">React</a>
|
|
387
|
+
<a href="/blog/category/bun" className="block text-muted-foreground hover:text-foreground">Bun</a>
|
|
388
|
+
</nav>
|
|
389
|
+
</aside>
|
|
390
|
+
<div className="min-w-0 flex-1">{children}</div>
|
|
391
|
+
</div>
|
|
392
|
+
);
|
|
393
|
+
}
|
|
394
|
+
```
|
|
395
|
+
|
|
396
|
+
### Layout chain
|
|
397
|
+
|
|
398
|
+
Layouts nest hierarchically. A page under `pages/blog/[slug].tsx` would be wrapped by `blog/_layout.tsx` and then the root layout. The chain is resolved automatically based on the page's file path.
|
|
399
|
+
|
|
400
|
+
---
|
|
401
|
+
|
|
402
|
+
## API Routes
|
|
403
|
+
|
|
404
|
+
Build REST endpoints alongside your frontend pages. API routes live in `src/api/` and share the same process as your pages.
|
|
405
|
+
|
|
406
|
+
### File-based API routing
|
|
407
|
+
|
|
408
|
+
Like pages, API routes use the file system. A file at `src/api/hello.ts` becomes `/api/hello`.
|
|
409
|
+
|
|
410
|
+
```tsx
|
|
411
|
+
import type { ApiHandler } from "@thexjs/core";
|
|
412
|
+
|
|
413
|
+
export const GET: ApiHandler = ({ request }) => {
|
|
414
|
+
return Response.json({ message: "Hello from x!" });
|
|
415
|
+
};
|
|
416
|
+
```
|
|
417
|
+
|
|
418
|
+
### Request & response
|
|
419
|
+
|
|
420
|
+
Each exported HTTP method receives the request and returns a standard `Response` object. Dynamic segments work the same as pages: `api/users/[id].ts` → `/api/users/:id`.
|
|
421
|
+
|
|
422
|
+
```tsx
|
|
423
|
+
import type { ApiHandler } from "@thexjs/core";
|
|
424
|
+
|
|
425
|
+
export const GET: ApiHandler = async ({ request }) => {
|
|
426
|
+
const users = await db.query("SELECT * FROM users");
|
|
427
|
+
return Response.json(users);
|
|
428
|
+
};
|
|
429
|
+
|
|
430
|
+
export const POST: ApiHandler = async ({ request }) => {
|
|
431
|
+
const body = await request.json();
|
|
432
|
+
const result = await db.query(
|
|
433
|
+
"INSERT INTO users (name, email) VALUES (?, ?) RETURNING *",
|
|
434
|
+
[body.name, body.email]
|
|
435
|
+
);
|
|
436
|
+
return Response.json(result, { status: 201 });
|
|
437
|
+
};
|
|
438
|
+
```
|
|
439
|
+
|
|
440
|
+
### POST endpoint example
|
|
441
|
+
|
|
442
|
+
```tsx
|
|
443
|
+
import type { ApiHandler } from "@thexjs/core";
|
|
444
|
+
|
|
445
|
+
export const POST: ApiHandler = async ({ request }) => {
|
|
446
|
+
const form = await request.formData();
|
|
447
|
+
const email = form.get("email");
|
|
448
|
+
const message = form.get("message");
|
|
449
|
+
|
|
450
|
+
if (!email || !message) {
|
|
451
|
+
return Response.json(
|
|
452
|
+
{ error: "Email and message are required" },
|
|
453
|
+
{ status: 400 }
|
|
454
|
+
);
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
await sendEmail({ email, message });
|
|
458
|
+
return Response.json({ success: true });
|
|
459
|
+
};
|
|
460
|
+
```
|
|
461
|
+
|
|
462
|
+
### API route tree
|
|
463
|
+
|
|
464
|
+
API routes support the same file-tree conventions as pages — nested folders, dynamic segments, and index files.
|
|
465
|
+
|
|
466
|
+
```
|
|
467
|
+
src/api/
|
|
468
|
+
hello.ts -> GET /api/hello
|
|
469
|
+
users.ts -> GET, POST /api/users
|
|
470
|
+
users/
|
|
471
|
+
[id].ts -> GET, PUT, DELETE /api/users/:id
|
|
472
|
+
auth/
|
|
473
|
+
login.ts -> POST /api/auth/login
|
|
474
|
+
register.ts -> POST /api/auth/register
|
|
475
|
+
```
|
|
476
|
+
|
|
477
|
+
### Process sharing
|
|
478
|
+
|
|
479
|
+
API routes run in the same Bun process as your pages and server functions. This means you can share database connections, in-memory caches, and configuration without any network overhead.
|
|
480
|
+
|
|
481
|
+
---
|
|
482
|
+
|
|
483
|
+
## Server Functions
|
|
484
|
+
|
|
485
|
+
Call server-side functions directly from the browser without writing REST endpoints. Server functions live in `src/actions/` and are invoked via fetch.
|
|
486
|
+
|
|
487
|
+
### Defining server functions
|
|
488
|
+
|
|
489
|
+
Create a file in `src/actions/` and export named async functions.
|
|
490
|
+
|
|
491
|
+
```ts
|
|
492
|
+
export async function greet(name: string) {
|
|
493
|
+
return `Hello, ${name}! The server time is ${new Date().toISOString()}.`;
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
export async function sendEmail({ to, subject, body }: {
|
|
497
|
+
to: string;
|
|
498
|
+
subject: string;
|
|
499
|
+
body: string;
|
|
500
|
+
}) {
|
|
501
|
+
return { sent: true, to };
|
|
502
|
+
}
|
|
503
|
+
```
|
|
504
|
+
|
|
505
|
+
### Calling from the browser
|
|
506
|
+
|
|
507
|
+
Server functions are called by sending a POST request to `/__x/actions/<filename>/<functionName>`. The arguments are sent as JSON in the request body.
|
|
508
|
+
|
|
509
|
+
```tsx
|
|
510
|
+
"use client";
|
|
511
|
+
|
|
512
|
+
import { useState } from "react";
|
|
513
|
+
|
|
514
|
+
export default function GreetForm() {
|
|
515
|
+
const [message, setMessage] = useState("");
|
|
516
|
+
|
|
517
|
+
async function handleSubmit(e: React.FormEvent) {
|
|
518
|
+
e.preventDefault();
|
|
519
|
+
const form = new FormData(e.target as HTMLFormElement);
|
|
520
|
+
const name = form.get("name");
|
|
521
|
+
|
|
522
|
+
const res = await fetch("/__x/actions/greet/greet", {
|
|
523
|
+
method: "POST",
|
|
524
|
+
headers: { "Content-Type": "application/json" },
|
|
525
|
+
body: JSON.stringify([name]),
|
|
526
|
+
});
|
|
527
|
+
|
|
528
|
+
const data = await res.text();
|
|
529
|
+
setMessage(data);
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
return (
|
|
533
|
+
<form onSubmit={handleSubmit} className="space-y-4">
|
|
534
|
+
<input
|
|
535
|
+
name="name"
|
|
536
|
+
placeholder="Enter your name"
|
|
537
|
+
className="rounded-xl border border-border bg-card px-4 py-2"
|
|
538
|
+
/>
|
|
539
|
+
<button type="submit" className="rounded-xl bg-primary px-4 py-2 text-primary-foreground">
|
|
540
|
+
Greet me
|
|
541
|
+
</button>
|
|
542
|
+
{message && <p className="text-muted-foreground">{message}</p>}
|
|
543
|
+
</form>
|
|
544
|
+
);
|
|
545
|
+
}
|
|
546
|
+
```
|
|
547
|
+
|
|
548
|
+
### Server functions from loaders
|
|
549
|
+
|
|
550
|
+
You can also import and call server functions directly in loaders — no HTTP needed since they share the same process.
|
|
551
|
+
|
|
552
|
+
```tsx
|
|
553
|
+
import type { RouteProps, LoaderArgs } from "@thexjs/core";
|
|
554
|
+
import { getDashboardData } from "../actions/dashboard";
|
|
555
|
+
|
|
556
|
+
export async function loader({ request }: LoaderArgs) {
|
|
557
|
+
const data = await getDashboardData();
|
|
558
|
+
return { data };
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
export default function Dashboard({ loaderData }: RouteProps<typeof loader>) {
|
|
562
|
+
return <div>...</div>;
|
|
563
|
+
}
|
|
564
|
+
```
|
|
565
|
+
|
|
566
|
+
---
|
|
567
|
+
|
|
568
|
+
## Content Collections
|
|
569
|
+
|
|
570
|
+
Write content in Markdown with frontmatter, and x automatically turns it into pages. Perfect for blogs, documentation, and any content-driven site.
|
|
571
|
+
|
|
572
|
+
### Configuration
|
|
573
|
+
|
|
574
|
+
Point the content directory in `x.config.ts` to a folder with your markdown files.
|
|
575
|
+
|
|
576
|
+
```tsx
|
|
577
|
+
import { defineConfig } from "@thexjs/core";
|
|
578
|
+
|
|
579
|
+
export default defineConfig({
|
|
580
|
+
contentDir: "content",
|
|
581
|
+
});
|
|
582
|
+
```
|
|
583
|
+
|
|
584
|
+
### Markdown with frontmatter
|
|
585
|
+
|
|
586
|
+
Each markdown file starts with frontmatter (YAML between `---` delimiters) followed by markdown content.
|
|
587
|
+
|
|
588
|
+
```markdown
|
|
589
|
+
---
|
|
590
|
+
title: Hello World
|
|
591
|
+
date: 2026-03-15
|
|
592
|
+
tags: [getting-started, tutorial]
|
|
593
|
+
author: Jane Doe
|
|
594
|
+
---
|
|
595
|
+
|
|
596
|
+
## Welcome to x!
|
|
597
|
+
|
|
598
|
+
This is your first post using x's content collection system.
|
|
599
|
+
|
|
600
|
+
You can write **markdown** with all the usual syntax:
|
|
601
|
+
|
|
602
|
+
- Lists
|
|
603
|
+
- **Bold** and *italic* text
|
|
604
|
+
- `inline code` and code blocks
|
|
605
|
+
|
|
606
|
+
```ts
|
|
607
|
+
const greeting = "Hello from x!";
|
|
608
|
+
console.log(greeting);
|
|
609
|
+
```
|
|
610
|
+
```
|
|
611
|
+
|
|
612
|
+
### Reading content in a loader
|
|
613
|
+
|
|
614
|
+
Use `scanContent` to discover files and `renderMarkdown` to convert markdown to HTML in your loaders.
|
|
615
|
+
|
|
616
|
+
```tsx
|
|
617
|
+
import type { RouteProps, LoaderArgs } from "@thexjs/core";
|
|
618
|
+
import { scanContent, renderMarkdown } from "@thexjs/core";
|
|
619
|
+
|
|
620
|
+
export async function loader({ params }: LoaderArgs) {
|
|
621
|
+
const posts = await scanContent("posts");
|
|
622
|
+
const post = posts.find((p) => p.slug === params.slug);
|
|
623
|
+
if (!post) throw new Response(null, { status: 404 });
|
|
624
|
+
const html = await renderMarkdown(post.body);
|
|
625
|
+
return { post: { ...post, html } };
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
export default function BlogPost({ loaderData }: RouteProps<typeof loader>) {
|
|
629
|
+
return (
|
|
630
|
+
<article className="prose max-w-none">
|
|
631
|
+
<h1 className="text-4xl font-bold">{loaderData.post.title}</h1>
|
|
632
|
+
<p className="text-sm text-muted-foreground">
|
|
633
|
+
{loaderData.post.date} — {loaderData.post.author}
|
|
634
|
+
</p>
|
|
635
|
+
<div className="mt-6 flex gap-2">
|
|
636
|
+
{loaderData.post.tags?.map((tag: string) => (
|
|
637
|
+
<span key={tag} className="rounded-full bg-primary/10 px-3 py-1 text-xs text-primary">
|
|
638
|
+
{tag}
|
|
639
|
+
</span>
|
|
640
|
+
))}
|
|
641
|
+
</div>
|
|
642
|
+
<div
|
|
643
|
+
className="mt-8 leading-relaxed"
|
|
644
|
+
dangerouslySetInnerHTML={{ __html: loaderData.post.html }}
|
|
645
|
+
/>
|
|
646
|
+
</article>
|
|
647
|
+
);
|
|
648
|
+
}
|
|
649
|
+
```
|
|
650
|
+
|
|
651
|
+
### scanContent API
|
|
652
|
+
|
|
653
|
+
`scanContent(directory)` scans a subdirectory of your content folder and returns an array of content entries. Each entry includes `slug`, `frontmatter`, and `content`.
|
|
654
|
+
|
|
655
|
+
### renderMarkdown API
|
|
656
|
+
|
|
657
|
+
`renderMarkdown(markdownString)` converts markdown to an HTML string. It supports syntax highlighting via Shiki and handles all standard markdown features.
|
|
658
|
+
|
|
659
|
+
---
|
|
660
|
+
|
|
661
|
+
## Middleware
|
|
662
|
+
|
|
663
|
+
Route-level middleware lets you intercept requests before they reach your page or API handler. Use it for authentication, redirects, logging, and validation.
|
|
664
|
+
|
|
665
|
+
### The _middleware.ts convention
|
|
666
|
+
|
|
667
|
+
Place a `_middleware.ts` file in any route directory. It runs for all routes in that directory and its subdirectories.
|
|
668
|
+
|
|
669
|
+
```
|
|
670
|
+
pages/
|
|
671
|
+
_middleware.ts -> runs for all routes
|
|
672
|
+
index.tsx
|
|
673
|
+
dashboard/
|
|
674
|
+
_middleware.ts -> runs only for /dashboard/*
|
|
675
|
+
settings.tsx
|
|
676
|
+
profile.tsx
|
|
677
|
+
admin/
|
|
678
|
+
_middleware.ts -> auth check for /admin/*
|
|
679
|
+
index.tsx
|
|
680
|
+
```
|
|
681
|
+
|
|
682
|
+
### Middleware context
|
|
683
|
+
|
|
684
|
+
A middleware function receives an object with `params` (dynamic route params), `request` (the original Request), and a `next` function to continue the chain.
|
|
685
|
+
|
|
686
|
+
```ts
|
|
687
|
+
import type { MiddlewareContext, MiddlewareNext } from "@thexjs/core";
|
|
688
|
+
|
|
689
|
+
export async function middleware(ctx: MiddlewareContext, next: MiddlewareNext) {
|
|
690
|
+
console.log(`[${ctx.request.method}] ${ctx.request.url}`);
|
|
691
|
+
return next(ctx);
|
|
692
|
+
}
|
|
693
|
+
```
|
|
694
|
+
|
|
695
|
+
### Auth middleware example
|
|
696
|
+
|
|
697
|
+
```ts
|
|
698
|
+
import type { MiddlewareContext, MiddlewareNext } from "@thexjs/core";
|
|
699
|
+
|
|
700
|
+
export async function middleware(ctx: MiddlewareContext, next: MiddlewareNext) {
|
|
701
|
+
const session = ctx.request.cookies.get("session");
|
|
702
|
+
|
|
703
|
+
if (!session) {
|
|
704
|
+
return new Response(null, {
|
|
705
|
+
status: 302,
|
|
706
|
+
headers: { Location: "/login" },
|
|
707
|
+
});
|
|
708
|
+
}
|
|
709
|
+
|
|
710
|
+
const user = await validateSession(session);
|
|
711
|
+
if (!user) {
|
|
712
|
+
return new Response(null, {
|
|
713
|
+
status: 302,
|
|
714
|
+
headers: { Location: "/login" },
|
|
715
|
+
});
|
|
716
|
+
}
|
|
717
|
+
|
|
718
|
+
ctx.params.user = user;
|
|
719
|
+
return next(ctx);
|
|
720
|
+
}
|
|
721
|
+
```
|
|
722
|
+
|
|
723
|
+
### MiddlewareNext
|
|
724
|
+
|
|
725
|
+
Call `next(ctx)` to pass control to the next middleware or the route handler. You can modify `ctx.params` to enrich the request context for downstream handlers.
|
|
726
|
+
|
|
727
|
+
### Redirect patterns
|
|
728
|
+
|
|
729
|
+
```ts
|
|
730
|
+
// Redirect to login
|
|
731
|
+
return new Response(null, {
|
|
732
|
+
status: 302,
|
|
733
|
+
headers: { Location: "/login?redirect=" + ctx.request.url },
|
|
734
|
+
});
|
|
735
|
+
|
|
736
|
+
// Redirect back after successful auth
|
|
737
|
+
const url = new URL(ctx.request.url);
|
|
738
|
+
const redirectTo = url.searchParams.get("redirect") || "/";
|
|
739
|
+
return new Response(null, {
|
|
740
|
+
status: 302,
|
|
741
|
+
headers: { Location: redirectTo },
|
|
742
|
+
});
|
|
743
|
+
```
|
|
744
|
+
|
|
745
|
+
---
|
|
746
|
+
|
|
747
|
+
## Data Layer
|
|
748
|
+
|
|
749
|
+
x provides built-in SQLite and PostgreSQL integrations. Connect to a database, run migrations, and query data directly from loaders and server functions.
|
|
750
|
+
|
|
751
|
+
### SQLite
|
|
752
|
+
|
|
753
|
+
Use `connectSQLite` to connect to a local SQLite database file. SQLite requires zero configuration and is perfect for development and single-server deployments.
|
|
754
|
+
|
|
755
|
+
```ts
|
|
756
|
+
import { connectSQLite, runSQLiteMigrations } from "@thexjs/core";
|
|
757
|
+
|
|
758
|
+
const db = connectSQLite("data/app.db");
|
|
759
|
+
|
|
760
|
+
await runSQLiteMigrations(db, [
|
|
761
|
+
{
|
|
762
|
+
version: 1,
|
|
763
|
+
sql: `
|
|
764
|
+
CREATE TABLE IF NOT EXISTS users (
|
|
765
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
766
|
+
name TEXT NOT NULL,
|
|
767
|
+
email TEXT UNIQUE NOT NULL,
|
|
768
|
+
created_at TEXT DEFAULT (datetime('now'))
|
|
769
|
+
)
|
|
770
|
+
`,
|
|
771
|
+
},
|
|
772
|
+
{
|
|
773
|
+
version: 2,
|
|
774
|
+
sql: `
|
|
775
|
+
CREATE TABLE IF NOT EXISTS posts (
|
|
776
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
777
|
+
user_id INTEGER REFERENCES users(id),
|
|
778
|
+
title TEXT NOT NULL,
|
|
779
|
+
body TEXT,
|
|
780
|
+
created_at TEXT DEFAULT (datetime('now'))
|
|
781
|
+
)
|
|
782
|
+
`,
|
|
783
|
+
},
|
|
784
|
+
]);
|
|
785
|
+
|
|
786
|
+
export { db };
|
|
787
|
+
```
|
|
788
|
+
|
|
789
|
+
### Querying SQLite
|
|
790
|
+
|
|
791
|
+
The database object supports prepared statements with `query` and `execute` methods.
|
|
792
|
+
|
|
793
|
+
```tsx
|
|
794
|
+
import type { RouteProps, LoaderArgs } from "@thexjs/core";
|
|
795
|
+
import { db } from "../lib/db";
|
|
796
|
+
|
|
797
|
+
export async function loader({}: LoaderArgs) {
|
|
798
|
+
const users = db.query(
|
|
799
|
+
"SELECT id, name, email FROM users ORDER BY created_at DESC"
|
|
800
|
+
).all();
|
|
801
|
+
return { users };
|
|
802
|
+
}
|
|
803
|
+
|
|
804
|
+
export default function Users({ loaderData }: RouteProps<typeof loader>) {
|
|
805
|
+
return (
|
|
806
|
+
<div>
|
|
807
|
+
<h1 className="text-3xl font-bold">Users</h1>
|
|
808
|
+
<ul className="mt-6 space-y-3">
|
|
809
|
+
{loaderData.users.map((u: any) => (
|
|
810
|
+
<li key={u.id} className="rounded-xl border border-border bg-card p-4">
|
|
811
|
+
<p className="font-semibold">{u.name}</p>
|
|
812
|
+
<p className="text-sm text-muted-foreground">{u.email}</p>
|
|
813
|
+
</li>
|
|
814
|
+
))}
|
|
815
|
+
</ul>
|
|
816
|
+
</div>
|
|
817
|
+
);
|
|
818
|
+
}
|
|
819
|
+
```
|
|
820
|
+
|
|
821
|
+
### PostgreSQL
|
|
822
|
+
|
|
823
|
+
For production deployments, use `connectPostgres` with a connection string. PostgreSQL provides concurrent access, connection pooling, and is suitable for multi-server deployments.
|
|
824
|
+
|
|
825
|
+
```ts
|
|
826
|
+
import { connectPostgres, runPostgresMigrations } from "@thexjs/core";
|
|
827
|
+
|
|
828
|
+
const db = connectPostgres({
|
|
829
|
+
connectionString: process.env.DATABASE_URL,
|
|
830
|
+
max: 20, // connection pool size
|
|
831
|
+
});
|
|
832
|
+
|
|
833
|
+
await runPostgresMigrations(db, [
|
|
834
|
+
{
|
|
835
|
+
version: 1,
|
|
836
|
+
sql: `
|
|
837
|
+
CREATE TABLE IF NOT EXISTS users (
|
|
838
|
+
id SERIAL PRIMARY KEY,
|
|
839
|
+
name VARCHAR(255) NOT NULL,
|
|
840
|
+
email VARCHAR(255) UNIQUE NOT NULL,
|
|
841
|
+
created_at TIMESTAMPTZ DEFAULT NOW()
|
|
842
|
+
)
|
|
843
|
+
`,
|
|
844
|
+
},
|
|
845
|
+
]);
|
|
846
|
+
|
|
847
|
+
export { db };
|
|
848
|
+
```
|
|
849
|
+
|
|
850
|
+
### Migration API
|
|
851
|
+
|
|
852
|
+
Both `runSQLiteMigrations` and `runPostgresMigrations` take an array of migration objects. Each migration has a `version` number (incrementing) and `sql` string. Migrations are tracked and only run once.
|
|
853
|
+
|
|
854
|
+
---
|
|
855
|
+
|
|
856
|
+
## Build & Deploy
|
|
857
|
+
|
|
858
|
+
x produces optimized production builds with static HTML export, a server entry point, and content collection rendering — all in a single command.
|
|
859
|
+
|
|
860
|
+
### Build command
|
|
861
|
+
|
|
862
|
+
Run `x build` to produce a production build. The build output goes to a `.x/` directory.
|
|
863
|
+
|
|
864
|
+
```bash
|
|
865
|
+
x build
|
|
866
|
+
[x] resolving routes...
|
|
867
|
+
[x] found 12 routes
|
|
868
|
+
[x] building static pages...
|
|
869
|
+
[x] building server bundle...
|
|
870
|
+
[x] rendering content collections...
|
|
871
|
+
[x] build complete in 1.2s
|
|
872
|
+
```
|
|
873
|
+
|
|
874
|
+
### Output structure
|
|
875
|
+
|
|
876
|
+
The `.x/` directory contains everything needed to deploy — static files, server bundle, and assets.
|
|
877
|
+
|
|
878
|
+
```
|
|
879
|
+
.x/
|
|
880
|
+
client/ # Client-side assets
|
|
881
|
+
assets/
|
|
882
|
+
*.js # Bundled JS
|
|
883
|
+
*.css # Extracted CSS
|
|
884
|
+
server/ # Server entry point
|
|
885
|
+
index.js # Bun server bundle
|
|
886
|
+
static/ # Prerendered HTML pages
|
|
887
|
+
index.html
|
|
888
|
+
about/index.html
|
|
889
|
+
blog/
|
|
890
|
+
hello-world/index.html
|
|
891
|
+
x.json # Build manifest
|
|
892
|
+
```
|
|
893
|
+
|
|
894
|
+
### Static page export
|
|
895
|
+
|
|
896
|
+
Pages with `mode = "static"` are exported as HTML files in `.x/static/`. Each page is fully rendered at build time, including its loader output.
|
|
897
|
+
|
|
898
|
+
### Server entry
|
|
899
|
+
|
|
900
|
+
Server-rendered pages are bundled into `.x/server/index.js`. This file contains all server routes, API handlers, server functions, and middleware.
|
|
901
|
+
|
|
902
|
+
### Production server
|
|
903
|
+
|
|
904
|
+
Use `x start` to run the production server. It serves static files from `.x/static/` and handles dynamic routes via the server bundle.
|
|
905
|
+
|
|
906
|
+
```bash
|
|
907
|
+
x start
|
|
908
|
+
[x] production server running at http://localhost:3000
|
|
909
|
+
```
|
|
910
|
+
|
|
911
|
+
### Docker deployment
|
|
912
|
+
|
|
913
|
+
Deploy with a minimal Docker image using the official Bun runtime. The build output is self-contained.
|
|
914
|
+
|
|
915
|
+
```dockerfile
|
|
916
|
+
FROM oven/bun:1 AS build
|
|
917
|
+
WORKDIR /app
|
|
918
|
+
COPY package.json bun.lock .
|
|
919
|
+
RUN bun install
|
|
920
|
+
COPY . .
|
|
921
|
+
RUN x build
|
|
922
|
+
|
|
923
|
+
FROM oven/bun:1-slim
|
|
924
|
+
WORKDIR /app
|
|
925
|
+
COPY --from=build /app/.x .x
|
|
926
|
+
EXPOSE 3000
|
|
927
|
+
CMD ["x", "start"]
|
|
928
|
+
```
|
|
929
|
+
|
|
930
|
+
---
|
|
931
|
+
|
|
932
|
+
## Configuration
|
|
933
|
+
|
|
934
|
+
Configure x via `x.config.ts` at your project root. Use `defineConfig` from `@thexjs/core` for type-safe configuration.
|
|
935
|
+
|
|
936
|
+
### defineConfig
|
|
937
|
+
|
|
938
|
+
All configuration options are optional. x provides sensible defaults so you can start with zero configuration and add settings as needed.
|
|
939
|
+
|
|
940
|
+
```ts
|
|
941
|
+
import { defineConfig } from "@thexjs/core";
|
|
942
|
+
|
|
943
|
+
export default defineConfig({
|
|
944
|
+
// Page routes
|
|
945
|
+
pagesDir: "src/pages",
|
|
946
|
+
|
|
947
|
+
// Layout directory (for root layouts)
|
|
948
|
+
layoutsDir: "src/layouts",
|
|
949
|
+
|
|
950
|
+
// API routes
|
|
951
|
+
apiDir: "src/api",
|
|
952
|
+
|
|
953
|
+
// Server functions
|
|
954
|
+
actionsDir: "src/actions",
|
|
955
|
+
|
|
956
|
+
// Content collections (markdown)
|
|
957
|
+
contentDir: "content",
|
|
958
|
+
|
|
959
|
+
// Dev server port
|
|
960
|
+
port: 3000,
|
|
961
|
+
|
|
962
|
+
// Legacy routes directory
|
|
963
|
+
routesDir: "src/routes",
|
|
964
|
+
});
|
|
965
|
+
```
|
|
966
|
+
|
|
967
|
+
### All options reference
|
|
968
|
+
|
|
969
|
+
| Option | Type | Default | Description |
|
|
970
|
+
|--------------|----------|---------------|------------------------------------|
|
|
971
|
+
| `pagesDir` | `string` | `"src/pages"` | File-based page routes |
|
|
972
|
+
| `layoutsDir` | `string` | `"src/layouts"` | Root layout directory |
|
|
973
|
+
| `apiDir` | `string` | `"src/api"` | File-based API routes |
|
|
974
|
+
| `actionsDir` | `string` | `"src/actions"` | Server functions |
|
|
975
|
+
| `contentDir` | `string` | `"content"` | Markdown content collections |
|
|
976
|
+
| `port` | `number` | `3000` | Dev server port |
|
|
977
|
+
| `routesDir` | `string` | `undefined` | Legacy routes directory |
|
|
978
|
+
|
|
979
|
+
### pagesDir
|
|
980
|
+
|
|
981
|
+
The directory containing your page route files. Defaults to `src/pages`. Each `.tsx` file becomes a route based on its file path.
|
|
982
|
+
|
|
983
|
+
### layoutsDir
|
|
984
|
+
|
|
985
|
+
The directory for root layout components. Layouts wrap pages and can be nested using the `_layout.tsx` convention inside page directories.
|
|
986
|
+
|
|
987
|
+
### apiDir
|
|
988
|
+
|
|
989
|
+
The directory for API route files. Files here respond to HTTP methods (`GET`, `POST`, etc.) and are served under `/api/...`.
|
|
990
|
+
|
|
991
|
+
### actionsDir
|
|
992
|
+
|
|
993
|
+
The directory for server functions. Exported async functions can be called from the browser via `fetch('/__x/actions/...')`.
|
|
994
|
+
|
|
995
|
+
### contentDir
|
|
996
|
+
|
|
997
|
+
The directory for markdown content collections. Files with frontmatter are scanned and can be loaded via `scanContent` and `renderMarkdown`.
|
|
998
|
+
|
|
999
|
+
### port
|
|
1000
|
+
|
|
1001
|
+
The port number for the dev server. Defaults to `3000`. Set to a different value if the default port is in use.
|
|
1002
|
+
|
|
1003
|
+
---
|
|
1004
|
+
|
|
1005
|
+
## Development
|
|
1006
|
+
|
|
1007
|
+
```bash
|
|
1008
|
+
bun install # install deps + links the x CLI
|
|
1009
|
+
bun test # run the test suite
|
|
1010
|
+
bun run typecheck
|
|
1011
|
+
bun run lint
|
|
1012
|
+
```
|
|
1013
|
+
|
|
1014
|
+
## Production
|
|
1015
|
+
|
|
1016
|
+
```bash
|
|
1017
|
+
x build # static export + server bundle -> .x/
|
|
1018
|
+
x start # start the production server from .x/
|
|
1019
|
+
```
|
|
1020
|
+
|
|
1021
|
+
---
|
|
1022
|
+
|
|
1023
|
+
<p align="center">
|
|
1024
|
+
<a href="./TASKS.md">Build Status</a> |
|
|
1025
|
+
<a href="./LICENSE">MIT License</a>
|
|
1026
|
+
</p>
|