To update document for history step application given a Document document, a session history entry entry, a boolean doNotReactivate, integers scriptHistoryLength and scriptHistoryIndex, NavigationType-or-null navigationType, an optional list of session history entries entriesForNavigationAPI, and an optional session history entry previousEntryForActivation:

  1. Let documentIsNew be true if document's latest entry is null; otherwise false.

  2. Let documentsEntryChanged be true if document's latest entry is not entry; otherwise false.

  3. Set document's history object's index to scriptHistoryIndex.

  4. Set document's history object's length to scriptHistoryLength.

  5. Let navigation be history's relevant global object's navigation API.

  6. If documentsEntryChanged is true:

    1. Let oldURL be document's latest entry's URL.

    2. Set document's latest entry to entry.

    3. Restore the history object state given document and entry.

    4. If documentIsNew is false:

      1. Assert: navigationType is not null.

      2. Update the navigation API entries for a same-document navigation given navigation, entry, and navigationType.

      3. Fire an event named popstate at document's relevant global object, using PopStateEvent, with the state attribute initialized to document's history object's state and hasUAVisualTransition initialized to true if a visual transition, to display a cached rendered state of the latest entry, was done by the user agent.

      4. Restore persisted state given entry.

      5. If oldURL's fragment is not equal to entry's URL's fragment, then queue a global task on the DOM manipulation task source given document's relevant global object to fire an event named hashchange at document's relevant global object, using HashChangeEvent, with the oldURL attribute initialized to the serialization of oldURL and the newURL attribute initialized to the serialization of entry's URL.

    5. Otherwise:

      1. Assert: entriesForNavigationAPI is given.

      2. Restore persisted state given entry.

      3. Initialize the navigation API entries for a new document given navigation, entriesForNavigationAPI, and entry.

  7. If all the following are true:

    • previousEntryForActivation is given;

    • navigationType is non-null; and

    • navigationType is "reload" or previousEntryForActivation's document is not document,

    then:

    1. If navigation's activation is null, then set navigation's activation to a new NavigationActivation object in navigation's relevant realm.

    2. Let previousEntryIndex be the result of getting the navigation API entry index of previousEntryForActivation within navigation.

    3. If previousEntryIndex is non-negative, then set activation's old entry to navigation's entry list[previousEntryIndex].

    4. Otherwise, if all the following are true:

      then set activation's old entry to a new NavigationHistoryEntry in navigation's relevant realm, whose session history entry is previousEntryForActivation.

    5. Set activation's new entry to navigation's current entry.

    6. Set activation's navigation type to navigationType.

  8. If documentIsNew is true:

    1. Assert: document's during-loading navigation ID for WebDriver BiDi is not null.

    2. Invoke WebDriver BiDi navigation committed with navigable and a new WebDriver BiDi navigation status whose id is document's during-loading navigation ID for WebDriver BiDi, status is "committed", and url is document's URL.

    3. Try to scroll to the fragment for document.

    4. At this point scripts may run for the newly-created document document.

  9. Otherwise, if documentsEntryChanged is false and doNotReactivate is false:

    1. Assert: entriesForNavigationAPI is given.

    2. Reactivate document given entry and entriesForNavigationAPI.

    documentsEntryChanged can be false for one of two reasons: either we are restoring from bfcache, or we are asynchronously finishing up a synchronous navigation which already synchronously set document's latest entry. The doNotReactivate argument distinguishes between these two cases.