Skip to content

@gluonjs/core / packages/router/src / createMemoryHistory

Function: createMemoryHistory() ​

createMemoryHistory(initialEntries?): RouterHistory

Creates a caller-owned history stack that does not read or change browser globals. Use it for tests, server requests, and other isolated Router owners.

The final item in initialEntries is the current location. Earlier items remain available to go(), while later push() calls truncate any forward entries from the current position.

Parameters ​

initialEntries? ​

readonly string[] = ...

Initial URL strings for the stack. Defaults to / and must contain at least one entry.

Returns ​

RouterHistory

A memory-backed history whose listeners and entries remain owned by the caller until destroy() is called.

Throws ​

An Error when initialEntries is empty.

Example ​

Create request- or test-owned history, observe back/forward traversal, then release both the listener and history when the owner is disposed:

ts
import { createMemoryHistory } from '@gluonjs/router/memory';

const history = createMemoryHistory(['/reports/41', '/reports/42']);
console.log(history.location.location); // '/reports/42'

const stop = history.listen((to, from, navigation) => {
  console.log(`${navigation.direction}: ${from.location} -> ${to.location}`);
});

history.push('/reports/43');
history.go(-1); // back: /reports/43 -> /reports/42

stop();
history.destroy();

Version-matched documentation. Examples are checked against the public package contracts.