Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,4 @@ dist
# Vite logs files
vite.config.js.timestamp-*
vite.config.ts.timestamp-*
.webssr/
87 changes: 86 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,87 @@
# WebSSR
SSR framework for Web components standard, which is based on WebCell design & infrastructure.

SSR framework for Web components standard, which is based on WebCell design & infrastructure.

## Overview

WebSSR is a lightweight **server-side rendering framework** for the [Web Components](https://developer.mozilla.org/en-US/docs/Web/API/Web_components) standard. It renders pages using JSX on the server via [DOM-Renderer](https://github.com/EasyWebApp/DOM-Renderer) and serialises shadow roots with the [Declarative Shadow DOM](https://developer.chrome.com/docs/css-ui/declarative-shadow-dom) standard so browsers can hydrate without JavaScript.

## Requirements

- **Node.js ≥ 22**

## Architecture

| Concern | Technology |
|---------|------------|
| HTTP server | [Koa](https://koajs.com/) |
| JSX → HTML streaming | [DOM-Renderer](https://github.com/EasyWebApp/DOM-Renderer) |
| HTML serialisation / hydration | [Declarative Shadow DOM](https://github.com/EasyWebApp/declarative-shadow-dom-polyfill) |
| File-based routing | Next.js App Router convention (`app/**/page.tsx`) |
| Client component bundling | Parcel 2 (custom transformer plugin) |

## Getting Started

```bash
npm install
npm start # production
npm run dev # development (auto-restarts on file changes)
```

The server listens on port **3000** by default. Override with the `PORT` environment variable.

## File-Based Routing

Pages live under the `app/` directory and follow the Next.js App Router naming convention:

```
app/
page.tsx → GET /
about/
page.tsx → GET /about
users/
[id]/
page.tsx → GET /users/:id
```

Each `page.tsx` must export an **async default function** that returns JSX:

```tsx
// app/page.tsx
import type { PageProps } from '../source/types.js';

export default async function HomePage({ params, searchParams }: PageProps) {
return <h1>Hello, {searchParams.name ?? 'World'}!</h1>;
}
```

## Client Components

Mark a module as a *client component* using the import attribute `with { runtime: 'client' }`:

```tsx
import { MyButton } from './MyButton' with { runtime: 'client' };
```

The **Parcel 2 transformer plugin** (`source/parcel-plugin/index.ts`) intercepts these imports:

- **Server build** – replaces the import with a lightweight stub object so that the SSR renderer can emit the right custom-element tag without executing browser-only code.
- **Client build** – leaves the import unchanged so Parcel bundles the real component code for the browser.

## Project Structure

```
source/
server.ts Koa HTTP server entry point
router.ts File-based routing (scans app/**/page.tsx)
renderer.ts JSX → HTML via DOMRenderer (static + streaming)
polyfill.ts happy-dom DOM environment setup for SSR
types.ts Shared TypeScript types (PageProps, PageComponent)
parcel-plugin/
index.ts Parcel 2 Transformer for client component boundaries

app/
page.tsx Example home page (GET /)
about/
page.tsx Example about page (GET /about)
```
52 changes: 52 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"name": "web-ssr",
"version": "0.1.0",
"description": "SSR framework for Web Components based on Declarative Shadow DOM",
"keywords": [
"web-components",
"ssr",
"server-side-rendering",
"declarative-shadow-dom",
"koa",
"jsx"
],
"license": "LGPL-3.0-or-later",
"packageManager": "pnpm@10.33.0",
"type": "module",
"engines": {
"node": ">=22"
},
"bin": {
"webssr": "./dist/cli.js"
},
"exports": {
".": "./dist/index.js",
"./parcel-plugin": "./dist/parcel-plugin/index.js"
},
Comment on lines +22 to +25
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TypeScript 文件直接导出去没法用吧?至少编译成 ESM 啊!

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in commit b41a4bcpackage.json exports now point to compiled output:

"exports": {
    ".": "./dist/index.js",
    "./parcel-plugin": "./dist/parcel-plugin/index.js"
}

The bin field also points to ./dist/cli.js. Run npm run build (→ tsc) to compile source/ to dist/ before publishing.

"scripts": {
"start": "node --import tsx/esm --import ./source/polyfill.ts source/server.ts",
"dev": "node --watch --import tsx/esm --import ./source/polyfill.ts source/server.ts",
"build": "tsc",
"test": "node --import tsx/esm --import ./source/polyfill.ts --test 'test/**/*.test.ts' 'test/**/*.test.tsx'"
},
"dependencies": {
"@koa/router": "^15.4.0",
"@parcel/core": "^2.16.4",
"commander-jsx": "^0.7.3",
"dom-renderer": "^2.6.4",
"happy-dom": "^20.8.9",
"koa": "^3.2.0",
"koa-static": "^5.0.0"
},
"overrides": {
"happy-dom": "^20.8.9"
},
"devDependencies": {
"@parcel/plugin": "^2.16.4",
"@types/koa": "^3.0.2",
"@types/koa-static": "^4.0.4",
"@types/node": "^22.0.0",
"tsx": "^4.19.3",
"typescript": "^5.8.3"
}
}
Loading