Git Product home page Git Product logo

asp.net-core-react-and-ssr's Introduction

ASP.NET Core, React with SSR and Storybook · GitHub license PRs Welcome

Spin up your next project using this ASP.NET Core, React with Server-Side Rendering and Storybook boilerplate.

  • .NET 6.0.8 .NET 6.0.8 to develop backend-code with C#
  • React 18.2.0 with Server-Side Rendering: to develop frontend-code with JavaScript. The boilerplate supports Server-Side Rendering
  • Storybook 6.5.10: to show clients prototypes of your pages quickly

Installation

Prerequisites

  • .NET: download and install the latest version
  • Node.js: download and install the latest LTS version
  • Docker: to run ASP.NET Core web app, Node for SSR and Storybook in containers. Download and install the latest version

Nice to have prerequisites

Quickstart

  1. Clone this repository

  2. Execute the following commands from the /fe folder to build fe:

    npm install

    npm run build-fe

  3. Open the project from the be folder in Visual Studio 2022, set the docker-compose project as the startup project and hit F5

The ASP.NET Core web application will be available at http://localhost:5000

The Storybook will be available at http://localhost:8091

How to develop new components and pages

To efficiently develop new components and pages, stop docker containers and start storybook and node server (for SSR) on your local machine. Remember to change SSR Node endpoint (ServerSideRenderingEndpoint) in appsettings.json and appsettings.Development.json under /be/src/Project/AspNetCore to http://localhost:3000

  1. Install npm packages from the /fe directory:

    npm install

  2. Start Storybook:

    npm run storybook

  3. Develop your React components and pages at /fe/src, reference them as shown in an example story at /fe/src/stories and preview your stories at http://localhost:9009

  4. Enable server-side rendering by adding your components and pages at /fe/server/middleware/renderer.js exactly as it has been done for HomePage and start the node server: from the /fe folder:

    node server/bootstrap.js

  5. You want to integrate your work into ASP.NET Core web application. To achieve this add your new page to /fe/webpack.config.js as shown in an example homepage component. To bundle, minify and copy the css and js assets to ASP.NET Core wwwroot execute:

    npm run build-fe

  6. Please reference the generated assets as shown in an example Index.cshtml at /be/src/Project/AspNetCore/Views

  7. Start Debugging (F5) or Start Without Debugging (Ctrl+F5) to see your pages live

Troubleshooting

  1. In case you experience a problem with webpack (especially on Windows), install it globally with the following command (yes, I know it's not recommended):

    npm install --global webpack

  2. SocketException: No connection could be made because the target machine actively refused it:

Please make sure that your node server is up and running. Check if your ServerSideRenderingEndpoint in appsettings.json and appsettings.Development.json point to http://localhost:3000 (for local node server) or http://react-ssr-web:3000 (for Docker node server).

  1. window is not defined or global is not defined:

There is a good chance your window or global variables are undefined when doing SSR. Please have a look at Stack Overflow to find ways to fix your code.

Disclaimer

The boilerplate has been based on the CRA (Create React App), Create React App preset for Storybook and standard ASP.NET Core MVC template.

Documentation

You will learn more about this boilerplate on the website.

Contributing

Found a serious bug? Your pull request is very welcome :)

Support

To get support regarding ASP.NET Core, React and Server-Side Rendering, contact me directly.

License

This boilerplate is MIT licensed.

asp.net-core-react-and-ssr's People

Contributors

dawiddworak88 avatar skasperek 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

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

asp.net-core-react-and-ssr's Issues

[Windows 10] docker build for the ASP.NET Core project doesn't work, but dotnet publish command does

I was about to post a question in a Docker forum but just as I was going to get a link to the misc/docker directory it disappeared, and I see that must be from the most recent commit. Here's what I was going to ask:

I was attempting to follow and run a sample application found at this repository. Originally I tried it on my Mac and the docker compose command worked perfectly fine, starting all 3 services. However when trying on my Windows work laptop it’s been an absolute nightmare. I’ve lost hours trying to figure out what’s causing it to fail on Windows, and believe it has to do with user permissions. The .NET Core app's Dockerfile-dev has a RUN dotnet publish -c Release -o out step, which works fine when running it directly on the Git Bash command line, but running docker build -f Dockerfile-dev . in that directory fails with this output:

Step 5/9 : RUN dotnet publish -c Release -o out
 ---> Running in c20e3f3e8110
Microsoft (R) Build Engine version 16.4.0+e901037fe for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.

/usr/share/dotnet/sdk/3.1.102/NuGet.targets(123,5): error : Unable to load the service index for source https://api.nuget.org/v3/index.json. [/app/AspNetCore.sln]
/usr/share/dotnet/sdk/3.1.102/NuGet.targets(123,5): error :   The SSL connection could not be established, see inner exception. [/app/AspNetCore.sln]
/usr/share/dotnet/sdk/3.1.102/NuGet.targets(123,5): error :   The remote certificate is invalid according to the validation procedure. [/app/AspNetCore.sln]
The command '/bin/sh -c dotnet publish -c Release -o out' returned a non-zero code: 1

The front-end's Dockerfile-ssr-dev has a chown -R node:node /home/node/aspnetcore-react-ssr command, and I'm wondering does there need to be an equivalent for the backend? I'll try cloning down the latest and following the updated README steps to see if the original problem disappears

My Windows Docker version is 19.03.12

comiple with ASP core 6 latest nugest

Hello can you please update to ASP core 6, also how does the preRender help, I am trying gto understand your article in how the strorybook is wired into the asp app, seem like magic :)

Tech Question

Hi,

I'm a junior web dev, I found this project amazing. I found the contact me link is not working so I used this place to post :) I have a small question regarding this project. I wonder if the express server behaves like a middleware between the server and CDN?

Regards,
Bangyan

[issue when run npm install]

Hello, I'm a newbie. I have installed nodejs as Latest Current Version: 19.0.1. Then I gone to the /fe folder and run: npm install and I got the issues in below picture. Please help me, thanks!
image

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.