# vite-react-ssg
**Repository Path**: ccvv22/vite-react-ssg
## Basic Information
- **Project Name**: vite-react-ssg
- **Description**: No description available
- **Primary Language**: JavaScript
- **License**: MIT
- **Default Branch**: main
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 0
- **Created**: 2024-04-16
- **Last Updated**: 2024-04-16
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
# Vite React SSG
Static-site generation for React on Vite.
See demo(also document): [docs](https://vite-react-ssg.netlify.app/)
**🎈 Support for [`@tanstack/router`](https://tanstack.com/router/latest/docs/framework/react/overview) and [`wouter`](https://github.com/molefrog/wouter) is in progress!**
[](https://www.npmjs.com/package/vite-react-ssg)
# Table of contents
- [Usage](#usage)
- [Use CSR during development](#use-csr-during-development)
- [Extra route options](#extra-route-options)
- [`entry`](#entry)
- [`getStaticPaths`](#getstaticpaths)
- [Data fetch](#data-fetch)
- [lazy](#lazy)
- [``](#clientonly)
- [Document head](#document-head)
- [Reactive head](#reactive-head)
- [Public Base Path](#public-base-path)
- [CSS in JS](#css-in-js)
- [Critical CSS](#critical-css)
- [Configuration](#configuration)
- [Custom Routes to Render](#custom-routes-to-render)
- [Https](#https)
- [Roadmap](#roadmap)
- [Credits](#credits)
## Usage
npm i -D vite-react-ssgreact-router-dom
```diff
// package.json
{
"scripts": {
- "build": "vite build"
+ "build": "vite-react-ssg build"
// If you need ssr when dev
- "dev": "vite",
+ "dev": "vite-react-ssg dev",
// OR if you want to use another vite config file
+ "build": "vite-react-ssg build -c another-vite.config.ts"
}
}
```
```ts
// src/main.ts
import { ViteReactSSG } from 'vite-react-ssg'
import routes from './App.tsx'
export const createRoot = ViteReactSSG(
// react-router-dom data routes
{ routes },
// function to have custom setups
({ router, routes, isClient, initialState }) => {
// do something.
},
)
```
```tsx
// src/App.tsx
import React from 'react'
import type { RouteRecord } from 'vite-react-ssg'
import './App.css'
const Layout = React.lazy(() => import('./Layout'))
export const routes: RouteRecord[] = [
{
path: '/',
element: ,
entry: 'src/Layout.tsx',
children: [
{
path: 'a',
Component: React.lazy(() => import('./pages/a')),
entry: 'src/pages/a.tsx',
},
{
index: true,
Component: React.lazy(() => import('./pages/index')),
// Used to obtain static resources through manifest
entry: 'src/pages/index.tsx',
},
{
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'],
},
],
},
]
```
### Use CSR during development
Vite React SSG provide SSR (Server-Side Rendering) during development to ensure consistency between development and production as much as possible.
But if you want to use CSR during development, just:
```diff
// package.json
{
"scripts": {
- "dev": "vite-react-ssg dev",
+ "dev": "vite",
"build": "vite-react-ssg build"
}
}
```
### Single Page SSG
For SSG of an index page only (i.e. without `react-router-dom`); import `vite-react-ssg/single-page` instead.
```tsx
// src/main.tsx
import { ViteReactSSG } from 'vite-react-ssg/single-page'
import App from './App.tsx'
export const createRoot = ViteReactSSG()
```
## 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.
```tsx
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'],
},
```
## lazy
These options work well with the `lazy` field.
```tsx
// src/pages/[page].tsx
export function Component() {
return (
{/* your component */}
)
}
export function getStaticPaths() {
return ['page1', 'page2']
}
export const entry = 'src/pages/[page].tsx'
```
```ts
// src/routes.ts
const routes = [
{
path: '/:page',
lazy: () => import('./pages/[page]')
}
]
```
See [example](./examples/lazy-pages/src/App.tsx).
## Data fetch
You can use react-router-dom's `loader` to fetch data at build time and use `useLoaderData` to get the data in the component.
See [example | with-loader](./examples/with-loader/src/pages/[docs].tsx).
## ``
If you need to render some component in browser only, you can wrap your component with ``.
```tsx
import { ClientOnly } from 'vite-react-ssg'
function MyComponent() {
return (
{() => {
return