Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion packages/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
"rollup": "^4.46.2",
"typescript": "^5.8.3",
"vite": "^5.4.19",
"urlpattern-polyfill": "^10.0.0",
"vitest": "^3.2.4"
},
"dependencies": {
Expand All @@ -77,7 +78,14 @@
"eventemitter2": "^6.4.5",
"jwt-decode": "^4.0.0",
"nanoid": "^3.3.11",
"phoenix": "1.8.3",
"phoenix": "1.8.3"
},
"peerDependencies": {
"urlpattern-polyfill": "^10.0.0"
},
"peerDependenciesMeta": {
"urlpattern-polyfill": {
"optional": true
}
}
}
21 changes: 19 additions & 2 deletions packages/client/src/clients/guide/client.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { GenericData } from "@knocklabs/types";
import { Store } from "@tanstack/store";
import { Channel, Socket } from "phoenix";
import { URLPattern } from "urlpattern-polyfill";

import Knock from "../../knock";

Expand Down Expand Up @@ -54,6 +53,23 @@ import {

// How long to wait until we resolve the guides order and determine the
// prevailing guide.
// Resolve URLPattern from the native global or the optional polyfill.
// Browsers that support URLPattern natively (Chrome 95+, Edge 95+, etc.) don't
// need the polyfill. For older browsers, install `urlpattern-polyfill` as a
// dependency in your project.
function getURLPattern(): typeof URLPattern {
const native = (globalThis as Record<string, unknown>).URLPattern as
| typeof URLPattern
| undefined;

if (native) return native;

throw new Error(
"URLPattern is not available. Install the `urlpattern-polyfill` package " +
"or use a browser that supports URLPattern natively.",
);
}

const DEFAULT_ORDER_RESOLUTION_DURATION = 50; // in milliseconds

// How often we should increment the counter to refresh the store state and
Expand Down Expand Up @@ -1156,11 +1172,12 @@ export class KnockGuideClient {
return localStep;
});

const URLPatternImpl = getURLPattern();
localGuide.activation_url_patterns =
remoteGuide.activation_url_patterns.map((rule) => {
return {
...rule,
pattern: new URLPattern({
pattern: new URLPatternImpl({
pathname: rule.pathname ?? undefined,
search: rule.search ?? undefined,
}),
Expand Down
Loading