Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions docs/framework/react/guide/file-based-routing.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ To enable file-based routing, you'll need to be using React with a supported bun
- Vite
- Rspack/Rsbuild
- Webpack
- Esbuild
- Others??? (let us know if you'd like to see support for a specific bundler)

If your bundler is not yet supported, you can reach out to us on Discord or GitHub to let us know. Till then, fear not! You can still use the [`@tanstack/router-cli`](#configuration-with-the-tanstack-router-cli) package to generate your route tree file.
Expand Down Expand Up @@ -240,6 +241,28 @@ export default {

Or, you can clone our [Quickstart Webpack example](https://github.com/TanStack/router/tree/main/examples/react/quickstart-webpack-file-based) and get started.

### Configuration with Esbuild

To use file-based routing with **Esbuild**, you'll need to install the `@tanstack/router-plugin` package.

```sh
npm install -D @tanstack/router-plugin
```

Once installed, you'll need to add the plugin to your configuration.

```tsx
// esbuild.config.js
import { TanStackRouterEsbuild } from '@tanstack/router-plugin/esbuild'

export default {
// ...
plugins: [TanStackRouterEsbuild()],
}
```

Or, you can clone our [Quickstart Esbuild example](https://github.com/TanStack/router/tree/main/examples/react/quickstart-esbuild-file-based) and get started.

### Configuration with the TanStack Router CLI

To use file-based routing with the TanStack Router CLI, you'll need to install the `@tanstack/router-cli` package.
Expand Down
10 changes: 10 additions & 0 deletions e2e/react-router/basic-esbuild-file-based/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
node_modules
.DS_Store
dist
dist-ssr
*.local

/test-results/
/playwright-report/
/blob-report/
/playwright/.cache/
24 changes: 24 additions & 0 deletions e2e/react-router/basic-esbuild-file-based/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite App</title>
<script src="https://cdn.tailwindcss.com"></script>
<style type="text/tailwindcss">
html {
color-scheme: light dark;
}
* {
@apply border-gray-200 dark:border-gray-800;
}
body {
@apply bg-gray-50 text-gray-950 dark:bg-gray-900 dark:text-gray-200;
}
</style>
</head>
<body>
<div id="app"></div>
<script type="module" src="/dist/main.js"></script>
</body>
</html>
28 changes: 28 additions & 0 deletions e2e/react-router/basic-esbuild-file-based/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "tanstack-router-e2e-react-basic-esbuild-file-based",
"private": true,
"type": "module",
"scripts": {
"dev": "esbuild src/main.tsx --serve=3001 --bundle --outfile=dist/main.js --watch --servedir=.",
"build": "esbuild src/main.tsx --bundle --outfile=dist/main.js",
"serve": "esbuild src/main.tsx --bundle --outfile=dist/main.js --servedir=.",
"start": "dev",
"test:e2e": "playwright test --project=chromium"
},
"dependencies": {
"@tanstack/react-router": "workspace:^",
"@tanstack/router-devtools": "workspace:^",
"@tanstack/router-plugin": "workspace:^",
"@tanstack/router-zod-adapter": "workspace:^",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"redaxios": "^0.5.1",
"zod": "^3.23.8"
},
"devDependencies": {
"@playwright/test": "^1.48.0",
"@types/react": "^18.2.47",
"@types/react-dom": "^18.2.18",
"esbuild": "^0.23.1"
}
}
29 changes: 29 additions & 0 deletions e2e/react-router/basic-esbuild-file-based/playwright.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { defineConfig, devices } from '@playwright/test'

/**
* See https://playwright.dev/docs/test-configuration.
*/
export default defineConfig({
testDir: './tests',

reporter: [['line']],

use: {
/* Base URL to use in actions like `await page.goto('/')`. */
baseURL: 'http://localhost:3001/',
},

webServer: {
command: 'pnpm run dev',
url: 'http://localhost:3001',
reuseExistingServer: !process.env.CI,
stdout: 'pipe',
},

projects: [
{
name: 'chromium',
use: { ...devices['Desktop Chrome'] },
},
],
})
10 changes: 10 additions & 0 deletions e2e/react-router/basic-esbuild-file-based/src/esbuild.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { TanStackRouterEsbuild } from '@tanstack/router-plugin/esbuild'

export default {
// ...
plugins: [
TanStackRouterEsbuild({
autoCodeSplitting: true,
}),
],
}
25 changes: 25 additions & 0 deletions e2e/react-router/basic-esbuild-file-based/src/main.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import React from 'react'
import ReactDOM from 'react-dom/client'
import { RouterProvider, createRouter } from '@tanstack/react-router'
import { routeTree } from './routeTree.gen'

// Set up a Router instance
const router = createRouter({
routeTree,
defaultPreload: 'intent',
defaultStaleTime: 5000,
})

// Register things for typesafety
declare module '@tanstack/react-router' {
interface Register {
router: typeof router
}
}

const rootElement = document.getElementById('app')!

if (!rootElement.innerHTML) {
const root = ReactDOM.createRoot(rootElement)
root.render(<RouterProvider router={router} />)
}
32 changes: 32 additions & 0 deletions e2e/react-router/basic-esbuild-file-based/src/posts.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { notFound } from '@tanstack/react-router'
import axios from 'redaxios'

export type PostType = {
id: string
title: string
body: string
}

export const fetchPost = async (postId: string) => {
console.info(`Fetching post with id ${postId}...`)
await new Promise((r) => setTimeout(r, 500))
const post = await axios
.get<PostType>(`https://jsonplaceholder.typicode.com/posts/${postId}`)
.then((r) => r.data)
.catch((err) => {
if (err.status === 404) {
throw notFound()
}
throw err
})

return post
}

export const fetchPosts = async () => {
console.info('Fetching posts...')
await new Promise((r) => setTimeout(r, 500))
return axios
.get<Array<PostType>>('https://jsonplaceholder.typicode.com/posts')
.then((r) => r.data.slice(0, 10))
}
Loading