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

feat(react): add SSR support to React apps #13234

Merged
merged 1 commit into from
Nov 21, 2022

Conversation

jaysoo
Copy link
Member

@jaysoo jaysoo commented Nov 17, 2022

Add a generator @nrwl/react:setup-ssr that generates SSR support and minimal Express server for serving the SSR and CSR app.

Changes

  • Add @nrwl/react:setup-ssr generator that adds an SSR entry point (main.server.ts) for React apps, and a server.ts file that contains a simple Express app.
  • Add @nrwl/web:ssr-dev-server executor that wraps the client and server build targets as a single target. Allows options override via browserTargetOptions and serverTargetOptions.
  • Move async iterable helpers to @nrwl/js so we can reused them in the ssr-dev-server (we can consider moving them to devkit as well)

Example

Say there is a React app demo at apps/demo, then user can run nx g @nrwl/react:setup-ssr demo, and the following three files will be added:

apps/demo
├── server.ts             # Express server
│   ├── main.server.tsx   # SSR entry file
│   └── ...
├── tsconfig.server.json  # Server-specific ts config
└── ...

@vercel
Copy link

vercel bot commented Nov 17, 2022

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated
nx-dev ✅ Ready (Inspect) Visit Preview Nov 21, 2022 at 6:51PM (UTC)

Copy link
Contributor

@Coly010 Coly010 left a comment

Choose a reason for hiding this comment

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

LGTM! 🎉

I left a couple of non-blocking comments/questions.

"setup-ssr": {
"factory": "./src/generators/setup-ssr/setup-ssr#setupSsrSchematic",
"schema": "./src/generators/setup-ssr/schema.json",
"description": "Set up SSR configuration for a project.",
Copy link
Contributor

Choose a reason for hiding this comment

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

I know it's a little verbose, but can we mention react project to prevent any ambiguity?

},
},
},
serve: {
Copy link
Contributor

Choose a reason for hiding this comment

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

It may not be a big deal, but in Angular we maintain serve as pointing to the browser build, and have a serve-ssr for running browser + server. We don't have a single serve-server to just serve the server.

So we align these? I followed the nguniversal approach of having serve and serve-ssr along with a server target with the last being the actual build for the server.

I don't mind, it can be cleanup, but not sure if we want the same DX across both or not

Copy link
Member Author

Choose a reason for hiding this comment

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

I see. I think it's not useful to have serve only do CSR. Especially if the server provides endpoints needed by the client (like backend-for-frontend).

@@ -48,31 +48,3 @@ function getNextAsyncIteratorFactory(options) {
}
};
}

export async function* mapAsyncIterator<T = any, I = any, O = any>(
Copy link
Member Author

Choose a reason for hiding this comment

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

Moved to its own file.

},
},
},
serve: {
Copy link
Member Author

Choose a reason for hiding this comment

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

I see. I think it's not useful to have serve only do CSR. Especially if the server provides endpoints needed by the client (like backend-for-frontend).

@github-actions
Copy link

This pull request has already been merged/closed. If you experience issues related to these changes, please open a new issue referencing this pull request.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 16, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants