Public
Like
ReactStatic
Val Town is a collaborative website to build and scale JavaScript apps.
Deploy APIs, crons, & store data – all from the browser, and deployed in milliseconds.
React SSR for Val Town
/** @jsxImportSource https://esm.sh/react */
import { render, React } from "https://esm.town/v/jxnblk/ReactStatic";
function App() {
return (
<html>
<body>
<h1>ReactStatic</h1>
<p>React SSR in Val Town</p>
</body>
</html>
);
}
export default render(App, []);
Custom middleware can be added in an array as the second argument.
Middleware can add data to the req.data
object or return a response for things like API endpoints.
export default render(App, [
analytics,
robots("User-agent: *\nAllow: /"),
getInitialProps
])
ReactStatic has a built-in middleware to handle request to /robots.txt
import { render, robots } from "https://esm.town/v/jxnblk/ReactStatic";
// ...
export default render(App, [
robots("User-agent: *\nAllow: /"),
])
// example middleware
async function api (req: Request, res: Response, next): Promise<Response> {
if (req.pathname !== "/api") return next();
if (req.method === "POST") {
return Repsonse.json({ message: "Hello POST request" });
}
return Response.json({ ok: true });
}
export default render(App, [ api ]);
// example middleware
async function getInitialProps (req: Request, res: Response, next) {
// fetch data or do async work to pass as props to the component
req.data = {
hello: "props",
};
return next();
}
export default render(App, [ getInitialProps ]);
/** @jsxImportSource https://esm.sh/react@18.3.1 */
import { render } from "https://esm.town/v/jxnblk/ReactStatic";
function App () {
return (
<html>
<head>
<title>ReactStatic</title>
</head>
<body>
hello
</body>
</html>
);
}
export default render(App, []);
React requires matching versions for SSR and hydration -- ensure you import react@18.3.1
when using this library.
Inspired by https://www.val.town/v/stevekrouse/react_http & https://www.val.town/v/stevekrouse/reactClientDemo
Migrated from folder: _LEAVE_AS_IS/ReactStatic