brease-client 0.6.0 → 0.7.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
@@ -107,6 +107,54 @@ export default {
107
107
  }
108
108
  ```
109
109
 
110
+ ### Structured data (JSON-LD)
111
+
112
+ `toMetadata` can't output scripts, so render the page's structured data with `jsonLd` (framework-free, from the
113
+ root export). Each item's `json` is escaped for use inside a script tag.
114
+
115
+ ```tsx
116
+ import { jsonLd } from 'brease-client'
117
+
118
+ {jsonLd(page.resolvedSeo).map((item, i) => (
119
+ <script key={i} type="application/ld+json" dangerouslySetInnerHTML={{ __html: item.json }} />
120
+ ))}
121
+ ```
122
+
123
+ `headTags` (HTML) and `breaseHead` (Astro) already include it; `jsonLdHtml(seo)` gives only the scripts.
124
+
125
+ ### Google Analytics (GA4)
126
+
127
+ When GA4 is linked in Brease (Connections → Google) with "Add the tag to the site" on, the site response carries
128
+ its measurement id. Render it once in the root layout; turning it off in Brease removes it without a deploy.
129
+
130
+ ```tsx
131
+ // app/layout.tsx
132
+ import Script from 'next/script'
133
+ import { ga4Tag } from 'brease-client'
134
+ import { brease } from '@/lib/brease'
135
+
136
+ export default async function RootLayout({ children }: { children: React.ReactNode }) {
137
+ const ga = ga4Tag(await brease.getSite())
138
+ return (
139
+ <html>
140
+ <body>
141
+ {children}
142
+ {ga && (
143
+ <>
144
+ <Script src={ga.src} strategy="afterInteractive" />
145
+ <Script id="brease-ga4" strategy="afterInteractive">{ga.inline}</Script>
146
+ </>
147
+ )}
148
+ </body>
149
+ </html>
150
+ )
151
+ }
152
+ ```
153
+
154
+ Consent mode starts denied: nothing is stored until your cookie banner calls `grantAnalytics()` (and
155
+ `grantAnalytics(false)` when consent is withdrawn). Without a banner, GA4 only receives cookieless pings. For
156
+ Astro or plain HTML, put `ga4Html(site)` in the `<head>`.
157
+
110
158
  ### Recommended structure (optional)
111
159
 
112
160
  Brease works with any routing: `getPage()` and friends don't care how a site is built. This structure is what Brease recommends, and what its editor moves a site towards when the site follows Brease conventions. It makes locales a setting: adding one, or changing how URLs are prefixed, needs no code change.
package/dist/astro.cjs CHANGED
@@ -1,5 +1,11 @@
1
1
  'use strict';
2
2
 
3
+ // src/structured-data.ts
4
+ var jsonLdText = (data) => JSON.stringify(data).replace(/</g, "\\u003c");
5
+ function jsonLd(seo) {
6
+ return (seo.structuredData ?? []).map((data) => ({ data, json: jsonLdText(data) }));
7
+ }
8
+
3
9
  // src/html.ts
4
10
  function headTagList(seo, opts = {}) {
5
11
  const tags = [{ tag: "title", attrs: {}, content: seo.title }];
@@ -34,12 +40,8 @@ function headTagList(seo, opts = {}) {
34
40
  meta("name", "twitter:description", tw.description);
35
41
  meta("name", "twitter:image", tw.image);
36
42
  if (seo.favicon) tags.push({ tag: "link", attrs: { rel: "icon", href: seo.favicon } });
37
- for (const data of seo.structuredData) {
38
- tags.push({
39
- tag: "script",
40
- attrs: { type: "application/ld+json" },
41
- content: JSON.stringify(data).replace(/</g, "\\u003c")
42
- });
43
+ for (const item of jsonLd(seo)) {
44
+ tags.push({ tag: "script", attrs: { type: "application/ld+json" }, content: item.json });
43
45
  }
44
46
  return tags;
45
47
  }
package/dist/astro.d.cts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { HeadOptions } from './html.cjs';
2
2
  export { HeadTag, headTagList, headTags } from './html.cjs';
3
- import { R as ResolvedSeo } from './types-V0ELFvwy.cjs';
3
+ import { R as ResolvedSeo } from './types-ecfDXrLa.cjs';
4
4
 
5
5
  declare function breaseHead(seo: ResolvedSeo, opts?: HeadOptions): string;
6
6
 
package/dist/astro.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { HeadOptions } from './html.js';
2
2
  export { HeadTag, headTagList, headTags } from './html.js';
3
- import { R as ResolvedSeo } from './types-V0ELFvwy.js';
3
+ import { R as ResolvedSeo } from './types-ecfDXrLa.js';
4
4
 
5
5
  declare function breaseHead(seo: ResolvedSeo, opts?: HeadOptions): string;
6
6
 
package/dist/astro.js CHANGED
@@ -1,5 +1,6 @@
1
- import { headTags } from './chunk-SSDAREDA.js';
2
- export { headTagList, headTags } from './chunk-SSDAREDA.js';
1
+ import { headTags } from './chunk-QZXUMDB7.js';
2
+ export { headTagList, headTags } from './chunk-QZXUMDB7.js';
3
+ import './chunk-N3QOL5EU.js';
3
4
  import './chunk-PKBMQBKP.js';
4
5
 
5
6
  // src/astro.ts
@@ -73,6 +73,29 @@ function resolvePath(settings, pathname) {
73
73
  return { slug: clean, locale: settings.defaultLocale };
74
74
  }
75
75
 
76
+ // src/analytics.ts
77
+ var MEASUREMENT_ID = /^G-[A-Z0-9]+$/;
78
+ function ga4Tag(site) {
79
+ const id = site.analytics?.ga4;
80
+ if (!id || !MEASUREMENT_ID.test(id)) return null;
81
+ const inline = [
82
+ "window.dataLayer=window.dataLayer||[];function gtag(){dataLayer.push(arguments);}",
83
+ "gtag('consent','default',{ad_storage:'denied',ad_user_data:'denied',ad_personalization:'denied',analytics_storage:'denied',wait_for_update:500});",
84
+ "gtag('js',new Date());",
85
+ `gtag('config','${id}');`
86
+ ].join("");
87
+ return { id, src: `https://www.googletagmanager.com/gtag/js?id=${id}`, inline };
88
+ }
89
+ function ga4Html(site) {
90
+ const tag = ga4Tag(site);
91
+ return tag ? `<script async src="${tag.src}"></script>
92
+ <script>${tag.inline}</script>` : "";
93
+ }
94
+ function grantAnalytics(granted = true) {
95
+ const gtag = globalThis.gtag;
96
+ gtag?.("consent", "update", { analytics_storage: granted ? "granted" : "denied" });
97
+ }
98
+
76
99
  // src/locales.ts
77
100
  function localeLinks(context) {
78
101
  const { settings, alternates, homes, locale: current } = context;
@@ -219,4 +242,4 @@ function pathOf(url) {
219
242
  }
220
243
  }
221
244
 
222
- export { BreaseError, buildPath, createBrease, env, localeHref, localeLinks, nativeName, normalizeSlug, resolvePath };
245
+ export { BreaseError, buildPath, createBrease, env, ga4Html, ga4Tag, grantAnalytics, localeHref, localeLinks, nativeName, normalizeSlug, resolvePath };
@@ -0,0 +1,10 @@
1
+ // src/structured-data.ts
2
+ var jsonLdText = (data) => JSON.stringify(data).replace(/</g, "\\u003c");
3
+ function jsonLd(seo) {
4
+ return (seo.structuredData ?? []).map((data) => ({ data, json: jsonLdText(data) }));
5
+ }
6
+ function jsonLdHtml(seo) {
7
+ return jsonLd(seo).map((d) => `<script type="application/ld+json">${d.json}</script>`).join("\n");
8
+ }
9
+
10
+ export { jsonLd, jsonLdHtml };
@@ -1,3 +1,5 @@
1
+ import { jsonLd } from './chunk-N3QOL5EU.js';
2
+
1
3
  // src/html.ts
2
4
  function headTagList(seo, opts = {}) {
3
5
  const tags = [{ tag: "title", attrs: {}, content: seo.title }];
@@ -32,12 +34,8 @@ function headTagList(seo, opts = {}) {
32
34
  meta("name", "twitter:description", tw.description);
33
35
  meta("name", "twitter:image", tw.image);
34
36
  if (seo.favicon) tags.push({ tag: "link", attrs: { rel: "icon", href: seo.favicon } });
35
- for (const data of seo.structuredData) {
36
- tags.push({
37
- tag: "script",
38
- attrs: { type: "application/ld+json" },
39
- content: JSON.stringify(data).replace(/</g, "\\u003c")
40
- });
37
+ for (const item of jsonLd(seo)) {
38
+ tags.push({ tag: "script", attrs: { type: "application/ld+json" }, content: item.json });
41
39
  }
42
40
  return tags;
43
41
  }
package/dist/html.cjs CHANGED
@@ -1,5 +1,11 @@
1
1
  'use strict';
2
2
 
3
+ // src/structured-data.ts
4
+ var jsonLdText = (data) => JSON.stringify(data).replace(/</g, "\\u003c");
5
+ function jsonLd(seo) {
6
+ return (seo.structuredData ?? []).map((data) => ({ data, json: jsonLdText(data) }));
7
+ }
8
+
3
9
  // src/html.ts
4
10
  function headTagList(seo, opts = {}) {
5
11
  const tags = [{ tag: "title", attrs: {}, content: seo.title }];
@@ -34,12 +40,8 @@ function headTagList(seo, opts = {}) {
34
40
  meta("name", "twitter:description", tw.description);
35
41
  meta("name", "twitter:image", tw.image);
36
42
  if (seo.favicon) tags.push({ tag: "link", attrs: { rel: "icon", href: seo.favicon } });
37
- for (const data of seo.structuredData) {
38
- tags.push({
39
- tag: "script",
40
- attrs: { type: "application/ld+json" },
41
- content: JSON.stringify(data).replace(/</g, "\\u003c")
42
- });
43
+ for (const item of jsonLd(seo)) {
44
+ tags.push({ tag: "script", attrs: { type: "application/ld+json" }, content: item.json });
43
45
  }
44
46
  return tags;
45
47
  }
package/dist/html.d.cts CHANGED
@@ -1,4 +1,4 @@
1
- import { R as ResolvedSeo } from './types-V0ELFvwy.cjs';
1
+ import { R as ResolvedSeo } from './types-ecfDXrLa.cjs';
2
2
 
3
3
  type HeadOptions = {
4
4
  xDefault?: string;
package/dist/html.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { R as ResolvedSeo } from './types-V0ELFvwy.js';
1
+ import { R as ResolvedSeo } from './types-ecfDXrLa.js';
2
2
 
3
3
  type HeadOptions = {
4
4
  xDefault?: string;
package/dist/html.js CHANGED
@@ -1,2 +1,3 @@
1
- export { headTagList, headTags, renderTag } from './chunk-SSDAREDA.js';
1
+ export { headTagList, headTags, renderTag } from './chunk-QZXUMDB7.js';
2
+ import './chunk-N3QOL5EU.js';
2
3
  import './chunk-PKBMQBKP.js';
package/dist/index.cjs CHANGED
@@ -77,6 +77,29 @@ function resolvePath(settings, pathname) {
77
77
  return { slug: clean, locale: settings.defaultLocale };
78
78
  }
79
79
 
80
+ // src/analytics.ts
81
+ var MEASUREMENT_ID = /^G-[A-Z0-9]+$/;
82
+ function ga4Tag(site) {
83
+ const id = site.analytics?.ga4;
84
+ if (!id || !MEASUREMENT_ID.test(id)) return null;
85
+ const inline = [
86
+ "window.dataLayer=window.dataLayer||[];function gtag(){dataLayer.push(arguments);}",
87
+ "gtag('consent','default',{ad_storage:'denied',ad_user_data:'denied',ad_personalization:'denied',analytics_storage:'denied',wait_for_update:500});",
88
+ "gtag('js',new Date());",
89
+ `gtag('config','${id}');`
90
+ ].join("");
91
+ return { id, src: `https://www.googletagmanager.com/gtag/js?id=${id}`, inline };
92
+ }
93
+ function ga4Html(site) {
94
+ const tag = ga4Tag(site);
95
+ return tag ? `<script async src="${tag.src}"></script>
96
+ <script>${tag.inline}</script>` : "";
97
+ }
98
+ function grantAnalytics(granted = true) {
99
+ const gtag = globalThis.gtag;
100
+ gtag?.("consent", "update", { analytics_storage: granted ? "granted" : "denied" });
101
+ }
102
+
80
103
  // src/locales.ts
81
104
  function localeLinks(context) {
82
105
  const { settings, alternates, homes, locale: current } = context;
@@ -173,6 +196,15 @@ function checkSections(files, pages = {}) {
173
196
  return warnings;
174
197
  }
175
198
 
199
+ // src/structured-data.ts
200
+ var jsonLdText = (data) => JSON.stringify(data).replace(/</g, "\\u003c");
201
+ function jsonLd(seo) {
202
+ return (seo.structuredData ?? []).map((data) => ({ data, json: jsonLdText(data) }));
203
+ }
204
+ function jsonLdHtml(seo) {
205
+ return jsonLd(seo).map((d) => `<script type="application/ld+json">${d.json}</script>`).join("\n");
206
+ }
207
+
176
208
  // src/index.ts
177
209
  var RETRY_AFTER_CAP_MS = 1e4;
178
210
  var SITE_TTL_MS = 6e4;
@@ -305,6 +337,11 @@ exports.BreaseError = BreaseError;
305
337
  exports.buildPath = buildPath;
306
338
  exports.checkSections = checkSections;
307
339
  exports.createBrease = createBrease;
340
+ exports.ga4Html = ga4Html;
341
+ exports.ga4Tag = ga4Tag;
342
+ exports.grantAnalytics = grantAnalytics;
343
+ exports.jsonLd = jsonLd;
344
+ exports.jsonLdHtml = jsonLdHtml;
308
345
  exports.localeHref = localeHref;
309
346
  exports.localeLinks = localeLinks;
310
347
  exports.nativeName = nativeName;
package/dist/index.d.cts CHANGED
@@ -1,5 +1,14 @@
1
- import { U as UrlSettings, S as Site, P as Page, a as PageRef, N as Navigation, b as Redirect, c as Release, d as SitemapEntry } from './types-V0ELFvwy.cjs';
2
- export { L as LocaleStrategy, e as NavigationItem, O as OpenGraph, f as PageSeo, R as ResolvedSeo, g as Robots, h as SiteSeo, T as Twitter } from './types-V0ELFvwy.cjs';
1
+ import { S as Site, U as UrlSettings, R as ResolvedSeo, P as Page, a as PageRef, N as Navigation, b as Redirect, c as Release, d as SitemapEntry } from './types-ecfDXrLa.cjs';
2
+ export { L as LocaleStrategy, e as NavigationItem, O as OpenGraph, f as PageSeo, g as Robots, h as SiteSeo, T as Twitter } from './types-ecfDXrLa.cjs';
3
+
4
+ type Ga4Tag = {
5
+ id: string;
6
+ src: string;
7
+ inline: string;
8
+ };
9
+ declare function ga4Tag(site: Pick<Site, 'analytics'>): Ga4Tag | null;
10
+ declare function ga4Html(site: Pick<Site, 'analytics'>): string;
11
+ declare function grantAnalytics(granted?: boolean): void;
3
12
 
4
13
  declare class BreaseError extends Error {
5
14
  code: string;
@@ -40,6 +49,13 @@ type PageLocales = Record<string, Record<string, {
40
49
  }>>;
41
50
  declare function checkSections(files: Record<string, string>, pages?: PageLocales): string[];
42
51
 
52
+ type JsonLd = {
53
+ data: Record<string, unknown>;
54
+ json: string;
55
+ };
56
+ declare function jsonLd(seo: Pick<ResolvedSeo, 'structuredData'>): JsonLd[];
57
+ declare function jsonLdHtml(seo: Pick<ResolvedSeo, 'structuredData'>): string;
58
+
43
59
  declare function normalizeSlug(input: string): string;
44
60
  declare function buildPath(settings: UrlSettings, slug: string, locale: string): string;
45
61
  declare function resolvePath(settings: UrlSettings, pathname: string): {
@@ -90,4 +106,4 @@ interface Brease {
90
106
  }
91
107
  declare function createBrease(opts?: BreaseOptions): Brease;
92
108
 
93
- export { type Brease, BreaseError, type BreaseOptions, type BreaseSection, type LocaleContext, type LocaleLink, Navigation, Page, PageRef, Redirect, Release, Site, SitemapEntry, UrlSettings, buildPath, checkSections, createBrease, localeHref, localeLinks, nativeName, normalizeSlug, resolvePath, sectionAttrs, sectionsAttrs };
109
+ export { type Brease, BreaseError, type BreaseOptions, type BreaseSection, type Ga4Tag, type JsonLd, type LocaleContext, type LocaleLink, Navigation, Page, PageRef, Redirect, Release, ResolvedSeo, Site, SitemapEntry, UrlSettings, buildPath, checkSections, createBrease, ga4Html, ga4Tag, grantAnalytics, jsonLd, jsonLdHtml, localeHref, localeLinks, nativeName, normalizeSlug, resolvePath, sectionAttrs, sectionsAttrs };
package/dist/index.d.ts CHANGED
@@ -1,5 +1,14 @@
1
- import { U as UrlSettings, S as Site, P as Page, a as PageRef, N as Navigation, b as Redirect, c as Release, d as SitemapEntry } from './types-V0ELFvwy.js';
2
- export { L as LocaleStrategy, e as NavigationItem, O as OpenGraph, f as PageSeo, R as ResolvedSeo, g as Robots, h as SiteSeo, T as Twitter } from './types-V0ELFvwy.js';
1
+ import { S as Site, U as UrlSettings, R as ResolvedSeo, P as Page, a as PageRef, N as Navigation, b as Redirect, c as Release, d as SitemapEntry } from './types-ecfDXrLa.js';
2
+ export { L as LocaleStrategy, e as NavigationItem, O as OpenGraph, f as PageSeo, g as Robots, h as SiteSeo, T as Twitter } from './types-ecfDXrLa.js';
3
+
4
+ type Ga4Tag = {
5
+ id: string;
6
+ src: string;
7
+ inline: string;
8
+ };
9
+ declare function ga4Tag(site: Pick<Site, 'analytics'>): Ga4Tag | null;
10
+ declare function ga4Html(site: Pick<Site, 'analytics'>): string;
11
+ declare function grantAnalytics(granted?: boolean): void;
3
12
 
4
13
  declare class BreaseError extends Error {
5
14
  code: string;
@@ -40,6 +49,13 @@ type PageLocales = Record<string, Record<string, {
40
49
  }>>;
41
50
  declare function checkSections(files: Record<string, string>, pages?: PageLocales): string[];
42
51
 
52
+ type JsonLd = {
53
+ data: Record<string, unknown>;
54
+ json: string;
55
+ };
56
+ declare function jsonLd(seo: Pick<ResolvedSeo, 'structuredData'>): JsonLd[];
57
+ declare function jsonLdHtml(seo: Pick<ResolvedSeo, 'structuredData'>): string;
58
+
43
59
  declare function normalizeSlug(input: string): string;
44
60
  declare function buildPath(settings: UrlSettings, slug: string, locale: string): string;
45
61
  declare function resolvePath(settings: UrlSettings, pathname: string): {
@@ -90,4 +106,4 @@ interface Brease {
90
106
  }
91
107
  declare function createBrease(opts?: BreaseOptions): Brease;
92
108
 
93
- export { type Brease, BreaseError, type BreaseOptions, type BreaseSection, type LocaleContext, type LocaleLink, Navigation, Page, PageRef, Redirect, Release, Site, SitemapEntry, UrlSettings, buildPath, checkSections, createBrease, localeHref, localeLinks, nativeName, normalizeSlug, resolvePath, sectionAttrs, sectionsAttrs };
109
+ export { type Brease, BreaseError, type BreaseOptions, type BreaseSection, type Ga4Tag, type JsonLd, type LocaleContext, type LocaleLink, Navigation, Page, PageRef, Redirect, Release, ResolvedSeo, Site, SitemapEntry, UrlSettings, buildPath, checkSections, createBrease, ga4Html, ga4Tag, grantAnalytics, jsonLd, jsonLdHtml, localeHref, localeLinks, nativeName, normalizeSlug, resolvePath, sectionAttrs, sectionsAttrs };
package/dist/index.js CHANGED
@@ -1,3 +1,4 @@
1
- export { BreaseError, buildPath, createBrease, localeHref, localeLinks, nativeName, normalizeSlug, resolvePath } from './chunk-OA7GXHT3.js';
1
+ export { BreaseError, buildPath, createBrease, ga4Html, ga4Tag, grantAnalytics, localeHref, localeLinks, nativeName, normalizeSlug, resolvePath } from './chunk-GJ3SOWDB.js';
2
2
  export { checkSections, sectionAttrs, sectionsAttrs } from './chunk-CQZLTZGN.js';
3
+ export { jsonLd, jsonLdHtml } from './chunk-N3QOL5EU.js';
3
4
  import './chunk-PKBMQBKP.js';
package/dist/next.d.cts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { Brease } from './index.cjs';
2
- import { R as ResolvedSeo } from './types-V0ELFvwy.cjs';
2
+ import { R as ResolvedSeo } from './types-ecfDXrLa.cjs';
3
3
 
4
4
  type BreaseMetadata = {
5
5
  title: {
package/dist/next.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { Brease } from './index.js';
2
- import { R as ResolvedSeo } from './types-V0ELFvwy.js';
2
+ import { R as ResolvedSeo } from './types-ecfDXrLa.js';
3
3
 
4
4
  type BreaseMetadata = {
5
5
  title: {
package/dist/next.js CHANGED
@@ -1,5 +1,6 @@
1
- import { env, createBrease } from './chunk-OA7GXHT3.js';
1
+ import { env, createBrease } from './chunk-GJ3SOWDB.js';
2
2
  import './chunk-CQZLTZGN.js';
3
+ import './chunk-N3QOL5EU.js';
3
4
  import './chunk-PKBMQBKP.js';
4
5
 
5
6
  // src/next.ts
@@ -66,6 +66,9 @@ type Site = {
66
66
  localeStrategy: LocaleStrategy;
67
67
  urlTemplate: string | null;
68
68
  seo: Record<string, SiteSeo>;
69
+ analytics?: {
70
+ ga4: string;
71
+ } | null;
69
72
  };
70
73
  type Page<T = Record<string, unknown>> = {
71
74
  pageId: string;
@@ -66,6 +66,9 @@ type Site = {
66
66
  localeStrategy: LocaleStrategy;
67
67
  urlTemplate: string | null;
68
68
  seo: Record<string, SiteSeo>;
69
+ analytics?: {
70
+ ga4: string;
71
+ } | null;
69
72
  };
70
73
  type Page<T = Record<string, unknown>> = {
71
74
  pageId: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "brease-client",
3
- "version": "0.6.0",
3
+ "version": "0.7.1",
4
4
  "description": "Brease delivery API client for Next.js, Astro and any fetch-capable runtime",
5
5
  "license": "MIT",
6
6
  "repository": {