Table of contents
1.
Introduction
2.
What is Static Rendering
3.
Render
4.
Example
4.1.
Program
4.2.
Output
5.
FAQs
6.
Key Takeaways
Last Updated: Mar 27, 2024

Static Rendering

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

We have often come across sites that take too long to load, even when they contain little to no components that may take a long time to load. Of course, we don’t want this to happen to the pages we have built. 

Thus, when having sites that are static in general, we don’t need to connect to the server and fetch data from it rather make the site static render. In this way, we can also test components that can be tested according to the desired React tree desired.

So in this blog, we will learn about a great way to make sure our web pages load without any delay. At the end of the blog, we will create components that render without any delay using the render function from Enzyme.

What is Static Rendering

Usually, when a user requests a page, the server converts the HTML elements to meaningful information that can then be shown as a response to the user. However, even though these pages were very static, it took a lot of time to reload every time a request was sent to the server.

Now though, the websites are dynamic concerning the type of requests and responses sent and received. Thus, to make sure that the applications load without any delay flawlessly for a great User Experience, we need these websites to load faster and with better Search Engine Optimization (SEO).

This is where Static Rendering comes into play. A Static Site Generator generates static HTML websites and pages based on data and templates. These generators have pre-built HTML pages and make them available to the user ahead of time. Thus, there is no apparent delay in loading the webpages as the generator renders the page at build time and the site is static.

Render

Enzyme is one of the best testing libraries for React Components testing using unit testing principles. Render Function in Enzyme is one of the most popular ways to render React components as static HTML and analyze the resulting HTML structure similar to that of a React Tree.

The render function returns a CheerioWrapper around the rendered HTML of the single node’s subtree of the resultant React tree structure created using the render function.

Cheerio is one of the most popular tools that can be used to parse HTML and XML in Node.js. Thus, here in this render function, we will be taking the return of the function as a CheerioWrapper as we will be using the third-party API to traverse through the HTML Tree.

Cheerio parses a markup and provides an API for manipulating the resulting data structure, i.e., the React tree in our case. Therefore we can call this function the Static Rendering API.

Thus, render acts as a way to test static JSX and HTML renders in your application when used alongside the concepts of unit testing and Cheerio as a third-party parsing and traversal library for HTML. This usage of a third-party Cheerio for parsing and traversing HTML sets render apart from the mount and shallow testing of React components.

Cheerio makes the parsing and traversing of HTML a much less hassle than when we need to test a React component. Here, we can take CheerioWrapper analogous to ReactWrapper and ShallowWrapper constructors.

Example

First, we create a React component, for example, Foo here, which will contain the component that we will use to render on and test if we can access the React tree in the way we need to.

Program

import React from "react";
export default class Foo extends React.Component {
componentDidMount() {
  console.log("Foo mount");
}
render() {
  return <h1>Hello Foo</h1>;
}
}

Then we create a test suite that contains the tests we need to run on our component. Here, we will be running a single test to check the length of the particular component using a chai assertion style.

import {render} from 'enzyme';

describe('<Foo/>', ()=>{
    it('renders three `.foo-bar`.s', ()=>{
        const wrapper = render(<Foo/>);
        expect(wrapper.find('.foo-bar')).to.have.length(3);
    })
})

Output

When we run the tests we are able to successfully pass the test in the suite as we are able to access the structure in the wrapper returned by render function. 

FAQs

1. When should we use static rendering?

Ans: The most appropriate sites to use static rendering is when the sites don’t need multiple requests to the server, in which case it would have no effect. This makes sure that the server doesn’t create additional browser-to-server requests.

 

2. What is prerendering?

Ans: Prerendering is when a client-side application is run during the build time to capture its initial state as a static HTML.
 

3. Does Static rendering increase SEO?

Ans: As the non-Static rendered sites are also accessible by google, there is not much difference in using Static Rendering in SEO.

 

4. Can we use render in functional components?

Ans: When we use a functional version, we can get a simple JSX value similar to the component rendering engine.

Key Takeaways

In this blog, we discussed Static Rendering and using the render function from Enzyme to test the React structure of the wrapper returned by the render.

You may want to learn more about rendering mechanism here, or about ShallowWrapper rendering here.

Check out this problem - Duplicate Subtree In Binary Tree

Learning never stops, and to feed your quest to learn and become more skilled, head over to our practice platform Coding Ninjas Studio to practice top problems, attempt mock tests, read interview experiences, and much more.!

Happy Learning!

Live masterclass