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
Copy file name to clipboardexpand all lines: doc-site/pages/advanced-fs-routing$.mdx
+12-6
Original file line number
Diff line number
Diff line change
@@ -30,25 +30,29 @@ As shown by the above example, here is the usual steps to customize pageStrategy
30
30
1. Define a `findPages` function and pass it to `PageStrategy` constructor.
31
31
2. Inside the `findPages`, use `helpers.watchFiles(baseDir, glob, fileHandler)` to find the files that you need.
32
32
- 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`.
35
35
- There are two more helpers inside `fileHandlerAPI` that help you to update page data. We will introduce them in the following section.
36
36
37
-
### Handle file change and update page data
37
+
### Handle file events and update page data
38
38
39
39
The `fileHandler` should conform to this interface:
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.
49
51
50
52
#### fileHandlerAPI.addPageData(dataPiece)
51
53
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
+
52
56
The dataPiece should conform to this interface:
53
57
54
58
```ts
@@ -89,7 +93,9 @@ interface DataPiece {
89
93
}
90
94
```
91
95
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".
93
99
94
100
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.
0 commit comments