@simmalugnt-se/payload-consent 0.6.4 → 0.6.6

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
@@ -1,5 +1,24 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.6.6
4
+
5
+ - A scan without a Site address scans `scanner.callbackBaseUrl`, then `serverURL`. Before, it took
6
+ `serverURL` only, so on a site without one (the boilerplate leaves it unset, so Admin calls its
7
+ own origin from every deployment) Scan now asked for an address and the monthly scan failed,
8
+ although the field's help promised the site's own address. The field now shows the address it
9
+ falls back to, in its placeholder and help.
10
+ - The result email's link to Admin works without `serverURL`: it is on the scanned site's origin.
11
+ Before, it began with `undefined`.
12
+ - The callback address is trimmed like the scanned one, so a `callbackBaseUrl` with spaces around
13
+ it no longer breaks the result's way back.
14
+ - The README shows `consent-scanner`'s `config/sites` (at `fa39703` or later) for binding a token
15
+ to its hosts.
16
+
17
+ ## 0.6.5
18
+
19
+ - The Scan tab shows the card's shape with Payload's shimmer while the latest scan loads. Before,
20
+ it said "The site has not been scanned yet" for a moment on every load of a scanned site.
21
+
3
22
  ## 0.6.4
4
23
 
5
24
  - The Scan tab's time left counts down. It grew while a page took its time, since it was worked
package/README.md CHANGED
@@ -122,20 +122,22 @@ consentPlugin({
122
122
  scanner: {
123
123
  url: process.env.CONSENT_SCANNER_URL,
124
124
  token: process.env.CONSENT_SCANNER_TOKEN,
125
- // Only when the scanner cannot reach serverURL, e.g. Docker beside a dev server:
126
- // callbackBaseUrl: 'http://host.docker.internal:3000',
125
+ // The site's address as the scanner reaches it, when Payload has no serverURL or the
126
+ // scanner cannot reach it (Docker beside a dev server: 'http://host.docker.internal:3000'):
127
+ // callbackBaseUrl: process.env.NEXT_PUBLIC_SITE_URL,
127
128
  // Optional: the pages to scan, in place of the site's sitemaps.
128
129
  pages: [{ collection: 'pages', url: (doc) => (doc.slug === 'home' ? '/' : `/${doc.slug}`) }],
129
130
  },
130
131
  })
131
132
  ```
132
133
 
133
- The scanner binds each token to its site's hosts (`token@example.se|example.vercel.app` in its
134
- `SCANNER_TOKENS`), and refuses a scan whose address or callback is on another host: give it the
134
+ The scanner binds each token to its site's hosts (`example 3f9c… example.se example.vercel.app` in
135
+ its `config/sites`), and refuses a scan whose address or callback is on another host: give it the
135
136
  host of the scanned address and, when results come back elsewhere (`serverURL` or
136
137
  `callbackBaseUrl`), that host too. A host covers itself with and without `www.`, and the scanner
137
138
  treats a site with and without `www.` as one, so a Site address of `https://example.se` still finds
138
- the pages on `https://www.example.se`. Both need `consent-scanner` at `0b7aba1` or later.
139
+ the pages on `https://www.example.se`. Both need `consent-scanner` at `0b7aba1` or later;
140
+ `config/sites` needs `fa39703` or later (before it, `SCANNER_TOKENS` in its `.env`).
139
141
 
140
142
  The scanner picks the pages to visit from the site's sitemaps. A site with search indexing off
141
143
  often serves empty ones, and then only the start page is scanned. With `scanner.pages`, a scan
@@ -146,10 +148,11 @@ Each collection goes as its own list, and the scanner picks from each as from on
146
148
  thousand news articles do not crowd out the pages; the lists share at most 500 addresses, each
147
149
  thinned evenly. Without the option, or when no page is left, the scanner reads the sitemaps.
148
150
 
149
- Scan now scans the site's address (the Site address field, or `serverURL`) as a new visitor,
150
- after rejecting, after accepting and, with several optional categories, with each one alone. The
151
- plugin presets c15t's cookie for each state, so no click is needed. While it runs, the card at the
152
- top of the Scan tab follows it page by page, with the time it has taken and a rough time left.
151
+ Scan now scans the site's address (the Site address field; with it empty, `callbackBaseUrl`, then
152
+ `serverURL`, as the field's placeholder and help say) as a new visitor, after rejecting, after
153
+ accepting and, with several optional categories, with each one alone. The plugin presets c15t's
154
+ cookie for each state, so no click is needed. While it runs, the card at the top of the Scan tab
155
+ follows it page by page, with the time it has taken and a rough time left.
153
156
 
154
157
  The scanner posts the result back to `/api/consent-scans/result`. The card then says what it
155
158
  means: nothing to fix, how many need action or are not explained, or that the scan could not see
@@ -1,6 +1,6 @@
1
1
  "use client";
2
2
  import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
3
- import { Button, Collapsible, Pill, SelectInput, TextInput, useConfig, useFormFields, useLocale, useTranslation, } from "@payloadcms/ui";
3
+ import { Button, Collapsible, Pill, SelectInput, ShimmerEffect, TextInput, useConfig, useFormFields, useLocale, useTranslation, } from "@payloadcms/ui";
4
4
  import { useCallback, useEffect, useRef, useState, } from "react";
5
5
  import { CATEGORIES } from "../defaults.js";
6
6
  import { CONSENT_SCANS } from "../types.js";
@@ -11,6 +11,7 @@ const COPY = {
11
11
  noScanner: "No scanner is set up. Give the plugin `scanner: { url, token }` (see the README), then scan from here.",
12
12
  scan: "Scan now",
13
13
  starting: "Starting…",
14
+ loading: "Loading the latest scan",
14
15
  card: {
15
16
  pills: {
16
17
  none: "Not scanned",
@@ -129,6 +130,7 @@ const COPY = {
129
130
  noScanner: "Ingen skanner är inställd. Ge pluginet `scanner: { url, token }` (se README), och skanna sedan härifrån.",
130
131
  scan: "Skanna nu",
131
132
  starting: "Startar…",
133
+ loading: "Hämtar den senaste skanningen",
132
134
  card: {
133
135
  pills: {
134
136
  none: "Inte skannad",
@@ -361,6 +363,13 @@ function ScanCard({ active, busy, copy, current, error, finishingSince, fresh, l
361
363
  ? ` · ${card.states(stateIds.length, statesLabel(copy, stateIds, language))}`
362
364
  : "", " · ", _jsx("a", { href: scansUrl, children: card.all })] }), pages.length ? (_jsx(Collapsible, { className: "consent-scan__pages", header: card.pages(pages.length), initCollapsed: true, children: _jsx("ul", { children: pages.map((page) => (_jsx("li", { children: _jsx("a", { href: page, rel: "noopener noreferrer", target: "_blank", title: page, children: pagePath(page) }) }, page))) }) })) : null] })) : scan ? (_jsx("p", { className: "consent-scan__meta", children: _jsx("a", { href: scansUrl, children: card.all }) })) : null] }));
363
365
  }
366
+ /**
367
+ * The card's shape while the latest scan loads, so a scanned site never shows "not scanned yet"
368
+ * first; Payload's shimmer, as Admin shows while it loads.
369
+ */
370
+ function LoadingCard({ label }) {
371
+ return (_jsxs("section", { "aria-busy": "true", "aria-label": label, className: "consent-scan__card", children: [_jsx(ShimmerEffect, { height: 20, width: 80 }), _jsx(ShimmerEffect, { height: 24, width: "40%" }), _jsx(ShimmerEffect, { height: 16, width: "70%" }), _jsx(ShimmerEffect, { height: 44 }), _jsx(ShimmerEffect, { height: 16, width: "55%" })] }));
372
+ }
364
373
  function capitalize(value) {
365
374
  return value ? `${value[0]?.toUpperCase()}${value.slice(1)}` : value;
366
375
  }
@@ -467,7 +476,7 @@ export function ScanPanel() {
467
476
  if (!hasScanner)
468
477
  return _jsx("p", { className: "consent-scan__note", children: copy.noScanner });
469
478
  const findings = scan?.findings ?? [];
470
- return (_jsxs("div", { className: "consent-scan", children: [_jsx(ScanCard, { active: active, busy: busy, copy: copy, current: current, error: error, finishingSince: finishingSince, fresh: Boolean(scan) && watched.current === scan?.id, language: language, live: live, now: now, onStart: start, pace: pace.id === scan?.id ? pace.marks : [], scan: scan ?? null, scansUrl: `${config.routes.admin}/collections/${CONSENT_SCANS}` }), scan?.status === "done"
479
+ return (_jsxs("div", { className: "consent-scan", children: [scan === undefined ? (_jsx(LoadingCard, { label: copy.loading })) : (_jsx(ScanCard, { active: active, busy: busy, copy: copy, current: current, error: error, finishingSince: finishingSince, fresh: Boolean(scan) && watched.current === scan?.id, language: language, live: live, now: now, onStart: start, pace: pace.id === scan?.id ? pace.marks : [], scan: scan, scansUrl: `${config.routes.admin}/collections/${CONSENT_SCANS}` })), scan?.status === "done"
471
480
  ? ["action", "unknown", "ok"].map((status) => {
472
481
  const rows = findings.filter((finding) => finding.status === status);
473
482
  if (!rows.length)
package/dist/global.js CHANGED
@@ -51,6 +51,8 @@ const necessaryOnly = {
51
51
  export function consentGlobal(options, { usersSlug, schedule }) {
52
52
  const time = `${String(schedule.hour).padStart(2, "0")}:${String(schedule.minute).padStart(2, "0")}`;
53
53
  const legal = options.legalPages;
54
+ // What a scan scans without an address here (`siteAddress`), when known before Payload starts.
55
+ const fallbackAddress = options.scanner?.callbackBaseUrl?.trim() || undefined;
54
56
  const global = {
55
57
  slug: CONSENT_GLOBAL,
56
58
  label: { en: "Cookies", sv: "Cookies" },
@@ -199,11 +201,16 @@ export function consentGlobal(options, { usersSlug, schedule }) {
199
201
  type: "text",
200
202
  label: { en: "Site address", sv: "Webbplatsens adress" },
201
203
  admin: {
202
- placeholder: "https://",
203
- description: {
204
- en: "The published site's start page. Empty: the site's own address (serverURL).",
205
- sv: "Den publicerade webbplatsens startsida. Tom: webbplatsens egen adress (serverURL).",
206
- },
204
+ placeholder: fallbackAddress ?? "https://",
205
+ description: fallbackAddress
206
+ ? {
207
+ en: `The published site's start page. Empty: ${fallbackAddress}, where the scanner sends its results.`,
208
+ sv: `Den publicerade webbplatsens startsida. Tom: ${fallbackAddress}, dit scannern skickar resultaten.`,
209
+ }
210
+ : {
211
+ en: "The published site's start page. Empty: Payload's serverURL.",
212
+ sv: "Den publicerade webbplatsens startsida. Tom: Payloads serverURL.",
213
+ },
207
214
  },
208
215
  },
209
216
  {
package/dist/notify.js CHANGED
@@ -51,6 +51,14 @@ function count(summary, language) {
51
51
  (language === "sv" ? "inget att åtgärda" : "nothing to handle"));
52
52
  }
53
53
  const escape = (value) => value.replace(/[&<>"]/g, (c) => ({ "&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;" })[c] ?? c);
54
+ const origin = (url) => {
55
+ try {
56
+ return new URL(url).origin;
57
+ }
58
+ catch {
59
+ return "";
60
+ }
61
+ };
54
62
  /**
55
63
  * Emails the users picked under Cookies → Scan about a finished scan, through the host's
56
64
  * email adapter, when it needs a look. Admin's default language picks the text.
@@ -79,7 +87,9 @@ export async function notifyScan(payload, outcome) {
79
87
  }
80
88
  })();
81
89
  const text = outcome.status === "failed" ? copy.failed(outcome) : copy.done(outcome);
82
- const link = `${payload.config.serverURL}${payload.config.routes.admin}/globals/${CONSENT_GLOBAL}`;
90
+ // Admin is on the scanned site's origin when Payload has no serverURL.
91
+ const base = payload.config.serverURL || origin(outcome.url);
92
+ const link = `${base}${payload.config.routes.admin}/globals/${CONSENT_GLOBAL}`;
83
93
  try {
84
94
  await payload.sendEmail({
85
95
  to,
package/dist/scans.d.ts CHANGED
@@ -38,6 +38,12 @@ export type ScanStage = {
38
38
  visited: number;
39
39
  total: number;
40
40
  };
41
+ /**
42
+ * The site's own address, scanned when the Scan tab has none: where the scanner reaches the site
43
+ * (`scanner.callbackBaseUrl`), else Payload's `serverURL`. Many sites leave `serverURL` unset, so
44
+ * Admin calls its own origin from every deployment.
45
+ */
46
+ export declare function siteAddress(payload: Payload, options: ConsentPluginOptions): string | undefined;
41
47
  /**
42
48
  * Asks the scanner to scan the site in every consent state and records the scan as queued. The
43
49
  * address is the one given (the Scan tab's field, saved or not), then the saved one, then the
package/dist/scans.js CHANGED
@@ -121,6 +121,14 @@ async function allServices(req) {
121
121
  });
122
122
  return docs;
123
123
  }
124
+ /**
125
+ * The site's own address, scanned when the Scan tab has none: where the scanner reaches the site
126
+ * (`scanner.callbackBaseUrl`), else Payload's `serverURL`. Many sites leave `serverURL` unset, so
127
+ * Admin calls its own origin from every deployment.
128
+ */
129
+ export function siteAddress(payload, options) {
130
+ return options.scanner?.callbackBaseUrl?.trim() || payload.config.serverURL || undefined;
131
+ }
124
132
  /**
125
133
  * Asks the scanner to scan the site in every consent state and records the scan as queued. The
126
134
  * address is the one given (the Scan tab's field, saved or not), then the saved one, then the
@@ -135,7 +143,7 @@ export async function startScan({ payload, options, url: given, trigger = "manua
135
143
  depth: 0,
136
144
  overrideAccess: true,
137
145
  }));
138
- const url = given?.trim() || global.scanUrl?.trim() || payload.config.serverURL;
146
+ const url = given?.trim() || global.scanUrl?.trim() || siteAddress(payload, options);
139
147
  if (!url || !/^https?:\/\//.test(url))
140
148
  return { error: "Enter the site's address, starting with https://", status: 400 };
141
149
  const { doc, services } = await readConsent({ payload });
@@ -145,7 +153,7 @@ export async function startScan({ payload, options, url: given, trigger = "manua
145
153
  payload.logger.error({ err: error, msg: "payload-consent: could not list the pages to scan" });
146
154
  return undefined;
147
155
  });
148
- const base = `${scanner.callbackBaseUrl ?? payload.config.serverURL}${payload.config.routes.api}/${CONSENT_SCANS}`;
156
+ const base = `${siteAddress(payload, options) ?? ""}${payload.config.routes.api}/${CONSENT_SCANS}`;
149
157
  const response = await fetch(new URL("/scans", scanner.url), {
150
158
  method: "POST",
151
159
  headers: { "content-type": "application/json", authorization: `Bearer ${scanner.token}` },
package/dist/types.d.ts CHANGED
@@ -45,8 +45,10 @@ export type ConsentPluginOptions = {
45
45
  url: string;
46
46
  token: string;
47
47
  /**
48
- * Where the scanner posts results, when it cannot reach the site at `serverURL`; for example
49
- * `http://host.docker.internal:3000` when it runs in Docker beside a local dev server.
48
+ * The site's address as the scanner reaches it: where it posts results, and what Scan now and
49
+ * the monthly scan scan when the Scan tab has no address. Defaults to `serverURL`. Set it when
50
+ * Payload has no `serverURL` (the site's public address), or when the scanner cannot reach
51
+ * `serverURL`, such as `http://host.docker.internal:3000` beside a local dev server.
50
52
  */
51
53
  callbackBaseUrl?: string;
52
54
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@simmalugnt-se/payload-consent",
3
- "version": "0.6.4",
3
+ "version": "0.6.6",
4
4
  "description": "Cookie consent for Payload sites: banner texts, services and scans in Admin",
5
5
  "keywords": [
6
6
  "payload",