brease-client 0.2.1 → 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 +80 -0
- package/dist/index.cjs +22 -9
- package/dist/index.d.cts +7 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +22 -9
- package/package.json +1 -1
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
|
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
|
|
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
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
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
|
|
149
|
-
const result = resolvePath(
|
|
150
|
-
return
|
|
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
|
|
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
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
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
|
|
139
|
-
const result = resolvePath(
|
|
140
|
-
return
|
|
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(/\/+$/, "");
|