-
-
Notifications
You must be signed in to change notification settings - Fork 1k
fix typos and wrong paths in server-routes.md #4461
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
AhmedBaset
wants to merge
1
commit into
TanStack:main
Choose a base branch
from
AhmedBaset:patch-2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -26,7 +26,7 @@ export const ServerRoute = createServerFileRoute().methods({ | |||
Because server routes can be defined in the same directory as your app routes, you can even use the same file for both! | ||||
|
||||
```tsx | ||||
// routes/hello.tsx | ||||
// routes/api/hello.tsx | ||||
|
||||
export const ServerRoute = createServerFileRoute().methods({ | ||||
POST: async ({ request }) => { | ||||
|
@@ -78,15 +78,15 @@ Server routes in TanStack Start, follow the same file-based routing conventions | |||
|
||||
## Unique Route Paths | ||||
|
||||
Each route can only have a single handler file associated with it. So, if you have a file named `routes/users.ts` which'd equal the request path of `/api/users`, you cannot have other files that'd also resolve to the same route. For example, the following files would all resolve to the same route and would error: | ||||
Each route can only have a single handler file associated with it. So, if you have a file named `routes/users.ts` which'd equal the request path of `/users`, you cannot have other files that'd also resolve to the same route. For example, the following files would all resolve to the same route and would error: | ||||
|
||||
- `/routes/users.index.ts` | ||||
- `/routes/users.ts` | ||||
- `/routes/users.index.ts` | ||||
- `/routes/users/index.ts` | ||||
|
||||
## Escaped Matching | ||||
|
||||
Just as with normal routes, server routes can match on escaped characters. For example, a file named `routes/users[.]json.ts` will create an API route at `/api/users[.]json`. | ||||
Just as with normal routes, server routes can match on escaped characters. For example, a file named `routes/api/users[.]json.ts` will create an API route at `/api/users[.]json`. | ||||
|
||||
## Pathless Layout Routes and Break-out Routes | ||||
|
||||
|
@@ -133,7 +133,7 @@ export default defineHandler((event) => { | |||
}) | ||||
``` | ||||
|
||||
## Defining an Server Route | ||||
## Defining a Server Route | ||||
|
||||
Server routes are created by exporting a `ServerRoute` from a route file. The `ServerRoute` export should be created by calling the `createServerFileRoute` function. The resulting builder object can then be used to: | ||||
|
||||
|
@@ -194,10 +194,10 @@ Once you've processed the request, you can return a `Response` object or `Promis | |||
|
||||
## Dynamic Path Params | ||||
|
||||
Server routes support dynamic path parameters in the same way as TanStack Router. For example, a file named `routes/users/$id.ts` will create an API route at `/users/$id` that accepts a dynamic `id` parameter. | ||||
Server routes support dynamic path parameters in the same way as TanStack Router. For example, a file named `routes/api/users/$id.ts` will create an API route at `/api/users/$id` that accepts a dynamic `id` parameter. | ||||
|
||||
```ts | ||||
// routes/users/$id.ts | ||||
// routes/api/users/$id.ts | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||
export const ServerRoute = createServerFileRoute().methods({ | ||||
GET: async ({ params }) => { | ||||
const { id } = params | ||||
|
@@ -209,10 +209,10 @@ export const ServerRoute = createServerFileRoute().methods({ | |||
// User ID: 123 | ||||
``` | ||||
|
||||
You can also have multiple dynamic path parameters in a single route. For example, a file named `routes/users/$id/posts/$postId.ts` will create an API route at `/api/users/$id/posts/$postId` that accepts two dynamic parameters. | ||||
You can also have multiple dynamic path parameters in a single route. For example, a file named `routes/api/users/$id/posts/$postId.ts` will create an API route at `/api/users/$id/posts/$postId` that accepts two dynamic parameters. | ||||
|
||||
```ts | ||||
// routes/users/$id/posts/$postId.ts | ||||
// routes/api/users/$id/posts/$postId.ts | ||||
export const ServerRoute = createServerFileRoute().methods({ | ||||
GET: async ({ params }) => { | ||||
const { id, postId } = params | ||||
|
@@ -226,10 +226,10 @@ export const ServerRoute = createServerFileRoute().methods({ | |||
|
||||
## Wildcard/Splat Param | ||||
|
||||
Server routes also support wildcard parameters at the end of the path, which are denoted by a `$` followed by nothing. For example, a file named `routes/file/$.ts` will create an API route at `/api/file/$` that accepts a wildcard parameter. | ||||
Server routes also support wildcard parameters at the end of the path, which are denoted by a `$` followed by nothing. For example, a file named `routes/api/file/$.ts` will create an API route at `/api/file/$` that accepts a wildcard parameter. | ||||
|
||||
```ts | ||||
// routes/file/$.ts | ||||
// routes/api/file/$.ts | ||||
export const ServerRoute = createServerFileRoute().methods({ | ||||
GET: async ({ params }) => { | ||||
const { _splat } = params | ||||
|
@@ -269,7 +269,7 @@ This is a common pattern for handling POST requests in Server routes/ You can al | |||
When returning JSON using a Response object, this is a common pattern: | ||||
|
||||
```ts | ||||
// routes/hello.ts | ||||
// routes/api/hello.ts | ||||
export const ServerRoute = createServerFileRoute().methods({ | ||||
GET: async ({ request }) => { | ||||
return new Response(JSON.stringify({ message: 'Hello, World!' }), { | ||||
|
@@ -289,7 +289,7 @@ export const ServerRoute = createServerFileRoute().methods({ | |||
Or you can use the `json` helper function to automatically set the `Content-Type` header to `application/json` and serialize the JSON object for you. | ||||
|
||||
```ts | ||||
// routes/hello.ts | ||||
// routes/api/hello.ts | ||||
import { json } from '@tanstack/react-start' | ||||
|
||||
export const ServerRoute = createServerFileRoute().methods({ | ||||
|
@@ -309,7 +309,7 @@ You can set the status code of the response by either: | |||
- Passing it as a property of the second argument to the `Response` constructor | ||||
|
||||
```ts | ||||
// routes/hello.ts | ||||
// routes/api/hello.ts | ||||
import { json } from '@tanstack/react-start' | ||||
|
||||
export const ServerRoute = createServerFileRoute().methods({ | ||||
|
@@ -328,7 +328,7 @@ You can set the status code of the response by either: | |||
- Using the `setResponseStatus` helper function from `@tanstack/react-start/server` | ||||
|
||||
```ts | ||||
// routes/hello.ts | ||||
// routes/api/hello.ts | ||||
import { json } from '@tanstack/react-start' | ||||
import { setResponseStatus } from '@tanstack/react-start/server' | ||||
|
||||
|
@@ -353,7 +353,7 @@ Sometimes you may need to set headers in the response. You can do this by either | |||
- Passing an object as the second argument to the `Response` constructor. | ||||
|
||||
```ts | ||||
// routes/hello.ts | ||||
// routes/api/hello.ts | ||||
export const ServerRoute = createServerFileRoute().methods({ | ||||
GET: async ({ request }) => { | ||||
return new Response('Hello, World!', { | ||||
|
@@ -371,7 +371,7 @@ Sometimes you may need to set headers in the response. You can do this by either | |||
- Or using the `setHeaders` helper function from `@tanstack/react-start/server`. | ||||
|
||||
```ts | ||||
// routes/hello.ts | ||||
// routes/api/hello.ts | ||||
import { setHeaders } from '@tanstack/react-start/server' | ||||
|
||||
export const ServerRoute = createServerFileRoute().methods({ | ||||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To match
router/docs/start/framework/react/server-routes.md
Line 49 in 3c4938e
While both
routes/hello.tsx
androutes/api/hello.tsx
are supported now,api/hello
makes more sense api routesThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not really, this example explicitly shows how to colocate with render routes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The server route is at
routes/hello.tsx
(/hello
) and the fetch request goes to/api/hello
One of them must change