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
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ArrowRight,
|
|
3
|
+
BookOpen,
|
|
4
|
+
Code2,
|
|
5
|
+
Cog,
|
|
6
|
+
Database,
|
|
7
|
+
FileJson,
|
|
8
|
+
Globe,
|
|
9
|
+
Layers,
|
|
10
|
+
Layout,
|
|
11
|
+
Rocket,
|
|
12
|
+
Shield,
|
|
13
|
+
Zap,
|
|
14
|
+
} from "lucide-react";
|
|
15
|
+
|
|
16
|
+
const sections = [
|
|
17
|
+
{
|
|
18
|
+
icon: BookOpen,
|
|
19
|
+
title: "Getting Started",
|
|
20
|
+
desc: "Install x, create your first project, and learn the basics.",
|
|
21
|
+
href: "/docs/getting-started",
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
icon: Globe,
|
|
25
|
+
title: "Routing",
|
|
26
|
+
desc: "File-based routing: static pages, dynamic segments, nested routes.",
|
|
27
|
+
href: "/docs/routing",
|
|
28
|
+
},
|
|
29
|
+
{
|
|
30
|
+
icon: FileJson,
|
|
31
|
+
title: "Pages & Loaders",
|
|
32
|
+
desc: "Static pages, server-rendered pages, and data loading.",
|
|
33
|
+
href: "/docs/pages",
|
|
34
|
+
},
|
|
35
|
+
{
|
|
36
|
+
icon: Layout,
|
|
37
|
+
title: "Layouts",
|
|
38
|
+
desc: "Nested layouts, dedicated layout directories, and root layouts.",
|
|
39
|
+
href: "/docs/layouts",
|
|
40
|
+
},
|
|
41
|
+
{
|
|
42
|
+
icon: Code2,
|
|
43
|
+
title: "API Routes",
|
|
44
|
+
desc: "Build REST endpoints alongside your frontend pages.",
|
|
45
|
+
href: "/docs/api-routes",
|
|
46
|
+
},
|
|
47
|
+
{
|
|
48
|
+
icon: Zap,
|
|
49
|
+
title: "Server Functions",
|
|
50
|
+
desc: "Call server-side functions from the browser without REST.",
|
|
51
|
+
href: "/docs/server-functions",
|
|
52
|
+
},
|
|
53
|
+
{
|
|
54
|
+
icon: Layers,
|
|
55
|
+
title: "Content Collections",
|
|
56
|
+
desc: "Markdown files with frontmatter that become routes.",
|
|
57
|
+
href: "/docs/content-collections",
|
|
58
|
+
},
|
|
59
|
+
{
|
|
60
|
+
icon: Shield,
|
|
61
|
+
title: "Middleware",
|
|
62
|
+
desc: "Route-level middleware for auth, redirects, and validation.",
|
|
63
|
+
href: "/docs/middleware",
|
|
64
|
+
},
|
|
65
|
+
{
|
|
66
|
+
icon: Database,
|
|
67
|
+
title: "Data Layer",
|
|
68
|
+
desc: "SQLite and PostgreSQL integration with migrations.",
|
|
69
|
+
href: "/docs/data-layer",
|
|
70
|
+
},
|
|
71
|
+
{
|
|
72
|
+
icon: Rocket,
|
|
73
|
+
title: "Build & Deploy",
|
|
74
|
+
desc: "Production builds, static export, and deployment.",
|
|
75
|
+
href: "/docs/build-deploy",
|
|
76
|
+
},
|
|
77
|
+
{
|
|
78
|
+
icon: Cog,
|
|
79
|
+
title: "Configuration",
|
|
80
|
+
desc: "x.config.ts reference — all options and defaults.",
|
|
81
|
+
href: "/docs/configuration",
|
|
82
|
+
},
|
|
83
|
+
];
|
|
84
|
+
|
|
85
|
+
export default function DocsHubPage() {
|
|
86
|
+
return (
|
|
87
|
+
<div className="py-12">
|
|
88
|
+
<div className="mb-12">
|
|
89
|
+
<p className="text-xs font-semibold uppercase tracking-[0.2em] text-primary">
|
|
90
|
+
Documentation
|
|
91
|
+
</p>
|
|
92
|
+
<h1 className="mt-4 text-4xl font-bold tracking-tight sm:text-5xl">
|
|
93
|
+
Everything you need to build with x
|
|
94
|
+
</h1>
|
|
95
|
+
<p className="mt-4 text-lg text-muted-foreground">
|
|
96
|
+
A fullstack framework for Bun. Static sites, server-rendered pages, API routes, and server
|
|
97
|
+
functions — all in a single process.
|
|
98
|
+
</p>
|
|
99
|
+
</div>
|
|
100
|
+
|
|
101
|
+
<div className="grid gap-4 sm:grid-cols-2">
|
|
102
|
+
{sections.map((s) => (
|
|
103
|
+
<a
|
|
104
|
+
key={s.title}
|
|
105
|
+
href={s.href}
|
|
106
|
+
className="group rounded-2xl border border-border bg-card p-6 transition-all hover:border-primary/30 hover:shadow-lg hover:shadow-primary/5"
|
|
107
|
+
>
|
|
108
|
+
<div className="flex h-10 w-10 items-center justify-center rounded-xl bg-primary/10 text-primary transition-colors group-hover:bg-primary/20">
|
|
109
|
+
<s.icon className="h-5 w-5" />
|
|
110
|
+
</div>
|
|
111
|
+
<h3 className="mt-4 text-lg font-semibold group-hover:text-primary transition-colors">
|
|
112
|
+
{s.title}
|
|
113
|
+
</h3>
|
|
114
|
+
<p className="mt-1 text-sm text-muted-foreground">{s.desc}</p>
|
|
115
|
+
<span className="mt-3 inline-flex items-center gap-1 text-xs font-medium text-primary opacity-0 transition-opacity group-hover:opacity-100">
|
|
116
|
+
Read more <ArrowRight className="h-3 w-3" />
|
|
117
|
+
</span>
|
|
118
|
+
</a>
|
|
119
|
+
))}
|
|
120
|
+
</div>
|
|
121
|
+
</div>
|
|
122
|
+
);
|
|
123
|
+
}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import type { RouteProps } from "@thexjs/core";
|
|
2
|
+
import { ArrowRight } from "lucide-react";
|
|
3
|
+
import { CodeBlock } from "../../components/code-block";
|
|
4
|
+
|
|
5
|
+
export default function DocPage({}: RouteProps) {
|
|
6
|
+
return (
|
|
7
|
+
<div>
|
|
8
|
+
<p className="text-xs font-semibold uppercase tracking-[0.2em] text-primary">Layouts</p>
|
|
9
|
+
<h1 className="mt-4 text-3xl font-bold tracking-tight sm:text-4xl">Layouts</h1>
|
|
10
|
+
<p className="mt-4 text-lg text-muted-foreground">
|
|
11
|
+
Layouts wrap your pages with shared UI. x supports nested layouts via a dedicated layouts
|
|
12
|
+
directory and the <span className="text-foreground">_layout.tsx</span> convention.
|
|
13
|
+
</p>
|
|
14
|
+
|
|
15
|
+
<h2 className="mt-12 text-xl font-bold tracking-tight">Layouts directory</h2>
|
|
16
|
+
<p className="mt-3 text-muted-foreground">
|
|
17
|
+
Configure a layouts directory in <span className="text-foreground">x.config.ts</span>.
|
|
18
|
+
Layouts follow the same file-tree hierarchy as pages.
|
|
19
|
+
</p>
|
|
20
|
+
<CodeBlock
|
|
21
|
+
label="x.config.ts"
|
|
22
|
+
code={`import { defineConfig } from "@thexjs/core";
|
|
23
|
+
|
|
24
|
+
export default defineConfig({
|
|
25
|
+
pagesDir: "src/pages",
|
|
26
|
+
layoutsDir: "src/layouts",
|
|
27
|
+
});`}
|
|
28
|
+
/>
|
|
29
|
+
|
|
30
|
+
<h2 className="mt-12 text-xl font-bold tracking-tight">Root layout</h2>
|
|
31
|
+
<p className="mt-3 text-muted-foreground">
|
|
32
|
+
The root layout wraps every page in your app. Create{" "}
|
|
33
|
+
<span className="text-foreground">src/layouts/main.tsx</span> to add a header, footer, or
|
|
34
|
+
global styling.
|
|
35
|
+
</p>
|
|
36
|
+
<CodeBlock
|
|
37
|
+
label="src/layouts/main.tsx"
|
|
38
|
+
code={`import type { ReactNode } from "react";
|
|
39
|
+
|
|
40
|
+
export default function RootLayout({ children }: { children: ReactNode }) {
|
|
41
|
+
return (
|
|
42
|
+
<div className="flex min-h-screen flex-col bg-background">
|
|
43
|
+
<header className="border-b border-border p-4">
|
|
44
|
+
<a href="/docs" className="text-lg font-bold">My App</a>
|
|
45
|
+
</header>
|
|
46
|
+
<main className="flex-1">{children}</main>
|
|
47
|
+
<footer className="border-t border-border p-4 text-center text-sm text-muted-foreground">
|
|
48
|
+
© 2026 My App
|
|
49
|
+
</footer>
|
|
50
|
+
</div>
|
|
51
|
+
);
|
|
52
|
+
}`}
|
|
53
|
+
/>
|
|
54
|
+
|
|
55
|
+
<h2 className="mt-12 text-xl font-bold tracking-tight">Nested layouts with _layout.tsx</h2>
|
|
56
|
+
<p className="mt-3 text-muted-foreground">
|
|
57
|
+
Place a <span className="text-foreground">_layout.tsx</span> file inside a pages folder to
|
|
58
|
+
create a nested layout. All pages in that folder (and subfolders) inherit it.
|
|
59
|
+
</p>
|
|
60
|
+
<CodeBlock
|
|
61
|
+
label="file tree"
|
|
62
|
+
lang="tree"
|
|
63
|
+
code={`pages/
|
|
64
|
+
_layout.tsx -> root layout
|
|
65
|
+
index.tsx
|
|
66
|
+
blog/
|
|
67
|
+
_layout.tsx -> nested layout for /blog/*
|
|
68
|
+
index.tsx
|
|
69
|
+
[slug].tsx`}
|
|
70
|
+
/>
|
|
71
|
+
|
|
72
|
+
<h2 className="mt-12 text-xl font-bold tracking-tight">Nested blog layout example</h2>
|
|
73
|
+
<p className="mt-3 text-muted-foreground">
|
|
74
|
+
A nested layout can add a sidebar, breadcrumbs, or section-specific navigation.
|
|
75
|
+
</p>
|
|
76
|
+
<CodeBlock
|
|
77
|
+
label="src/pages/blog/_layout.tsx"
|
|
78
|
+
code={`import type { ReactNode } from "react";
|
|
79
|
+
|
|
80
|
+
export default function BlogLayout({ children }: { children: ReactNode }) {
|
|
81
|
+
return (
|
|
82
|
+
<div className="flex gap-8">
|
|
83
|
+
<aside className="w-64 shrink-0">
|
|
84
|
+
<nav className="space-y-2">
|
|
85
|
+
<a href="/blog" className="block font-semibold">All posts</a>
|
|
86
|
+
<a href="/blog/category/react" className="block text-muted-foreground hover:text-foreground">React</a>
|
|
87
|
+
<a href="/blog/category/bun" className="block text-muted-foreground hover:text-foreground">Bun</a>
|
|
88
|
+
</nav>
|
|
89
|
+
</aside>
|
|
90
|
+
<div className="min-w-0 flex-1">{children}</div>
|
|
91
|
+
</div>
|
|
92
|
+
);
|
|
93
|
+
}`}
|
|
94
|
+
/>
|
|
95
|
+
|
|
96
|
+
<h2 className="mt-12 text-xl font-bold tracking-tight">Layout chain</h2>
|
|
97
|
+
<p className="mt-3 text-muted-foreground">
|
|
98
|
+
Layouts nest hierarchically. A page under{" "}
|
|
99
|
+
<span className="text-foreground">pages/blog/[slug].tsx</span> would be wrapped by{" "}
|
|
100
|
+
<span className="text-foreground">blog/_layout.tsx</span> and then the root layout. The
|
|
101
|
+
chain is resolved automatically based on the page's file path.
|
|
102
|
+
</p>
|
|
103
|
+
|
|
104
|
+
<div className="mt-16 border-t border-border pt-8">
|
|
105
|
+
<a
|
|
106
|
+
href="/docs"
|
|
107
|
+
className="inline-flex items-center gap-1 text-sm text-muted-foreground transition-colors hover:text-foreground"
|
|
108
|
+
>
|
|
109
|
+
<ArrowRight className="h-3.5 w-3.5 rotate-180" /> Back to docs
|
|
110
|
+
</a>
|
|
111
|
+
</div>
|
|
112
|
+
</div>
|
|
113
|
+
);
|
|
114
|
+
}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import type { RouteProps } from "@thexjs/core";
|
|
2
|
+
import { ArrowRight } from "lucide-react";
|
|
3
|
+
import { CodeBlock } from "../../components/code-block";
|
|
4
|
+
|
|
5
|
+
export default function DocPage({}: RouteProps) {
|
|
6
|
+
return (
|
|
7
|
+
<div>
|
|
8
|
+
<p className="text-xs font-semibold uppercase tracking-[0.2em] text-primary">Middleware</p>
|
|
9
|
+
<h1 className="mt-4 text-3xl font-bold tracking-tight sm:text-4xl">Middleware</h1>
|
|
10
|
+
<p className="mt-4 text-lg text-muted-foreground">
|
|
11
|
+
Route-level middleware lets you intercept requests before they reach your page or API
|
|
12
|
+
handler. Use it for authentication, redirects, logging, and validation.
|
|
13
|
+
</p>
|
|
14
|
+
|
|
15
|
+
<h2 className="mt-12 text-xl font-bold tracking-tight">The _middleware.ts convention</h2>
|
|
16
|
+
<p className="mt-3 text-muted-foreground">
|
|
17
|
+
Place a <span className="text-foreground">_middleware.ts</span> file in any route directory.
|
|
18
|
+
It runs for all routes in that directory and its subdirectories.
|
|
19
|
+
</p>
|
|
20
|
+
<CodeBlock
|
|
21
|
+
label="file tree"
|
|
22
|
+
lang="tree"
|
|
23
|
+
code={`pages/
|
|
24
|
+
_middleware.ts -> runs for all routes
|
|
25
|
+
index.tsx
|
|
26
|
+
dashboard/
|
|
27
|
+
_middleware.ts -> runs only for /dashboard/*
|
|
28
|
+
settings.tsx
|
|
29
|
+
profile.tsx
|
|
30
|
+
admin/
|
|
31
|
+
_middleware.ts -> auth check for /admin/*
|
|
32
|
+
index.tsx`}
|
|
33
|
+
/>
|
|
34
|
+
|
|
35
|
+
<h2 className="mt-12 text-xl font-bold tracking-tight">Middleware context</h2>
|
|
36
|
+
<p className="mt-3 text-muted-foreground">
|
|
37
|
+
A middleware function receives an object with{" "}
|
|
38
|
+
<span className="text-foreground">params</span> (dynamic route params),{" "}
|
|
39
|
+
<span className="text-foreground">request</span> (the original Request), and a{" "}
|
|
40
|
+
<span className="text-foreground">next</span> function to continue the chain.
|
|
41
|
+
</p>
|
|
42
|
+
<CodeBlock
|
|
43
|
+
label="src/pages/_middleware.ts"
|
|
44
|
+
code={`import type { MiddlewareContext, MiddlewareNext } from "@thexjs/core";
|
|
45
|
+
|
|
46
|
+
export async function middleware(ctx: MiddlewareContext, next: MiddlewareNext) {
|
|
47
|
+
console.log(\`[${"$"}{ctx.request.method}] ${"$"}{ctx.request.url}\`);
|
|
48
|
+
return next(ctx);
|
|
49
|
+
}`}
|
|
50
|
+
/>
|
|
51
|
+
|
|
52
|
+
<h2 className="mt-12 text-xl font-bold tracking-tight">Auth middleware example</h2>
|
|
53
|
+
<p className="mt-3 text-muted-foreground">
|
|
54
|
+
A common use case is checking for an auth cookie and redirecting unauthenticated users.
|
|
55
|
+
</p>
|
|
56
|
+
<CodeBlock
|
|
57
|
+
label="src/pages/dashboard/_middleware.ts"
|
|
58
|
+
code={`import type { MiddlewareContext, MiddlewareNext } from "@thexjs/core";
|
|
59
|
+
|
|
60
|
+
export async function middleware(ctx: MiddlewareContext, next: MiddlewareNext) {
|
|
61
|
+
const session = ctx.request.cookies.get("session");
|
|
62
|
+
|
|
63
|
+
if (!session) {
|
|
64
|
+
return new Response(null, {
|
|
65
|
+
status: 302,
|
|
66
|
+
headers: { Location: "/login" },
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const user = await validateSession(session);
|
|
71
|
+
if (!user) {
|
|
72
|
+
return new Response(null, {
|
|
73
|
+
status: 302,
|
|
74
|
+
headers: { Location: "/login" },
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
ctx.params.user = user;
|
|
79
|
+
return next(ctx);
|
|
80
|
+
}`}
|
|
81
|
+
/>
|
|
82
|
+
|
|
83
|
+
<h2 className="mt-12 text-xl font-bold tracking-tight">MiddlewareNext</h2>
|
|
84
|
+
<p className="mt-3 text-muted-foreground">
|
|
85
|
+
Call <span className="text-foreground">next(ctx)</span> to pass control to the next
|
|
86
|
+
middleware or the route handler. You can modify{" "}
|
|
87
|
+
<span className="text-foreground">ctx.params</span> to enrich the request context for
|
|
88
|
+
downstream handlers.
|
|
89
|
+
</p>
|
|
90
|
+
|
|
91
|
+
<h2 className="mt-12 text-xl font-bold tracking-tight">Redirect patterns</h2>
|
|
92
|
+
<p className="mt-3 text-muted-foreground">
|
|
93
|
+
Return a <span className="text-foreground">Response</span> with a 302 status and a{" "}
|
|
94
|
+
<span className="text-foreground">Location</span> header to redirect. You can also return
|
|
95
|
+
JSON responses for API middleware validation errors.
|
|
96
|
+
</p>
|
|
97
|
+
<CodeBlock
|
|
98
|
+
label="redirect example"
|
|
99
|
+
code={`// Redirect to login
|
|
100
|
+
return new Response(null, {
|
|
101
|
+
status: 302,
|
|
102
|
+
headers: { Location: "/login?redirect=" + ctx.request.url },
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
// Redirect back after successful auth
|
|
106
|
+
const url = new URL(ctx.request.url);
|
|
107
|
+
const redirectTo = url.searchParams.get("redirect") || "/";
|
|
108
|
+
return new Response(null, {
|
|
109
|
+
status: 302,
|
|
110
|
+
headers: { Location: redirectTo },
|
|
111
|
+
});`}
|
|
112
|
+
/>
|
|
113
|
+
|
|
114
|
+
<div className="mt-16 border-t border-border pt-8">
|
|
115
|
+
<a
|
|
116
|
+
href="/docs"
|
|
117
|
+
className="inline-flex items-center gap-1 text-sm text-muted-foreground transition-colors hover:text-foreground"
|
|
118
|
+
>
|
|
119
|
+
<ArrowRight className="h-3.5 w-3.5 rotate-180" /> Back to docs
|
|
120
|
+
</a>
|
|
121
|
+
</div>
|
|
122
|
+
</div>
|
|
123
|
+
);
|
|
124
|
+
}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import type { RouteProps } from "@thexjs/core";
|
|
2
|
+
import { ArrowRight } from "lucide-react";
|
|
3
|
+
import { CodeBlock } from "../../components/code-block";
|
|
4
|
+
|
|
5
|
+
export default function DocPage({}: RouteProps) {
|
|
6
|
+
return (
|
|
7
|
+
<div>
|
|
8
|
+
<p className="text-xs font-semibold uppercase tracking-[0.2em] text-primary">Pages</p>
|
|
9
|
+
<h1 className="mt-4 text-3xl font-bold tracking-tight sm:text-4xl">Pages & loaders</h1>
|
|
10
|
+
<p className="mt-4 text-lg text-muted-foreground">
|
|
11
|
+
x supports two page modes — static prerendering and server-side rendering — both powered by
|
|
12
|
+
loaders.
|
|
13
|
+
</p>
|
|
14
|
+
|
|
15
|
+
<h2 className="mt-12 text-xl font-bold tracking-tight">Page modes</h2>
|
|
16
|
+
<p className="mt-3 text-muted-foreground">
|
|
17
|
+
By default, pages are server-rendered (SSR). Export{" "}
|
|
18
|
+
<span className="text-foreground">mode = "static"</span> to prerender at build time.
|
|
19
|
+
</p>
|
|
20
|
+
|
|
21
|
+
<h2 className="mt-12 text-xl font-bold tracking-tight">Static pages</h2>
|
|
22
|
+
<p className="mt-3 text-muted-foreground">
|
|
23
|
+
Static pages are rendered at build time and exported as HTML. Use this for marketing pages,
|
|
24
|
+
blog posts, or any content that doesn't need per-request rendering.
|
|
25
|
+
</p>
|
|
26
|
+
<CodeBlock
|
|
27
|
+
label="src/pages/about.tsx"
|
|
28
|
+
code={`import type { RouteProps } from "@thexjs/core";
|
|
29
|
+
|
|
30
|
+
export const mode = "static";
|
|
31
|
+
|
|
32
|
+
export default function About({}: RouteProps) {
|
|
33
|
+
return (
|
|
34
|
+
<div className="max-w-2xl mx-auto py-12">
|
|
35
|
+
<h1 className="text-4xl font-bold">About</h1>
|
|
36
|
+
<p className="mt-4 text-muted-foreground">
|
|
37
|
+
This page is prerendered at build time.
|
|
38
|
+
</p>
|
|
39
|
+
</div>
|
|
40
|
+
);
|
|
41
|
+
}`}
|
|
42
|
+
/>
|
|
43
|
+
|
|
44
|
+
<h2 className="mt-12 text-xl font-bold tracking-tight">Server pages with loaders</h2>
|
|
45
|
+
<p className="mt-3 text-muted-foreground">
|
|
46
|
+
Server pages (the default) run a <span className="text-foreground">loader</span> function on
|
|
47
|
+
every request. The loader can fetch data, query a database, or call an external API.
|
|
48
|
+
</p>
|
|
49
|
+
<CodeBlock
|
|
50
|
+
label="src/pages/products.tsx"
|
|
51
|
+
code={`import type { RouteProps, LoaderArgs } from "@thexjs/core";
|
|
52
|
+
|
|
53
|
+
export async function loader({ request }: LoaderArgs) {
|
|
54
|
+
const res = await fetch("https://api.example.com/products");
|
|
55
|
+
const products = await res.json();
|
|
56
|
+
return { products };
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export default function Products({ loaderData }: RouteProps<typeof loader>) {
|
|
60
|
+
return (
|
|
61
|
+
<div>
|
|
62
|
+
<h1 className="text-3xl font-bold">Products</h1>
|
|
63
|
+
<ul className="mt-6 space-y-4">
|
|
64
|
+
{loaderData.products.map((p: any) => (
|
|
65
|
+
<li key={p.id} className="rounded-xl border border-border bg-card p-4">
|
|
66
|
+
<h2 className="font-semibold">{p.name}</h2>
|
|
67
|
+
<p className="text-sm text-muted-foreground">{p.price}</p>
|
|
68
|
+
</li>
|
|
69
|
+
))}
|
|
70
|
+
</ul>
|
|
71
|
+
</div>
|
|
72
|
+
);
|
|
73
|
+
}`}
|
|
74
|
+
/>
|
|
75
|
+
|
|
76
|
+
<h2 className="mt-12 text-xl font-bold tracking-tight">Loader with dynamic params</h2>
|
|
77
|
+
<p className="mt-3 text-muted-foreground">
|
|
78
|
+
Combined with dynamic routing, loaders receive{" "}
|
|
79
|
+
<span className="text-foreground">params</span> parsed from the URL path.
|
|
80
|
+
</p>
|
|
81
|
+
<CodeBlock
|
|
82
|
+
label="src/pages/products/[id].tsx"
|
|
83
|
+
code={`import type { RouteProps, LoaderArgs } from "@thexjs/core";
|
|
84
|
+
|
|
85
|
+
export async function loader({ params }: LoaderArgs) {
|
|
86
|
+
const product = await db.query(
|
|
87
|
+
"SELECT * FROM products WHERE id = ?", [params.id]
|
|
88
|
+
);
|
|
89
|
+
if (!product) throw new Response(null, { status: 404 });
|
|
90
|
+
return { product };
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export default function ProductDetail({ loaderData }: RouteProps<typeof loader>) {
|
|
94
|
+
const { product } = loaderData;
|
|
95
|
+
return (
|
|
96
|
+
<div>
|
|
97
|
+
<h1 className="text-3xl font-bold">{product.name}</h1>
|
|
98
|
+
<p className="mt-2 text-muted-foreground">{product.description}</p>
|
|
99
|
+
<p className="mt-4 text-2xl font-bold text-primary">${"$"}{product.price}</p>
|
|
100
|
+
</div>
|
|
101
|
+
);
|
|
102
|
+
}`}
|
|
103
|
+
/>
|
|
104
|
+
|
|
105
|
+
<h2 className="mt-12 text-xl font-bold tracking-tight">RouteProps type</h2>
|
|
106
|
+
<p className="mt-3 text-muted-foreground">
|
|
107
|
+
The <span className="text-foreground">RouteProps</span> type provides typed access to{" "}
|
|
108
|
+
<span className="text-foreground">loaderData</span>,{" "}
|
|
109
|
+
<span className="text-foreground">params</span>, and{" "}
|
|
110
|
+
<span className="text-foreground">request</span>. Pass your loader function as the type
|
|
111
|
+
parameter for full type safety.
|
|
112
|
+
</p>
|
|
113
|
+
<CodeBlock
|
|
114
|
+
label="type usage"
|
|
115
|
+
code={`import type { RouteProps } from "@thexjs/core";
|
|
116
|
+
|
|
117
|
+
// loaderData is automatically typed via the generic
|
|
118
|
+
export default function Page({ loaderData, params, request }: RouteProps<typeof loader>) {
|
|
119
|
+
// loaderData has the return type of loader()
|
|
120
|
+
// params has the dynamic segment types
|
|
121
|
+
// request is the standard Request object
|
|
122
|
+
}`}
|
|
123
|
+
/>
|
|
124
|
+
|
|
125
|
+
<div className="mt-16 border-t border-border pt-8">
|
|
126
|
+
<a
|
|
127
|
+
href="/docs"
|
|
128
|
+
className="inline-flex items-center gap-1 text-sm text-muted-foreground transition-colors hover:text-foreground"
|
|
129
|
+
>
|
|
130
|
+
<ArrowRight className="h-3.5 w-3.5 rotate-180" /> Back to docs
|
|
131
|
+
</a>
|
|
132
|
+
</div>
|
|
133
|
+
</div>
|
|
134
|
+
);
|
|
135
|
+
}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import type { RouteProps } from "@thexjs/core";
|
|
2
|
+
import { ArrowRight } from "lucide-react";
|
|
3
|
+
import { CodeBlock } from "../../components/code-block";
|
|
4
|
+
|
|
5
|
+
export default function DocPage({}: RouteProps) {
|
|
6
|
+
return (
|
|
7
|
+
<div>
|
|
8
|
+
<p className="text-xs font-semibold uppercase tracking-[0.2em] text-primary">Routing</p>
|
|
9
|
+
<h1 className="mt-4 text-3xl font-bold tracking-tight sm:text-4xl">File-based routing</h1>
|
|
10
|
+
<p className="mt-4 text-lg text-muted-foreground">
|
|
11
|
+
x uses the file system as your route table. Drop a file in{" "}
|
|
12
|
+
<span className="text-foreground">src/pages/</span>, get a route.
|
|
13
|
+
</p>
|
|
14
|
+
|
|
15
|
+
<h2 className="mt-12 text-xl font-bold tracking-tight">How it works</h2>
|
|
16
|
+
<p className="mt-3 text-muted-foreground">
|
|
17
|
+
Every <span className="text-foreground">.tsx</span> file in your pages directory becomes a
|
|
18
|
+
route. The file path determines the URL pattern.
|
|
19
|
+
</p>
|
|
20
|
+
|
|
21
|
+
<CodeBlock
|
|
22
|
+
label="routing table"
|
|
23
|
+
code={`pages/index.tsx -> /
|
|
24
|
+
pages/about.tsx -> /about
|
|
25
|
+
pages/contact.tsx -> /contact
|
|
26
|
+
pages/blog/index.tsx -> /blog
|
|
27
|
+
pages/blog/[slug].tsx -> /blog/:slug
|
|
28
|
+
pages/dashboard/
|
|
29
|
+
settings.tsx -> /dashboard/settings
|
|
30
|
+
profile.tsx -> /dashboard/profile
|
|
31
|
+
pages/_404.tsx -> catch-all 404`}
|
|
32
|
+
/>
|
|
33
|
+
|
|
34
|
+
<h2 className="mt-12 text-xl font-bold tracking-tight">Static routes</h2>
|
|
35
|
+
<p className="mt-3 text-muted-foreground">
|
|
36
|
+
Simple files map to exact URL paths.{" "}
|
|
37
|
+
<span className="text-foreground">pages/about.tsx</span> becomes{" "}
|
|
38
|
+
<span className="text-foreground">/about</span>.
|
|
39
|
+
</p>
|
|
40
|
+
<CodeBlock
|
|
41
|
+
label="src/pages/about.tsx"
|
|
42
|
+
code={`export default function About() {
|
|
43
|
+
return <h1 className="text-3xl font-bold">About us</h1>;
|
|
44
|
+
}`}
|
|
45
|
+
/>
|
|
46
|
+
|
|
47
|
+
<h2 className="mt-12 text-xl font-bold tracking-tight">Dynamic segments</h2>
|
|
48
|
+
<p className="mt-3 text-muted-foreground">
|
|
49
|
+
Wrap a filename in square brackets to create a dynamic segment. The value is available via
|
|
50
|
+
the <span className="text-foreground">params</span> object in loaders.
|
|
51
|
+
</p>
|
|
52
|
+
<CodeBlock
|
|
53
|
+
label="src/pages/blog/[slug].tsx"
|
|
54
|
+
code={`import type { RouteProps, LoaderArgs } from "@thexjs/core";
|
|
55
|
+
|
|
56
|
+
export async function loader({ params }: LoaderArgs) {
|
|
57
|
+
const post = await getPost(params.slug);
|
|
58
|
+
return { title: post.title, content: post.content };
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export default function BlogPost({ loaderData }: RouteProps<typeof loader>) {
|
|
62
|
+
return (
|
|
63
|
+
<article>
|
|
64
|
+
<h1 className="text-3xl font-bold">{loaderData.title}</h1>
|
|
65
|
+
<div>{loaderData.content}</div>
|
|
66
|
+
</article>
|
|
67
|
+
);
|
|
68
|
+
}`}
|
|
69
|
+
/>
|
|
70
|
+
<p className="mt-4 text-muted-foreground">
|
|
71
|
+
Multiple dynamic segments work too:{" "}
|
|
72
|
+
<span className="text-foreground">pages/product/[category]/[id].tsx</span> →{" "}
|
|
73
|
+
<span className="text-foreground">/product/:category/:id</span>.
|
|
74
|
+
</p>
|
|
75
|
+
|
|
76
|
+
<h2 className="mt-12 text-xl font-bold tracking-tight">Nested routes with folders</h2>
|
|
77
|
+
<p className="mt-3 text-muted-foreground">
|
|
78
|
+
Organize routes in folders for nested URL structures. Each folder can have its own{" "}
|
|
79
|
+
<span className="text-foreground">index.tsx</span>.
|
|
80
|
+
</p>
|
|
81
|
+
<CodeBlock
|
|
82
|
+
label="file tree"
|
|
83
|
+
lang="tree"
|
|
84
|
+
code={`pages/dashboard/
|
|
85
|
+
index.tsx -> /dashboard
|
|
86
|
+
settings.tsx -> /dashboard/settings
|
|
87
|
+
profile.tsx -> /dashboard/profile
|
|
88
|
+
billing/
|
|
89
|
+
index.tsx -> /dashboard/billing
|
|
90
|
+
history.tsx -> /dashboard/billing/history`}
|
|
91
|
+
/>
|
|
92
|
+
|
|
93
|
+
<h2 className="mt-12 text-xl font-bold tracking-tight">Catch-all 404 page</h2>
|
|
94
|
+
<p className="mt-3 text-muted-foreground">
|
|
95
|
+
Create <span className="text-foreground">pages/_404.tsx</span> to show a custom not-found
|
|
96
|
+
page for unmatched routes.
|
|
97
|
+
</p>
|
|
98
|
+
<CodeBlock
|
|
99
|
+
label="src/pages/_404.tsx"
|
|
100
|
+
code={`export default function NotFound() {
|
|
101
|
+
return (
|
|
102
|
+
<div className="text-center py-20">
|
|
103
|
+
<h1 className="text-6xl font-bold text-muted-foreground">404</h1>
|
|
104
|
+
<p className="mt-4 text-lg text-muted-foreground">Page not found</p>
|
|
105
|
+
<a href="/docs" className="mt-6 inline-block text-primary hover:underline">
|
|
106
|
+
Go home
|
|
107
|
+
</a>
|
|
108
|
+
</div>
|
|
109
|
+
);
|
|
110
|
+
}`}
|
|
111
|
+
/>
|
|
112
|
+
|
|
113
|
+
<div className="mt-16 border-t border-border pt-8">
|
|
114
|
+
<a
|
|
115
|
+
href="/docs"
|
|
116
|
+
className="inline-flex items-center gap-1 text-sm text-muted-foreground transition-colors hover:text-foreground"
|
|
117
|
+
>
|
|
118
|
+
<ArrowRight className="h-3.5 w-3.5 rotate-180" /> Back to docs
|
|
119
|
+
</a>
|
|
120
|
+
</div>
|
|
121
|
+
</div>
|
|
122
|
+
);
|
|
123
|
+
}
|