Git Product home page Git Product logo

react-18-ssr's Introduction

Hi there, I'm Hong Kuan ๐Ÿ‘‹

Currently working on web frontend, using ReactJS / Next.js.

GitHub Stats

react-18-ssr's People

Contributors

maxam2017 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

Watchers

 avatar  avatar  avatar

react-18-ssr's Issues

Weird DOM behavior when code splitting

First off, thanks for publishing this repo. It's straight forward and better written than Meta's.

I wanted to post this in case it helps anyone and also get feedback. I am trying to build a pure React SSR setup like yourself but using a bundler (Rollup) for more optimized code splitting on the client (goodbye Next.JS ๐Ÿ‘‹) . I ran into 3 problems with hacks to resolve but wish I didn't have to, so curious if I'm missing something.

  1. When I use the HTML component when the client bundle loads, I get UI hydration mismatch errors from the server. To resolve, I check to see if I'm client and if I am, I only hydrate <App /> and not the surrounding HTML.

Conditional hydration example:

import AppRoutes from "./AppRoutes";
import Html from "./components/Html";

export default function App() {
  if (typeof window !== "undefined") {
    return <AppRoutes />;
  }
  return (
    <Html title="React SSR Demo">
      <AppRoutes />
    </Html>
  );
}
  1. I got non-deterministic behavior with onShellReady when streaming my components that use dynamic imports. On initial page load the DOM would be just a white page and the rendered HTML would say JavaScript not enabled. To resolve I used onAllReady. This is fine for SEO optimized applications for full SSG, but it's not ideal for SSR applications trying to optimize hydration time.

Example SSR handling with Express

app.get("*", async (req, res) => {
  console.log(req.url);
  const stream = ReactDOMServer.renderToPipeableStream(
    <StaticRouter location={req.url}>
      <App />
    </StaticRouter>,
    {
      onAllReady() {
        res.set("content-type", "text/html");
        stream.pipe(res);
        res.end();
      },
      onError() {},
    }
  );
  setTimeout(() => {
    stream.abort();
  }, 5000);
});
  1. I actually have always found the bootstrapScripts on the renderToPipeableStream API to be awkward. It seems, at least to me, it kind of randomly injects the hydration script randomly into the DOM. Unfortunately, when using it here I could only get it to show up after the root html tag which isn't valid.

I removed that line completely and just modified the Html component like so:

import { PropsWithChildren } from "react";

type HtmlProps = PropsWithChildren<{ title: string }>;

export default function Html({ children, title }: HtmlProps) {
  return (
    <html lang="en">
      <head>
        <meta charSet="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <meta name="theme-color" content="#000000" />
        <meta
          name="description"
          content="Web site created using create-react-app"
        />
        <title>{title}</title>
      </head>
      <body>
        <noscript
          dangerouslySetInnerHTML={{
            __html: `<b>Enable JavaScript to run this app.</b>`,
          }}
        />
        <div id="root">{children}</div>
        <script defer async type="module" src="/index.js"></script>
      </body>
    </html>
  );
}

I'll try and post a repo soon for complete repro, but open to any feedback or code!

How to reslove scss module

Hi, I have a problem with the path when running the server.
I used your example to try to combine the alias path.

I was successful by adding some properties inside the alias that I based on the parcel guideline
image

next, I try to run the server and it's can't resolve those aliases.
I don't exactly understand that correctly, by adding config for ts-node and using tsconfig-paths plugin to resolve those alias
its error passed
image

and now, i have a problem with the sass module
image

Hope to receive your response

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.