Skip to content

Commit

Permalink
docs: PageProps for readme and add as const to getConfig return (#1167)
Browse files Browse the repository at this point in the history
updated readme to include the changes from:
https://waku.gg/blog/type-safe-routing

added some missing code snippet langs too

---------

Co-authored-by: Tyler <[email protected]>
  • Loading branch information
tylersayshi and tylersayshi authored Jan 21, 2025
1 parent 0cc9550 commit ecf1577
Showing 1 changed file with 53 additions and 29 deletions.
82 changes: 53 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ We recommend other frameworks for heavy ecommerce or enterprise applications. Wa

Start a new Waku project with the `create` command for your preferred package manager. It will scaffold a new project with our default [Waku starter](https://github.com/dai-shi/waku/tree/main/examples/01_template).

```
```sh
npm create waku@latest
```

Expand Down Expand Up @@ -232,7 +232,7 @@ export default async function AboutPage() {
export const getConfig = async () => {
return {
render: 'static',
};
} as const;
};
```

Expand All @@ -247,7 +247,7 @@ export default async function BlogIndexPage() {
export const getConfig = async () => {
return {
render: 'static',
};
} as const;
};
```

Expand All @@ -261,9 +261,12 @@ If statically prerendering a segment route at build time, a `staticPaths` array

```tsx
// ./src/pages/blog/[slug].tsx
import type { PageProps } from 'waku/router';

// Create blog article pages
export default async function BlogArticlePage({ slug }) {
export default async function BlogArticlePage({
slug,
}: PageProps<'/blog/[slug]'>) {
const data = await getData(slug);

return <>{/* ...*/}</>;
Expand All @@ -277,15 +280,18 @@ export const getConfig = async () => {
return {
render: 'static',
staticPaths: ['introducing-waku', 'introducing-pages-router'],
};
} as const;
};
```

```tsx
// ./src/pages/shop/[category].tsx
import type { PageProps } from 'waku/router';

// Create product category pages
export default async function ProductCategoryPage({ category }) {
export default async function ProductCategoryPage({
category,
}: PageProps<'/shop/[category]'>) {
const data = await getData(category);

return <>{/* ...*/}</>;
Expand All @@ -298,17 +304,20 @@ const getData = async (category) => {
export const getConfig = async () => {
return {
render: 'dynamic',
};
} as const;
};
```

Static paths (or other config values) can also be generated programmatically.

```tsx
// ./src/pages/blog/[slug].tsx
import type { PageProps } from 'waku/router';

// Create blog article pages
export default async function BlogArticlePage({ slug }) {
export default async function BlogArticlePage({
slug,
}: PageProps<'/blog/[slug]'>) {
const data = await getData(slug);

return <>{/* ...*/}</>;
Expand All @@ -324,7 +333,7 @@ export const getConfig = async () => {
return {
render: 'static',
staticPaths,
};
} as const;
};

const getStaticPaths = async () => {
Expand All @@ -338,26 +347,34 @@ Routes can contain multiple segments (e.g., `/shop/[category]/[product]`) by cre

```tsx
// ./src/pages/shop/[category]/[product].tsx
import type { PageProps } from 'waku/router';

// Create product category pages
export default async function ProductDetailPage({ category, product }) {
export default async function ProductDetailPage({
category,
product,
}: PageProps<'/shop/[category]/[product]'>) {
return <>{/* ...*/}</>;
}

export const getConfig = async () => {
return {
render: 'dynamic',
};
} as const;
};
```

For static prerendering of nested segment routes, the `staticPaths` array is instead composed of ordered arrays.

```tsx
// ./src/pages/shop/[category]/[product].tsx
import type { PageProps } from 'waku/router';

// Create product detail pages
export default async function ProductDetailPage({ category, product }) {
export default async function ProductDetailPage({
category,
product,
}: PageProps<'/shop/[category]/[product]'>) {
return <>{/* ...*/}</>;
}

Expand All @@ -368,7 +385,7 @@ export const getConfig = async () => {
['same-category', 'some-product'],
['same-category', 'another-product'],
],
};
} as const;
};
```

Expand All @@ -380,16 +397,19 @@ Wildcard routes receive a prop with segment values as an ordered array. For exam

```tsx
// ./src/pages/app/[...catchAll].tsx
import type { PageProps } from 'waku/router';

// Create dashboard page
export default async function DashboardPage({ catchAll }) {
export default async function DashboardPage({
catchAll,
}: PageProps<'/app/[...catchAll]'>) {
return <>{/* ...*/}</>;
}

export const getConfig = async () => {
return {
render: 'dynamic',
};
} as const;
};
```

Expand Down Expand Up @@ -467,7 +487,7 @@ export const getConfig = async () => {
};
```

### Root elements
### Root element

The attributes of `<html>`, `<head>`, or `<body>` elements can be customized with the root element API. Create a special `_root.tsx` file in the `./src/pages` directory that accepts a `children` prop of type `ReactNode`.

Expand Down Expand Up @@ -608,7 +628,7 @@ export default async function HomePage() {
export const getConfig = async () => {
return {
render: 'static',
};
} as const;
};
```

Expand Down Expand Up @@ -643,7 +663,7 @@ const getMetadata = async () => {
export const getConfig = async () => {
return {
render: 'static',
};
} as const;
};
```

Expand Down Expand Up @@ -722,7 +742,7 @@ export default async function HomePage() {
export const getConfig = async () => {
return {
render: 'static',
};
} as const;
};
```

Expand All @@ -734,10 +754,14 @@ All of the wonderful patterns enabled by React server components are supported.

```tsx
// ./src/pages/blog/[slug].tsx
import type { PageProps } from 'waku/router';

import { MDX } from '../../components/mdx';
import { getArticle, getStaticPaths } from '../../lib/blog';

export default async function BlogArticlePage({ slug }) {
export default async function BlogArticlePage({
slug,
}: PageProps<'/blog/[slug]'>) {
const article = await getArticle(slug);

return (
Expand All @@ -755,7 +779,7 @@ export const getConfig = async () => {
return {
render: 'static',
staticPaths,
};
} as const;
};
```

Expand Down Expand Up @@ -986,15 +1010,15 @@ export const ClientComponent = () => {

Waku projects can be deployed to Vercel with the [Vercel CLI](https://vercel.com/docs/cli) automatically.

```
```sh
vercel
```

#### Pure SSG

Adding the `--with-vercel-static` flag to the build script will produce static sites without serverless functions.

```
```json
{
"scripts": {
"build": "waku build --with-vercel-static"
Expand All @@ -1008,7 +1032,7 @@ Note: When rendering in static mode, please be sure to return `render: 'static'`

Waku projects can be deployed to Netlify with the [Netlify CLI](https://docs.netlify.com/cli/get-started/).

```
```sh
npm run build -- --with-netlify
netlify deploy
```
Expand All @@ -1017,7 +1041,7 @@ netlify deploy

Adding the `--with-netlify-static` flag to the build script will produce static sites without Netlify functions.

```
```json
{
"scripts": {
"build": "waku build --with-netlify-static"
Expand All @@ -1029,28 +1053,28 @@ Note: When rendering in static mode, please be sure to return `render: 'static'`

### Cloudflare (experimental)

```
```sh
npm run build -- --with-cloudflare
npx wrangler dev # or deploy
```

### PartyKit (experimental)

```
```sh
npm run build -- --with-partykit
npx partykit dev # or deploy
```

### Deno Deploy (experimental)

```
```sh
npm run build -- --with-deno
deployctl deploy --prod dist/serve-deno.js --exclude node_modules
```

### AWS Lambda (experimental)

```
```sh
npm run build -- --with-aws-lambda
```

Expand Down

0 comments on commit ecf1577

Please sign in to comment.