Advanced server-side rendering
In the previous sections, we have successfully created a server that can do server-side rendering with hot reloading, which is very useful for development but will worsen the performance in production. Let’s create another server function for a production server now, which will build files, use compression, and not load Vite middleware for hot reloading. Follow these steps to create the production server:
- In the root of our project, install the
compression
dependency with the following command:$ npm install compression@1.7.4
- Edit
server.js
and define a new function for the production server, above thecreateDevServer
function:async function createProdServer() {
- In this function, we define a new Express app and use the
compression
package and theserve-static
package to serve our client:const app = express() app.use((await import('compression')).default()) app.use( ...