@wapitee/typhoonx-hydrogen 0.4.0 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -42,11 +42,11 @@ const {nonce, header, NonceProvider} = createContentSecurityPolicy({
42
42
 
43
43
  ### Props
44
44
 
45
- | Prop | Type | Required | Description |
46
- | -------------- | -------- | -------- | ------------------------------------------------------------------------ |
47
- | `merchantId` | `string` | yes | TyphoonX merchant ID (`TPX-…`) |
48
- | `shopId` | `string` | yes | Shopify store ID |
49
- | `cookieDomain` | `string` | no | Cookie `Domain` for a first-party client id. Omit for a host-only cookie |
45
+ | Prop | Type | Required | Description |
46
+ | -------------- | -------- | -------- | ----------------------------------------------------------------------------------------------------------------- |
47
+ | `merchantId` | `string` | yes | TyphoonX merchant ID (`TPX-…`) |
48
+ | `shopId` | `string` | yes | Shopify store ID |
49
+ | `cookieDomain` | `string` | no | Cookie `Domain` for a first-party client id. Defaults to the current hostname's apex domain (e.g. `.example.com`) |
50
50
 
51
51
  ## License
52
52
 
@@ -1,17 +1,20 @@
1
1
  /**
2
2
  * Returns the TyphoonX client id from the `__typhoon_client_id` cookie, or
3
- * creates a new UUID and writes it when the cookie is missing.
3
+ * creates a new UUID when the cookie is missing.
4
4
  *
5
5
  * The cookie is scoped to `Path=/`, uses `SameSite=Lax`, and expires after
6
6
  * one year. When `cookieDomain` is set, that value is used as the cookie
7
- * `Domain` so the id can be shared across subdomains. The `Secure` flag is
8
- * added automatically on HTTPS pages.
7
+ * `Domain`. Otherwise the Domain defaults to the highest domain the browser
8
+ * will accept (e.g. `.example.com` for `www.example.com`) so the id is
9
+ * shared across subdomains. An existing id is written again with that Domain;
10
+ * a previous host-only cookie is expired (omit `Domain`) so only the apex
11
+ * cookie remains. The `Secure` flag is added automatically on HTTPS pages.
9
12
  *
10
13
  * @remarks
11
14
  * This helper must run in the browser. It reads and writes `document.cookie`.
12
15
  *
13
- * @param cookieDomain - Optional cookie `Domain` attribute, typically the
14
- * storefront apex domain.
16
+ * @param cookieDomain - Optional cookie `Domain` attribute. Omit to use the
17
+ * current hostname's apex domain.
15
18
  * @returns The existing or newly created client id.
16
19
  */
17
20
  export declare function getOrCreateClientId(cookieDomain?: string): string;
@@ -1 +1 @@
1
- {"version":3,"file":"client-id.d.ts","sourceRoot":"","sources":["../src/client-id.ts"],"names":[],"mappings":"AAMA;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,mBAAmB,CAAC,YAAY,CAAC,EAAE,MAAM,GAAG,MAAM,CA8BjE"}
1
+ {"version":3,"file":"client-id.d.ts","sourceRoot":"","sources":["../src/client-id.ts"],"names":[],"mappings":"AAMA;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,mBAAmB,CAAC,YAAY,CAAC,EAAE,MAAM,GAAG,MAAM,CAqCjE"}
package/dist/client-id.js CHANGED
@@ -4,18 +4,21 @@ const COOKIE = '__typhoon_client_id';
4
4
  const MAX_AGE = 31_536_000;
5
5
  /**
6
6
  * Returns the TyphoonX client id from the `__typhoon_client_id` cookie, or
7
- * creates a new UUID and writes it when the cookie is missing.
7
+ * creates a new UUID when the cookie is missing.
8
8
  *
9
9
  * The cookie is scoped to `Path=/`, uses `SameSite=Lax`, and expires after
10
10
  * one year. When `cookieDomain` is set, that value is used as the cookie
11
- * `Domain` so the id can be shared across subdomains. The `Secure` flag is
12
- * added automatically on HTTPS pages.
11
+ * `Domain`. Otherwise the Domain defaults to the highest domain the browser
12
+ * will accept (e.g. `.example.com` for `www.example.com`) so the id is
13
+ * shared across subdomains. An existing id is written again with that Domain;
14
+ * a previous host-only cookie is expired (omit `Domain`) so only the apex
15
+ * cookie remains. The `Secure` flag is added automatically on HTTPS pages.
13
16
  *
14
17
  * @remarks
15
18
  * This helper must run in the browser. It reads and writes `document.cookie`.
16
19
  *
17
- * @param cookieDomain - Optional cookie `Domain` attribute, typically the
18
- * storefront apex domain.
20
+ * @param cookieDomain - Optional cookie `Domain` attribute. Omit to use the
21
+ * current hostname's apex domain.
19
22
  * @returns The existing or newly created client id.
20
23
  */
21
24
  export function getOrCreateClientId(cookieDomain) {
@@ -25,22 +28,44 @@ export function getOrCreateClientId(cookieDomain) {
25
28
  .map((part) => part.trim())
26
29
  .find((part) => part.startsWith(prefix))
27
30
  ?.slice(prefix.length);
28
- if (existing) {
29
- return decodeURIComponent(existing);
31
+ const id = existing ? decodeURIComponent(existing) : crypto.randomUUID();
32
+ const domain = cookieDomain ?? apexCookieDomain(window.location.hostname);
33
+ const https = window.location.protocol === 'https:';
34
+ if (existing && domain) {
35
+ const expire = [`${COOKIE}=`, 'Path=/', 'SameSite=Lax', 'Max-Age=0'];
36
+ if (https) {
37
+ expire.push('Secure');
38
+ }
39
+ document.cookie = expire.join('; ');
30
40
  }
31
- const id = crypto.randomUUID();
32
41
  const segments = [
33
42
  `${COOKIE}=${id}`,
34
43
  'Path=/',
35
44
  'SameSite=Lax',
36
45
  `Max-Age=${String(MAX_AGE)}`,
37
46
  ];
38
- if (cookieDomain) {
39
- segments.push(`Domain=${cookieDomain}`);
47
+ if (domain) {
48
+ segments.push(`Domain=${domain}`);
40
49
  }
41
- if (window.location.protocol === 'https:') {
50
+ if (https) {
42
51
  segments.push('Secure');
43
52
  }
44
53
  document.cookie = segments.join('; ');
45
54
  return id;
46
55
  }
56
+ function apexCookieDomain(hostname) {
57
+ const labels = hostname.split('.');
58
+ const secure = window.location.protocol === 'https:' ? '; Secure' : '';
59
+ for (let n = 2; n <= labels.length; n += 1) {
60
+ const domain = `.${labels.slice(-n).join('.')}`;
61
+ const token = crypto.randomUUID();
62
+ const suffix = `Path=/; SameSite=Lax; Domain=${domain}${secure}`;
63
+ document.cookie = `__typhoon_d=${token}; ${suffix}; Max-Age=60`;
64
+ const accepted = document.cookie.includes(`__typhoon_d=${token}`);
65
+ document.cookie = `__typhoon_d=; ${suffix}; Max-Age=0`;
66
+ if (accepted) {
67
+ return domain;
68
+ }
69
+ }
70
+ return undefined;
71
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wapitee/typhoonx-hydrogen",
3
- "version": "0.4.0",
3
+ "version": "0.4.1",
4
4
  "description": "TyphoonX Hydrogen Analytics subscriber",
5
5
  "homepage": "https://github.com/Wapitee-Interactive-Marketing-Limited/typhoonx-js/tree/main/packages/typhoonx-hydrogen",
6
6
  "bugs": {