Skip to content

Commit

Permalink
fix(routing): flakey onloadEvent detection
Browse files Browse the repository at this point in the history
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
  • Loading branch information
UberMouse committed Jan 25, 2024
1 parent f99d57c commit 43f17c6
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 12 deletions.
10 changes: 0 additions & 10 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,6 @@ export type StateMachineToInterpreter<T> = T extends StateMachine<
? Interpreter<TContext, TSchema, TEvents, TState, any>
: 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<any, any> = {};

Expand Down
4 changes: 2 additions & 2 deletions src/xstateTree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -462,7 +462,7 @@ export function buildRootComponent(
} = routing;
const initialMeta = {
...(routing.history.location.state?.meta ?? {}),
onloadEvent: isLikelyPageLoad(),
onloadEvent: true,
} as SharedMeta;

const queryString = getQueryString();
Expand Down

0 comments on commit 43f17c6

Please sign in to comment.