-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
18cd028
Add RSC-based static site generator
devongovett 6109204
Support outputting multiple streams from packagers
devongovett 3371aa0
Add example
devongovett 5b65d90
Rename to react-static
devongovett 849f5c4
Add main and source fields
devongovett 038b762
oops
devongovett 2d91920
Apply source field only in development parcel builds
devongovett bf0f6db
fix node condition
devongovett File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Add example
- Loading branch information
commit 3371aa06da8310f32d3a79970cacf16a0a7b2148
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"extends": "@parcel/config-react-ssg" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
}, | ||
"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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -125,6 +125,7 @@ export default (new Packager({ | |
|
||
return [ | ||
{ | ||
type: 'html', | ||
contents: Readable.from(response), | ||
}, | ||
{ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.