Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add RSC-based static site generator #10057

Merged
merged 8 commits into from
Dec 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add example
  • Loading branch information
devongovett committed Dec 23, 2024
commit 3371aa06da8310f32d3a79970cacf16a0a7b2148
3 changes: 3 additions & 0 deletions packages/examples/react-static/.parcelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "@parcel/config-react-ssg"
}
8 changes: 8 additions & 0 deletions packages/examples/react-static/components/Counter.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
"use client";

import { useState } from "react";

export function Counter() {
let [count, setCount] = useState(0);
return <button onClick={() => setCount(count + 1)}>Count: {count}</button>;
}
17 changes: 17 additions & 0 deletions packages/examples/react-static/components/Nav.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import type { PageProps } from "../types";

export function Nav({pages, currentPage}: PageProps) {
return (
<nav>
<ul>
{pages.map(page => (
<li key={page.url}>
<a href={page.url} aria-current={page.url === currentPage.url ? 'page' : undefined}>
{page.name.replace('.html', '')}
</a>
</li>
))}
</ul>
</nav>
);
}
67 changes: 67 additions & 0 deletions packages/examples/react-static/components/client.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
"use client-entry";

import {useState, use, startTransition, useInsertionEffect, ReactElement} from 'react';
import ReactDOM from 'react-dom/client';
import {createFromReadableStream, createFromFetch} from 'react-server-dom-parcel/client';
import {rscStream} from 'rsc-html-stream/client';

// Stream in initial RSC payload embedded in the HTML.
let initialRSCPayload = createFromReadableStream<ReactElement>(rscStream);
let updateRoot: ((root: ReactElement, cb?: (() => void) | null) => void) | null = null;

function Content() {
// Store the current root element in state, along with a callback
// to call once rendering is complete.
let [[root, cb], setRoot] = useState<[ReactElement, (() => void) | null]>([use(initialRSCPayload), null]);
updateRoot = (root, cb) => setRoot([root, cb ?? null]);
useInsertionEffect(() => cb?.());
return root;
}

// Hydrate initial page content.
startTransition(() => {
ReactDOM.hydrateRoot(document, <Content />);
});

// A very simple router. When we navigate, we'll fetch a new RSC payload from the server,
// and in a React transition, stream in the new page. Once complete, we'll pushState to
// update the URL in the browser.
async function navigate(pathname: string, push = false) {
let res = fetch(pathname.replace(/\.html$/, '.rsc'));
let root = await createFromFetch<ReactElement>(res);
startTransition(() => {
updateRoot!(root, () => {
if (push) {
history.pushState(null, '', pathname);
push = false;
}
});
});
}

// Intercept link clicks to perform RSC navigation.
document.addEventListener('click', e => {
let link = (e.target as Element).closest('a');
if (
link &&
link instanceof HTMLAnchorElement &&
link.href &&
(!link.target || link.target === '_self') &&
link.origin === location.origin &&
!link.hasAttribute('download') &&
e.button === 0 && // left clicks only
!e.metaKey && // open in new tab (mac)
!e.ctrlKey && // open in new tab (windows)
!e.altKey && // download
!e.shiftKey &&
!e.defaultPrevented
) {
e.preventDefault();
navigate(link.pathname, true);
}
});

// When the user clicks the back button, navigate with RSC.
window.addEventListener('popstate', e => {
navigate(location.pathname);
});
8 changes: 8 additions & 0 deletions packages/examples/react-static/components/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
h1 {
font-family: system-ui;
}

nav a[aria-current] {
background: black;
color: white
}
22 changes: 22 additions & 0 deletions packages/examples/react-static/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "parcel-rsc-static-example",
"version": "0.0.0",
"private": true,
"source": "pages/*.tsx",
"targets": {
"default": {
"context": "react-server",
"scopeHoist": false
Copy link
Member Author

Choose a reason for hiding this comment

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

Slightly unfortunate: because we want to skip the JS packager and run the assets during the static packager directly, we have to turn off scope hoisting. It's easy to emulate the dev packager's output with node's vm but hard to handle the structure generated by the scope hoisting transformer.

}
},
"scripts": {
"start": "parcel --config .parcelrc",
"build": "parcel build"
},
"dependencies": {
"react": "^19",
"react-dom": "^19",
"react-server-dom-parcel": "^0.0.1",
"rsc-html-stream": "^0.0.4"
}
}
22 changes: 22 additions & 0 deletions packages/examples/react-static/pages/Index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import type { PageProps } from "../types";
import { Resources } from "@parcel/runtime-rsc";
import { Counter } from "../components/Counter";
import { Nav } from '../components/Nav';
import '../components/style.css';
import '../components/client';

export default function Index({pages, currentPage}: PageProps) {
return (
<html>
<head>
<title>Static RSC</title>
<Resources />
</head>
<body>
<h1>This is an RSC!</h1>
<Nav pages={pages} currentPage={currentPage} />
<Counter />
</body>
</html>
);
}
20 changes: 20 additions & 0 deletions packages/examples/react-static/pages/Other.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import type { PageProps } from "../types";
import { Resources } from "@parcel/runtime-rsc";
import {Nav} from '../components/Nav';
import '../components/style.css';
import '../components/client';

export default function Index({pages, currentPage}: PageProps) {
return (
<html>
<head>
<title>Static RSC</title>
<Resources />
</head>
<body>
<h1>This is another RSC!</h1>
<Nav pages={pages} currentPage={currentPage} />
</body>
</html>
);
}
Empty file.
1 change: 1 addition & 0 deletions packages/packagers/react-ssg/src/ReactSSGPackager.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ export default (new Packager({

return [
{
type: 'html',
contents: Readable.from(response),
},
{
Expand Down
4 changes: 3 additions & 1 deletion packages/reporters/cli/src/CLIReporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,9 @@ export async function _report(

if (
options.serveOptions &&
event.bundleGraph.getEntryBundles().some(b => b.env.isBrowser())
event.bundleGraph
.getEntryBundles()
.some(b => b.env.isBrowser() || b.type === 'html')
) {
persistMessage(
chalk.blue.bold(
Expand Down