GithubHelp home page GithubHelp logo

bholmesdev / simple-rsc Goto Github PK

View Code? Open in Web Editor NEW
674.0 674.0 59.0 211 KB

A simple React Server Components implementation that you can build yourself ๐Ÿ™Œ

Home Page: https://www.youtube.com/watch?v=Fctw7WjmxpU

JavaScript 100.00%
react

simple-rsc's Introduction

Simple RSC โš›๏ธ

A simple React Server Components implementation that you can build yourself ๐Ÿ™Œ

Watch the live demo with Dan Abramov here!

โญ๏ธ Goals

  • ๐ŸŒŠ Show how React server components are streamed to the browser with a simple Node server.
  • โš™๏ธ Demo a build process to bundle server components and handle client components with the "use client" directive.
  • ๐Ÿ“ Reveal how a server component requests appear to the client with a developer panel.

Getting started

First, install dependencies with "peer dependency" errors disabled:

npm i --legacy-peer-deps

This is due to experimental version conflicts. React Server Components are still quite new!

Then, start the Node development server:

npm run dev

This should trigger a build and start your server at http://localhost:3000.

Project structure

This project is broken up into the app/ and server/ directories. The most important entrypoints are listed below:

# ๐Ÿฅž your full-stack application
app/ 
  page.jsx # server entrypoint.
  _client.jsx # client script that requests and renders your `page.jsx`.

# ๐Ÿ’ฟ your backend that builds and renders the `app/`
server.js

๐Ÿ™‹โ€โ™€๏ธ What is not included?

  • File-based routing conventions. This repo includes a single index route, with support for processing query params. If you need multiple routes, you can try NextJS' new app/ directory.
  • Advance bundling for CSS-in-JS. A Tailwind script is included for playing with styles.
  • Advice on production deploys. This is a learning tool to show how React Server Components are used, not the bedrock for your next side project! See React's updated "Start a New React Project" guide for advice on building production-ready apps.

simple-rsc's People

Contributors

bholmesdev avatar deathstar10 avatar gaearon avatar tusheer avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

simple-rsc's Issues

Integrating styled-components with React Server Components example

I've been exploring the simple-rsc repo and I'm interested in integrating styled-components with React Server Components. I wanted to get your thoughts on whether it's a good direction to take.

src/Title.js:

'use client';

import styled from "styled-components";

const Title = styled.h1``;

export default Title;

Then, we can update the page.jsx file to use the Title styled-component:

src/page.jsx:

import { Suspense } from "react";
import { getAll, getById } from "../db/fetch.js";
import SearchableAlbumList from "./SearchableAlbumList";
import Title from "../client/Title";

export default async function ServerRoot({ search }) {
  return (
    <>
      <Title>Welcome to server components?</Title>
      <h2>Search results for "{search}"</h2>
      <Suspense fallback={<h2>Loading...</h2>}>
        <Albums search={search} />
      </Suspense>
    </>
  );
}

async function Albums({ search }) {
  const albums = await getAll();
  return (
    <SearchableAlbumList search={search} albums={albums} />
  );
}

Would you consider this a viable approach to add styled-components to React Server Components?

I'd appreciate your insight and any recommendations on best practices for this integration, especially since integrating styled-components with SSR can be challenging to begin with.

Cheers!

Integrate SSR

Hello! Firstly, thank you for your effort. I found your explanation on how to integrate RSC into a common project very helpful in understanding the process better. Currently, I am working on an example which is fairly simple, but I am trying to figure out how to combine RSC with SSR. You can see the demo here: https://github.com/sviridoff/rsc-webpack-swc-fastify-demo. I have replicated the RSC result hardcoded in the HTML file here: https://github.com/sviridoff/rsc-webpack-swc-fastify-demo/blob/main/public/page.html#L12. However, when I try to hydrate it (https://github.com/sviridoff/rsc-webpack-swc-fastify-demo/blob/main/src/bootstrap.tsx#L16), it throws an error because it does not match the result. I'm not sure if the strategy I am attempting is correct, as I haven't found any examples to follow. Do you have any suggestions on a better strategy to hydrate an HTML with the result of the RSC? Thank you.

Cannot get the repo started on local

I am on windows node v18.14.1, I cloned the repo but cannot get it started.
It gives an error of
Error: Objects are not valid as a React child (found: [object Promise])

I think Page.js is not treated as a server component, hence you are not allowed to have the async component. Do you mind giving more details on the environment? Thank you

Support Webpack

Hi! So exciting to see a new RSC demo in the wild, we've needed something like this for a while.

Curious if there's any plans to support React's RSC webpack plugin in the demo? I like how this is a bit more vanilla, but it would also be cool to see a version that uses more official tooling.

Integrating styled-components with React Server Components example

I've been exploring the simple-rsc repo and I'm interested in integrating styled-components with React Server Components. I wanted to get your thoughts on whether it's a good direction to take in this example project.

I propose creating a separate client-side file for the Title styled-component as shown below:

client/Title.js:
`'use client';

import styled from "styled-components";

const Title = styled.h1``;

export default Title;
`

Then, we can update the page.jsx file to use the Title styled-component:

`import { Suspense } from "react";
import { getAll, getById } from "../db/fetch.js";
import SearchableAlbumList from "./SearchableAlbumList";
import Title from "../client/Title";

export default async function ServerRoot({ search }) {
return (
<>
<Title>Welcome to server components?</Title>

Search results for "{search}"


<Suspense fallback={

Loading...

}>


</>
);
}

async function Albums({ search }) {
const albums = await getAll();
return (

);
}
`
In this example, I replaced the

tag with a Title styled-component imported from a separate client-side file. Would you consider this a viable approach to add styled-components to React Server Components in this project?

I'd appreciate your insight and any recommendations on best practices for this integration, especially since integrating styled-components with SSR can be challenging to begin with. If you believe this approach is a good direction for the project, please let me know, and I look forward to hearing your thoughts.

Cheers!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.