Just go and follow the instructions right here:
Wait! Are you on some kind of Unix based system? Mac? Linux? If so you might want to change the
.envfile, which I have set to
80which works on Windows. Razzle's default is
3000so you can remove that line if you feel like it.
The project is up and running.
Go wild and experiment!
I am not joking. Close this article right now and try the project instead!
Just kidding! 😄 You can read on later…
Are you lazy? Start the project on Codenvy instead! One click and the project is up and running after login.
Are you still reading? All right, I will hold your hand and walk you through it.
Things will get boring here. I have told you to try it yourself instead.
All right, jokes aside. We are getting serious here.
- public ⬅Static files are here.
- application/Main.js ⬅The main React.js component.
- server.js ⬅Serves the minified HTML.
- client.js, index.js, static.js ⬅”Lower level” scripts (will discuss later).
As you may already figured out. The
Main.js contains the React.js component. So basically you can change the main content of your site here.
server.js renders the React.js application, collects the styles, and serves all of this content as HTML when you visit the site.
Inspect the source code! Yes, the dream came true. It is a properly minified one-liner. You can modify your main HTML here, mainly your header.
Styling components in
Main.js is this easy with Styled Components:
This is how Server Side Rendering works with Styled Components in
It also includes some extra, like compression for static resources and HTML minification:
src/index.js is the main entry point for the server. This is required by Razzle.
src/client.js is the main entry point for the browser. This is also required by Razzle.
.env contains some environment variable definitions.
You can find the build scripts in
package.json under the
This is how the main entry point (
index.js) starts the server (
Here is the full simplest version of index.js.
The client.js entry point hydrates the already rendered application in the browser:
In package.json, the
start script is using a build and run approach:
Server Side Rendering makes it a bit more complex to use Material-UI properly. Setting the
userAgent is not necessarily straightforward (at least for me). However, the documentation is not that bad.
The following codes with the links are hopefully giving some context:
Read this solution on Stack Overflow and press the vote up button if you find it useful too.
Single route static site generation
The optional static.js is called from the main server entry point (
index.js). It grabs the already generated static resources and copies them into the
static folder. Plus it visits the URL and saves the HTML response:
The static site generation script is somewhat complex:
- A regular Razzle build is created with the static setting (
RAZZLE_STATIC=true) baked into the generated server side code.
- This build is started in
- The static generator is only executed this time. It performs the steps described before and closes the server.
About the example
I wanted to create a more current and appealing version of Michael Morgenstern’s Search is Back!. Why? Because I thought I could do it better and I still think I can.
I quickly realized that I will need to combine a couple of technologies to implement my vision.
So “…after testing a bunch of example projects, I ended up finding and using Razzle.” (source)
I also needed to choose between Material-UI and Muse UI. (Using material design was no question for me at this point.) I considered other libraries as well. But I quickly fell in love with Material-UI’s checkbox animation. It was really robust anyway.
First, I wanted to go with Muse UI, which is a port of Material-UI because I thought I would like Vue.js more than React.js.
However, its documentation was Chinese only at the time, so I choose the original library instead.
Eventually, I created this example project. So I was able to start with my real one called Facebook Searcher:
It is a simple search form that builds a link so you can make custom searches currently for people on Facebook the regular way.
You can already use the implemented fields and it already gives more freedom than its competitor.
For example, you can list Facebook users with an interest in both sexes (which Facebook supports). It is still under development and some features are waiting for implementation.
So feel free to let me know if you want to support this project in any way!
What is Razzle?
Server side rendering is easy with Razzle. This was a must have for me. Plus, it doesn’t force you to use solutions that you don’t want to use.
So it “…is […] minimalistic…” (source)
Hot module reloading is also a nice feature to have. Customizing or extending defaults is simple too.
Facebook Searcher was first “deployed” to an old-school hosting provider. I had Secure Shell access there, so I was able to install Node.js.
I used Process Manager 2 to make sure it is running no matter what. Of course, this wasn’t as fast as having a static website. Plus, I didn’t have a good framework for deployments and updates in place. After a while, I started to save the HTML by hand and publish the application on Netlify this way. It was more reliable and the application loaded way faster too.
Later on, I switched to Firebase Hosting, which is even faster and even more reliable in general. I implemented the static site generation feature somewhere at this point. Doing it by hand was unsustainable for sure. Now I can build and deploy Facebook Searcher with just one command. I can now focus hard on implementing the remaining features.
Deploying our application to Firebase Hosting is easy. Moreover, we can still deploy it as a Firebase Function instead (basic example). Maybe this topic alone would worth another article regardless the easiness.
Currently, my project doesn’t come with rooting. If you need it for your project, you can add a router or you can consider using After.js instead, which is basically Razzle with routing and related capabilities.
I am also looking forward to updating the project to the 1. version of Material-UI once they release it and add back the checkbox animations that I love so much.
Experimenting and or reading this article you can use Material-UI quickly and easily in a sophisticated way. So go ahead and build something wonderful! 🎉
Would you use this setup? Do you prefer something else?
⬇ Reply, ask and share your experience in the comments below!
Did you enjoy this article?
⬅ 👏Clap the article a couple of times!
Share this step-by-step guide with your friends and colleges!
⬅ Use the Twitter and Facebook buttons to do so.
Copy the link from the address bar. Message your peers directly. 👋Say hi and send the article to them.
by Erik Engi
Subscribe now and get actionable technical articles into your inbox.
Erik will only email you when he has something important to say. Just like you, he is usually very busy. So this will only happen every now and then and you can unsubscribe at any time.
Erik Engi - Medium
Read writing from Erik Engi on Medium. Web developer, Ex-Codementor, Creator of Best Arcade Script and Member of the CodersClan.
Work with Erik Engi
Do you or your company have valuable projects and products? Want to extend your team with a world-class developer?
Hire now – Erik is available for hire
HTML5 web application development. Erik Engi works with companies and developers world wide. Hire a world-class developer for your team!
Erik can work full time for you or at your company.
Do you or your team need programming help? Book 1:1 sessions with Erik!
Originally published at codementor.io.