The Navigation API: intercept navigations in a theme without a router library
A theme that loads pages of a portfolio or a filtered list with fetch() needs three things: intercept the link click, update the URL with history.pushState(), and handle the back button with popstate. The History API makes this fiddly: popstate does not fire on pushState, form submissions and programmatic navigations are separate cases, and it is easy to break the back button. The Navigation API gives one central navigate event for all of them.
Problem: A portfolio theme loads category pages with AJAX. Links are intercepted one by one, the back button sometimes shows the wrong grid, and a reload in the middle of a filter sequence loses the scroll position.
Solution: Listen to navigation‘s navigate event, check that the navigation is same-origin and targets a portfolio URL, call event.intercept() with a handler that fetches the page and swaps the grid, and let the browser handle URL, history entries, back/forward and scroll restoration. Browsers without the API keep the normal page loads.
One listener for links, back and forward
// js/portfolio-navigation.js
if ( 'navigation' in window ) {
navigation.addEventListener( 'navigate', ( event ) => {
const url = new URL( event.destination.url );
if ( ! event.canIntercept
|| event.hashChange
|| event.downloadRequest !== null
|| event.formData
|| url.origin !== location.origin
|| ! url.pathname.startsWith( '/portfolio/' ) ) {
return; // normal navigation
}
event.intercept( {
async handler() {
const response = await fetch( url, { signal: event.signal } );
const doc = new DOMParser().parseFromString( await response.text(), 'text/html' );
const fresh = doc.querySelector( '.ha-portfolio-grid' );
if ( ! fresh ) {
location.reload();
return;
}
document.querySelector( '.ha-portfolio-grid' ).replaceChildren( ...fresh.children );
document.title = doc.title;
},
} );
} );
}
- The event fires for link clicks,
location.hrefchanges, back and forward, andnavigation.navigate()calls: one place instead of click handlers pluspopstate. event.intercept()turns the navigation into a same-document navigation. The URL and the history entry are updated by the browser; the handler only renders.event.signalis aborted when the user navigates again before the fetch is done, so an outdated response does not overwrite a newer one.- Scroll position: by default the browser restores it after the handler’s promise settles (for back/forward) or scrolls to the top (for new navigations).
- Downloads, hash links, form posts and other origins pass through untouched.
Navigate from code and read the state
document.querySelector( '#ha-sort' ).addEventListener( 'change', ( e ) => {
const url = new URL( location.href );
url.searchParams.set( 'orderby', e.target.value );
navigation.navigate( url.href, { history: 'replace', state: { orderby: e.target.value } } );
} );
console.log( navigation.currentEntry.getState() ); // { orderby: 'date' }
history: 'replace' replaces the current entry instead of adding one, so changing the sort five times does not create five back-button steps. The state object is stored with the entry and survives reloads.
The server side stays normal
Every URL the script handles must also work as a normal page load: the category pages are regular WordPress archives, and the script only fetches them. That makes the fallback automatic and keeps the pages indexable. The fetch could use the REST API instead of whole pages; parsing the HTML has the advantage that the theme’s templates stay the single source of the markup.
NOTE: The Navigation API is available in Chrome and Edge since version 102 (2022). Firefox and Safari do not support it yet (May 2025), which is why the code is wrapped in 'navigation' in window: there, links are simply normal page loads. For the animation between the old and the new grid, combine the handler with document.startViewTransition(): View Transitions in WordPress themes.