@reykjavik/webtools 0.2.1 → 0.2.2

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/CHANGELOG.md CHANGED
@@ -4,11 +4,20 @@
4
4
 
5
5
  - ... <!-- Add new lines here. -->
6
6
 
7
+ ## 0.2.2
8
+
9
+ _2024-12-19_
10
+
11
+ - `@reykjavik/webtools/CookieHubConsent`:
12
+ - feat: Allow passing explicit `undefined` as `accountId` to skip loading
13
+ - fix: Note that `window.cookiehub` is possibly `undefined` (before load)
14
+
7
15
  ## 0.2.1
8
16
 
9
17
  _2024-12-17_
10
18
 
11
- - fix: Make typing of failed ResultTuple/ResultTupleObj less ambiguous
19
+ - `@reykjavik/webtools/errorhandling`:
20
+ - fix: Make typing of failed ResultTuple/ResultTupleObj less ambiguous
12
21
 
13
22
  ## 0.2.0
14
23
 
@@ -2,7 +2,7 @@ import React, { ReactNode } from 'react';
2
2
  import { EitherObj } from '@reykjavik/hanna-utils';
3
3
  declare global {
4
4
  interface Window {
5
- cookiehub: CookieHub;
5
+ cookiehub?: CookieHub;
6
6
  }
7
7
  }
8
8
  type CookieHub = {
@@ -180,9 +180,11 @@ export type CookieHubProviderProps = EitherObj<{
180
180
  * extracted from the script embed URL like this:
181
181
  * `"https://cookiehub.net/c2/[ACCOUNT_ID].js"`
182
182
  *
183
+ * Pass `undefined` to disable/skip the script loading
184
+ *
183
185
  * @see https://support.cookiehub.com/article/155-manual-implementation-guide
184
186
  */
185
- accountId: string;
187
+ accountId: string | undefined;
186
188
  }, {
187
189
  /**
188
190
  * The full CookieHub embed script URL.
@@ -65,14 +65,26 @@ const moveCookiehubScriptInDomTree = () => {
65
65
  */
66
66
  const CookieHubProvider = (props) => {
67
67
  const [state, setState] = (0, react_1.useState)(initialConsentState);
68
+ const scriptUrLOrId = props.scriptUrl
69
+ ? `@ ${props.scriptUrl}`
70
+ : props.accountId || undefined;
68
71
  (0, react_1.useEffect)(() => {
72
+ if (!scriptUrLOrId) {
73
+ return;
74
+ }
75
+ const scriptSrc = scriptUrLOrId.startsWith('@ ')
76
+ ? scriptUrLOrId.slice(2)
77
+ : scriptUrlTemplate.replace(idToken, scriptUrLOrId);
78
+ if (window.cookiehub && document.querySelector('script#cookiehub-script')) {
79
+ // We can't load the script from more than one source at a time
80
+ console.warn('CookieHub script already loaded.');
81
+ return;
82
+ }
83
+ const opts = props.options || {};
69
84
  const script = document.createElement('script');
70
85
  script.async = true;
71
- const opts = props.options || {};
72
- script.src =
73
- props.scriptUrl != null
74
- ? props.scriptUrl
75
- : scriptUrlTemplate.replace(idToken, props.accountId);
86
+ script.id = 'cookiehub-script';
87
+ script.src = scriptSrc;
76
88
  script.onload = () => {
77
89
  window.cookiehub.load({
78
90
  ...opts,
@@ -124,8 +136,11 @@ const CookieHubProvider = (props) => {
124
136
  props.onError && (script.onerror = props.onError);
125
137
  document.body.append(script);
126
138
  },
139
+ // Unless we can find a safe way to tear down the CookieHub script and
140
+ // clean up after it we can only load it once, so monitoring anything other
141
+ // than accountId or scriptUrl is pointless.
127
142
  // eslint-disable-next-line react-hooks/exhaustive-deps
128
- []);
143
+ [scriptUrLOrId]);
129
144
  return (react_1.default.createElement(CookieHubContext.Provider, { value: state }, props.children));
130
145
  };
131
146
  exports.CookieHubProvider = CookieHubProvider;
package/README.md CHANGED
@@ -723,8 +723,8 @@ export default function App() {
723
723
  The Component's props have detailed JSDoc comments (displayed in your code
724
724
  editor), but there's a brief summary:
725
725
 
726
- - `accountId?: string` — Your CookieHub account ID. (alternative to
727
- `scriptUrl` prop).
726
+ - `accountId?: string | undefined` — Your CookieHub account ID. (alternative
727
+ to `scriptUrl` prop). Pass `undefined` to skip loading the script.
728
728
  - `scriptUrl?: string` — The full CookieHub embed script URL. (alternative to
729
729
  `accountId` prop).
730
730
  - `options?: CookieHubOptions` — Raw CookieHub options object that gets used
@@ -2,7 +2,7 @@ import React, { ReactNode } from 'react';
2
2
  import { EitherObj } from '@reykjavik/hanna-utils';
3
3
  declare global {
4
4
  interface Window {
5
- cookiehub: CookieHub;
5
+ cookiehub?: CookieHub;
6
6
  }
7
7
  }
8
8
  type CookieHub = {
@@ -180,9 +180,11 @@ export type CookieHubProviderProps = EitherObj<{
180
180
  * extracted from the script embed URL like this:
181
181
  * `"https://cookiehub.net/c2/[ACCOUNT_ID].js"`
182
182
  *
183
+ * Pass `undefined` to disable/skip the script loading
184
+ *
183
185
  * @see https://support.cookiehub.com/article/155-manual-implementation-guide
184
186
  */
185
- accountId: string;
187
+ accountId: string | undefined;
186
188
  }, {
187
189
  /**
188
190
  * The full CookieHub embed script URL.
@@ -39,14 +39,26 @@ const moveCookiehubScriptInDomTree = () => {
39
39
  */
40
40
  export const CookieHubProvider = (props) => {
41
41
  const [state, setState] = useState(initialConsentState);
42
+ const scriptUrLOrId = props.scriptUrl
43
+ ? `@ ${props.scriptUrl}`
44
+ : props.accountId || undefined;
42
45
  useEffect(() => {
46
+ if (!scriptUrLOrId) {
47
+ return;
48
+ }
49
+ const scriptSrc = scriptUrLOrId.startsWith('@ ')
50
+ ? scriptUrLOrId.slice(2)
51
+ : scriptUrlTemplate.replace(idToken, scriptUrLOrId);
52
+ if (window.cookiehub && document.querySelector('script#cookiehub-script')) {
53
+ // We can't load the script from more than one source at a time
54
+ console.warn('CookieHub script already loaded.');
55
+ return;
56
+ }
57
+ const opts = props.options || {};
43
58
  const script = document.createElement('script');
44
59
  script.async = true;
45
- const opts = props.options || {};
46
- script.src =
47
- props.scriptUrl != null
48
- ? props.scriptUrl
49
- : scriptUrlTemplate.replace(idToken, props.accountId);
60
+ script.id = 'cookiehub-script';
61
+ script.src = scriptSrc;
50
62
  script.onload = () => {
51
63
  window.cookiehub.load({
52
64
  ...opts,
@@ -98,8 +110,11 @@ export const CookieHubProvider = (props) => {
98
110
  props.onError && (script.onerror = props.onError);
99
111
  document.body.append(script);
100
112
  },
113
+ // Unless we can find a safe way to tear down the CookieHub script and
114
+ // clean up after it we can only load it once, so monitoring anything other
115
+ // than accountId or scriptUrl is pointless.
101
116
  // eslint-disable-next-line react-hooks/exhaustive-deps
102
- []);
117
+ [scriptUrLOrId]);
103
118
  return (React.createElement(CookieHubContext.Provider, { value: state }, props.children));
104
119
  };
105
120
  // ---------------------------------------------------------------------------
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@reykjavik/webtools",
3
- "version": "0.2.1",
3
+ "version": "0.2.2",
4
4
  "description": "Misc. JS/TS helpers used by Reykjavík City's web dev teams.",
5
5
  "main": "index.js",
6
6
  "repository": "ssh://git@github.com:reykjavikcity/webtools.git",