vite-plugin-ssr-config 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,244 @@
1
+ # vite-plugin-ssr-config
2
+
3
+ [vite-plugin-ssr-config](https://github.com/yracnet/vite-plugin-ssr-config) configures server-side rendering (SSR) with Vite, providing essential setups and options for building both client and server bundles, along with necessary React components for SSR, specifically for [React](https://reactjs.org/ "react - A JavaScript library for building user interfaces"), [React Router](https://reactrouter.com/ "react-router - Declarative routing for React"), and [React Query](https://tanstack.com/query "react-query - Data fetching and caching library").
4
+
5
+ ## Additional Resources
6
+
7
+ For more detailed information and resources related to `vite-plugin-ssr-config`, please refer to the following:
8
+
9
+ - **npm Package**: [vite-plugin-ssr-config](https://www.npmjs.com/package/vite-plugin-ssr-config)
10
+ - **GitHub Repository**: [yracnet/vite-plugin-ssr-config](https://github.com/yracnet/vite-plugin-ssr-config)
11
+ - **Dev.to Article**: [Create an SSR Application with Vite, React, React Query and React Router](https://dev.to/yracnet/create-an-ssr-application-with-vite-react-react-query-and-react-router-2dd5)
12
+ - **Tutorial**: [Tutorial](./tutorial.md)
13
+
14
+ ## Install
15
+
16
+ To add this plugin to your project, run the following commands:
17
+
18
+ ```bash
19
+ yarn add vite-plugin-ssr-config vite-plugin-pages react-router-dom -D
20
+ ```
21
+
22
+ This will install:
23
+
24
+ - vite-plugin-ssr-config: The plugin for server-side rendering (SSR) with Vite.
25
+ - vite-plugin-pages: Automatically generate route files for your pages.
26
+ - react-router-dom: The routing library for React, used to manage navigation within the app.
27
+
28
+ ```bash
29
+ yarn add react-router-dom
30
+ ```
31
+
32
+ ## Basic Configuration Example
33
+
34
+ To use the plugin, you need to integrate it with Vite’s `defineConfig` method and add it to the plugins array. Here’s the basic configuration example for a React SSR project:
35
+
36
+ ```typescript
37
+ import { defineConfig } from "vite";
38
+ import react from "@vitejs/plugin-react";
39
+ import pages from "vite-plugin-pages";
40
+ import ssr from "vite-plugin-ssr-config";
41
+
42
+ export default defineConfig({
43
+ plugins: [react(), pages(), ssr()],
44
+ });
45
+ ```
46
+
47
+ ## Default Configuration
48
+
49
+ The following default values are provided for each configurable attribute in the plugin:
50
+
51
+ ```typescript
52
+ import { defineConfig } from "vite";
53
+ import react from "@vitejs/plugin-react";
54
+ import pages from "vite-plugin-pages";
55
+ import ssr from "vite-plugin-ssr-config";
56
+
57
+ export default defineConfig({
58
+ plugins: [
59
+ react(),
60
+ pages(),
61
+ ssr({
62
+ root: process.cwd(), // Root directory, typically the project root.
63
+
64
+ // React-related files
65
+ entryClient: ".ssr/entryClient.jsx", // Entry point for the client-side app.
66
+ entryRender: ".ssr/entryRender.jsx", // Entry point for the server-side app.
67
+ rootDocument: ".ssr/root.jsx", // Root document for React SSR.
68
+
69
+ // Server-side files
70
+ server: ".ssr/server.js", // Main server file.
71
+ handler: ".ssr/handler.js", // Request handler for SSR.
72
+
73
+ // React SSR-specific files
74
+ pageServer: ".ssr/pageServer.jsx", // Server-side page rendering.
75
+ pageBrowser: ".ssr/pageBrowser.jsx", // Browser-side page rendering.
76
+ rootRoutes: ".ssr/rootRoutes.jsx", // Root routes for SSR.
77
+ errorBoundary: ".ssr/errorBoundary.jsx", // Error boundary for SSR rendering.
78
+
79
+ // Scripts
80
+ liveReload: ".ssr/liveReload.jsx", // Script for live reloading.
81
+ viteScripts: ".ssr/viteScripts.jsx", // Vite-related scripts.
82
+
83
+ // Output directories
84
+ clientOutDir: "dist/client", // Client-side output directory.
85
+ serverOutDir: "dist", // Server-side output directory.
86
+
87
+ // Build options
88
+ clientMinify: true, // Whether to minify the client-side code.
89
+ serverMinify: false, // Whether to minify the server-side code.
90
+ disableBuild: false, // Whether to disable the build process entirely.
91
+
92
+ // Config callbacks
93
+ clientBuild: (config: UserConfig) => config, // Client-side Vite configuration.
94
+ serverBuild: (config: UserConfig) => config, // Server-side Vite configuration.
95
+ }),
96
+ ],
97
+ });
98
+ ```
99
+
100
+ > Important Note: PageServer uses `suspense: true` in all requests to ensure proper SSR rendering. On the other hand, PageBrowser uses `suspense: false` to allow smooth client-side navigation. This setup guarantees correct SSR rendering while preventing flickering and inconsistencies between the server-rendered content and the client-side state during hydration.
101
+
102
+ ## Customization
103
+
104
+ You can customize the default values by providing your own configuration for the plugin in your `vite.config.js` file. For example, to change the entry point for your server-side app, you would set the `entryRender` value:
105
+
106
+ ```typescript
107
+ ssr({
108
+ entryRender: "src/server/entryRender.js",
109
+ });
110
+ ```
111
+
112
+ This allows you to tailor the plugin to your project’s specific needs, including modifying file paths and directories for SSR output, live reload functionality, and more.
113
+
114
+ # Configuration Options
115
+
116
+ Here are all the configurable options available with `vite-plugin-ssr-config`:
117
+
118
+ ### `root` (string)
119
+
120
+ The root directory of your project. Defaults to the current working directory (`process.cwd()`).
121
+
122
+ ### `entryClient` (string)
123
+
124
+ The entry point for the client-side application. Defaults to `.ssr/entryClient.jsx`.
125
+
126
+ ### `entryRender` (string)
127
+
128
+ The entry point for the server-side application. Defaults to `.ssr/entryRender.jsx`.
129
+
130
+ ### `rootDocument` (string)
131
+
132
+ The root document for React SSR. Defaults to `.ssr/root.jsx`.
133
+
134
+ ### `server` (string)
135
+
136
+ The main server file. Defaults to `.ssr/server.js`.
137
+
138
+ ### `handler` (string)
139
+
140
+ The request handler file for SSR. Defaults to `.ssr/handler.js`.
141
+
142
+ ### `pageServer` (string)
143
+
144
+ The server-side page rendering file. Defaults to `.ssr/pageServer.jsx`.
145
+
146
+ ### `pageBrowser` (string)
147
+
148
+ The browser-side page rendering file. Defaults to `.ssr/pageBrowser.jsx`.
149
+
150
+ ### `rootRoutes` (string)
151
+
152
+ The root routes for SSR. Defaults to `.ssr/rootRoutes.jsx`.
153
+
154
+ ### `errorBoundary` (string)
155
+
156
+ The error boundary for SSR rendering. Defaults to `.ssr/errorBoundary.jsx`.
157
+
158
+ ### `liveReload` (string)
159
+
160
+ The script for live reloading. Defaults to `.ssr/liveReload.jsx`.
161
+
162
+ ### `viteScripts` (string)
163
+
164
+ The Vite-related scripts file. Defaults to `.ssr/viteScripts.jsx`.
165
+
166
+ ### `clientOutDir` (string)
167
+
168
+ The output directory for the client-side bundle. Defaults to `dist/client`.
169
+
170
+ ### `serverOutDir` (string)
171
+
172
+ The output directory for the server-side bundle. Defaults to `dist`.
173
+
174
+ ### `clientMinify` (boolean | "terser" | "esbuild")
175
+
176
+ Controls whether to minify the client-side code. Defaults to `true`.
177
+
178
+ ### `serverMinify` (boolean | "terser" | "esbuild")
179
+
180
+ Controls whether to minify the server-side code. Defaults to `false`.
181
+
182
+ ### `disableBuild` (boolean)
183
+
184
+ Whether to disable the build process entirely. Defaults to `false`.
185
+
186
+ ### `clientBuild` (function)
187
+
188
+ A callback to customize the client-side Vite build configuration. Defaults to an identity function `(config) => config`.
189
+
190
+ ### `serverBuild` (function)
191
+
192
+ A callback to customize the server-side Vite build configuration. Defaults to an identity function `(config) => config`.
193
+
194
+ # Execution and Compilation
195
+
196
+ The following commands are available in the `package.json` file to manage development, builds, and previewing your Vite project with SSR. These commands utilize custom build modes, providing flexibility in how the project is built for SSR (Server-Side Rendering) and client-side code.
197
+
198
+ ## package.json
199
+
200
+ ```json
201
+ {
202
+ "scripts": {
203
+ "dev": "vite",
204
+ "build": "vite build",
205
+ "preview": "vite preview"
206
+ }
207
+ }
208
+ ```
209
+
210
+ ## Commands
211
+
212
+ ### `dev`
213
+
214
+ Runs the Vite development server for the project in development mode.
215
+
216
+ ```bash
217
+ npm run dev
218
+ ```
219
+
220
+ This will start the Vite development server with hot module replacement (HMR), allowing you to preview your application in a local environment.
221
+
222
+ ### `build`
223
+
224
+ Builds the application with server-side rendering and client-side rendering. This command runs two different build client and server-side bundles.
225
+
226
+ ```bash
227
+ npm run build
228
+ ```
229
+
230
+ It consists of:
231
+
232
+ 1. **`clean`**: Cleans the existing build files.
233
+ 2. **`client`**: Builds the client-side bundle.
234
+ 3. **`server`**: Builds the server-side bundle.
235
+
236
+ This is typically used for SSR applications where both the server and client code need to be built separately.
237
+
238
+ ## Summary
239
+
240
+ These custom commands are designed to provide flexibility in your Vite SSR workflow. Whether you need a full build with SSR or just client or server-side parts, these scripts allow you to run specific builds with ease.
241
+
242
+ ## Use Case
243
+
244
+ This plugin is intended for projects that require SSR with Vite, specifically React apps. It helps in managing SSR entry files, routing, page rendering, and output structure for both server and client builds.