API
Extra route options
The RouteObject of vite-react-ssg is based on react-router, and vite-react-ssg receives some additional properties.
entry
Used to obtain static resources.If you introduce static resources (such as css files) in that route and use lazy loading (such as React.lazy or route.lazy), you should set the entry field. It should be the path from root to the target file.
eg: src/pages/page1.tsx
getStaticPaths
The getStaticPaths()
function should return an array of path
to determine which paths will be pre-rendered by vite-react-ssg.
This function is only valid for dynamic route.
const route = {
path: 'nest/:b',
Component: React.lazy(() => import('./pages/nest/[b]')),
entry: 'src/pages/nest/[b].tsx',
// To determine which paths will be pre-rendered
getStaticPaths: () => ['nest/b1', 'nest/b2'],
},
With React-router lazy
These options work well with the lazy
field.
// src/pages/[page].tsx
export function Component() {
return (
<div>{/* your component */}</div>
)
}
export function getStaticPaths() {
return ['page1', 'page2']
}
export const entry = 'src/pages/[page].tsx'
// src/routes.ts
const routes = [
{
path: '/:page',
lazy: () => import('./pages/[page]')
}
]
See example.