A lightweight TypeScript utility that navigates back to a specific route in browser history using the Navigation API, preserving scroll position
npm install go-back-toA lightweight TypeScript utility that navigates back to a specific route in browser history using the Navigation API, preserving scroll position.
Perfect for when you have a back button in your layout that needs to return users to an overview page preserving url state & scroll position, even after they've navigated through multiple nested routes. It uses the Navigation API to calculate how many steps back in the History it has to go to reach the targetPathname.

- 🎯 Navigate back to a specific pathname in history
- 📜 Preserves scroll position automatically (thanks to Navigation API)
- 🔍 Flexible pathname matching (exact string or custom function)
- 🔄 Graceful fallback when Navigation API is not available
- 📦 Zero dependencies
- 🎨 Full TypeScript support
- ⚛️ Works with any framework (React, Vue, Svelte, vanilla JS, etc.)
``bash`
npm install go-back-toor
pnpm add go-back-toor
yarn add go-back-to
`ts
import { goBackTo } from "go-back-to";
// Navigate back to the home page using the Navigation API preserving url state & scroll position
goBackTo({ targetPathname: "/" });
`
1. Navigation API: The function uses the native browser Navigation API to access browser history entries. Because it relies on the browser's native API rather than framework-specific routing, it works with any framework.
2. History Search: It searches backwards through history to find the closest entry matching your target pathname.
3. Navigation via history.go(): Once the matching entry is found, it uses window.history.go(-stepsBack) to navigate. This approach has key advantages:window.location.href
- Preserves scroll position: The browser automatically restores the exact scroll position you were at, which is perfect for nested routes and long pages.
- Preserves page state: Unlike , using history.go() maintains the page's JavaScript state, form inputs, and component state.fallbackUrl
4. Fallback: If the Navigation API is not available or no matching entry is found, it falls back to the provided , or uses targetPathname if it's a string, or uses history.back().
This will loop through all NavigationHistoryEntry items backwards until you return true. The matcher function receives the full URL object, allowing you to check pathname, search params, origin, and more.
`ts
import { goBackTo } from "go-back-to";
// Go back to a search page
goBackTo({
targetPathname: (url) =>
url.pathname.startsWith("/search"),
});
`
The fallbackUrl is only needed when using a custom pathname matcher function and you want to support browsers that don't support the Navigation API. When targetPathname is a string, it will automatically be used as the fallback URL if the Navigation API is unavailable or no matching entry is found.
`ts
import { goBackTo } from "go-back-to";
// When using a custom matcher, provide fallbackUrl for browsers without Navigation API support
goBackTo({
targetPathname: (url) => url.pathname.startsWith("/dashboard"),
fallbackUrl: "/dashboard", // Used if Navigation API is unavailable or no match found
});
`
When using with React Router, Next.js, or other frameworks, use fallbackCallback to navigate using their router instead of directly setting window.location.href:
`ts
import { goBackTo } from "go-back-to";
import { useNavigate } from "react-router"; // or your framework's navigation
const navigate = useNavigate();
goBackTo({
targetPathname: "/",
fallbackCallback: () => navigate("/"), // Uses React Router navigation
});
`
`ts
import { goBackTo } from "go-back-to";
// Go back to any page with a specific search param
goBackTo({
targetPathname: (url) => url.searchParams.has("filter"),
});
`
Since goBackTo is a plain function, you can use it directly in event handlers. For Next.js App Router, use fallbackCallback with the router:
`tsx
import { goBackTo } from "go-back-to";
import { useRouter } from "next/navigation";
function BackButton() {
const router = useRouter();
return (
onClick={() =>
goBackTo({
targetPathname: "/",
fallbackCallback: () => router.push("/"),
})
}
>
Go to Home
);
}
`
Navigates back to the target route in browser history.
#### Options
| Option | Type | Default | Description |
|--------|------|---------|-------------|
| targetPathname | string \| ((url: URL) => boolean) | "/" | The target pathname to go back to. Can be an exact string match or a custom matcher function that receives the full URL object. |fallbackUrl
| | string | undefined | Fallback URL to navigate to if Navigation API is not available and no matching history entry is found. |fallbackCallback
| | () => void | undefined | Callback function to call instead of directly setting window.location.href. Useful for framework-specific navigation (e.g., React Router, Next.js). You should handle the navigation logic and URL determination within this callback. |
#### Returns
void
The function works in all modern browsers. For browsers that don't support the Navigation API, it gracefully falls back to using window.location.href or history.back().
See Navigation API browser support on Can I Use for detailed compatibility information.
This is a monorepo using pnpm workspaces. To develop:
`bashInstall dependencies
pnpm install
$3
There are test apps available to test the function with different routers:
-
test-apps/react-router-esm - React Router (ESM)
- test-apps/tanstack-router - TanStack RouterTo run a test app:
`bash
cd test-apps/tanstack-router
pnpm dev
``MIT