Skip to content
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

docs: add dynamic routes page #219

Merged
merged 6 commits into from
Dec 15, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 1 addition & 1 deletion apps/documentation/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ name = "tuono"
path = ".tuono/main.rs"

[dependencies]
tuono_lib = "0.15.0"
tuono_lib = "0.16.2"
glob = "0.3.1"
time = { version = "0.3", features = ["macros"] }
serde = { version = "1.0.202", features = ["derive"] }
Expand Down
2 changes: 1 addition & 1 deletion apps/documentation/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"clsx": "2.1.1",
"react": "18.3.1",
"react-dom": "18.3.1",
"tuono": "npm:[email protected].0"
"tuono": "npm:[email protected].2"
},
"devDependencies": {
"@mdx-js/rollup": "3.1.0",
Expand Down
10 changes: 5 additions & 5 deletions apps/documentation/src/components/sidebar/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ export const sidebarElements: Array<SidebarElement> = [
label: 'Defining routes',
href: '/documentation/routing/defining-routes',
},
{
type: 'element',
label: 'Dynamic routes',
href: '/documentation/routing/dynamic-routes',
},
{
type: 'element',
label: 'Link and navigation',
Expand All @@ -126,11 +131,6 @@ export const sidebarElements: Array<SidebarElement> = [
label: 'Loading state',
href: '/documentation/routing/loading-state',
},
{
type: 'element',
label: 'Dynamic routes',
href: '/documentation/routing/dynamic-routes',
},
{
type: 'element',
label: 'Layouts',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,53 @@ import Breadcrumbs, { Element } from '@/components/breadcrumbs'

<Breadcrumbs breadcrumbs={[{ label: 'Dynamic routes' }]} />

# Loading state
# Dynamic routes

Todo
> 🚧 Only available for SSR apps. This feature will be soon available also for static generated apps.

When the exact segment names are not known in advance and you need to generate routes from dynamic data,
you can use dynamic segments, which are populated at request time.

## Convention

A dynamic segment can be created by wrapping a
file or a folder name in square brackets: `[segmentName]`.

The dynamic segment can be used in both folder and file name:
Valerioageno marked this conversation as resolved.
Show resolved Hide resolved

- `src/routes/blog/[slug].tsx`
- `src/routes/blog/[slug]/index.tsx`

The same path can have multiple dynamic segments:
Valerioageno marked this conversation as resolved.
Show resolved Hide resolved

- `src/routes/blog/[post]/[comment].tsx`

### Example

Assuming you're working on a blog with multiple posts,
you can use a dynamic segment to define the post page: `src/routes/blog/[slug].tsx`.

The `[slug]` is the dynamic segment which will be used to identify individual blog posts.

```tsx
import { useRouter } from 'tuono'

export default function Page() {
const router = useRouter()
return <p>Post: {router.pathname}</p>
}
```

## Catch all segments

Dynamic Segments can be extended to catch-all subsequent segments by adding
an ellipsis inside the brackets `[...segmentName]`.

Given the following `src/routes/shop/[...slug].tsx`,
the following paths will be matched:

- `/shop/clothes`
- `/shop/clothes/tops`
- `/shop`
- `/shop/clothes/tops/t-shirts`
- and so on.
3 changes: 0 additions & 3 deletions apps/documentation/tuono.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ const config: TuonoConfig = {
'@': 'src',
'@tabler/icons-react': '@tabler/icons-react/dist/esm/icons/index.mjs',
},
optimizeDeps: {
exclude: ['@mdx-js/react'],
},
plugins: [
{ enforce: 'pre', ...mdx({ providerImportSource: '@mdx-js/react' }) },
],
Expand Down
34 changes: 17 additions & 17 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading