You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -250,11 +250,11 @@ The core files of **`@fastify/react`** that make all of that (and a bit more) wo
250
250
251
251
<div style="font-size: 1.2em !important">
252
252
253
-
The way this works is via the `/:` prefix.
253
+
The way this works is via the `$app/` prefix.
254
254
255
255
</div>
256
256
257
-
Notice how `client/index.html` imports the React application mounting script from `/:mount.js`, and `client/index.js` loads routes from `/:routes.js`, the application factory function from `/:create.jsx` and the [route context](/react/route-context) initialization module from `/:context.js`.
257
+
Notice how `client/index.html` imports the React application mounting script from `$app/mount.js`, and `client/index.js` loads routes from `$app/routes.js`, the application factory function from `$app/create.jsx` and the [route context](/react/route-context) initialization module from `$app/context.js`.
258
258
259
259
What this prefix does is **first check if the file exists** in your Vite project root directory, **and if not**, provide the **default versions** stored inside the `@fastify/react` package instead.
260
260
@@ -271,12 +271,12 @@ Below is a quick rundown of all smart imports available.
271
271
<tr>
272
272
<td>
273
273
274
-
`/:core.js`
274
+
`$app/core.js`
275
275
276
276
</td>
277
277
<td>
278
278
279
-
This is used by `/:create.jsx` internally to create your React application instance, but it's also the location where to importthe [`useRouteContext()`](/react/route-context) hookfrom.
279
+
This is used by `$app/create.jsx` internally to create your React application instance.
280
280
281
281
It also exports the `isServer` convenience flag.
282
282
@@ -285,7 +285,7 @@ It also exports the `isServer` convenience flag.
285
285
<tr>
286
286
<td>
287
287
288
-
`/:create.jsx`
288
+
`$app/create.jsx`
289
289
290
290
</td>
291
291
<td>
@@ -297,7 +297,7 @@ Where your React application factory function is exported from. It must be named
297
297
<tr>
298
298
<td>
299
299
300
-
`/:mount.js`
300
+
`$app/mount.js`
301
301
302
302
</td>
303
303
<td>
@@ -309,7 +309,7 @@ The Vite application mount script, imported by `index.html`.
309
309
<tr>
310
310
<td>
311
311
312
-
`/:resource.js`
312
+
`$app/resource.js`
313
313
314
314
</td>
315
315
<td>
@@ -321,7 +321,7 @@ Utilities to enable suspensed state in data fetching.
321
321
<tr>
322
322
<td>
323
323
324
-
`/:root.jsx`
324
+
`$app/root.jsx`
325
325
326
326
</td>
327
327
<td>
@@ -333,7 +333,7 @@ The main React component for your application.
333
333
<tr>
334
334
<td>
335
335
336
-
`/:context.js`
336
+
`$app/context.js`
337
337
338
338
</td>
339
339
<td>
@@ -345,7 +345,7 @@ The [route context](/react/route-context) initialization file.
345
345
<tr>
346
346
<td>
347
347
348
-
`/:layouts/default.jsx`
348
+
`$app/layouts/default.jsx`
349
349
350
350
</td>
351
351
<td>
@@ -363,15 +363,15 @@ The graph below indicates the relationships between them:
Copy file name to clipboardExpand all lines: docs/react/route-context.md
+8-6Lines changed: 8 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,7 @@
2
2
3
3
# Route Context
4
4
5
-
In **`@fastify/react`** applications, the **route context** is an object available in every [**route module**](/react/route-modules) and [**layout module**](/react/route-layouts). It is populated via the [**route context initialization module**](/react/route-context#init-module), and accessed via the `useRouteContext()` hook available from the `/:core.jsx` smart import — a default implementation is provided by **`@fastivy/react`** but you can extend or create your own by providing your own implementation of the `core.js` file.
5
+
In **`@fastify/react`** applications, the **route context** is an object available in every [**route module**](/react/route-modules) and [**layout module**](/react/route-layouts). It is populated via the [**route context initialization module**](/react/route-context#init-module), and accessed via the `useRouteContext()` hook.
6
6
7
7
This route context implementation is extremely simple and deliberately limited to essential features. Its goal is to be easy to understand, extend or completely modify if needed. Note that some aspects of this implementation are tightly coupled to the server. In **`@fastify/react`**'s source code, you can see the implementation of [`server/context.js`](https://github.com/fastify/fastify-vite/blob/dev/packages/fastify-react/server/context.js) and [its use in `createRoute()`](https://github.com/fastify/fastify-vite/blob/dev/packages/fastify-react/index.js) that first populates the route context object on the server and prepares it for hydration.
8
8
@@ -15,7 +15,7 @@ import FastifyReact from '@fastify/react'
@@ -135,7 +135,7 @@ export default function Index (props) {
135
135
```jsx [client/pages/using-store.jsx]
136
136
import { useState } from 'react'
137
137
import { Link } from 'react-router'
138
-
import { useRouteContext } from '/:core.jsx'
138
+
import { useRouteContext } from '@fastify/react/client'
139
139
140
140
export function getMeta () {
141
141
return { title: 'Todo List — Using Store' }
@@ -174,10 +174,12 @@ export default function Index (props) {
174
174
175
175
## Access hook
176
176
177
-
As shown in the snippets above, the **route context** is accessed via the **`useRouteContext()`** hook, implemented in the `core.js` internal file and made available via `/:core.js`[smart import](/react/project-structure#smart-imports), which allows it to be shadowed by your own implementation in your project directory.
177
+
As shown in the snippets above, the **route context** is accessed via the **`useRouteContext()`** hook, available via the `@fastfiy/react/client` module.
178
+
179
+
> Contrary to all other [virtual modules](https://fastify-vite.dev/config/react/virtual-modules), which are available via the `$app` prefix, the `useRouteContext()` hook is provided by `@fastify/react/client`. This file is responsible for defining the route context, the Valtio state and also provides a couple of helpers. It's the only part of the setup you're encouraged to avoid changing. But in highly customized setups, you could still provide your own `useRouteContext()` from a local module.
Copy file name to clipboardExpand all lines: docs/react/route-layouts.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,7 @@
2
2
3
3
`@fastify/react` will automatically load layouts from the `layouts/` folder.
4
4
5
-
By default, the `/:layouts/default.jsx`[**smart import**](/react/project-structure#smart-imports) is used. If a project is missing `/layouts/default.jsx` file, the one provided by the virtual module is automatically used. **The default layout is defined as follows**:
5
+
By default, the `$app/layouts/default.jsx`[**smart import**](/react/project-structure#smart-imports) is used. If a project is missing `/layouts/default.jsx` file, the one provided by the virtual module is automatically used. **The default layout is defined as follows**:
6
6
7
7
```jsx
8
8
import { Suspense } from'react'
@@ -26,7 +26,7 @@ That will cause the route to be wrapped in the layout component exported by a Re
Copy file name to clipboardExpand all lines: docs/react/route-modules.md
+76-11Lines changed: 76 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,7 @@
2
2
3
3
# Route Modules
4
4
5
-
Route modules are Vue components placed under the [route search path](/react/router-setup#routes-location), set to `<project-root>/pages` by default, or `client/pages` in the starter template, which follows the recommendation of using `client/` as the Vite project root.
5
+
Route modules are React components placed under the [route search path](/react/router-setup#routes-location), set to `<project-root>/pages` by default, or `client/pages` in the starter template, which follows the recommendation of using `client/` as the Vite project root.
6
6
7
7
Below is a rundown of supported exports:
8
8
@@ -22,7 +22,7 @@ Below is a rundown of supported exports:
22
22
</td>
23
23
<td>
24
24
25
-
The Vue component.
25
+
The React component.
26
26
27
27
</td>
28
28
</tr>
@@ -114,7 +114,7 @@ During client-side navigation (post first-render), a JSON request is fired to an
114
114
The objet returned by `getData()` gets automatically assigned as `data` in the [universal route context](/react/route-context) object and is accessible from `getMeta()` and `onEnter()` functions and also via the `useRouteContext()` hook.
115
115
116
116
```jsx
117
-
import { useRouteContext } from'/:core.jsx'
117
+
import { useRouteContext } from'$app/core.jsx'
118
118
119
119
exportfunctiongetData (ctx) {
120
120
return {
@@ -134,15 +134,80 @@ export default function Index (props) {
134
134
135
135
> Under the hood, it uses the [`unihead`](https://github.com/galvez/unihead) library, which has a SSR function and a browser library that allows for dynamic changes during client-side navigation. This is a very small library built specifically for `@fastify/vite` core renderers, and used in the current implementation of `createHtmlFunction()` for `@fastify/react`. This may change in the future as other libraries are considered, but for most use cases it should be enough.
136
136
137
-
To populate `<title>`, `<meta>` and `<link>`elements, export a `getMeta()` function that returns an object matching the interface expected by [unihead](https://github.com/galvez/unihead):
137
+
To populate `<head>`elements, export a `getMeta()` function that returns an object matching the interface expected by [unhead](https://github.com/unjs/unhead):
138
138
139
139
```ts
140
-
interfaceRouteMeta {
141
-
title?:string|null,
142
-
html?:Record<string, string> |null
143
-
body?:Record<string, string> |null
144
-
meta?:Record<string, string>[] |null,
145
-
link?:Record<string, string>[] |null,
140
+
interfaceReactiveHead {
141
+
/**
142
+
* The `<title>` HTML element defines the document's title that is shown in a browser's title bar or a page's tab.
143
+
* It only contains text; tags within the element are ignored.
Copy file name to clipboardExpand all lines: docs/react/router-setup.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,7 @@
4
4
5
5
By default, routes are loaded from the `<project-root>/pages` folder, where `<project-root>` refers to the `root` setting in your Vite configuration file.
6
6
7
-
The route paths are **dynamically inferred from the directory structure**, very much like **Next.js**, and passed to the **React Router** instance in `/:create.js`
7
+
The route paths are **dynamically inferred from the directory structure**, very much like **Next.js**, and passed to the **React Router** instance in `$app/create.js`
8
8
9
9
Alternatively, you can also export a `path` constant from your route modules, in which case it will be used to **override the dynamically inferred paths**:
0 commit comments