abiriadev / seia

:eight_pointed_black_star: Lightweight SSR framework for React Server Components
https://seia.dev
MIT License
79 stars 7 forks source link
react-server-components rsc server-components ssr vite vitejs
# Seia ![React](https://img.shields.io/badge/react%2019-%2320232a.svg?style=for-the-badge&logo=react&logoColor=%2361DAFB) ![Vite](https://img.shields.io/badge/vite-%23646CFF.svg?style=for-the-badge&logo=vite&logoColor=white) ![Vite](https://img.shields.io/badge/swc-%23F8C557.svg?style=for-the-badge&logo=swc&logoColor=333333) ![Hono](https://img.shields.io/badge/hono-%23E36002.svg?style=for-the-badge&logo=hono&logoColor=white) _Lightweight SSR framework for React Server Components_ [Quickstart](#rocket-quickstart) • [Try demo](./examples/blue-archive-students) • [Documentation](https://seia.dev) • [Wiki](https://github.com/abiriadev/seia/wiki) • [Community](https://discord.gg/xuSH6QuQwG)

:sparkles: Key Features

:rocket: Quickstart

We provide the create-seia-app wizard to help you get your new project up and running quickly

$ npm create seia-app # yarn create seia-app or pnpm create seia-app

After running the command, it will start to ask you a few questions. Choose the options that best suit your needs. Once you've made your selections, a new project will be created with the specified settings.

Navigate into your newly created project directory and install the dependencies. After that, build your project using the following command:

$ npm run build # yarn build or pnpm build

This will generate a dist folder containing the server and client bundles. To start the SSR server, use:

$ npm start # yarn start or pnpm start

If everything is set up correctly, you should see the server running at http://localhost:5314.

:zap: Migrate from Vite

If you already have a Vite project and want to migrate it to Seia, follow these steps.

First, install Seia as a dependency to your project:

$ npm install seia.js

[!IMPORTANT]
Seia currently requires react and react-dom as peer dependencies with the exact version 19.0.0-beta-26f2496093-20240514.
Once React 19 has a stable release, Seia will be updated to support the stable version.

After installing Seia, you can optionally add the following commands to your package.json's scripts section for convenience.

"scripts": {
    "build": "seia build",
    "start": "seia start"
}

Then, you can follow the same steps as outlined in the Quickstart Guide to build the project and start the SSR server.

:question: What is Server Components?

React Server Components is a new concept first introduced in React 18 that allows you to render components on the server, but not on the client. What does that mean?

In traditional SSR architectures, the server renders the components and sends the HTML to the client. The client then re-renders the components and hydrates the HTML to make it interactive. This means your components run on both the server and the client, allowing you to access only the intersection of Node.js (or other runtimes) and web browser APIs.

With server components, you don't have to worry about this limitation. You can specify which components should be rendered on the server and which should be rendered on the client. This allows you to access the full Node.js API on the server and the full web browser API on the client, giving you the best of both worlds.

Imagine a simple example like this:

import { readFile } from 'node:fs/promises'

const FileContent = async ({ path }: { path: string }) => {
    const buffer = await readFile(path, 'utf-8')

    return <HtmlEscape>{buffer}</HtmlEscape>
}

We've created a basic file server with React!

The FileContent component reads a file from the file system on the server and returns its content as formatted HTML. We all know that reading server files from a browser is simply not possible! Therefore, the FileContent component should be a server component, ensuring that it will only render on the server.

Then, how do we specify that this component is a server component?

The answer is; we don't have to!

Every components are considered server components by default. If you want to specify that a component should be client component, you can simply write "use client" at the top of the file.

'use client'

import { useState } from 'react'

const Counter = () => {
    const [count, setCount] = useState(0)

    return (
        <div>
            <button onClick={() => setCount(count + 1)}>Increment</button>
            <p>Count: {count}</p>
        </div>
    )
}

:fire: Try Demo

You can try a simple demo of Seia with the Blue Archive Students sample. This project is a simple SSR application that fetches and displays the list of students of the Blue Archive.

:point_right: Go to sample project README

:book: Documentation

For comprehensive details about the Seia API, configuration, and more, please visit the API documentation.

:busts_in_silhouette: Join the Community

Become part of the community by joining our official Discord server!

:heart: Support

Your support is invaluable for the ongoing maintenance and improvement of Seia. Here are some ways you can contribute:

:memo: Future Roadmap

:raising_hand: Where's the name from?

Yurizono Seia(百合ゆりぞのセイア), member of the Tea Party at Trinity in the Blue Archive.

:grey_question: What does the default port number 5314 mean?

The port number 5314 is a L33T of "SEIA". This choice is inspired by Vite using port 5173.

:pray: Special Thanks

:sparkling_heart: Sponsors

:scroll: License

MIT