|
| 1 | +--- |
| 2 | +title: Pages and Layouts |
| 3 | +description: Create your first page and shared layout with the Pages Router. |
| 4 | +--- |
| 5 | + |
| 6 | +The Pages Router has a file-system based router built on the concept of pages (like Next.js pages folder). |
| 7 | + |
| 8 | +When a file is added to the `pages` directory, it's automatically available as a route. |
| 9 | + |
| 10 | +In Brisa framework, a **page** is a [Brisa Component](/docs/components-details) exported from a `.js`, `.jsx`, `.ts`, or `.tsx` file in the `pages` directory. Each page is associated with a route based on its file name. |
| 11 | + |
| 12 | +**Example**: If you create `pages/about.js` that exports a Brisa component like below, it will be accessible at `/about`. |
| 13 | + |
| 14 | +```jsx |
| 15 | +export default function About() { |
| 16 | + return <div>About</div> |
| 17 | +} |
| 18 | +``` |
| 19 | + |
| 20 | +See the difference between React Components and Brisa Components [here](/docs/components-details). |
| 21 | + |
| 22 | +## Index routes |
| 23 | + |
| 24 | +The router will automatically route files named `index` to the root of the directory. |
| 25 | + |
| 26 | +- `pages/index.js` → `/` |
| 27 | +- `pages/blog/index.js` → `/blog` |
| 28 | + |
| 29 | +## Nested routes |
| 30 | + |
| 31 | +The router supports nested files. If you create a nested folder structure, files will automatically be routed in the same way still. |
| 32 | + |
| 33 | +- `pages/blog/first-post.js` → `/blog/first-post` |
| 34 | +- `pages/dashboard/settings/username.js` → `/dashboard/settings/username` |
| 35 | + |
| 36 | +## Pages with Dynamic Routes |
| 37 | + |
| 38 | +Brisa supports pages with dynamic routes. For example, if you create a file called `pages/posts/[id].js`, then it will be accessible at `posts/1`, `posts/2`, etc. |
| 39 | + |
| 40 | +> To learn more about dynamic routing, check the [Dynamic Routing documentation](/docs/building-your-application/routing/dynamic-routes). |
| 41 | +
|
| 42 | +## Layout |
| 43 | + |
| 44 | +The global layout is defined inside `/src/layout/index`. By default Brisa supports a default layout, but you can modify it here. |
| 45 | + |
| 46 | + |
| 47 | +```jsx filename="src/layout/index.js" |
| 48 | +import { RequestContext } from "brisa"; |
| 49 | + |
| 50 | +export default function Layout({ children }: { children: JSX.Element }, { route }: RequestContext) { |
| 51 | + return ( |
| 52 | + <html> |
| 53 | + <head> |
| 54 | + <title id="title">My layout</title> |
| 55 | + <link rel="icon" href="favicon.ico" /> |
| 56 | + </head> |
| 57 | + <body> |
| 58 | + {children} |
| 59 | + </body> |
| 60 | + </html> |
| 61 | + ) |
| 62 | +} |
| 63 | +``` |
| 64 | + |
| 65 | +It must have the same structure: `html`, `head` and `body`. If for example you forget to put the `head`, you may have issues and you will be alerted with an error during development. |
| 66 | + |
| 67 | +All the components of Brisa (pages and layouts included), apart from the props, receive a second argument which is the **context of the request**, apart from having access to the request, you have access to a series of extra information such as the **route** of the page. In the layouts, having access to the page route is very useful to **create different layouts**. |
| 68 | + |
| 69 | +### Example of multi-layouts |
| 70 | + |
| 71 | +```tsx filename="src/layout/index.js" |
| 72 | +import { type RequestContext } from "brisa"; |
| 73 | +import UserLayout from './user-layout' |
| 74 | +import GlobalLayout from './global-layout' |
| 75 | + |
| 76 | +export default function Layout({ children }: { children: JSX.Element }, { route }: RequestContext) { |
| 77 | + // pathname: /en/user/aralroca/settings or /es/usuario/pepe |
| 78 | + if(route.name.startsWith('/user/[username]')) { |
| 79 | + return <UserLayout>{children}<UserLayout> |
| 80 | + } |
| 81 | + |
| 82 | + return <GlobalLayout>{children}</GlobalLayout> |
| 83 | +} |
| 84 | +``` |
| 85 | + |
| 86 | +## Data Fetching |
| 87 | + |
| 88 | +Inside your layout, you can fetch data directly with `fetch`, in the same way that you can do it in pages: |
| 89 | + |
| 90 | +```jsx filename="src/layout/index.js" |
| 91 | +import { RequestContext } from "brisa"; |
| 92 | + |
| 93 | +export default async function Layout({ children }: { children: JSX.Element }, { route }: RequestContext) { |
| 94 | + const data = await fetch(/* data url */).then(r => r.json()); |
| 95 | + |
| 96 | + return ( |
| 97 | + <html> |
| 98 | + <head> |
| 99 | + <title id="title">My layout</title> |
| 100 | + <link rel="icon" href="favicon.ico" /> |
| 101 | + </head> |
| 102 | + <body> |
| 103 | + {children} |
| 104 | + </body> |
| 105 | + </html> |
| 106 | + ) |
| 107 | +} |
| 108 | +``` |
| 109 | + |
| 110 | +The `fetch` is directly native and has no wrapper to control the cache. We recommend that you do not do the same `fetch` in several places, but use the [`context`](/docs/building-your-application/data-fetching/request-context) to store the data and consume it from any component. |
0 commit comments