brease-client 0.2.0 → 0.3.0

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
@@ -32,6 +32,8 @@ await brease.getPages({ locale: 'en' }) // [{ pageId, locale, slug, url, hidden
32
32
  await brease.getNavigation('main', { locale: 'en' }) // { key, locale, items } | null
33
33
  await brease.getRedirects() // [{ source, destination, status }]
34
34
  await brease.resolve('/en/about') // { slug: 'about', locale: 'en' } | null
35
+ await brease.route(['en', 'about']) // the same from catch-all path segments; null for shared "_" pages
36
+ await brease.routes() // [{ path: ['en', 'about'] }, …]: static params for every published page
35
37
  await brease.sitemap('https://example.com') // [{ url, alternates }]
36
38
  ```
37
39
 
@@ -39,6 +41,7 @@ await brease.sitemap('https://example.com') // [{ url, alternates }]
39
41
  - 429 and 5xx responses (and network errors) are retried twice with backoff; `Retry-After` is honoured.
40
42
  - Commit pinning: the `x-brease-commit` header is sent from `VERCEL_GIT_COMMIT_SHA`, so every deployment reads the content release that was built and tested with its code.
41
43
  - `sitemap()` has no `lastModified`: releases are immutable snapshots and do not track per-page edit times.
44
+ - `getSite()` is cached for a minute, so a locale added in Brease is served without a restart.
42
45
  - `buildPath(settings, slug, locale)` and `resolvePath(settings, pathname)` are exported for routing code that already has the site settings.
43
46
 
44
47
  ## Next.js (App Router)
@@ -103,6 +106,72 @@ export default {
103
106
  }
104
107
  ```
105
108
 
109
+ ### Several locales
110
+
111
+ One optional catch-all route serves every locale and page, whatever the site's URL strategy (`/about`, `/de/uber-uns`, a custom template): Brease decides which locale a path belongs to, so adding a locale later needs no code change. The root layout lives in that route too, so `<html lang>` follows the page.
112
+
113
+ ```
114
+ app/
115
+ [[...path]]/
116
+ layout.tsx ← root layout: <html lang={locale}>
117
+ page.tsx ← every page in every locale
118
+ not-found.tsx
119
+ api/…, robots.ts, sitemap.ts, route handlers ← unchanged; static routes win over the catch-all
120
+ ```
121
+
122
+ ```tsx
123
+ // app/[[...path]]/layout.tsx (replaces app/layout.tsx)
124
+ import { brease } from '@/lib/brease'
125
+
126
+ type Props = { children: React.ReactNode; params: Promise<{ path?: string[] }> }
127
+
128
+ export default async function RootLayout({ children, params }: Props) {
129
+ const route = await brease.route((await params).path)
130
+ const locale = route?.locale ?? (await brease.getSite()).defaultLocale
131
+ return (
132
+ <html lang={locale}>
133
+ <body>{children}</body>
134
+ </html>
135
+ )
136
+ }
137
+ ```
138
+
139
+ ```tsx
140
+ // app/[[...path]]/page.tsx (replaces app/page.tsx and app/[slug]/page.tsx)
141
+ import { notFound } from 'next/navigation'
142
+ import { toMetadata } from 'brease-client/next'
143
+ import { brease } from '@/lib/brease'
144
+
145
+ export const revalidate = 60
146
+
147
+ type Props = { params: Promise<{ path?: string[] }> }
148
+
149
+ async function load(params: Props['params']) {
150
+ const route = await brease.route((await params).path)
151
+ const page = route && (await brease.getPage<Content>(route.slug, { locale: route.locale }))
152
+ return page && route ? { page, locale: route.locale } : null
153
+ }
154
+
155
+ export const generateStaticParams = () => brease.routes()
156
+
157
+ export async function generateMetadata({ params }: Props) {
158
+ const loaded = await load(params)
159
+ return loaded ? toMetadata(loaded.page.resolvedSeo) : {}
160
+ }
161
+
162
+ export default async function Page({ params }: Props) {
163
+ const loaded = await load(params)
164
+ if (!loaded) notFound()
165
+ // Pass `loaded.locale` to everything else that reads Brease: navigations, shared "_" pages, site SEO.
166
+ return <Template page={loaded.page} locale={loaded.locale} />
167
+ }
168
+ ```
169
+
170
+ - Everything that read a fixed locale (`getPage(…, { locale: 'hu' })`, `getNavigation`, `site.seo.hu`) takes the locale from the route instead.
171
+ - Links inside content are already locale URLs; build other internal links with `buildPath(site, slug, locale)`.
172
+ - Per-page `opengraph-image` / `twitter-image` files move into `[[...path]]` too and load the page the same way.
173
+ - Text written into components (button labels, captions) stays as it is; moving it into Brease content is a separate change.
174
+
106
175
  ### Revalidation signature
107
176
 
108
177
  After a content-only release goes live, Brease POSTs JSON `{ urls, tags?, releaseId, siteId }` to the site's revalidate URL with two headers:
@@ -142,6 +211,17 @@ const html = `<head>${headTags(page.resolvedSeo, { xDefault: 'https://example.co
142
211
 
143
212
  `customHead` and `customCode` from the site's SEO settings are inserted as raw HTML (they are authored by the site's team); pass `{ includeCustom: false }` to leave them out.
144
213
 
214
+ ## Editor markers
215
+
216
+ The Brease editor's Edit tool finds content through `data-brease` attributes:
217
+
218
+ - `data-brease="hero.title"` on an element that renders a page content field (a path into `page.content`)
219
+ - `data-brease="nav:footer"` on the element that wraps a navigation menu; clicking it opens that navigation's editor
220
+
221
+ ```tsx
222
+ <nav data-brease="nav:footer">{footer.items.map(/* … */)}</nav>
223
+ ```
224
+
145
225
  ## Images
146
226
 
147
227
  ```ts
@@ -151,7 +231,7 @@ const props = imageProps(page.content.hero.image, { sizes: '(min-width: 768px) 5
151
231
  // <img src={props.src} srcSet={props.srcSet} sizes={props.sizes} />
152
232
  ```
153
233
 
154
- Supabase Storage URLs are rewritten to image transformation URLs; other URLs pass through unchanged (with an empty srcset).
234
+ Brease files are served as-is from an S3 bucket, which doesn't resize images: the helpers return the URL unchanged with an empty `srcset`. For responsive images, let your framework resize them, e.g. `next/image` with the bucket's host in `images.remotePatterns`.
155
235
 
156
236
  ## Without JavaScript
157
237
 
package/dist/image.cjs CHANGED
@@ -1,40 +1,18 @@
1
1
  'use strict';
2
2
 
3
3
  // src/image.ts
4
- var OBJECT_PATH = "/storage/v1/object/public/";
5
- var RENDER_PATH = "/storage/v1/render/image/public/";
6
4
  var DEFAULT_WIDTHS = [320, 640, 960, 1280, 1920];
7
- function isSupabaseImage(src) {
8
- return src.includes(OBJECT_PATH) || src.includes(RENDER_PATH);
5
+ function imageUrl(src, _opts = {}) {
6
+ return src;
9
7
  }
10
- function imageUrl(src, opts = {}) {
11
- if (!isSupabaseImage(src)) return src;
12
- let url;
13
- try {
14
- url = new URL(src.replace(OBJECT_PATH, RENDER_PATH));
15
- } catch {
16
- return src;
17
- }
18
- if (opts.width) url.searchParams.set("width", String(Math.round(opts.width)));
19
- if (opts.height) url.searchParams.set("height", String(Math.round(opts.height)));
20
- if (opts.quality) url.searchParams.set("quality", String(opts.quality));
21
- if (opts.resize) url.searchParams.set("resize", opts.resize);
22
- return url.toString();
23
- }
24
- function srcset(src, opts = {}) {
25
- if (!isSupabaseImage(src)) return "";
26
- return (opts.widths ?? DEFAULT_WIDTHS).map((w) => `${imageUrl(src, { width: w, quality: opts.quality, resize: opts.resize })} ${w}w`).join(", ");
8
+ function srcset(_src, _opts = {}) {
9
+ return "";
27
10
  }
28
11
  function imageProps(src, opts = {}) {
29
- return {
30
- src: imageUrl(src, { width: opts.width, height: opts.height, quality: opts.quality }),
31
- srcSet: srcset(src, { widths: opts.widths, quality: opts.quality }),
32
- sizes: opts.sizes ?? "100vw"
33
- };
12
+ return { src: imageUrl(src), srcSet: srcset(), sizes: opts.sizes ?? "100vw" };
34
13
  }
35
14
 
36
15
  exports.DEFAULT_WIDTHS = DEFAULT_WIDTHS;
37
16
  exports.imageProps = imageProps;
38
17
  exports.imageUrl = imageUrl;
39
- exports.isSupabaseImage = isSupabaseImage;
40
18
  exports.srcset = srcset;
package/dist/image.d.cts CHANGED
@@ -6,9 +6,8 @@ type ImageOptions = {
6
6
  resize?: Resize;
7
7
  };
8
8
  declare const DEFAULT_WIDTHS: number[];
9
- declare function isSupabaseImage(src: string): boolean;
10
- declare function imageUrl(src: string, opts?: ImageOptions): string;
11
- declare function srcset(src: string, opts?: {
9
+ declare function imageUrl(src: string, _opts?: ImageOptions): string;
10
+ declare function srcset(_src: string, _opts?: {
12
11
  widths?: number[];
13
12
  quality?: number;
14
13
  resize?: Resize;
@@ -25,4 +24,4 @@ declare function imageProps(src: string, opts?: {
25
24
  sizes: string;
26
25
  };
27
26
 
28
- export { DEFAULT_WIDTHS, type ImageOptions, type Resize, imageProps, imageUrl, isSupabaseImage, srcset };
27
+ export { DEFAULT_WIDTHS, type ImageOptions, type Resize, imageProps, imageUrl, srcset };
package/dist/image.d.ts CHANGED
@@ -6,9 +6,8 @@ type ImageOptions = {
6
6
  resize?: Resize;
7
7
  };
8
8
  declare const DEFAULT_WIDTHS: number[];
9
- declare function isSupabaseImage(src: string): boolean;
10
- declare function imageUrl(src: string, opts?: ImageOptions): string;
11
- declare function srcset(src: string, opts?: {
9
+ declare function imageUrl(src: string, _opts?: ImageOptions): string;
10
+ declare function srcset(_src: string, _opts?: {
12
11
  widths?: number[];
13
12
  quality?: number;
14
13
  resize?: Resize;
@@ -25,4 +24,4 @@ declare function imageProps(src: string, opts?: {
25
24
  sizes: string;
26
25
  };
27
26
 
28
- export { DEFAULT_WIDTHS, type ImageOptions, type Resize, imageProps, imageUrl, isSupabaseImage, srcset };
27
+ export { DEFAULT_WIDTHS, type ImageOptions, type Resize, imageProps, imageUrl, srcset };
package/dist/image.js CHANGED
@@ -1,36 +1,15 @@
1
1
  import './chunk-PKBMQBKP.js';
2
2
 
3
3
  // src/image.ts
4
- var OBJECT_PATH = "/storage/v1/object/public/";
5
- var RENDER_PATH = "/storage/v1/render/image/public/";
6
4
  var DEFAULT_WIDTHS = [320, 640, 960, 1280, 1920];
7
- function isSupabaseImage(src) {
8
- return src.includes(OBJECT_PATH) || src.includes(RENDER_PATH);
5
+ function imageUrl(src, _opts = {}) {
6
+ return src;
9
7
  }
10
- function imageUrl(src, opts = {}) {
11
- if (!isSupabaseImage(src)) return src;
12
- let url;
13
- try {
14
- url = new URL(src.replace(OBJECT_PATH, RENDER_PATH));
15
- } catch {
16
- return src;
17
- }
18
- if (opts.width) url.searchParams.set("width", String(Math.round(opts.width)));
19
- if (opts.height) url.searchParams.set("height", String(Math.round(opts.height)));
20
- if (opts.quality) url.searchParams.set("quality", String(opts.quality));
21
- if (opts.resize) url.searchParams.set("resize", opts.resize);
22
- return url.toString();
23
- }
24
- function srcset(src, opts = {}) {
25
- if (!isSupabaseImage(src)) return "";
26
- return (opts.widths ?? DEFAULT_WIDTHS).map((w) => `${imageUrl(src, { width: w, quality: opts.quality, resize: opts.resize })} ${w}w`).join(", ");
8
+ function srcset(_src, _opts = {}) {
9
+ return "";
27
10
  }
28
11
  function imageProps(src, opts = {}) {
29
- return {
30
- src: imageUrl(src, { width: opts.width, height: opts.height, quality: opts.quality }),
31
- srcSet: srcset(src, { widths: opts.widths, quality: opts.quality }),
32
- sizes: opts.sizes ?? "100vw"
33
- };
12
+ return { src: imageUrl(src), srcSet: srcset(), sizes: opts.sizes ?? "100vw" };
34
13
  }
35
14
 
36
- export { DEFAULT_WIDTHS, imageProps, imageUrl, isSupabaseImage, srcset };
15
+ export { DEFAULT_WIDTHS, imageProps, imageUrl, srcset };
package/dist/index.cjs CHANGED
@@ -79,6 +79,7 @@ function resolvePath(settings, pathname) {
79
79
 
80
80
  // src/index.ts
81
81
  var RETRY_AFTER_CAP_MS = 1e4;
82
+ var SITE_TTL_MS = 6e4;
82
83
  var sleep = (ms) => new Promise((r) => setTimeout(r, ms));
83
84
  function createBrease(opts = {}) {
84
85
  const apiUrl = (opts.apiUrl ?? env("BREASE_API_URL") ?? "https://api.brease.io").replace(/\/+$/, "");
@@ -86,7 +87,7 @@ function createBrease(opts = {}) {
86
87
  const retries = opts.retries ?? 2;
87
88
  const baseMs = opts.retryBaseMs ?? 300;
88
89
  const doFetch = opts.fetch ?? ((input, init) => fetch(input, init));
89
- let sitePromise;
90
+ let site;
90
91
  async function request(path, query = {}, nullOn404 = false) {
91
92
  const token = opts.token ?? env("BREASE_TOKEN");
92
93
  if (!token)
@@ -123,11 +124,14 @@ function createBrease(opts = {}) {
123
124
  }
124
125
  const client = {
125
126
  getSite() {
126
- sitePromise ?? (sitePromise = request("/content-api/site").catch((err) => {
127
- sitePromise = void 0;
128
- throw err;
129
- }));
130
- return sitePromise;
127
+ if (!site || Date.now() - site.at > SITE_TTL_MS) {
128
+ const promise = request("/content-api/site").catch((err) => {
129
+ if (site?.promise === promise) site = void 0;
130
+ throw err;
131
+ });
132
+ site = { promise, at: Date.now() };
133
+ }
134
+ return site.promise;
131
135
  },
132
136
  getPage(slug, { locale }) {
133
137
  return request("/content-api/page", { slug, locale }, true);
@@ -145,9 +149,18 @@ function createBrease(opts = {}) {
145
149
  return request("/content-api/release");
146
150
  },
147
151
  async resolve(pathname) {
148
- const site = await client.getSite();
149
- const result = resolvePath(site, pathname);
150
- return site.locales.includes(result.locale) ? result : null;
152
+ const site2 = await client.getSite();
153
+ const result = resolvePath(site2, pathname);
154
+ return site2.locales.includes(result.locale) ? result : null;
155
+ },
156
+ async route(path) {
157
+ const joined = Array.isArray(path) ? path.map((p) => decodeURIComponent(p)).join("/") : path ?? "";
158
+ const result = await client.resolve(`/${joined}`);
159
+ return result && !result.slug.split("/").some((part) => part.startsWith("_")) ? result : null;
160
+ },
161
+ async routes() {
162
+ const refs = await client.getPages();
163
+ return refs.filter((r) => !r.hidden && !r.slug.split("/").some((part) => part.startsWith("_"))).map((r) => ({ path: r.url.split("/").filter(Boolean) }));
151
164
  },
152
165
  async sitemap(baseUrl) {
153
166
  const base = baseUrl.replace(/\/+$/, "");
package/dist/index.d.cts CHANGED
@@ -43,6 +43,13 @@ interface Brease {
43
43
  slug: string;
44
44
  locale: string;
45
45
  } | null>;
46
+ route(path?: string | string[]): Promise<{
47
+ slug: string;
48
+ locale: string;
49
+ } | null>;
50
+ routes(): Promise<{
51
+ path: string[];
52
+ }[]>;
46
53
  sitemap(baseUrl: string): Promise<SitemapEntry[]>;
47
54
  }
48
55
  declare function createBrease(opts?: BreaseOptions): Brease;
package/dist/index.d.ts CHANGED
@@ -43,6 +43,13 @@ interface Brease {
43
43
  slug: string;
44
44
  locale: string;
45
45
  } | null>;
46
+ route(path?: string | string[]): Promise<{
47
+ slug: string;
48
+ locale: string;
49
+ } | null>;
50
+ routes(): Promise<{
51
+ path: string[];
52
+ }[]>;
46
53
  sitemap(baseUrl: string): Promise<SitemapEntry[]>;
47
54
  }
48
55
  declare function createBrease(opts?: BreaseOptions): Brease;
package/dist/index.js CHANGED
@@ -69,6 +69,7 @@ function resolvePath(settings, pathname) {
69
69
 
70
70
  // src/index.ts
71
71
  var RETRY_AFTER_CAP_MS = 1e4;
72
+ var SITE_TTL_MS = 6e4;
72
73
  var sleep = (ms) => new Promise((r) => setTimeout(r, ms));
73
74
  function createBrease(opts = {}) {
74
75
  const apiUrl = (opts.apiUrl ?? env("BREASE_API_URL") ?? "https://api.brease.io").replace(/\/+$/, "");
@@ -76,7 +77,7 @@ function createBrease(opts = {}) {
76
77
  const retries = opts.retries ?? 2;
77
78
  const baseMs = opts.retryBaseMs ?? 300;
78
79
  const doFetch = opts.fetch ?? ((input, init) => fetch(input, init));
79
- let sitePromise;
80
+ let site;
80
81
  async function request(path, query = {}, nullOn404 = false) {
81
82
  const token = opts.token ?? env("BREASE_TOKEN");
82
83
  if (!token)
@@ -113,11 +114,14 @@ function createBrease(opts = {}) {
113
114
  }
114
115
  const client = {
115
116
  getSite() {
116
- sitePromise ?? (sitePromise = request("/content-api/site").catch((err) => {
117
- sitePromise = void 0;
118
- throw err;
119
- }));
120
- return sitePromise;
117
+ if (!site || Date.now() - site.at > SITE_TTL_MS) {
118
+ const promise = request("/content-api/site").catch((err) => {
119
+ if (site?.promise === promise) site = void 0;
120
+ throw err;
121
+ });
122
+ site = { promise, at: Date.now() };
123
+ }
124
+ return site.promise;
121
125
  },
122
126
  getPage(slug, { locale }) {
123
127
  return request("/content-api/page", { slug, locale }, true);
@@ -135,9 +139,18 @@ function createBrease(opts = {}) {
135
139
  return request("/content-api/release");
136
140
  },
137
141
  async resolve(pathname) {
138
- const site = await client.getSite();
139
- const result = resolvePath(site, pathname);
140
- return site.locales.includes(result.locale) ? result : null;
142
+ const site2 = await client.getSite();
143
+ const result = resolvePath(site2, pathname);
144
+ return site2.locales.includes(result.locale) ? result : null;
145
+ },
146
+ async route(path) {
147
+ const joined = Array.isArray(path) ? path.map((p) => decodeURIComponent(p)).join("/") : path ?? "";
148
+ const result = await client.resolve(`/${joined}`);
149
+ return result && !result.slug.split("/").some((part) => part.startsWith("_")) ? result : null;
150
+ },
151
+ async routes() {
152
+ const refs = await client.getPages();
153
+ return refs.filter((r) => !r.hidden && !r.slug.split("/").some((part) => part.startsWith("_"))).map((r) => ({ path: r.url.split("/").filter(Boolean) }));
141
154
  },
142
155
  async sitemap(baseUrl) {
143
156
  const base = baseUrl.replace(/\/+$/, "");
package/dist/next.cjs CHANGED
@@ -37,7 +37,7 @@ function toMetadata(seo, opts = {}) {
37
37
  });
38
38
  }
39
39
  function breaseFetch(tags = [], revalidate) {
40
- return (input, init) => fetch(input, {
40
+ return (input, init) => env("BREASE_PREVIEW") ? fetch(input, { ...init, cache: "no-store" }) : fetch(input, {
41
41
  ...init,
42
42
  next: { tags: ["brease", ...tags], ...revalidate !== void 0 ? { revalidate } : {} }
43
43
  });
package/dist/next.js CHANGED
@@ -31,7 +31,7 @@ function toMetadata(seo, opts = {}) {
31
31
  });
32
32
  }
33
33
  function breaseFetch(tags = [], revalidate) {
34
- return (input, init) => fetch(input, {
34
+ return (input, init) => env("BREASE_PREVIEW") ? fetch(input, { ...init, cache: "no-store" }) : fetch(input, {
35
35
  ...init,
36
36
  next: { tags: ["brease", ...tags], ...revalidate !== void 0 ? { revalidate } : {} }
37
37
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "brease-client",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "Brease delivery API client for Next.js, Astro and any fetch-capable runtime",
5
5
  "license": "MIT",
6
6
  "repository": {