@tanstack/react-router 1.130.17 → 1.131.3
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/dist/llms/rules/guide.js
CHANGED
|
@@ -6608,256 +6608,6 @@ declare module '@tanstack/react-router' {
|
|
|
6608
6608
|
|
|
6609
6609
|
As long as there are any required properties on the \`StaticDataRouteOption\`, you'll be required to pass in an object.
|
|
6610
6610
|
|
|
6611
|
-
# TanStack Start
|
|
6612
|
-
|
|
6613
|
-
TanStack Start is a full-stack framework for building server-rendered React applications built on top of [TanStack Router](https://tanstack.com/router).
|
|
6614
|
-
|
|
6615
|
-
To set up a TanStack Start project, you'll need to:
|
|
6616
|
-
|
|
6617
|
-
1. Install the dependencies
|
|
6618
|
-
2. Add a configuration file
|
|
6619
|
-
3. Create required templating
|
|
6620
|
-
|
|
6621
|
-
Follow this guide to build a basic TanStack Start web application. Together, we will use TanStack Start to:
|
|
6622
|
-
|
|
6623
|
-
- Serve an index page...
|
|
6624
|
-
- Which displays a counter...
|
|
6625
|
-
- With a button to increment the counter persistently.
|
|
6626
|
-
|
|
6627
|
-
[Here is what that will look like](https://stackblitz.com/github/tanstack/router/tree/main/examples/react/start-basic-counter)
|
|
6628
|
-
|
|
6629
|
-
Create a new project if you're starting fresh.
|
|
6630
|
-
|
|
6631
|
-
\`\`\`shell
|
|
6632
|
-
mkdir myApp
|
|
6633
|
-
cd myApp
|
|
6634
|
-
npm init -y
|
|
6635
|
-
\`\`\`
|
|
6636
|
-
|
|
6637
|
-
Create a \`tsconfig.json\` file with at least the following settings:
|
|
6638
|
-
|
|
6639
|
-
\`\`\`jsonc
|
|
6640
|
-
{
|
|
6641
|
-
"compilerOptions": {
|
|
6642
|
-
"jsx": "react-jsx",
|
|
6643
|
-
"moduleResolution": "Bundler",
|
|
6644
|
-
"module": "Preserve",
|
|
6645
|
-
"target": "ES2022",
|
|
6646
|
-
"skipLibCheck": true,
|
|
6647
|
-
},
|
|
6648
|
-
}
|
|
6649
|
-
\`\`\`
|
|
6650
|
-
|
|
6651
|
-
# Install Dependencies
|
|
6652
|
-
|
|
6653
|
-
TanStack Start is powered by the following packages and need to be installed as dependencies:
|
|
6654
|
-
|
|
6655
|
-
- [@tanstack/start](https://github.com/tanstack/start)
|
|
6656
|
-
- [@tanstack/react-router](https://tanstack.com/router)
|
|
6657
|
-
- [Vite](https://vite.dev/)
|
|
6658
|
-
|
|
6659
|
-
To install them, run:
|
|
6660
|
-
|
|
6661
|
-
\`\`\`shell
|
|
6662
|
-
npm i @tanstack/react-start @tanstack/react-router vite
|
|
6663
|
-
\`\`\`
|
|
6664
|
-
|
|
6665
|
-
You'll also need React and the Vite React plugin, so install their dependencies as well:
|
|
6666
|
-
|
|
6667
|
-
\`\`\`shell
|
|
6668
|
-
npm i react react-dom @vitejs/plugin-react
|
|
6669
|
-
\`\`\`
|
|
6670
|
-
|
|
6671
|
-
Please, for you, your fellow developers, and your users' sake, use TypeScript:
|
|
6672
|
-
|
|
6673
|
-
\`\`\`shell
|
|
6674
|
-
npm i -D typescript @types/react @types/react-dom
|
|
6675
|
-
\`\`\`
|
|
6676
|
-
|
|
6677
|
-
# Update Configuration Files
|
|
6678
|
-
|
|
6679
|
-
We'll then update our \`package.json\` to use Vite's CLI and set \`"type": "module"\`:
|
|
6680
|
-
|
|
6681
|
-
\`\`\`jsonc
|
|
6682
|
-
{
|
|
6683
|
-
// ...
|
|
6684
|
-
"type": "module",
|
|
6685
|
-
"scripts": {
|
|
6686
|
-
"dev": "vite dev",
|
|
6687
|
-
"build": "vite build",
|
|
6688
|
-
"start": "vite start",
|
|
6689
|
-
},
|
|
6690
|
-
}
|
|
6691
|
-
\`\`\`
|
|
6692
|
-
|
|
6693
|
-
Then configure TanStack Start's \`app.config.ts\` file:
|
|
6694
|
-
|
|
6695
|
-
\`\`\`typescript
|
|
6696
|
-
// app.config.ts
|
|
6697
|
-
import { defineConfig } from '@tanstack/react-start/config'
|
|
6698
|
-
|
|
6699
|
-
export default defineConfig({})
|
|
6700
|
-
\`\`\`
|
|
6701
|
-
|
|
6702
|
-
# Add the Basic Templating
|
|
6703
|
-
|
|
6704
|
-
There are 2 required files for TanStack Start usage:
|
|
6705
|
-
|
|
6706
|
-
1. The router configuration
|
|
6707
|
-
2. The root of your application
|
|
6708
|
-
|
|
6709
|
-
Once configuration is done, we'll have a file tree that looks like the following:
|
|
6710
|
-
|
|
6711
|
-
\`\`\`
|
|
6712
|
-
.
|
|
6713
|
-
├── app/
|
|
6714
|
-
│ ├── routes/
|
|
6715
|
-
│ │ └── \`__root.tsx\`
|
|
6716
|
-
│ ├── \`router.tsx\`
|
|
6717
|
-
│ ├── \`routeTree.gen.ts\`
|
|
6718
|
-
├── \`.gitignore\`
|
|
6719
|
-
├── \`app.config.ts\`
|
|
6720
|
-
├── \`package.json\`
|
|
6721
|
-
└── \`tsconfig.json\`
|
|
6722
|
-
\`\`\`
|
|
6723
|
-
|
|
6724
|
-
## The Router Configuration
|
|
6725
|
-
|
|
6726
|
-
This is the file that will dictate the behavior of TanStack Router used within Start for both the server and the client. Here, you can configure everything
|
|
6727
|
-
from the default [preloading functionality](../preloading.md) to [caching staleness](../data-loading.md).
|
|
6728
|
-
|
|
6729
|
-
\`\`\`tsx
|
|
6730
|
-
// app/router.tsx
|
|
6731
|
-
import { createRouter as createTanStackRouter } from '@tanstack/react-router'
|
|
6732
|
-
import { routeTree } from './routeTree.gen'
|
|
6733
|
-
|
|
6734
|
-
export function createRouter() {
|
|
6735
|
-
const router = createTanStackRouter({
|
|
6736
|
-
routeTree,
|
|
6737
|
-
})
|
|
6738
|
-
|
|
6739
|
-
return router
|
|
6740
|
-
}
|
|
6741
|
-
|
|
6742
|
-
declare module '@tanstack/react-router' {
|
|
6743
|
-
interface Register {
|
|
6744
|
-
router: ReturnType<typeof createRouter>
|
|
6745
|
-
}
|
|
6746
|
-
}
|
|
6747
|
-
\`\`\`
|
|
6748
|
-
|
|
6749
|
-
> \`routeTree.gen.ts\` is not a file you're expected to have at this point.
|
|
6750
|
-
> It will be generated when you run TanStack Start (via \`npm run dev\` or \`npm run start\`) for the first time.
|
|
6751
|
-
|
|
6752
|
-
## The Root of Your Application
|
|
6753
|
-
|
|
6754
|
-
Finally, we need to create the root of our application. This is the entry point for all application routes. The code in this file will wrap all other routes in the application.
|
|
6755
|
-
|
|
6756
|
-
\`\`\`tsx
|
|
6757
|
-
// app/routes/__root.tsx
|
|
6758
|
-
import { createRootRoute, HeadContent, Scripts } from '@tanstack/react-router'
|
|
6759
|
-
import { Outlet } from '@tanstack/react-router'
|
|
6760
|
-
import * as React from 'react'
|
|
6761
|
-
|
|
6762
|
-
export const Route = createRootRoute({
|
|
6763
|
-
head: () => ({
|
|
6764
|
-
meta: [
|
|
6765
|
-
{
|
|
6766
|
-
charSet: 'utf-8',
|
|
6767
|
-
},
|
|
6768
|
-
{
|
|
6769
|
-
name: 'viewport',
|
|
6770
|
-
content: 'width=device-width, initial-scale=1',
|
|
6771
|
-
},
|
|
6772
|
-
{
|
|
6773
|
-
title: 'TanStack Start Starter',
|
|
6774
|
-
},
|
|
6775
|
-
],
|
|
6776
|
-
}),
|
|
6777
|
-
component: RootComponent,
|
|
6778
|
-
})
|
|
6779
|
-
|
|
6780
|
-
function RootComponent() {
|
|
6781
|
-
return (
|
|
6782
|
-
<RootDocument>
|
|
6783
|
-
<Outlet />
|
|
6784
|
-
</RootDocument>
|
|
6785
|
-
)
|
|
6786
|
-
}
|
|
6787
|
-
|
|
6788
|
-
function RootDocument({ children }: { children: React.ReactNode }) {
|
|
6789
|
-
return (
|
|
6790
|
-
<html>
|
|
6791
|
-
<head>
|
|
6792
|
-
<HeadContent />
|
|
6793
|
-
</head>
|
|
6794
|
-
<body>
|
|
6795
|
-
{children}
|
|
6796
|
-
<Scripts />
|
|
6797
|
-
</body>
|
|
6798
|
-
</html>
|
|
6799
|
-
)
|
|
6800
|
-
}
|
|
6801
|
-
\`\`\`
|
|
6802
|
-
|
|
6803
|
-
# Writing Your First Route
|
|
6804
|
-
|
|
6805
|
-
Now that we have the basic templating setup, we can write our first route. This is done by creating a new file in the \`app/routes\` directory.
|
|
6806
|
-
|
|
6807
|
-
\`\`\`tsx
|
|
6808
|
-
// app/routes/index.tsx
|
|
6809
|
-
import * as fs from 'fs'
|
|
6810
|
-
import { createFileRoute, useRouter } from '@tanstack/react-router'
|
|
6811
|
-
import { createServerFn } from '@tanstack/react-start'
|
|
6812
|
-
|
|
6813
|
-
const filePath = 'count.txt'
|
|
6814
|
-
|
|
6815
|
-
async function readCount() {
|
|
6816
|
-
return parseInt(
|
|
6817
|
-
await fs.promises.readFile(filePath, 'utf-8').catch(() => '0'),
|
|
6818
|
-
)
|
|
6819
|
-
}
|
|
6820
|
-
|
|
6821
|
-
const getCount = createServerFn({
|
|
6822
|
-
method: 'GET',
|
|
6823
|
-
}).handler(() => {
|
|
6824
|
-
return readCount()
|
|
6825
|
-
})
|
|
6826
|
-
|
|
6827
|
-
const updateCount = createServerFn({ method: 'POST' })
|
|
6828
|
-
.validator((d: number) => d)
|
|
6829
|
-
.handler(async ({ data }) => {
|
|
6830
|
-
const count = await readCount()
|
|
6831
|
-
await fs.promises.writeFile(filePath, \`\${count + data}\`)
|
|
6832
|
-
})
|
|
6833
|
-
|
|
6834
|
-
export const Route = createFileRoute('/')({
|
|
6835
|
-
component: Home,
|
|
6836
|
-
loader: async () => await getCount(),
|
|
6837
|
-
})
|
|
6838
|
-
|
|
6839
|
-
function Home() {
|
|
6840
|
-
const router = useRouter()
|
|
6841
|
-
const state = Route.useLoaderData()
|
|
6842
|
-
|
|
6843
|
-
return (
|
|
6844
|
-
<button
|
|
6845
|
-
onClick={() => {
|
|
6846
|
-
updateCount({ data: 1 }).then(() => {
|
|
6847
|
-
router.invalidate()
|
|
6848
|
-
})
|
|
6849
|
-
}}
|
|
6850
|
-
>
|
|
6851
|
-
Add 1 to {state}?
|
|
6852
|
-
</button>
|
|
6853
|
-
)
|
|
6854
|
-
}
|
|
6855
|
-
\`\`\`
|
|
6856
|
-
|
|
6857
|
-
That's it! 🤯 You've now set up a TanStack Start project and written your first route. 🎉
|
|
6858
|
-
|
|
6859
|
-
You can now run \`npm run dev\` to start your server and navigate to \`http://localhost:3000\` to see your route in action.
|
|
6860
|
-
|
|
6861
6611
|
# Type Safety
|
|
6862
6612
|
|
|
6863
6613
|
TanStack Router is built to be as type-safe as possible within the limits of the TypeScript compiler and runtime. This means that it's not only written in TypeScript, but that it also **fully infers the types it's provided and tenaciously pipes them through the entire routing experience**.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/react-router",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.131.3",
|
|
4
4
|
"description": "Modern and scalable routing for React applications",
|
|
5
5
|
"author": "Tanner Linsley",
|
|
6
6
|
"license": "MIT",
|
|
@@ -79,8 +79,8 @@
|
|
|
79
79
|
"isbot": "^5.1.22",
|
|
80
80
|
"tiny-invariant": "^1.3.3",
|
|
81
81
|
"tiny-warning": "^1.0.3",
|
|
82
|
-
"@tanstack/history": "1.
|
|
83
|
-
"@tanstack/router-core": "1.
|
|
82
|
+
"@tanstack/history": "1.131.2",
|
|
83
|
+
"@tanstack/router-core": "1.131.3"
|
|
84
84
|
},
|
|
85
85
|
"devDependencies": {
|
|
86
86
|
"@testing-library/jest-dom": "^6.6.3",
|