Skip to content

Commit 3f957cd

Browse files
committed
feat: create the example
0 parents  commit 3f957cd

23 files changed

+2770
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
dist/
2+
node_modules/

README.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# vite-plugin-ssr with Tanstack react query
2+
3+
This is a demo of how to use [vite-plugin-ssr](https://vite-plugin-ssr.com/) with [react-query](https://tanstack.com/query/latest/).
4+
5+
This is a very simple example just to show the integration between the two libraries.
6+
7+
## How to use
8+
9+
### Install dependencies
10+
11+
I used [pnpm](https://pnpm.io/) to install the dependencies, but you can use npm or yarn as well.
12+
13+
```bash
14+
pnpm install
15+
```
16+
17+
or
18+
19+
```bash
20+
yarn install
21+
```
22+
23+
or
24+
25+
```bash
26+
npm install
27+
```
28+
29+
### Run dev server
30+
31+
Use the "dev" script from package.json, be sure to have the 3000 port available.
32+
33+
### Build
34+
35+
Use the "build" script from package.json, the build will be in the "dist" folder.
36+
By default this example is using prerendering, you can change this in the "vite.config.js" file.
37+
38+
### Run
39+
40+
Use the "server:prod" to run the server in production mode. or use the "prod" script to build and run the server.
41+
42+
## How it works
43+
44+
The main idea is to use the "react-query" library to prefetch the data on the server side and then hydrate the client with the same data.
45+
46+
On the client we can reuse the same data to avoid fetching it again and then we are free to use the library to mutate or refetch the data as we want.
47+
48+
## Features
49+
50+
- React Query
51+
- Prerendering by default (can be disabled in vite.config.js)
52+
- Server Side Rendering is possible as well
53+
- TypeScript
54+
- Client routing provided by [vite-plugin-ssr](https://vite-plugin-ssr.com/)
55+
- Client refetching example with react-query
56+
57+
## Contributing
58+
59+
Pull requests and issues are welcome 😊!

package.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"scripts": {
3+
"dev": "npm run server",
4+
"prod": "npm run build && npm run server:prod",
5+
"build": "vite build",
6+
"server": "ts-node ./server/index.ts",
7+
"server:prod": "cross-env NODE_ENV=production ts-node ./server/index.ts"
8+
},
9+
"dependencies": {
10+
"@tanstack/react-query": "^4.29.1",
11+
"@tanstack/react-query-devtools": "^4.29.1",
12+
"@types/compression": "^1.7.2",
13+
"@types/express": "^4.17.14",
14+
"@types/node": "^18.11.9",
15+
"@types/react": "^18.0.8",
16+
"@types/react-dom": "^18.0.3",
17+
"@vitejs/plugin-react": "^3.0.0",
18+
"compression": "^1.7.4",
19+
"cross-env": "^7.0.3",
20+
"express": "^4.18.1",
21+
"react": "^18.2.0",
22+
"react-dom": "^18.2.0",
23+
"sirv": "^2.0.2",
24+
"ts-node": "^10.9.1",
25+
"typescript": "^4.9.4",
26+
"vite": "^4.0.3",
27+
"vite-plugin-ssr": "^0.4.112"
28+
},
29+
"type": "module"
30+
}

0 commit comments

Comments
 (0)