A URL identifies a navigable application state; a history entry records a step through that state. Search filters, selected case IDs, and pagination cursors often belong in the URL when a reload, bookmark, or shared link should reproduce them. Temporary input text or an unsubmitted draft usually does not. Use pushState when the user has moved to a meaningful new state and expects Back to undo it. Use replaceState for a correction to the current entry, such as normalizing a default filter or replacing a transient cursor after the server resolves it. These methods change history, but do not fetch content or emit popstate at call time; the router must render the new state and handle later history traversal.
URL State and History Entry Contracts
Working case
Reviewer 29 filters the queue to Pending, opens case 47, then presses Back. The expected result is the same Pending queue and page position, not an empty default queue. Encode the filter and cursor in the queue URL and add a history entry when opening the case. A spelling correction to the filter parameter should replace the current entry so Back does not walk through invalid URLs. Keep private record data out of history state; the state object is for small navigation metadata, while authorization and current content still come from the server.
Implementation boundary
function historyAction(previousUrl, nextUrl, userCommitted) {
if (previousUrl === nextUrl) return "none";
return userCommitted ? "push" : "replace";
}
console.log(historyAction("/cases?state=pending", "/cases/47", true));
// Output: pushParse query values into a typed route model, validate them, and create a canonical URL before rendering. Treat the URL as input, not as proof the viewer may read a case. A client-side transition should update document title, route content, and focus deliberately; a full document request must still serve the same view when opened directly. Store only small serializable navigation hints in history state and avoid copying entire private records into it. On popstate, re-derive the route from the current URL and reconcile data freshness before showing a result.
Cost and boundaries
Parsing q query fields is O(q), and each meaningful transition adds an entry that the browser may retain during the session. Encoding every keystroke as a push entry creates a long, unusable Back chain; replacing too aggressively removes real steps. URL length and logs also make sensitive values a poor fit for query strings. A compact cursor is useful, but it must still be validated and tied to the filter it represents. Test the human Back sequence, not just the route parser's return value.
Failure trace
An engineer calls pushState for every search character. Back now walks through dozens of partial queries, and a copied URL may include a private free-text note. Another implementation changes the URL but forgets to rerender, leaving address and content at odds. Debounce or keep typing local until the user commits a search, then create one meaningful history step. Reconcile the router and URL together, and test direct entry as well as browser Back and Forward.
Verification
- Direct entry and reload reproduce the same filtered view.
- Back reverses a committed route change without replaying every keystroke.
- An old case URL still requires current authorization.
Practice drill
Open Pending page two, select case 47, use Back, then Forward, and reload at each step. The filter, cursor, title, and selected case should agree with the address. Type a 20-character search without committing; Back should not require twenty presses. Commit the search once and inspect the entry count. Edit the URL to an invalid cursor and confirm the route responds with a safe normalized or error state. Switch accounts and ensure old history entries never bypass record authorization.
Decision note
Let the URL reproduce shareable state and let each history entry represent one meaningful user decision.
Common Mistakes
- Putting private form text in a query string or history state.
- Calling pushState without updating visible content.
- Replacing a meaningful entry that users expect Back to revisit.
Connected lessons
Navigation, History, and Page Lifecycle; Back-Forward Cache and Restored State; Route Scroll and Focus Restoration; Navigation Prefetch and Private Data Budget; Browser Routing and History State; Cursor Pagination for Changing Collections; Authorization: check permission for this record on every request.
Apply and check
Build Project: history-safe review journey and review Web Development: navigation and delivery decisions quiz.
