DEV Community

Cover image for A Comprehensive Guide to Building SSR Apps with React, React Router and Vite

A Comprehensive Guide to Building SSR Apps with React, React Router and Vite

Francisco Mendes on August 11, 2022

Introduction In recent years, there have been two immensely popular ways of rendering web pages, Single Page Applications and Server Sid...
Collapse
 
jlguenego profile image
Jean-Louis GUENEGO

Weldone for this example. It save me time. 🙂

Just a small improvement: Test if the process.env.NODE_ENV is "production" because by default it is development as state the official node guys:
nodejs.dev/en/learn/nodejs-the-dif...

Collapse
 
rumbis profile image
George Alexandrou

Thank you very much

Collapse
 
redbar0n profile image
Magne

SSR with bare-bones Vite, such as with their react-ssr template, is pretty low-level / hardcore.

For an easier time, try vite-plugin-ssr.com/

Collapse
 
franciscomendes10866 profile image
Francisco Mendes

Thanks for the comment 😊, I happen to know this plugin and I really like it because it already has several standardizations such as data fetching 🚀.

However, I also think it's good to understand how things work behind the scenes since the tools we use on a daily basis have layers and layers of abstractions 🎁.

Collapse
 
redbar0n profile image
Magne

or even RakkasJS!

Collapse
 
andev70 profile image
Andev70

there is a mistake.

if you run following in the console
yarn create vite react-ssr --template react-ts
this will create only a client side react app template

to create a react-ssr(react-server-side-rendering app) with vite then you need to run following command in terminal

if you are using npm:

npm create vite@latest react-ssr

if you are using yarn :

yarn create vite react-ssr
## if you are using pnpm :
pnpm create vite@latest react-ssr

after running this command then a list of options will be shown in the terminal like following

❯ Vanilla
Vue
React
Preact
Lit
Svelte
Others

keep pressing the down arrow untill the arrow shown in the terminal options comes next to the other option and then press enter

after above action again a list will appear like following:

create-vite-extra ↗
create-electron-vite ↗
Enter fullscreen mode Exit fullscreen mode

choose the create vite extra option then enter

after that a list will appear again like following

❯   ssr-vanilla
    ssr-vue
    ssr-react
    ssr-preact
    ssr-svelte
    ssr-solid
    deno-vanilla
    deno-vue
    deno-react
    deno-preact
    deno-lit
    deno-svelte
    deno-solid
    library
    ssr-transform
Enter fullscreen mode Exit fullscreen mode

keep coming down untill you reach the react ssr then press enter then it will initialise a react-ssr project

then enter following commands, replace pnpm with whatever package manager you are using

cd react-ssr

pnpm i
Or 
yarn 
or 
npm install
Enter fullscreen mode Exit fullscreen mode

then you are done now you can run pnpm run dev to spin up the dev server

Collapse
 
dbish6 profile image
David Bishop • Edited

I don't understand how you would deploy an ssr application like this. When you're serving you are doing "NODE_ENV=production node server", which makes no sense because wouldn't you run the server from the build for production? This is my first time doing ssr and It is confusing me to the max how I can run the server in production, I have no clue. Please, someone, help me understand because you deploy the build folder, right. And that has no way to run the server when building with vite.

Collapse
 
sundayjava profile image
Sunday David Udoekong

I`m having issues here. Anytime i import deps using import ArrowBackIcon from '@mui/icons-material/ArrowBack'; it will not work. but if i use import {ArrowBack} from '@mui/icons-material'; it will work. and is not all deps they are named export, some are default export like react-slick. How can i solve this issue?

Collapse
 
sstark97 profile image
Aitor Santana Cabrera

Hello, I have a problem when I use these config in Netlify. I try to transform y CSR portfolio yo SSR and when I deploy t o Netlify doesnt work. Here my repository:
GitHub Repo

Collapse
 
jlguenego profile image
Jean-Louis GUENEGO

Note : When a web client request the url "/" the SIRV take the lead to give back index.html, so no SSR happens in this case.

Collapse
 
eduhsoto profile image
Eduardo Hernández Soto

why show when nmp run serve window = document.defaultView,
^

ReferenceError: document is not defined

Collapse
 
andev70 profile image
Andev70

I think you are using
in the wrong file, you should use the BrowseRouter component inside client-entry file

Collapse
 
kayodeadechinan profile image
Kayode Adechinan

Great article! Thansk for sharing. Please, how one can handle path variables ?

Collapse
 
jeffealamedadev profile image
jeffealamedadev

Hi there, thanks for sharing this. I got a question, this could work if I have a Rails API back end?

Collapse
 
andev70 profile image
Andev70

This is node specific so it will not work for rails backend. If you want to fetch data from rails api externally then thats ok but integrated react ssr will not work like this