Skip to content

Commit

Permalink
add session key
Browse files Browse the repository at this point in the history
  • Loading branch information
guitavano committed Dec 4, 2024
1 parent 54d296b commit 68b54a5
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions blocks/matcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,17 @@ type MatchFunc<TConfig = any> =

export type MatcherStickiness = "session" | "none";

export interface MatcherModule extends
export interface MatcherModule<TProps> extends
BlockModule<
MatchFunc,
MatchFunc<TProps>,
boolean | ((ctx: MatchContext) => boolean),
(ctx: MatchContext) => boolean
> {
sticky?: MatcherStickiness;
sessionKey?: (
props: TProps,
ctx: MatchContext,
) => string | null;
}

const charByType = {
Expand All @@ -100,7 +104,7 @@ const matcherBlock: Block<
> = {
type: "matchers",
adapt: <TConfig = unknown>(
{ default: func, sticky }: MatcherModule,
{ default: func, sticky, sessionKey }: MatcherModule<TConfig>,
) =>
(
$live: TConfig,
Expand Down Expand Up @@ -152,7 +156,8 @@ const matcherBlock: Block<
result ??= matcherFunc(ctx);
} else {
hasher.hash(uniqueId);
const cookieName = `${DECO_MATCHER_PREFIX}${hasher.result()}`;
const _sessionKey = sessionKey ? `_${sessionKey?.($live, ctx)}` : "";
const cookieName = `${DECO_MATCHER_PREFIX}${hasher.result()}${_sessionKey}`;
hasher.reset();
const isMatchFromCookie = cookieValue.boolean(
getCookies(ctx.request.headers)[cookieName],
Expand Down

0 comments on commit 68b54a5

Please sign in to comment.