The History API and NextJS

The History API and NextJS

Photo by Jess Bailey on Unsplash

The History API in modern web browsers provides a way to interact with the browser's history stack. This stack is essentially a list of pages the user has visited in the same tab or frame, including their URLs and, optionally, state objects. Here's how information is stored and manipulated using the History API:

  1. Direct Visit to a Page: If a user directly accesses a Next.js page (e.g., by entering the URL in the address bar or clicking a link from an external site), the page is initially rendered on the server (or statically generated if it's a static page). In this case, the History API isn't involved in the initial loading of the page. The browser's default behaviour handles the loading, and the server provides the fully rendered HTML. Once the page is loaded, React 'hydrates' it, making it fully interactive.

  2. Clicking a Link for the First Time in the App: If the user is already on a Next.js page and clicks a Link component for the first time:

    • History API Interaction: Next.js intercepts the click event and uses the History API to navigate to the new page without a full page reload.

    • pushState Method: Specifically, Next.js uses the pushState method of the History API. This method updates the URL in the address bar and adds an entry to the browser's history stack without reloading the page.

    • Client-Side Navigation: The content for the new page is fetched (if not prefetched) and rendered on the client side. This process is much faster than a traditional page load, as it doesn't require a round trip to the server.

  3. Effect on Browser History:

    • The use of pushState means that the user's action (clicking the link) results in a new entry in the browser's history. This allows the user to use the browser's back button to return to the previous page.

    • This behaviour mimics traditional navigation but is achieved through client-side rendering, which is a cornerstone of single-page applications (SPAs) like those built with Next.js.

  4. First-Time Navigation vs. Subsequent Navigations:

    • In the first-time navigation using a Link, Next.js sets up the client-side routing mechanism, leveraging the History API for subsequent navigations.

    • For any subsequent clicks on Link components within the app, Next.js continues to use the History API to manage the browser's history stack and handle URL changes, providing a seamless navigation experience without full page reloads.

Storing Information in the History Stack

  1. URLs: Each entry in the history stack is primarily a URL. When you navigate to a new page, the browser automatically adds a new entry with that page's URL.

  2. State Object: The History API allows you to associate a state object with each history entry. This state object can hold any kind of data you want to preserve with that entry.

Manipulating History

  1. pushState(): This method adds a new entry to the history stack. It's useful for adding an entry without navigating away from the current page.

    Example:

     history.pushState({ page: 1 }, "title 1", "/page1");
    

    This code adds a new history entry with the URL /page1, a title of "title 1" (which most browsers currently don't use), and a state object { page: 1 }.

  2. replaceState(): This method replaces the current entry on the history stack. It's used when you want to change the current URL without adding a new entry to the stack.

    Example:

     history.replaceState({ page: 2 }, "title 2", "/page2");
    

    Here, the current history entry is replaced with a new URL /page2, a title "title 2", and a state object { page: 2 }.

  3. Navigation: The browser's forward and back buttons allow the user to navigate through the history stack. Additionally, you can programmatically navigate with history.back(), history.forward(), and history.go().

Benefits and Limitations

  • State Persistence: The state object in the history entry allows for some level of state persistence without storing it in the global scope or using external storage.

  • Limitations: The size of the state object is limited (the exact limit varies by browser), and it's not suitable for large amounts of data.

Accessing History API in Chrome's Developer Console

  1. Open Developer Console: Right-click on any webpage (except pages like the Chrome start page) and select "Inspect" or use the shortcut Ctrl + Shift + I (Windows/Linux) or Cmd + Opt + I (Mac) to open the Developer Tools.

  2. Go to the Console Tab: In the Developer Tools, find and click on the "Console" tab.

  3. Interact with the History API: You can now use JavaScript to interact with the History API directly from the console. For example, you can type history.length to see the number of items in the history stack for the current tab, or use history.back() to navigate back one page.

Using the History API in Web Page Scripts

In your JavaScript code within a web page, you can directly use the History API. For example:

// Go back to the previous page
function goBack() {
  history.back();
}

// Go forward to the next page
function goForward() {
  history.forward();
}

// Add a new entry to the history stack
function pushNewEntry(url) {
  history.pushState({ someData: 'example' }, 'New Page', url);
}

Things to Note

  • Security Restrictions: Note that for security reasons, you can only manipulate the history of the current tab or frame. You cannot access the global browser history across all tabs due to privacy and security concerns.

  • Page Reloads: Remember that history.back() or history.forward() in the console will act just like the browser's back and forward buttons, which might cause page reloads if the previous or next page was not loaded via AJAX.

  • Testing: If you're testing History API functionality, it's often better to do so in the context of an actual web page's script to see how it behaves as part of your web application.

Using the History API through Chrome's Developer Console is a convenient way to experiment with and understand how the API works. However, for practical applications, it's typically used within the scripts of web pages or web applications.

Conclusion

The History API provides a way to interact with the browser's session history in a detailed and controlled manner, enhancing the capabilities of single-page applications (SPAs) like those built with Next.js. It allows developers to maintain a coherent user experience with functional forward and back navigation while dynamically manipulating the content and URL of the page.