Skip to content

Commit f239001

Browse files
committed
doc: improve Advanced FS Routing doc
1 parent 0b86214 commit f239001

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

doc-site/pages/advanced-fs-routing$.mdx

+12-6
Original file line numberDiff line numberDiff line change
@@ -30,25 +30,29 @@ As shown by the above example, here is the usual steps to customize pageStrategy
3030
1. Define a `findPages` function and pass it to `PageStrategy` constructor.
3131
2. Inside the `findPages`, use `helpers.watchFiles(baseDir, glob, fileHandler)` to find the files that you need.
3232
- vite-pages will pass the glob(or glob array) to [chokidar](https://github.com/paulmillr/chokidar). vite-pages use chokidar to scan the fileSystem and watch for files.
33-
- Whenever a file is scaned, added or updated, vite-pages will call the fileHandler with that file. When the file is unlinked, vite-pages will automatically delete the related page data.
34-
3. Inside the `fileHandler`, read the infomation from `file` and register page data by calling `fileHandlerAPI.addPageData`.
33+
- Whenever a file is scaned, added or updated, vite-pages will call the `fileHandler` with that file. When the file is deleted, vite-pages will automatically delete the related page data.
34+
3. Inside the `fileHandler`, read the infomation from the `file` and register page data by calling `fileHandlerAPI.addPageData`.
3535
- There are two more helpers inside `fileHandlerAPI` that help you to update page data. We will introduce them in the following section.
3636

37-
### Handle file change and update page data
37+
### Handle file events and update page data
3838

3939
The `fileHandler` should conform to this interface:
4040

4141
```ts
4242
type FileHandler = (
4343
file: File,
44-
api: PageAPIs
44+
fileHandlerAPI: PageAPIs
4545
) => void | Promise<void> | DataPiece | Promise<DataPiece>
4646
```
4747
48-
The `HandlerAPI` contains a set of helpers that help you to update page data.
48+
It will be called when a file is added or updated. You should extract [page data](/page-data) from this file.
49+
50+
The `fileHandlerAPI` contains a set of helpers that help you to update page data.
4951
5052
#### fileHandlerAPI.addPageData(dataPiece)
5153
54+
When you have extracted some [page data](/page-data) (which is called dataPiece) in the `fileHandler`, call `addPageData` to register the data. So that vite-pages will load the data into theme for rendering.
55+
5256
The dataPiece should conform to this interface:
5357
5458
```ts
@@ -89,7 +93,9 @@ interface DataPiece {
8993
}
9094
```
9195

92-
In most cases, `dataPath` is the path of the currently handled file. And `staticData` is statically extracted from the file content (js docblock or markdown frontmatter). Vite-pages has exported a helper `extractStaticData` to do that.
96+
In normal cases, `dataPath` is the path of the currently handled file. And `staticData` is statically extracted from the file content (js docblock or markdown frontmatter). Vite-pages exports a helper `extractStaticData` to do that.
97+
98+
> When a watched file is updated, before calling `fileHandler`, vite-pages will automatically unlink all the previous registered page data from this file. So you don't need to worry about "outdated data from old version of files".
9399
94100
Checkout [the custom-find-pages2 fixture](https://github.com/vitejs/vite-plugin-react-pages/blob/main/packages/playground/custom-find-pages2/vite.config.ts) for an example.
95101

0 commit comments

Comments
 (0)