v1.0.1-alpha.73
Repository
Current version released
3 years ago
Versions
- v1.0.1-alpha.75Latest
- v1.0.1-beta.0
- v1.0.1-alpha.74
- v1.0.1-alpha.73
- v1.0.1-alpha.71
- v1.0.1-alpha.72
- v1.0.1-alpha.70
- v1.0.1-alpha.69
- v1.0.1-alpha.68
- v1.0.1-alpha.67
- v1.0.1-alpha.65
- v1.0.1-alpha.66
- v1.0.1-alpha.64
- v1.0.1-alpha.63
- v1.0.1-alpha.62
- v1.0.1-alpha.61
- v1.0.1-alpha.60
- v1.0.1-alpha.59
- v1.0.1-alpha.58
- v1.0.1-alpha.57
- v1.0.1-alpha.56
- v1.0.1-alpha.55
- v1.0.1-alpha.54
- v1.0.1-alpha.53
- v1.0.1-alpha.52
@virtualstate/app-history
Native JavaScript app-history implementation
Support
Test Coverage
Install
Snowpack
const { AppHistory } = await import("https://cdn.skypack.dev/@virtualstate/app-history");
Or
import { AppHistory } from "https://cdn.skypack.dev/@virtualstate/app-history";
npm / yarn / GitHub
npm i --save @virtualstate/app-history
Or
yarn add @virtualstate/app-history
Then
import { AppHistory } from "@virtualstate/app-history";
Navigation
import { AppHistory } from "./app-history";
const appHistory = new AppHistory();
// Set initial url
appHistory.navigate("/");
appHistory.navigate("/skipped");
// Use .finished to wait for the transition to complete
await appHistory.navigate("/awaited").finished;
Waiting for events
import { AppHistory } from "./app-history";
const appHistory = new AppHistory();
appHistory.addEventListener("navigate", async ({ destination }) => {
if (destination.url === "/disallow") {
throw new Error("No!");
}
});
await appHistory.navigate("/allowed").finished; // Resolves
await appHistory.navigate("/disallow").finished; // Rejects
Transitions
import { AppHistory } from "./app-history";
import { loadPhotoIntoCache } from "./cache";
const appHistory = new AppHistory();
appHistory.addEventListener("navigate", async ({ destination, transitionWhile }) => {
transitionWhile(loadPhotoIntoCache(destination.url));
});
State
import { AppHistory } from "./app-history";
const appHistory = new AppHistory();
appHistory.addEventListener("currentchange", () => {
console.log({ updatedState: appHistory.current?.getState() });
});
await appHistory.updateCurrent({
state: {
items: [
"first",
"second"
],
index: 0
}
}).finished;
await appHistory.updateCurrent({
state: {
...appHistory.current.getState(),
index: 1
}
}).finished;
Updating browser url
This is a pending development task. The below code will help visually update the window
This can be achieved various ways, but if your application completely utilises
the app history interface, then you can directly use pushState
to immediately
update the window’s url.
This does not take into account the browser’s native back/forward functionality, which would need to be investigated further.
import { AppHistory } from "./app-history";
const appHistory = new AppHistory();
const origin = typeof location === "undefined" ? "https://example.com" : location.origin;
appHistory.addEventListener("currentchange", () => {
const { current } = appHistory;
if (!current || !current.sameDocument) return;
const state = current.getState() ?? {};
const { pathname } = new URL(current.url, origin);
if (typeof window !== "undefined" && window.history) {
window.history.pushState(state, state.title, origin)
}
})