Skip to content

Commit

Permalink
use visibilitychange api instead of focus event
Browse files Browse the repository at this point in the history
  • Loading branch information
dferber90 committed Feb 13, 2021
1 parent f9571ce commit 30c9038
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
1 change: 0 additions & 1 deletion example/pages/examples/pure-static-site-generation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { GetStaticProps } from "next";
import { Layout } from "../../components/Layout";
import { Result } from "../../components/Result";
import { getFlags } from "@happykit/flags/server";
import { InitialFlagState } from "@happykit/flags/client";

type AppFlags = { size: "small" | "medium" | "large" };
type StaticProps = { flags: AppFlags };
Expand Down
8 changes: 5 additions & 3 deletions package/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,12 +223,14 @@ export function useFlags<F extends Flags = Flags>(
if (!shouldRevalidateOnFocus) return;

function handleFocus() {
dispatch({ type: "evaluate", input });
if (document.visibilityState === "visible") {
dispatch({ type: "evaluate", input });
}
}

window.addEventListener("focus", handleFocus);
document.addEventListener("visibilitychange", handleFocus);
return () => {
window.removeEventListener("focus", handleFocus);
document.removeEventListener("visibilitychange", handleFocus);
};
}, [state, currentUser, currentTraits, shouldRevalidateOnFocus]);

Expand Down

0 comments on commit 30c9038

Please sign in to comment.