brease-client 0.7.0 → 0.8.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 +88 -25
- package/dist/astro.cjs +8 -6
- package/dist/astro.d.cts +1 -1
- package/dist/astro.d.ts +1 -1
- package/dist/astro.js +3 -2
- package/dist/chunk-N3QOL5EU.js +10 -0
- package/dist/{chunk-SSDAREDA.js → chunk-QZXUMDB7.js} +4 -6
- package/dist/{chunk-7BBL2LET.js → chunk-SO4BD4ME.js} +129 -3
- package/dist/html.cjs +8 -6
- package/dist/html.d.cts +1 -1
- package/dist/html.d.ts +1 -1
- package/dist/html.js +2 -1
- package/dist/image.cjs +8 -2
- package/dist/image.d.cts +6 -1
- package/dist/image.d.ts +6 -1
- package/dist/image.js +8 -2
- package/dist/index.cjs +146 -2
- package/dist/index.d.cts +50 -3
- package/dist/index.d.ts +50 -3
- package/dist/index.js +2 -1
- package/dist/next.cjs +67 -49
- package/dist/next.d.cts +3 -4
- package/dist/next.d.ts +3 -4
- package/dist/next.js +10 -49
- package/dist/{types-ecfDXrLa.d.cts → types-DeS_3KtO.d.cts} +29 -3
- package/dist/{types-ecfDXrLa.d.ts → types-DeS_3KtO.d.ts} +29 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -6,7 +6,7 @@ Typed client for the Brease delivery API. Zero runtime dependencies, ESM + CJS,
|
|
|
6
6
|
pnpm add brease-client
|
|
7
7
|
```
|
|
8
8
|
|
|
9
|
-
Set
|
|
9
|
+
Set these environment variables (Brease writes them into your Vercel project at onboarding):
|
|
10
10
|
|
|
11
11
|
| Variable | Purpose |
|
|
12
12
|
| --- | --- |
|
|
@@ -26,22 +26,23 @@ const brease = createBrease({
|
|
|
26
26
|
fetch // optional custom fetch, e.g. to add framework cache options
|
|
27
27
|
})
|
|
28
28
|
|
|
29
|
-
await brease.getSite() // name, locales, defaultLocale, localeStrategy, global SEO
|
|
29
|
+
await brease.getSite() // name, url (the public address), locales, defaultLocale, localeStrategy, global SEO, analytics
|
|
30
30
|
await brease.getPage<Home>('', { locale: 'en' }) // Page<Home> | null ('' is the home page)
|
|
31
|
-
await brease.getPages({ locale: 'en' }) // [{ pageId, locale, slug, url, hidden }]
|
|
31
|
+
await brease.getPages({ locale: 'en' }) // [{ pageId, locale, slug, url, hidden, name, index, parentId, order }]
|
|
32
32
|
await brease.getNavigation('main', { locale: 'en' }) // { key, locale, items } | null
|
|
33
|
-
await brease.getRedirects() // [{ source, destination, status }]
|
|
33
|
+
await brease.getRedirects() // [{ source, destination, status }], cached for a minute
|
|
34
34
|
await brease.resolve('/en/about') // { slug: 'about', locale: 'en' } | null
|
|
35
35
|
await brease.route(['en', 'about']) // the same from catch-all path segments; null for shared "_" pages
|
|
36
36
|
await brease.routes() // [{ path: ['en', 'about'] }, …]: static params for every published page
|
|
37
37
|
await brease.localeContext(['en', 'about']) // { locale, settings, alternates, homes } for BreaseLocaleProvider
|
|
38
|
-
await brease.sitemap(
|
|
38
|
+
await brease.sitemap(site.url) // [{ url, alternates }]: pages search may show
|
|
39
39
|
```
|
|
40
40
|
|
|
41
41
|
- `null` means 404. Every other failure throws a `BreaseError` with `status`, `code`, `requestId` and `details`.
|
|
42
42
|
- 429 and 5xx responses (and network errors) are retried twice with backoff; `Retry-After` is honoured.
|
|
43
43
|
- 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.
|
|
44
|
-
- `sitemap()` has no `lastModified`: releases are immutable snapshots and do not track per-page edit times.
|
|
44
|
+
- `sitemap()` lists only pages search may show: not hidden, not kept out of search (`index: false`, the page's "Show in search results" switch), and no site-wide `_` pages. It has no `lastModified`: releases are immutable snapshots and do not track per-page edit times.
|
|
45
|
+
- `site.url` is the site's public address (its primary domain), the one Brease uses in canonical URLs and hreflang; use it for the sitemap and `metadataBase` rather than an env variable of your own.
|
|
45
46
|
- `getSite()` is cached for a minute, so a locale added in Brease is served without a restart.
|
|
46
47
|
- `buildPath(settings, slug, locale)` and `resolvePath(settings, pathname)` are exported for routing code that already has the site settings.
|
|
47
48
|
|
|
@@ -72,8 +73,7 @@ async function load(params: Props['params']) {
|
|
|
72
73
|
}
|
|
73
74
|
|
|
74
75
|
export async function generateStaticParams() {
|
|
75
|
-
|
|
76
|
-
return pages.filter((p) => !p.hidden).map((p) => ({ slug: p.url.split('/').filter(Boolean) }))
|
|
76
|
+
return (await brease.routes()).map((r) => ({ slug: r.path })) // published pages, without site-wide "_" pages
|
|
77
77
|
}
|
|
78
78
|
|
|
79
79
|
export async function generateMetadata({ params }: Props) {
|
|
@@ -88,25 +88,74 @@ export default async function Page({ params }: Props) {
|
|
|
88
88
|
}
|
|
89
89
|
```
|
|
90
90
|
|
|
91
|
+
`page.resolvedSeo` is final: the title already has the site's title template applied (unless the page chose to show
|
|
92
|
+
its title as written), and every field falls back to the site's defaults. Render it as is; don't add your own
|
|
93
|
+
template or a home-page exception in code.
|
|
94
|
+
|
|
91
95
|
```ts
|
|
92
|
-
// app/api/revalidate/route.ts
|
|
96
|
+
// app/api/brease/revalidate/route.ts: the path Brease calls after a content release
|
|
93
97
|
import { revalidateHandler } from 'brease-client/next'
|
|
94
98
|
|
|
95
|
-
export const POST = revalidateHandler() // uses BREASE_REVALIDATE_SECRET
|
|
99
|
+
export const POST = revalidateHandler() // uses BREASE_REVALIDATE_SECRET; refreshes at once
|
|
96
100
|
```
|
|
97
101
|
|
|
102
|
+
Other frameworks: `verifyRevalidate(request, secret)` (root export) checks the signature and returns the paths and
|
|
103
|
+
cache tags to refresh, or why not (401 for a bad signature, 400 for a bad body).
|
|
104
|
+
|
|
105
|
+
Redirects managed in Brease apply per request, so a new one works as soon as it's published (within a minute), with
|
|
106
|
+
no rebuild:
|
|
107
|
+
|
|
98
108
|
```ts
|
|
99
|
-
//
|
|
100
|
-
import {
|
|
109
|
+
// proxy.ts (middleware.ts before Next 16)
|
|
110
|
+
import { type NextRequest, NextResponse } from 'next/server'
|
|
111
|
+
import { matchRedirect } from 'brease-client'
|
|
112
|
+
import { brease } from '@/lib/brease'
|
|
101
113
|
|
|
102
|
-
export
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
return redirects.map((r) => ({ source: r.source, destination: r.destination, permanent: r.status === 301 || r.status === 308 }))
|
|
106
|
-
}
|
|
114
|
+
export async function proxy(req: NextRequest) {
|
|
115
|
+
const hit = matchRedirect(await brease.getRedirects().catch(() => []), req.nextUrl.pathname)
|
|
116
|
+
return hit ? NextResponse.redirect(new URL(hit.destination, req.url), hit.status) : NextResponse.next()
|
|
107
117
|
}
|
|
118
|
+
|
|
119
|
+
export const config = { matcher: ['/((?!_next|api|.*\\..*).*)'] }
|
|
108
120
|
```
|
|
109
121
|
|
|
122
|
+
Sources may use parameters as in Next's redirects (`/blog/:slug`, `/docs/:rest*`). `breaseRedirects()` (from
|
|
123
|
+
`brease-client/next`, for `next.config`) still works, but only reads redirects at build time.
|
|
124
|
+
|
|
125
|
+
### Structured data (JSON-LD)
|
|
126
|
+
|
|
127
|
+
Structured data is content: the page's own entries (its SEO in Brease) and the site's (site SEO) come together in
|
|
128
|
+
`page.resolvedSeo.structuredData`. `toMetadata` can't output scripts, so render them with `jsonLd` (framework-free,
|
|
129
|
+
from the root export) on every page; each item's `json` is escaped for use inside a script tag. Entities your code
|
|
130
|
+
already emits shouldn't be added in Brease too.
|
|
131
|
+
|
|
132
|
+
```tsx
|
|
133
|
+
import { jsonLd } from 'brease-client'
|
|
134
|
+
|
|
135
|
+
{jsonLd(page.resolvedSeo).map((item, i) => (
|
|
136
|
+
<script key={i} type="application/ld+json" dangerouslySetInnerHTML={{ __html: item.json }} />
|
|
137
|
+
))}
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
`headTags` (HTML) and `breaseHead` (Astro) already include it; `jsonLdHtml(seo)` gives only the scripts.
|
|
141
|
+
|
|
142
|
+
### Custom code
|
|
143
|
+
|
|
144
|
+
Site-wide head code and a page's own code (both from SEO settings in Brease) run on the live site only when the site
|
|
145
|
+
renders them. `toMetadata` can't, so render `customTags(page.resolvedSeo)` (root export) as real elements:
|
|
146
|
+
|
|
147
|
+
```tsx
|
|
148
|
+
import { customTags } from 'brease-client'
|
|
149
|
+
|
|
150
|
+
{customTags(page.resolvedSeo).map(({ tag: Tag, attrs, content }, i) => {
|
|
151
|
+
const props = Object.fromEntries(Object.entries(attrs).map(([k, v]) => [k, v === '' ? true : v]))
|
|
152
|
+
return content ? <Tag key={i} {...props} dangerouslySetInnerHTML={{ __html: content }} /> : <Tag key={i} {...props} />
|
|
153
|
+
})}
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
Only `script`, `style`, `meta`, `link` and `noscript` tags are kept. `headTags` (HTML) and `breaseHead` (Astro)
|
|
157
|
+
include the code already.
|
|
158
|
+
|
|
110
159
|
### Google Analytics (GA4)
|
|
111
160
|
|
|
112
161
|
When GA4 is linked in Brease (Connections → Google) with "Add the tag to the site" on, the site response carries
|
|
@@ -229,7 +278,7 @@ A locale switcher is then a few lines in any framework: a list of `<a href={l.hr
|
|
|
229
278
|
- Everything that read a fixed locale (`getPage(…, { locale: 'hu' })`, `getNavigation`, `site.seo.hu`) takes it from the route instead.
|
|
230
279
|
- Links inside content are already locale URLs. Build other internal links with `localeHref(settings, slug, locale)`.
|
|
231
280
|
- Text written into components (button labels, captions) stays as it is; moving it into Brease content is a separate change.
|
|
232
|
-
- `
|
|
281
|
+
- `proxy.ts`: `matchRedirect(await brease.getRedirects(), pathname)` applies the redirects managed in Brease per request (see above).
|
|
233
282
|
|
|
234
283
|
### Revalidation signature
|
|
235
284
|
|
|
@@ -274,13 +323,17 @@ const html = `<head>${headTags(page.resolvedSeo, { xDefault: 'https://example.co
|
|
|
274
323
|
|
|
275
324
|
The Brease editor's Edit tool finds content through `data-brease` attributes:
|
|
276
325
|
|
|
277
|
-
- `data-brease=
|
|
278
|
-
- `data-brease=
|
|
279
|
-
- `data-brease
|
|
326
|
+
- `data-brease={fieldPath('hero.title')}` on an element that renders a field of the page being shown (a path into `page.content`, e.g. `hero.title` or `sections.2.items.0.label`)
|
|
327
|
+
- `data-brease={fieldPath('footer.copyright', global.pageId)}` for a field of another page, such as site-wide content (`_global`): the path is prefixed with that page's id (`<pageId>:footer.copyright`). Take the id from the page you fetched, never a fixed one.
|
|
328
|
+
- `data-brease={navPath('footer')}` (`nav:footer`) on the element that wraps a navigation menu; clicking it opens that navigation's editor
|
|
329
|
+
- `data-brease-block`: a section root carries its section type (`{section.type}`); other blocks (header, footer, a hero outside the sections list) carry their component name. A hint for the AI.
|
|
280
330
|
- `data-brease-sections="sections"` and `data-brease-section="<id>"`: the section list and its sections (see Sections)
|
|
281
331
|
|
|
282
332
|
```tsx
|
|
283
|
-
|
|
333
|
+
import { fieldPath, navPath } from 'brease-client'
|
|
334
|
+
|
|
335
|
+
<nav data-brease={navPath('footer')}>{footer.items.map(/* … */)}</nav>
|
|
336
|
+
<p data-brease={fieldPath('footer.copyright', global.pageId)}>{global.content.footer.copyright}</p>
|
|
284
337
|
```
|
|
285
338
|
|
|
286
339
|
## Sections
|
|
@@ -296,7 +349,7 @@ src/brease/sections/ (or brease/sections/)
|
|
|
296
349
|
```
|
|
297
350
|
|
|
298
351
|
- A page's sections are one list, `content.sections: [{ id, type, ...fields }]`, with the same ids in the same order in every locale. Fixed parts (a page title, header, footer, site-wide content) stay outside it.
|
|
299
|
-
- `fields` describes the section's fields; Brease builds new sections from it (lorem ipsum, a placeholder image, `default`s, `enum`s' first values, `minItems` list items). Images are `{ assetId, url, alt }
|
|
352
|
+
- `fields` describes the section's fields; Brease builds new sections from it (lorem ipsum, a placeholder image, `default`s, `enum`s' first values, `minItems` list items). Images are `{ assetId, url, alt, width?, height? }` (`BreaseImage`), links `{ label, href }` (`BreaseLink`; an internal link also carries the page's `pageId`, and the delivery API sets its `href` for the requested locale), rich text `{ "type": "string", "format": "html" }`.
|
|
300
353
|
- `"shared": true` keeps one content for every page that shows the type, on the site-wide page (`_global`) under `content.shared.<type>`; pages list only `{ id, type }` for it, and its markers use the other-page prefix (`<_global pageId>:shared.<type>.<field>`).
|
|
301
354
|
- Markers: the list's element carries `data-brease-sections="sections"`, each section's root `data-brease-section={id}` and `data-brease-block={type}`, fields inside `data-brease="sections.<index>.<field>"`.
|
|
302
355
|
|
|
@@ -323,10 +376,20 @@ export function Sections({ sections }: { sections: BreaseSection[] }) {
|
|
|
323
376
|
import { imageProps, srcset } from 'brease-client/image'
|
|
324
377
|
|
|
325
378
|
const props = imageProps(page.content.hero.image, { sizes: '(min-width: 768px) 50vw, 100vw' })
|
|
326
|
-
// <img src={props.src} srcSet={props.srcSet} sizes={props.sizes} />
|
|
379
|
+
// <img src={props.src} srcSet={props.srcSet} sizes={props.sizes} alt={props.alt} width={props.width} height={props.height} />
|
|
327
380
|
```
|
|
328
381
|
|
|
329
|
-
|
|
382
|
+
An image field is `{ assetId, url, alt, width?, height? }` (`BreaseImage`); `imageProps` takes the field or a plain
|
|
383
|
+
URL. Brease files are served as-is from storage, which doesn't resize images: the helpers return the URL unchanged
|
|
384
|
+
with an empty `srcset`. For responsive images, let your framework resize them, e.g. `next/image` with the host of
|
|
385
|
+
your Brease image URLs in `images.remotePatterns`.
|
|
386
|
+
|
|
387
|
+
## Content links
|
|
388
|
+
|
|
389
|
+
Link fields are `{ label, href }`. A link to another page of the site also carries that page's `pageId`, and the
|
|
390
|
+
delivery API sets `href` to the page's URL in the locale you asked for, so renaming or moving a page never breaks a
|
|
391
|
+
link and every locale links to its own version. External links are only the `href`. Rich text links to pages carry
|
|
392
|
+
`data-brease-page="<pageId>"`, and their `href` is filled in the same way.
|
|
330
393
|
|
|
331
394
|
## Without JavaScript
|
|
332
395
|
|
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
|
|
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-
|
|
3
|
+
import { R as ResolvedSeo } from './types-DeS_3KtO.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-
|
|
3
|
+
import { R as ResolvedSeo } from './types-DeS_3KtO.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-
|
|
2
|
-
export { headTagList, headTags } from './chunk-
|
|
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
|
|
@@ -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
|
|
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
|
}
|
|
@@ -96,6 +96,29 @@ function grantAnalytics(granted = true) {
|
|
|
96
96
|
gtag?.("consent", "update", { analytics_storage: granted ? "granted" : "denied" });
|
|
97
97
|
}
|
|
98
98
|
|
|
99
|
+
// src/custom-code.ts
|
|
100
|
+
function customTags(seo) {
|
|
101
|
+
return [seo.customHead, seo.customCode].flatMap((html) => html ? parseTags(html) : []);
|
|
102
|
+
}
|
|
103
|
+
function parseTags(html) {
|
|
104
|
+
const out = [];
|
|
105
|
+
const re = /<(script|style|noscript)\b([^>]*)>([\s\S]*?)<\/\1\s*>|<(meta|link)\b([^>]*?)\/?>/gi;
|
|
106
|
+
for (const m of html.matchAll(re)) {
|
|
107
|
+
if (m[1]) {
|
|
108
|
+
const tag = m[1].toLowerCase();
|
|
109
|
+
out.push({ tag, attrs: attrsOf(m[2] ?? ""), ...m[3]?.trim() ? { content: m[3] } : {} });
|
|
110
|
+
} else if (m[4])
|
|
111
|
+
out.push({ tag: m[4].toLowerCase(), attrs: attrsOf(m[5] ?? "") });
|
|
112
|
+
}
|
|
113
|
+
return out;
|
|
114
|
+
}
|
|
115
|
+
function attrsOf(source) {
|
|
116
|
+
const attrs = {};
|
|
117
|
+
for (const m of source.matchAll(/([^\s"'=<>/]+)(?:\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s"'>]+)))?/g))
|
|
118
|
+
attrs[m[1].toLowerCase()] = m[2] ?? m[3] ?? m[4] ?? "";
|
|
119
|
+
return attrs;
|
|
120
|
+
}
|
|
121
|
+
|
|
99
122
|
// src/locales.ts
|
|
100
123
|
function localeLinks(context) {
|
|
101
124
|
const { settings, alternates, homes, locale: current } = context;
|
|
@@ -114,6 +137,98 @@ function nativeName(locale) {
|
|
|
114
137
|
}
|
|
115
138
|
var localeHref = (settings, slug, locale) => buildPath(settings, slug, locale);
|
|
116
139
|
|
|
140
|
+
// src/markers.ts
|
|
141
|
+
function fieldPath(path, pageId) {
|
|
142
|
+
return pageId ? `${pageId}:${path}` : path;
|
|
143
|
+
}
|
|
144
|
+
function navPath(key) {
|
|
145
|
+
return `nav:${key}`;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// src/redirects.ts
|
|
149
|
+
function matchRedirect(redirects, pathname) {
|
|
150
|
+
const path = normalize(pathname);
|
|
151
|
+
for (const r of redirects) {
|
|
152
|
+
const params = matchSource(normalize(r.source), path);
|
|
153
|
+
if (!params) continue;
|
|
154
|
+
const destination = r.destination.replace(
|
|
155
|
+
/:([A-Za-z_][A-Za-z0-9_]*)\*?/g,
|
|
156
|
+
(whole, name) => name in params ? params[name] : whole
|
|
157
|
+
);
|
|
158
|
+
if (normalize(destination) === path) continue;
|
|
159
|
+
return { destination, status: r.status };
|
|
160
|
+
}
|
|
161
|
+
return null;
|
|
162
|
+
}
|
|
163
|
+
var normalize = (p) => (p.length > 1 ? p.replace(/\/+$/, "") : p) || "/";
|
|
164
|
+
function matchSource(source, path) {
|
|
165
|
+
const want = source.split("/").filter(Boolean);
|
|
166
|
+
const have = path.split("/").filter(Boolean);
|
|
167
|
+
const params = {};
|
|
168
|
+
for (let i = 0; i < want.length; i++) {
|
|
169
|
+
const seg = want[i];
|
|
170
|
+
const rest = /^:([A-Za-z_][A-Za-z0-9_]*)\*$/.exec(seg);
|
|
171
|
+
if (rest) {
|
|
172
|
+
params[rest[1]] = have.slice(i).join("/");
|
|
173
|
+
return params;
|
|
174
|
+
}
|
|
175
|
+
const value = have[i];
|
|
176
|
+
if (value === void 0) return null;
|
|
177
|
+
const param = /^:([A-Za-z_][A-Za-z0-9_]*)$/.exec(seg);
|
|
178
|
+
if (param) params[param[1]] = value;
|
|
179
|
+
else if (decodeURIComponent(value) !== decodeURIComponent(seg)) return null;
|
|
180
|
+
}
|
|
181
|
+
return want.length === have.length ? params : null;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
// src/signature.ts
|
|
185
|
+
var MAX_SKEW_MS = 5 * 6e4;
|
|
186
|
+
async function hmacHex(secret, message) {
|
|
187
|
+
const enc = new TextEncoder();
|
|
188
|
+
const key = await crypto.subtle.importKey(
|
|
189
|
+
"raw",
|
|
190
|
+
enc.encode(secret),
|
|
191
|
+
{ name: "HMAC", hash: "SHA-256" },
|
|
192
|
+
false,
|
|
193
|
+
["sign"]
|
|
194
|
+
);
|
|
195
|
+
const sig = new Uint8Array(await crypto.subtle.sign("HMAC", key, enc.encode(message)));
|
|
196
|
+
return Array.from(sig, (b) => b.toString(16).padStart(2, "0")).join("");
|
|
197
|
+
}
|
|
198
|
+
async function verifySignature(body, timestamp, signature, secret, now = Date.now()) {
|
|
199
|
+
if (!timestamp || !signature) return false;
|
|
200
|
+
if (!Number.isFinite(Number(timestamp)) || Math.abs(now - Number(timestamp)) > MAX_SKEW_MS) return false;
|
|
201
|
+
const expected = await hmacHex(secret, `${timestamp}.${body}`);
|
|
202
|
+
if (expected.length !== signature.length) return false;
|
|
203
|
+
let diff = 0;
|
|
204
|
+
for (let i = 0; i < expected.length; i++) diff |= expected.charCodeAt(i) ^ signature.charCodeAt(i);
|
|
205
|
+
return diff === 0;
|
|
206
|
+
}
|
|
207
|
+
async function verifyRevalidate(req, secret) {
|
|
208
|
+
const raw = await req.text();
|
|
209
|
+
const ok = await verifySignature(
|
|
210
|
+
raw,
|
|
211
|
+
req.headers.get("x-brease-timestamp"),
|
|
212
|
+
req.headers.get("x-brease-signature"),
|
|
213
|
+
secret
|
|
214
|
+
);
|
|
215
|
+
if (!ok) return { ok: false, reason: "signature" };
|
|
216
|
+
let body;
|
|
217
|
+
try {
|
|
218
|
+
body = JSON.parse(raw || "{}");
|
|
219
|
+
} catch {
|
|
220
|
+
return { ok: false, reason: "body" };
|
|
221
|
+
}
|
|
222
|
+
const paths = new Set(body.paths ?? []);
|
|
223
|
+
for (const url of body.urls ?? []) {
|
|
224
|
+
try {
|
|
225
|
+
paths.add(new URL(url, "http://localhost").pathname);
|
|
226
|
+
} catch {
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
return { ok: true, paths: [...paths], tags: ["brease", ...body.tags ?? []], body };
|
|
230
|
+
}
|
|
231
|
+
|
|
117
232
|
// src/index.ts
|
|
118
233
|
var RETRY_AFTER_CAP_MS = 1e4;
|
|
119
234
|
var SITE_TTL_MS = 6e4;
|
|
@@ -125,6 +240,7 @@ function createBrease(opts = {}) {
|
|
|
125
240
|
const baseMs = opts.retryBaseMs ?? 300;
|
|
126
241
|
const doFetch = opts.fetch ?? ((input, init) => fetch(input, init));
|
|
127
242
|
let site;
|
|
243
|
+
let redirects;
|
|
128
244
|
async function request(path, query = {}, nullOn404 = false) {
|
|
129
245
|
const token = opts.token ?? env("BREASE_TOKEN");
|
|
130
246
|
if (!token)
|
|
@@ -179,8 +295,16 @@ function createBrease(opts = {}) {
|
|
|
179
295
|
getNavigation(key, { locale }) {
|
|
180
296
|
return request(`/content-api/navigations/${encodeURIComponent(key)}`, { locale }, true);
|
|
181
297
|
},
|
|
298
|
+
// Cached for a minute, like the site: a proxy applies redirects per request (matchRedirect).
|
|
182
299
|
getRedirects() {
|
|
183
|
-
|
|
300
|
+
if (!redirects || Date.now() - redirects.at > SITE_TTL_MS) {
|
|
301
|
+
const promise = request("/content-api/redirects").catch((err) => {
|
|
302
|
+
if (redirects?.promise === promise) redirects = void 0;
|
|
303
|
+
throw err;
|
|
304
|
+
});
|
|
305
|
+
redirects = { promise, at: Date.now() };
|
|
306
|
+
}
|
|
307
|
+
return redirects.promise;
|
|
184
308
|
},
|
|
185
309
|
getRelease() {
|
|
186
310
|
return request("/content-api/release");
|
|
@@ -222,7 +346,9 @@ function createBrease(opts = {}) {
|
|
|
222
346
|
},
|
|
223
347
|
async sitemap(baseUrl) {
|
|
224
348
|
const base = baseUrl.replace(/\/+$/, "");
|
|
225
|
-
const refs = (await client.getPages()).filter(
|
|
349
|
+
const refs = (await client.getPages()).filter(
|
|
350
|
+
(r) => !r.hidden && r.index !== false && !r.slug.split("/").some((part) => part.startsWith("_"))
|
|
351
|
+
);
|
|
226
352
|
const byPage = /* @__PURE__ */ new Map();
|
|
227
353
|
for (const ref of refs) {
|
|
228
354
|
const alternates = byPage.get(ref.pageId) ?? {};
|
|
@@ -242,4 +368,4 @@ function pathOf(url) {
|
|
|
242
368
|
}
|
|
243
369
|
}
|
|
244
370
|
|
|
245
|
-
export { BreaseError, buildPath, createBrease, env, ga4Html, ga4Tag, grantAnalytics, localeHref, localeLinks, nativeName, normalizeSlug, resolvePath };
|
|
371
|
+
export { BreaseError, buildPath, createBrease, customTags, env, fieldPath, ga4Html, ga4Tag, grantAnalytics, hmacHex, localeHref, localeLinks, matchRedirect, nativeName, navPath, normalizeSlug, resolvePath, verifyRevalidate, verifySignature };
|
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
|
|
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
package/dist/html.d.ts
CHANGED
package/dist/html.js
CHANGED
package/dist/image.cjs
CHANGED
|
@@ -8,8 +8,14 @@ function imageUrl(src, _opts = {}) {
|
|
|
8
8
|
function srcset(_src, _opts = {}) {
|
|
9
9
|
return "";
|
|
10
10
|
}
|
|
11
|
-
function imageProps(
|
|
12
|
-
|
|
11
|
+
function imageProps(image, opts = {}) {
|
|
12
|
+
const src = typeof image === "string" ? image : image.url;
|
|
13
|
+
const extra = typeof image === "string" ? {} : {
|
|
14
|
+
...image.alt !== void 0 ? { alt: image.alt } : {},
|
|
15
|
+
...image.width ? { width: image.width } : {},
|
|
16
|
+
...image.height ? { height: image.height } : {}
|
|
17
|
+
};
|
|
18
|
+
return { src: imageUrl(src), srcSet: srcset(), sizes: opts.sizes ?? "100vw", ...extra };
|
|
13
19
|
}
|
|
14
20
|
|
|
15
21
|
exports.DEFAULT_WIDTHS = DEFAULT_WIDTHS;
|
package/dist/image.d.cts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { B as BreaseImage } from './types-DeS_3KtO.cjs';
|
|
2
|
+
|
|
1
3
|
type Resize = 'cover' | 'contain' | 'fill';
|
|
2
4
|
type ImageOptions = {
|
|
3
5
|
width?: number;
|
|
@@ -12,7 +14,7 @@ declare function srcset(_src: string, _opts?: {
|
|
|
12
14
|
quality?: number;
|
|
13
15
|
resize?: Resize;
|
|
14
16
|
}): string;
|
|
15
|
-
declare function imageProps(
|
|
17
|
+
declare function imageProps(image: string | BreaseImage, opts?: {
|
|
16
18
|
sizes?: string;
|
|
17
19
|
widths?: number[];
|
|
18
20
|
width?: number;
|
|
@@ -22,6 +24,9 @@ declare function imageProps(src: string, opts?: {
|
|
|
22
24
|
src: string;
|
|
23
25
|
srcSet: string;
|
|
24
26
|
sizes: string;
|
|
27
|
+
alt?: string;
|
|
28
|
+
width?: number;
|
|
29
|
+
height?: number;
|
|
25
30
|
};
|
|
26
31
|
|
|
27
32
|
export { DEFAULT_WIDTHS, type ImageOptions, type Resize, imageProps, imageUrl, srcset };
|
package/dist/image.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { B as BreaseImage } from './types-DeS_3KtO.js';
|
|
2
|
+
|
|
1
3
|
type Resize = 'cover' | 'contain' | 'fill';
|
|
2
4
|
type ImageOptions = {
|
|
3
5
|
width?: number;
|
|
@@ -12,7 +14,7 @@ declare function srcset(_src: string, _opts?: {
|
|
|
12
14
|
quality?: number;
|
|
13
15
|
resize?: Resize;
|
|
14
16
|
}): string;
|
|
15
|
-
declare function imageProps(
|
|
17
|
+
declare function imageProps(image: string | BreaseImage, opts?: {
|
|
16
18
|
sizes?: string;
|
|
17
19
|
widths?: number[];
|
|
18
20
|
width?: number;
|
|
@@ -22,6 +24,9 @@ declare function imageProps(src: string, opts?: {
|
|
|
22
24
|
src: string;
|
|
23
25
|
srcSet: string;
|
|
24
26
|
sizes: string;
|
|
27
|
+
alt?: string;
|
|
28
|
+
width?: number;
|
|
29
|
+
height?: number;
|
|
25
30
|
};
|
|
26
31
|
|
|
27
32
|
export { DEFAULT_WIDTHS, type ImageOptions, type Resize, imageProps, imageUrl, srcset };
|
package/dist/image.js
CHANGED
|
@@ -8,8 +8,14 @@ function imageUrl(src, _opts = {}) {
|
|
|
8
8
|
function srcset(_src, _opts = {}) {
|
|
9
9
|
return "";
|
|
10
10
|
}
|
|
11
|
-
function imageProps(
|
|
12
|
-
|
|
11
|
+
function imageProps(image, opts = {}) {
|
|
12
|
+
const src = typeof image === "string" ? image : image.url;
|
|
13
|
+
const extra = typeof image === "string" ? {} : {
|
|
14
|
+
...image.alt !== void 0 ? { alt: image.alt } : {},
|
|
15
|
+
...image.width ? { width: image.width } : {},
|
|
16
|
+
...image.height ? { height: image.height } : {}
|
|
17
|
+
};
|
|
18
|
+
return { src: imageUrl(src), srcSet: srcset(), sizes: opts.sizes ?? "100vw", ...extra };
|
|
13
19
|
}
|
|
14
20
|
|
|
15
21
|
export { DEFAULT_WIDTHS, imageProps, imageUrl, srcset };
|