diff --git a/src/createRouter.ts b/src/createRouter.ts index ad0292a9..37d45355 100644 --- a/src/createRouter.ts +++ b/src/createRouter.ts @@ -144,6 +144,9 @@ export function createRouter(...args: any[]): UmbrellaCoreRouter { history.go(amount); }, + skipNextRender() { + skipHandlingNextApplicationTriggeredNavigation = true; + }, getInitialRoute() { if (__DEV__) { assert("[RouterSessionHistory].getInitialRoute", [ diff --git a/src/types.ts b/src/types.ts index d0976f11..e2edef3a 100644 --- a/src/types.ts +++ b/src/types.ts @@ -434,6 +434,13 @@ export type RouterSession = { */ reset(options: SessionOpts): void; + /** + * Skips next render (but allows history change) + * - push()/replace() after calling this function will skip render + * - going back/forward won't skip render + */ + skipNextRender(): void; + /** * Blocks navigation and registers a listener that is called when * navigation is blocked. Returns a function to unblock navigation. diff --git a/test/createRouter.test.ts b/test/createRouter.test.ts index bb075178..7ee07128 100644 --- a/test/createRouter.test.ts +++ b/test/createRouter.test.ts @@ -46,6 +46,37 @@ describe("createRouter", () => { expect(route.href).toBe("/"); }); + it("should work with skipRender() function ", () => { + const config: RouterOpts = { + session: { + type: "memory", + }, + }; + + const { routes, session } = createRouter(config, { + foo: defineRoute("/foo"), + bar: defineRoute("/bar"), + bar2: defineRoute("/bar2"), + }); + + let route = session.getInitialRoute(); + + session.listen((nextRoute) => (route = nextRoute)); + + expect(route.href).toBe("/"); + session.skipNextRender(); + routes.bar().push(); + expect(route.href).toBe("/"); + routes.bar().push(); + expect(route.href).toBe("/bar"); + session.skipNextRender(); + routes.bar2().push(); + routes.foo().push(); + session.back(); + // going back should ignore skipRender + expect(route.href).toBe("/bar2"); + }); + it("link should work", () => { const config: RouterOpts = { session: {