Hello, thanks to give me a star for this project.
React App with SSR Server Side Rendering. Webpack 5 installed manually. In dev mode we use live reload thanks to webpack-dev-middleware
& webpack-hot-middleware
modules.
Main modules used are redux-saga
, loadable-component
, react-refresh
, react-jss
and typescript
.
Free and easy to use CI/CD with Github Actions and Scaleway.
Check out this app in live reacteasyssrjckf9fbl-reacteasyssrfront.functions.fnc.fr-par.scw.cloud
All this project is turned about SSR Server Side Rendering. And this is not an easy task. This boilerplate repository allows to overcome this work.
Clone the repo
git clone https://github.com/tomtom94/react-easy-ssr.git
cd react-easy-ssr
Node.js version v16.3.0 minimum (because we need to use the js optional chaining operator). Hopefully you got nvm
command already installed (best way to install node), hence just do
nvm use
it's gonna use the .nvmrc
file with v16.20.0
Get prepared with the env vars
cp .env-development .env
npm install
Run dev mode with
npm run dev
it's gonna start an hot dev middleware with an express server ;) ready to work http://localhost:3000
npm install
Write in your server provider the environment variables BACKEND_BASE_URL
Run build mode with
npm run build
it's gonna build in dist/
Then run in production mode
npm run start
;) it's gonna start the only one SSR express server out of the box for internet http://localhost:3000
or environment port used.
docker build -t react-easy-ssr .
docker run -p 80:80 react-easy-ssr
Then open http://localhost:80
You better use a good search engine like Qwant.com, don't use Google. please.
When pushing or merging on master branch, you can trigger Github Actions with a commit message that includes #major
, #minor
or #patch
.
Example of commit message in order to start a deployment :
git commit -m "#major this is a big commit"
git commit -m "#minor this is a medium commit"
git commit -m "#patch this is a tiny commit"
- Check typescript
npm run tsc
- Check eslint
npm run lint
- Check prettier
npm run prettier
The main rule is we don't use a frontend framework. All components come from wherever we need it, but we are not stick to one. No need of material-ui
, no need of bootstrap
etc... However we are used to copy past source code from them. For example we made a copy past of the wonderfull material-ui
<Grid />
which is so much powerful check it out in this repo. We don't use many components that generate their own css stylesheet, because we need to control this carefully in order to make the famous SSR.
style-components
and fontawesome
modules are also installed if ever you wanna use it. And yes we care of them for the SSR also.
Please note we don't use classical CSS style. We use JSS (it means js in css). material-ui
module also uses react-jss
this is why we didn't installed material-ui
else it would be stupid to generate twice the react-jss
stylesheet on the SSR, and inefficiente to make an ultra fast App.
Either you install material-ui
and you make all your css components with it (which is recommended if you do this for big company), or you get free and install react-jss
like we did.
Let's see how we fetch our data to feed our redux store. You can find this code in the <Movies />
component.
const willMount = useRef(true)
if (willMount.current && !process.env.BROWSER) {
dispatch(triggerMovies('GET_MOVIES'))
willMount.current = false
}
useEffect(() => {
dispatch(triggerMovies('GET_MOVIES'))
return () => {
dispatch(clearMovies())
}
}, [dispatch])
- 1st part is only for server side, we dispatch the redux action : with
useRef
you can be sure the action won't be triggered multiple times in an infinite loop. - 2nd part is only for client side, we dispatch the redux action : but when you trigger this action there is a redux-saga selector which will check if data hasn't been already fetched during 1st part, if yes no need to fetch again. And we clear the error if there are some before leaving the component.
This way your App is able to fetch data on the server & client side independantly.
Webpack setup only allows us to import files with ES6 in type
- .js .jsx .ts .tsx
- .png .jpe .jpeg .gif .ico
- .woff .woff2
- .css (remember
react-jss
generates its own stylesheet via its own plugins, not via webpack loaders)
You can add more Webpack loader
to your project...
If ever you wanna brainstorm, download my resume you are gonna find my phone number