Get the latest tech news
Using Rust Back End to Serve an SPA
In web development and deployment, most software engineers are familiar with either: Separating the built SPA and the backend (Client-Side Rendering), or Return HTML directly from the backend (Server-Side Rendering) I recently (re)discovered 1 that there is a third way: embedding the built SPA into the backend’s binary file, and serving it directly. I think this is an elegant approach, as the pros are: Simpler deployment as we only have one binary file in the end Simpler code where we don’t have to take into account CORS and the backend endpoint since the frontend and backend are served from the same origin 2 The cons are quite clear:
No matter how good is it, it’s still an unconventional approach; expect lots of push back from people Increased binary size and memory usage because of the static file embedding Slightly reduced DX due to no frontend hot reloading While the code that I’m going to show you is in that specific stack, I think it’s not challenging to adopt the mindset to other languages and frameworks and libraries . For simplicity, I’d start with a monorepo setup, where the backend and the frontend are in separate folders in the same Git repository.
Or read this on Hacker News