From 43f17c67c60f566164ba7dcaa010719139183973 Mon Sep 17 00:00:00 2001 From: Taylor Lodge Date: Fri, 26 Jan 2024 10:57:59 +1300 Subject: [PATCH] fix(routing): flakey onloadEvent detection Encountered issues with this during dev and there is no reason to complicate the logic If you are using routing, you can only have one routing root and it must be the top one, so this event is always triggered from a page load --- src/utils.ts | 10 ---------- src/xstateTree.tsx | 4 ++-- 2 files changed, 2 insertions(+), 12 deletions(-) diff --git a/src/utils.ts b/src/utils.ts index fb185bb..d04703f 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -64,16 +64,6 @@ export type StateMachineToInterpreter = T extends StateMachine< ? Interpreter : never; -export function isLikelyPageLoad(): boolean { - // without performance API, we can't tell if this is a page load - if (typeof performance === "undefined") { - return false; - } - - // if it's been < 5 seconds since the page was loaded, it's probably a page load - return performance.now() < 5000; -} - export function difference(a: any, b: any) { const result: Record = {}; diff --git a/src/xstateTree.tsx b/src/xstateTree.tsx index c3bbb5b..d684318 100644 --- a/src/xstateTree.tsx +++ b/src/xstateTree.tsx @@ -30,7 +30,7 @@ import { GetSlotNames, Slot } from "./slots"; import { GlobalEvents, AnyXstateTreeMachine, XstateTreeHistory } from "./types"; import { useConstant } from "./useConstant"; import { useService } from "./useService"; -import { assertIsDefined, isLikelyPageLoad, mergeMeta } from "./utils"; +import { assertIsDefined, mergeMeta } from "./utils"; export const emitter = new TinyEmitter(); @@ -462,7 +462,7 @@ export function buildRootComponent( } = routing; const initialMeta = { ...(routing.history.location.state?.meta ?? {}), - onloadEvent: isLikelyPageLoad(), + onloadEvent: true, } as SharedMeta; const queryString = getQueryString();