Skip to content

Commit 551ff7b

Browse files
committed
sync vue/react, starters
1 parent ad86ee5 commit 551ff7b

19 files changed

+168
-89
lines changed

docs/react/project-structure.md

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,13 @@ The Vite application HTML template:
9999
<div id="root"><!-- element --></div>
100100
</body>
101101
<!-- hydration -->
102-
<script type="module" src="/:mount.js"></script>
102+
<script type="module" src="/$app/mount.js"></script>
103103
</html>
104104
```
105105

106106
It needs to have `head`, `element` and `hydration` placeholders.
107107

108-
And it must import `/:mount.js` as the main module.
108+
And it must import `/$app/mount.js` as the main module.
109109

110110
</td>
111111
</tr>
@@ -167,11 +167,11 @@ export default {
167167
}
168168
```
169169
```js [client/index.js]
170-
import routes from '/:routes.js'
171-
import create from '/:create.jsx'
170+
import routes from '$app/routes.js'
171+
import create from '$app/create.jsx'
172172

173173
export default {
174-
context: import('/:context.js'),
174+
context: import('$app/context.js'),
175175
routes,
176176
create,
177177
}
@@ -188,7 +188,7 @@ export default {
188188
<div id="root"><!-- element --></div>
189189
</body>
190190
<!-- hydration -->
191-
<script type="module" src="/:mount.js"></script>
191+
<script type="module" src="/$app/mount.js"></script>
192192
</html>
193193
```
194194
```js [client/pages/index.jsx]
@@ -250,11 +250,11 @@ The core files of **`@fastify/react`** that make all of that (and a bit more) wo
250250
251251
<div style="font-size: 1.2em !important">
252252
253-
The way this works is via the `/:` prefix.
253+
The way this works is via the `$app/` prefix.
254254
255255
</div>
256256
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`.
258258
259259
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.
260260
@@ -271,12 +271,12 @@ Below is a quick rundown of all smart imports available.
271271
<tr>
272272
<td>
273273
274-
`/:core.js`
274+
`$app/core.js`
275275
276276
</td>
277277
<td>
278278
279-
This is used by `/:create.jsx` internally to create your React application instance, but it's also the location where to import the [`useRouteContext()`](/react/route-context) hook from.
279+
This is used by `$app/create.jsx` internally to create your React application instance.
280280
281281
It also exports the `isServer` convenience flag.
282282
@@ -285,7 +285,7 @@ It also exports the `isServer` convenience flag.
285285
<tr>
286286
<td>
287287
288-
`/:create.jsx`
288+
`$app/create.jsx`
289289
290290
</td>
291291
<td>
@@ -297,7 +297,7 @@ Where your React application factory function is exported from. It must be named
297297
<tr>
298298
<td>
299299
300-
`/:mount.js`
300+
`$app/mount.js`
301301
302302
</td>
303303
<td>
@@ -309,7 +309,7 @@ The Vite application mount script, imported by `index.html`.
309309
<tr>
310310
<td>
311311
312-
`/:resource.js`
312+
`$app/resource.js`
313313
314314
</td>
315315
<td>
@@ -321,7 +321,7 @@ Utilities to enable suspensed state in data fetching.
321321
<tr>
322322
<td>
323323
324-
`/:root.jsx`
324+
`$app/root.jsx`
325325
326326
</td>
327327
<td>
@@ -333,7 +333,7 @@ The main React component for your application.
333333
<tr>
334334
<td>
335335
336-
`/:context.js`
336+
`$app/context.js`
337337
338338
</td>
339339
<td>
@@ -345,7 +345,7 @@ The [route context](/react/route-context) initialization file.
345345
<tr>
346346
<td>
347347
348-
`/:layouts/default.jsx`
348+
`$app/layouts/default.jsx`
349349
350350
</td>
351351
<td>
@@ -363,15 +363,15 @@ The graph below indicates the relationships between them:
363363
364364
```mermaid
365365
flowchart TD
366-
R("/:resource.js") --> A
367-
V("/:root.jsx") --> B
368-
A("/:core.jsx") --> B("/:create.jsx")
369-
C("/:routes.js") --> D
366+
R("$app/resource.js") --> A
367+
V("$app/root.jsx") --> B
368+
A("$app/core.jsx") --> B("$app/create.jsx")
369+
C("$app/routes.js") --> D
370370
C --> E
371-
B --> D("/:mount.js")
372-
B --> E("/:index.js")
373-
T("/:context.js") --> D
374-
T("/:context.js") --> E
371+
B --> D("$app/mount.js")
372+
B --> E("$app/index.js")
373+
T("$app/context.js") --> D
374+
T("$app/context.js") --> E
375375
D --> CSR("Client-Side Rendering")
376376
E --> SSR("Server-Side Rendering")
377377
```

docs/react/route-context.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
# Route Context
44

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.
66

77
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.
88

@@ -15,7 +15,7 @@ import FastifyReact from '@fastify/react'
1515

1616
const server = Fastify()
1717
await server.register(FastifyVite, {
18-
root: import.meta.url,
18+
root: import.meta.dirname,
1919
renderer: {
2020
...FastifyReact,
2121
createRoute () {
@@ -86,7 +86,7 @@ export const actions = {
8686
```jsx [client/pages/using-data.jsx]
8787
import { useState } from 'react'
8888
import { Link } from 'react-router'
89-
import { useRouteContext } from '/:core.jsx'
89+
import { useRouteContext } from '@fastify/react/client'
9090

9191
export function getMeta () {
9292
return { title: 'Todo List — Using Data' }
@@ -135,7 +135,7 @@ export default function Index (props) {
135135
```jsx [client/pages/using-store.jsx]
136136
import { useState } from 'react'
137137
import { Link } from 'react-router'
138-
import { useRouteContext } from '/:core.jsx'
138+
import { useRouteContext } from '@fastify/react/client'
139139
140140
export function getMeta () {
141141
return { title: 'Todo List — Using Store' }
@@ -174,10 +174,12 @@ export default function Index (props) {
174174

175175
## Access hook
176176

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.
178180
179181
```js
180-
import { useRouteContext } from '/:core.jsx'
182+
import { useRouteContext } from '@fastify/react/client'
181183

182184
export default function Index (props) {
183185
const { ... } = useRouteContext()

docs/react/route-layouts.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
`@fastify/react` will automatically load layouts from the `layouts/` folder.
44

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**:
66

77
```jsx
88
import { Suspense } from 'react'
@@ -26,7 +26,7 @@ That will cause the route to be wrapped in the layout component exported by a Re
2626

2727
```jsx
2828
import { Suspense } from 'react'
29-
import { useRouteContext } from '/:core.jsx'
29+
import { useRouteContext } from '@fastify/react/client'
3030

3131
export default function Auth ({ children }) {
3232
const { actions, state, snapshot } = useRouteContext()

docs/react/route-modules.md

Lines changed: 76 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
# Route Modules
44

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.
66

77
Below is a rundown of supported exports:
88

@@ -22,7 +22,7 @@ Below is a rundown of supported exports:
2222
</td>
2323
<td>
2424

25-
The Vue component.
25+
The React component.
2626

2727
</td>
2828
</tr>
@@ -114,7 +114,7 @@ During client-side navigation (post first-render), a JSON request is fired to an
114114
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.
115115

116116
```jsx
117-
import { useRouteContext } from '/:core.jsx'
117+
import { useRouteContext } from '$app/core.jsx'
118118

119119
export function getData (ctx) {
120120
return {
@@ -134,15 +134,80 @@ export default function Index (props) {
134134

135135
> 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.
136136
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):
138138

139139
```ts
140-
interface RouteMeta {
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+
interface ReactiveHead {
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.
144+
*
145+
* @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/title
146+
*/
147+
title?: ResolvableTitle;
148+
/**
149+
* Generate the title from a template.
150+
*/
151+
titleTemplate?: ResolvableTitleTemplate;
152+
/**
153+
* Variables used to substitute in the title and meta content.
154+
*/
155+
templateParams?: ResolvableProperties<{
156+
separator?: '|' | '-' | '·' | string;
157+
} & Record<string, Stringable | ResolvableProperties<Record<string, Stringable>>>>;
158+
/**
159+
* The `<base>` HTML element specifies the base URL to use for all relative URLs in a document.
160+
* There can be only one <base> element in a document.
161+
*
162+
* @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base
163+
*/
164+
base?: ResolvableBase;
165+
/**
166+
* The `<link>` HTML element specifies relationships between the current document and an external resource.
167+
* This element is most commonly used to link to stylesheets, but is also used to establish site icons
168+
* (both "favicon" style icons and icons for the home screen and apps on mobile devices) among other things.
169+
*
170+
* @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/link#attr-as
171+
*/
172+
link?: ResolvableArray<ResolvableLink>;
173+
/**
174+
* The `<meta>` element represents metadata that cannot be expressed in other HTML elements, like `<link>` or `<script>`.
175+
*
176+
* @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/meta
177+
*/
178+
meta?: ResolvableArray<ResolvableMeta>;
179+
/**
180+
* The `<style>` HTML element contains style information for a document, or part of a document.
181+
* It contains CSS, which is applied to the contents of the document containing the `<style>` element.
182+
*
183+
* @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/style
184+
*/
185+
style?: ResolvableArray<(ResolvableStyle | string)>;
186+
/**
187+
* The `<script>` HTML element is used to embed executable code or data; this is typically used to embed or refer to JavaScript code.
188+
*
189+
* @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script
190+
*/
191+
script?: ResolvableArray<(ResolvableScript | string)>;
192+
/**
193+
* The `<noscript>` HTML element defines a section of HTML to be inserted if a script type on the page is unsupported
194+
* or if scripting is currently turned off in the browser.
195+
*
196+
* @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/noscript
197+
*/
198+
noscript?: ResolvableArray<(ResolvableNoscript | string)>;
199+
/**
200+
* Attributes for the `<html>` HTML element.
201+
*
202+
* @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/html
203+
*/
204+
htmlAttrs?: ResolvableHtmlAttributes;
205+
/**
206+
* Attributes for the `<body>` HTML element.
207+
*
208+
* @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/body
209+
*/
210+
bodyAttrs?: ResolvableBodyAttributes;
146211
}
147212
```
148213

@@ -163,7 +228,7 @@ export function getMeta (ctx) {
163228
return {
164229
title: ctx.data.page.title,
165230
meta: [
166-
{ name: 'twitter:title', value: ctx.data.page.title },
231+
{ name: 'twitter:title', content: ctx.data.page.title },
167232
]
168233
}
169234
}

docs/react/router-setup.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
By default, routes are loaded from the `<project-root>/pages` folder, where `<project-root>` refers to the `root` setting in your Vite configuration file.
66

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`
88

99
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**:
1010

0 commit comments

Comments
 (0)