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
compressiondependency with the following command:$ npm install compression@1.7.4
- Edit
server.jsand define a new function for the production server, above thecreateDevServerfunction:async function createProdServer() { - In this function, we define a new Express app and use the
compressionpackage and theserve-staticpackage to serve our client:Â Â const app = express() Â Â app.use((await import('compression')).default()) Â Â app.use( Â Â Â Â ...