DEV Community

Cover image for Server-side rendering, client-side rendering, and pre-rendering(Spring)
Isaac Tonyloi
Isaac Tonyloi

Posted on

Server-side rendering, client-side rendering, and pre-rendering(Spring)

Image description

Server-side rendering, client-side rendering, and pre-rendering are three different approaches to rendering web applications. In this article, we will explore the differences between these three approaches and the advantages and disadvantages of each.

Server-side rendering is the traditional approach to rendering web applications. In this approach, the server generates the HTML for a page and sends it to the client's browser. This means that the client's browser does not have to do any additional work to render the page, as all of the necessary HTML has already been generated on the server. One advantage of this approach is that it can be faster for the client, as the browser does not have to do any additional work to render the page. However, server-side rendering can also be slower, as the server has to generate the HTML for each request.

Client-side rendering, on the other hand, involves rendering the page on the client's browser rather than on the server. In this approach, the server sends minimal HTML to the client, and the rest of the rendering is done by JavaScript running on the client's browser. This approach can be slower for the initial page load, as the client's browser has to render the page. However, it can be faster for subsequent page navigations, as the browser does not have to send a new request to the server for each navigation.

Pre-rendering is a hybrid approach that involves rendering the page on the server and then sending the fully rendered HTML to the client. This can be faster for the initial page load, as the HTML is already fully rendered and ready to be displayed by the client's browser. However, it can be slower for subsequent page navigations, as the browser has to send a new request to the server for each navigation.

Importance of rendering in web application performance

Rendering is the process of generating and displaying the content of a web page to the user. It plays a crucial role in the performance of a web application, as it determines how quickly the page is able to display its content to the user.
The faster a web page can render its content, the better the user experience will be. This is because users expect web pages to load quickly, and may become frustrated if a page takes too long to load or render its content. In addition, faster rendering can also lead to improved search engine rankings, as search engines tend to favor fast-loading pages.
Several factors can affect the rendering performance of a web page, including the complexity of the content, the amount of data that needs to be fetched and processed, and the resources of the device or server that is rendering the page.

Server-side Rendering

Server-side rendering (SSR) is a technique for rendering a web application on the server, before sending the resulting HTML to the client. This allows the initial HTML of the page to be displayed to the user more quickly, since it does not need to be fetched and rendered on the client side.
In the Spring framework, server-side rendering can be achieved using a combination of technologies, such as Spring MVC and Thymeleaf.
Here is an example of how SSR can be implemented in a Spring web application using these technologies:

  1. Create a Spring MVC controller to handle the request for the web page:
@Controller
public class PageController {

    @GetMapping("/page")
    public String page(Model model) {
        // Add data to the model that will be used to render the page
        model.addAttribute("message", "Hello, world!");
        return "page";
    }
}
Enter fullscreen mode Exit fullscreen mode
  1. Create a Thymeleaf template to define the HTML structure of the page.
<!DOCTYPE html>
<html>
  <head>
    <title>My Page</title>
  </head>
  <body>
    <h1 th:text="${message}"></h1>
  </body>
</html>
Enter fullscreen mode Exit fullscreen mode
  1. Configure the application to use Thymeleaf as the template engine:

@Configuration
public class ThymeleafConfig {

    @Bean
    public SpringTemplateEngine templateEngine(ITemplateResolver templateResolver) {
        SpringTemplateEngine engine = new SpringTemplateEngine();
        engine.setTemplateResolver(templateResolver);
        return engine;
    }
}

Enter fullscreen mode Exit fullscreen mode

When the user requests the web page, the Spring MVC controller will handle the request and render the Thymeleaf template on the server side, generating the initial HTML of the page. This HTML will then be sent to the client, where it can be displayed in the user's browser.

This is a simplified example of how SSR can be implemented in a Spring web application, but it illustrates the basic principles of how the server-side rendering process works. Additional functionality, such as handling data fetching and rendering dynamic content, can be added as needed.

Server-side rendering (SSR) has a number of advantages, including:

Improved initial load time: Since the initial HTML of the page is generated on the server and sent to the client, the page can be displayed more quickly to the user. This is especially useful for pages with complex or large amounts of content, as it can reduce the time the user needs to wait for the page to load.

Easier for search engines to index: Search engines rely on the initial HTML of a page to understand its content and determine its relevance to search queries. With server-side rendering, the initial HTML of the page is generated on the server and can be easily crawled and indexed by search engines.

However, server-side rendering can also have some disadvantages, including:

Added load on the server: Generating the initial HTML of the page on the server can add extra load to the server, which can impact the overall performance of the application.
Limited dynamic content: Since the initial HTML of the page is generated on the server, it can be more difficult to update the page with dynamic or interactive content that requires client-side processing.

Client-side rendering .

Client-side rendering (CSR) is a technique for rendering a web application on the client side, using JavaScript. This means that the initial HTML of the page is sent to the client without any content, and the content is then fetched and rendered on the client side using JavaScript.

In the Spring framework, client-side rendering can be achieved using a combination of technologies, such as Spring MVC and a JavaScript framework such as React or Angular.
Here is an example of how CSR can be implemented in a Spring web application using these technologies:

  1. Create a Spring MVC controller to handle the request for the web page:
@Controller
public class PageController {

    @GetMapping("/page")
    public String page() {
        return "page";
    }
}
Enter fullscreen mode Exit fullscreen mode
  1. Create an HTML file to define the structure of the page:
<!DOCTYPE html>
<html>
  <head>
    <title>My Page</title>
  </head>
  <body>
    <div id="root"></div>
  </body>
</html>

Enter fullscreen mode Exit fullscreen mode
  1. Create a React component to render the content of the page:
import React from 'react';

class Page extends React.Component {
  render() {
    return <h1>Hello, world!</h1>;
  }
}

export default Page;
Enter fullscreen mode Exit fullscreen mode
  1. Use the ReactDOM library to render the component in the page:
import React from 'react';
import ReactDOM from 'react-dom';
import Page from './Page';

ReactDOM.render(<Page />, document.getElementById('root'));
Enter fullscreen mode Exit fullscreen mode

When the user requests the web page, the Spring MVC controller will handle the request and return the HTML file to the client. The client's browser will then execute the JavaScript code, which will fetch and render the content of the page using the React component.

This is a simplified example of how CSR can be implemented in a Spring web application, but it illustrates the basic principles of how the client-side rendering process works. Additional functionality, such as handling data fetching and rendering dynamic content, can be added as needed.

Client-side rendering (CSR) has a number of advantages, including:

Dynamic and interactive content: Since the content of the page is rendered on the client side using JavaScript, it is easier to update the page with dynamic or interactive content that requires client-side processing.

No need to refresh the page: With CSR, it is possible to update the page without requiring the user to refresh the page, which can provide a more seamless and responsive user experience.

However, client-side rendering can also have some disadvantages, including:

Added load on the client device: Executing the JavaScript code required for client-side rendering can add extra load to the client device, which can impact the performance of the application.

Slower initial load time: Since the initial HTML of the page is sent to the client without any content, it may take longer for the page to be displayed to the user. This can be especially noticeable on slower or resource-constrained devices.

Factors to consider when comparing SSR and CSR in terms of performance

There are several factors to consider when comparing server-side rendering (SSR) and client-side rendering (CSR) in terms of performance:

Initial load time: SSR can have an advantage over CSR in terms of initial load time, since the initial HTML of the page is generated on the server and sent to the client, while with CSR the initial HTML is sent without any content and the content is fetched and rendered on the client side. This can be especially noticeable for pages with complex or large amounts of content.

Frequency of updates: SSR can be more efficient for pages that are updated infrequently, since the server only needs to generate the HTML for the page when the content changes. For pages with frequent updates, CSR may be more efficient, since it allows the content to be updated without requiring a server round-trip.

Server and client resources: SSR can put more load on the server, since it requires the server to generate the HTML for the page. CSR can put more load on the client device, since it requires the client to execute the JavaScript code needed to render the page.

Examples of when SSR may have a performance advantage over CSR:

Pages with complex or large amounts of content: Since SSR generates the initial HTML of the page on the server, it can be more efficient for pages with complex or large amounts of content, as it reduces the amount of data that needs to be transferred to the client.

Pages that are updated infrequently: If a page is updated infrequently, it may be more efficient to use SSR, as the server only needs to generate the HTML for the page when the content changes.

Examples of when CSR may have a performance advantage over SSR:

Pages with frequent updates: If a page is updated frequently, it may be more efficient to use CSR, as it allows the content to be updated without requiring a server round-trip.

Pages with dynamic or interactive content: CSR can be more efficient for pages with dynamic or interactive content, as it allows the content to be updated on the client side using JavaScript.

There are several trade-offs to consider when choosing between server-side rendering (SSR) and client-side rendering (CSR) in terms of performance:

Initial load time: SSR can have an advantage over CSR in terms of initial load time, since the initial HTML of the page is generated on the server and sent to the client, while with CSR the initial HTML is sent without any content and the content is fetched and rendered on the client side. This can be especially noticeable for pages with complex or large amounts of content.

Frequency of updates: SSR can be more efficient for pages that are updated infrequently, since the server only needs to generate the HTML for the page when the content changes. For pages with frequent updates, CSR may be more efficient, since it allows the content to be updated without requiring a server round-trip.

Server and client resources: SSR can put more load on the server, since it requires the server to generate the HTML for the page. CSR can put more load on the client device, since it requires the client to execute the JavaScript code needed to render the page.

It is important to consider the specific needs and goals of the web application when choosing between SSR and CSR, as each approach has its own trade-offs and may be more or less suitable depending on the specific requirements of the application. Factors to consider may include the complexity and size of the content, the frequency of updates, and the available server and client resources.

In general, SSR may be more suitable for applications that prioritize initial load time and search engine optimization, while CSR may be more suitable for applications that require dynamic and interactive content and do not need to refresh the page. However, the decision will ultimately depend on the specific needs and goals of the web application.

Top comments (0)