|
| 1 | +# React Starter Kit |
| 2 | + |
| 3 | +> A minimal, flexible React template built with Vite supporting multiple rendering modes |
| 4 | +
|
| 5 | +## ✨ Features |
| 6 | + |
| 7 | +- 🔄 **Multiple Rendering Modes**: SSR, SSG, and SPA support with route-level control |
| 8 | +- 🚀 **File-based API Routes**: Build serverless APIs with simple file structure |
| 9 | +- 🎯 **Framework-agnostic**: Pure React with Vite - no complex abstractions |
| 10 | +- 🔍 **SEO Ready**: Built-in meta tags and server-side rendering for better SEO |
| 11 | +- 📦 **Universal Deployment**: Compatible with Stormkit, Netlify, Vercel and more |
| 12 | +- ⚡ **Hot Module Replacement**: Instant updates during development |
| 13 | +- 🏷️ **TypeScript First**: Full TypeScript support out of the box |
| 14 | +- 🎨 **Modern Tooling**: Vite for lightning-fast builds and development |
| 15 | + |
| 16 | +## 🚀 Quick Start |
| 17 | + |
| 18 | +### Installation |
| 19 | + |
| 20 | +```bash |
| 21 | +# Clone or use as template |
| 22 | +git clone <repository-url> |
| 23 | +cd react-starter |
| 24 | + |
| 25 | +# Install dependencies |
| 26 | +npm install |
| 27 | +# or |
| 28 | +yarn install |
| 29 | +# or |
| 30 | +pnpm install |
| 31 | +``` |
| 32 | + |
| 33 | +### Development |
| 34 | + |
| 35 | +```bash |
| 36 | +npm run dev |
| 37 | +``` |
| 38 | + |
| 39 | +Visit `http://localhost:5173` to see your app running with HMR enabled. |
| 40 | + |
| 41 | +## 📁 Project Structure |
| 42 | + |
| 43 | +``` |
| 44 | +src/ |
| 45 | +├── api/ # API routes (serverless functions) |
| 46 | +│ └── hello.ts # Example API endpoint |
| 47 | +├── pages/ # Application pages |
| 48 | +│ ├── home.tsx # Home page (SPA) |
| 49 | +│ ├── about.tsx # About page (SPA) |
| 50 | +│ └── ssr.tsx # SSR example with fetchData |
| 51 | +├── components/ # Reusable components |
| 52 | +├── context.ts # React context for data sharing |
| 53 | +├── entry-client.tsx # Client-side entry point |
| 54 | +├── entry-server.tsx # Server-side entry point |
| 55 | +├── prerender.ts # SSG route configuration |
| 56 | +└── App.tsx # Main application component |
| 57 | +``` |
| 58 | + |
| 59 | +## 🔧 Build Commands |
| 60 | + |
| 61 | +### Development Server |
| 62 | + |
| 63 | +```bash |
| 64 | +npm run dev |
| 65 | +``` |
| 66 | + |
| 67 | +Starts development server with HMR at `http://localhost:5173` |
| 68 | + |
| 69 | +### Single Page Application (SPA) |
| 70 | + |
| 71 | +```bash |
| 72 | +npm run build:spa |
| 73 | +``` |
| 74 | + |
| 75 | +Builds a traditional SPA. Output: `.stormkit/public/` |
| 76 | + |
| 77 | +### Server-Side Rendering (SSR) |
| 78 | + |
| 79 | +```bash |
| 80 | +npm run build:ssr |
| 81 | +``` |
| 82 | + |
| 83 | +Builds for serverless deployment with SSR. Output: `.stormkit/server/` |
| 84 | + |
| 85 | +### Static Site Generation (SSG) |
| 86 | + |
| 87 | +```bash |
| 88 | +npm run build:spa # Build SPA first |
| 89 | +npm run build:ssg # Generate static pages |
| 90 | +``` |
| 91 | + |
| 92 | +Pre-renders specified routes at build time. Output: `.stormkit/public/` |
| 93 | + |
| 94 | +### API Only |
| 95 | + |
| 96 | +```bash |
| 97 | +npm run build:api |
| 98 | +``` |
| 99 | + |
| 100 | +Builds only the API functions. Output: `.stormkit/api/` |
| 101 | + |
| 102 | +## 🎯 Rendering Modes |
| 103 | + |
| 104 | +### Single Page Application (Default) |
| 105 | + |
| 106 | +All routes are client-side rendered by default: |
| 107 | + |
| 108 | +```tsx |
| 109 | +// src/pages/home.tsx |
| 110 | +export default function Home() { |
| 111 | + return <h1>Welcome to Home</h1>; |
| 112 | +} |
| 113 | +``` |
| 114 | + |
| 115 | +### Server-Side Rendering |
| 116 | + |
| 117 | +Add a `fetchData` export to enable SSR: |
| 118 | + |
| 119 | +```tsx |
| 120 | +import { useContext } from "react"; |
| 121 | +import Context from "~/context"; |
| 122 | + |
| 123 | +// src/pages/ssr.tsx |
| 124 | +export async function fetchData() { |
| 125 | + const data = await fetch("https://api.example.com/data"); |
| 126 | + return { |
| 127 | + head: { |
| 128 | + // meta tags |
| 129 | + }, |
| 130 | + context: { |
| 131 | + myParam: data.myParam; |
| 132 | + } |
| 133 | + }; |
| 134 | +} |
| 135 | + |
| 136 | +export default function SSRPage({ data }: { data: any }) { |
| 137 | + const context = useContext(Context); |
| 138 | + return <h1>Server-rendered: {data.myParam}</h1>; |
| 139 | +} |
| 140 | +``` |
| 141 | + |
| 142 | +### Static Site Generation |
| 143 | + |
| 144 | +Configure routes to pre-render in `src/prerender.ts`: |
| 145 | + |
| 146 | +```tsx |
| 147 | +// src/prerender.ts |
| 148 | + |
| 149 | +// Export an array of paths to be prerendered. |
| 150 | +export default ["/", "/about", "/blog/post-1"]; |
| 151 | +``` |
| 152 | + |
| 153 | +## 🔌 API Routes |
| 154 | + |
| 155 | +Create API endpoints by adding files to `src/api/`: |
| 156 | + |
| 157 | +```typescript |
| 158 | +// src/api/hello.ts |
| 159 | +export default async (req: http.IncomingMessage, res: http.ServerResponse) => { |
| 160 | + res.setHeader("Content-Type", "application/json"); |
| 161 | + res.writeHead(200, "Success"); |
| 162 | + res.write( |
| 163 | + JSON.stringify({ |
| 164 | + payload: |
| 165 | + "This is an API function - can be deployed as a serverless function!", |
| 166 | + }) |
| 167 | + ); |
| 168 | + res.end(); |
| 169 | +}; |
| 170 | +``` |
| 171 | + |
| 172 | +Access at: `http://localhost:5173/api/hello` |
| 173 | + |
| 174 | +## 🚀 Deployment |
| 175 | + |
| 176 | +### Stormkit |
| 177 | + |
| 178 | +Import this application on Stormkit (either self-hosted or cloud) and simply click deploy. It works with zero-config. |
| 179 | + |
| 180 | +### Static Hosting |
| 181 | + |
| 182 | +```bash |
| 183 | +npm run build:spa |
| 184 | +npm run build:ssg # Optional: for pre-rendered pages |
| 185 | +``` |
| 186 | + |
| 187 | +Deploy the `.stormkit/public` folder. |
| 188 | + |
| 189 | +## 🔧 Configuration |
| 190 | + |
| 191 | +### Vite Configuration |
| 192 | + |
| 193 | +- `vite.config.ts` - Development server |
| 194 | +- `vite.config.ssr.ts` - SSR build |
| 195 | +- `vite.config.spa.ts` - SPA build |
| 196 | +- `vite.config.api.ts` - API build |
| 197 | + |
| 198 | +## 🛠️ Advanced Usage |
| 199 | + |
| 200 | +### Custom Server |
| 201 | + |
| 202 | +```typescript |
| 203 | +// server.ts |
| 204 | +import { handler } from "./.stormkit/server/server.mjs"; |
| 205 | + |
| 206 | +const server = express(); |
| 207 | +server.use(handler); |
| 208 | +server.listen(3000); |
| 209 | +``` |
| 210 | + |
| 211 | +## 🤝 Contributing |
| 212 | + |
| 213 | +1. Fork the repository |
| 214 | +2. Create your feature branch: `git checkout -b feature/amazing-feature` |
| 215 | +3. Commit your changes: `git commit -m 'Add amazing feature'` |
| 216 | +4. Push to the branch: `git push origin feature/amazing-feature` |
| 217 | +5. Open a Pull Request |
| 218 | + |
| 219 | +## 📚 Resources |
| 220 | + |
| 221 | +- [Vite Documentation](https://vitejs.dev/) |
| 222 | +- [React Documentation](https://react.dev/) |
| 223 | +- [Stormkit Documentation](https://stormkit.io/docs) |
| 224 | + |
| 225 | +## 🌟 Showcase |
| 226 | + |
| 227 | +Websites built with this template: |
| 228 | + |
| 229 | +| Site | Description | Features Used | |
| 230 | +| -------------------------------------------------------- | --------------------------------- | --------------- | |
| 231 | +| [Stormkit.io](https://stormkit.io) | Deploy full-stack JavaScript apps | SSR, API Routes | |
| 232 | +| [Add your site](https://github.com/your-repo/issues/new) | Submit your project | - | |
| 233 | + |
| 234 | +## 📄 License |
| 235 | + |
| 236 | +MIT © |
0 commit comments