@flyo/nitro-next 2.0.1 → 2.2.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/dist/client.d.mts +73 -2
- package/dist/client.d.ts +73 -2
- package/dist/client.js +28 -0
- package/dist/client.js.map +1 -1
- package/dist/client.mjs +27 -0
- package/dist/client.mjs.map +1 -1
- package/dist/proxy.d.mts +2 -2
- package/dist/proxy.d.ts +2 -2
- package/dist/proxy.js +8 -2
- package/dist/proxy.js.map +1 -1
- package/dist/proxy.mjs +8 -2
- package/dist/proxy.mjs.map +1 -1
- package/dist/server.d.mts +154 -5
- package/dist/server.d.ts +154 -5
- package/dist/server.js +122 -13
- package/dist/server.js.map +1 -1
- package/dist/server.mjs +117 -12
- package/dist/server.mjs.map +1 -1
- package/package.json +1 -1
package/dist/client.d.mts
CHANGED
|
@@ -1,7 +1,78 @@
|
|
|
1
1
|
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
|
-
import { Block, Entity } from '@flyo/nitro-typescript';
|
|
2
|
+
import { Translation, Block, Entity } from '@flyo/nitro-typescript';
|
|
3
3
|
import { ImageLoaderProps } from 'next/image';
|
|
4
4
|
|
|
5
|
+
/**
|
|
6
|
+
* A single language option for a language switcher, derived from a page's or
|
|
7
|
+
* entity's `translation[]`. Framework-agnostic plain data — render it however
|
|
8
|
+
* you like.
|
|
9
|
+
*/
|
|
10
|
+
interface FlyoLanguageLink {
|
|
11
|
+
/** Locale shortcode, e.g. `"de"`. */
|
|
12
|
+
shortcode: string;
|
|
13
|
+
/** Full language name from the CMS translation, e.g. `"Deutsch"` (when available). */
|
|
14
|
+
name?: string;
|
|
15
|
+
/** Fully-resolved localized href, or `null` when this locale has no linked translation. */
|
|
16
|
+
href: string | null;
|
|
17
|
+
/** Localized page/entity title, when available. */
|
|
18
|
+
title?: string;
|
|
19
|
+
/** `true` when this entry is the currently active locale. */
|
|
20
|
+
isCurrent: boolean;
|
|
21
|
+
/** `true` when a linked translation actually exists for this locale. */
|
|
22
|
+
exists: boolean;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Map a page's or entity's `translation[]` into a switcher-ready array of typed
|
|
26
|
+
* links — the building block for a language switcher.
|
|
27
|
+
*
|
|
28
|
+
* Flyo only returns `translation` entries for languages that actually have a
|
|
29
|
+
* translation. Pass `options.locales` (e.g. `flyo.state.locales`) to also get an
|
|
30
|
+
* entry for every configured locale that is *missing* a translation — those come
|
|
31
|
+
* back as `{ href: null, exists: false }` so you can render a fallback (a disabled
|
|
32
|
+
* item, a link to the home page, …).
|
|
33
|
+
*
|
|
34
|
+
* Pure — no React or server-only APIs, so it is safe to call from server *or*
|
|
35
|
+
* client components.
|
|
36
|
+
*
|
|
37
|
+
* This is a pure *data* helper — it maps a `translation[]` to switcher links and
|
|
38
|
+
* renders nothing. **The Flyo route helpers already call it for you and publish
|
|
39
|
+
* the result**, so page and entity routes need no switcher code at all; the
|
|
40
|
+
* footer just reads the store (see `publishLanguageLinks` / `readLanguageLinks`
|
|
41
|
+
* in `@flyo/nitro-next/server`). Call this directly only when you publish links
|
|
42
|
+
* by hand from a raw `translation[]` — e.g. on a custom route Flyo doesn't
|
|
43
|
+
* resolve:
|
|
44
|
+
*
|
|
45
|
+
* ```tsx
|
|
46
|
+
* getLanguageLinks(translations, { currentLang, locales });
|
|
47
|
+
* ```
|
|
48
|
+
*
|
|
49
|
+
* **Render each link as a native `<a>`, not `next/link`'s `<Link>`.** A language
|
|
50
|
+
* switch must refresh the shared chrome — the localized nav, footer and
|
|
51
|
+
* `<html lang>` — that lives in the root layout. In the Next.js App Router, soft
|
|
52
|
+
* (client-side) navigation re-renders only the page segment, *not* shared
|
|
53
|
+
* layouts, so a `<Link>` would leave that chrome in the previous language while
|
|
54
|
+
* only the page body updates. A plain `<a>` triggers a full-document navigation,
|
|
55
|
+
* forcing a fresh server render in the new locale so every part updates. (Regular
|
|
56
|
+
* nav links can stay `<Link>` — the nav is identical within a language.)
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* ```tsx
|
|
60
|
+
* const links = getLanguageLinks(page.translation, {
|
|
61
|
+
* currentLang: lang,
|
|
62
|
+
* locales: flyo.state.locales,
|
|
63
|
+
* });
|
|
64
|
+
* // Native <a> (full-document nav) — NOT next/link, so shared layout chrome
|
|
65
|
+
* // re-renders in the new locale.
|
|
66
|
+
* // links.map(l => l.exists
|
|
67
|
+
* // ? <a key={l.shortcode} href={l.href!} aria-current={l.isCurrent || undefined}>{l.name ?? l.shortcode}</a>
|
|
68
|
+
* // : <span key={l.shortcode} aria-disabled>{l.shortcode}</span>)
|
|
69
|
+
* ```
|
|
70
|
+
*/
|
|
71
|
+
declare function getLanguageLinks(translations: Translation[] | undefined, options?: {
|
|
72
|
+
currentLang?: string;
|
|
73
|
+
locales?: string[];
|
|
74
|
+
}): FlyoLanguageLink[];
|
|
75
|
+
|
|
5
76
|
/**
|
|
6
77
|
* Check if running in production environment
|
|
7
78
|
*/
|
|
@@ -164,4 +235,4 @@ declare function EditableSection({ block, children, className, as: Tag, }: {
|
|
|
164
235
|
as?: React.ElementType;
|
|
165
236
|
}): react_jsx_runtime.JSX.Element;
|
|
166
237
|
|
|
167
|
-
export { type EditableBlock, EditableSection, FlyoCdnLoader, FlyoClientWrapper, FlyoMetric, FlyoWysiwyg, type WysiwygJson, type WysiwygNode, editable, isProd };
|
|
238
|
+
export { type EditableBlock, EditableSection, FlyoCdnLoader, FlyoClientWrapper, type FlyoLanguageLink, FlyoMetric, FlyoWysiwyg, type WysiwygJson, type WysiwygNode, editable, getLanguageLinks, isProd };
|
package/dist/client.d.ts
CHANGED
|
@@ -1,7 +1,78 @@
|
|
|
1
1
|
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
|
-
import { Block, Entity } from '@flyo/nitro-typescript';
|
|
2
|
+
import { Translation, Block, Entity } from '@flyo/nitro-typescript';
|
|
3
3
|
import { ImageLoaderProps } from 'next/image';
|
|
4
4
|
|
|
5
|
+
/**
|
|
6
|
+
* A single language option for a language switcher, derived from a page's or
|
|
7
|
+
* entity's `translation[]`. Framework-agnostic plain data — render it however
|
|
8
|
+
* you like.
|
|
9
|
+
*/
|
|
10
|
+
interface FlyoLanguageLink {
|
|
11
|
+
/** Locale shortcode, e.g. `"de"`. */
|
|
12
|
+
shortcode: string;
|
|
13
|
+
/** Full language name from the CMS translation, e.g. `"Deutsch"` (when available). */
|
|
14
|
+
name?: string;
|
|
15
|
+
/** Fully-resolved localized href, or `null` when this locale has no linked translation. */
|
|
16
|
+
href: string | null;
|
|
17
|
+
/** Localized page/entity title, when available. */
|
|
18
|
+
title?: string;
|
|
19
|
+
/** `true` when this entry is the currently active locale. */
|
|
20
|
+
isCurrent: boolean;
|
|
21
|
+
/** `true` when a linked translation actually exists for this locale. */
|
|
22
|
+
exists: boolean;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Map a page's or entity's `translation[]` into a switcher-ready array of typed
|
|
26
|
+
* links — the building block for a language switcher.
|
|
27
|
+
*
|
|
28
|
+
* Flyo only returns `translation` entries for languages that actually have a
|
|
29
|
+
* translation. Pass `options.locales` (e.g. `flyo.state.locales`) to also get an
|
|
30
|
+
* entry for every configured locale that is *missing* a translation — those come
|
|
31
|
+
* back as `{ href: null, exists: false }` so you can render a fallback (a disabled
|
|
32
|
+
* item, a link to the home page, …).
|
|
33
|
+
*
|
|
34
|
+
* Pure — no React or server-only APIs, so it is safe to call from server *or*
|
|
35
|
+
* client components.
|
|
36
|
+
*
|
|
37
|
+
* This is a pure *data* helper — it maps a `translation[]` to switcher links and
|
|
38
|
+
* renders nothing. **The Flyo route helpers already call it for you and publish
|
|
39
|
+
* the result**, so page and entity routes need no switcher code at all; the
|
|
40
|
+
* footer just reads the store (see `publishLanguageLinks` / `readLanguageLinks`
|
|
41
|
+
* in `@flyo/nitro-next/server`). Call this directly only when you publish links
|
|
42
|
+
* by hand from a raw `translation[]` — e.g. on a custom route Flyo doesn't
|
|
43
|
+
* resolve:
|
|
44
|
+
*
|
|
45
|
+
* ```tsx
|
|
46
|
+
* getLanguageLinks(translations, { currentLang, locales });
|
|
47
|
+
* ```
|
|
48
|
+
*
|
|
49
|
+
* **Render each link as a native `<a>`, not `next/link`'s `<Link>`.** A language
|
|
50
|
+
* switch must refresh the shared chrome — the localized nav, footer and
|
|
51
|
+
* `<html lang>` — that lives in the root layout. In the Next.js App Router, soft
|
|
52
|
+
* (client-side) navigation re-renders only the page segment, *not* shared
|
|
53
|
+
* layouts, so a `<Link>` would leave that chrome in the previous language while
|
|
54
|
+
* only the page body updates. A plain `<a>` triggers a full-document navigation,
|
|
55
|
+
* forcing a fresh server render in the new locale so every part updates. (Regular
|
|
56
|
+
* nav links can stay `<Link>` — the nav is identical within a language.)
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* ```tsx
|
|
60
|
+
* const links = getLanguageLinks(page.translation, {
|
|
61
|
+
* currentLang: lang,
|
|
62
|
+
* locales: flyo.state.locales,
|
|
63
|
+
* });
|
|
64
|
+
* // Native <a> (full-document nav) — NOT next/link, so shared layout chrome
|
|
65
|
+
* // re-renders in the new locale.
|
|
66
|
+
* // links.map(l => l.exists
|
|
67
|
+
* // ? <a key={l.shortcode} href={l.href!} aria-current={l.isCurrent || undefined}>{l.name ?? l.shortcode}</a>
|
|
68
|
+
* // : <span key={l.shortcode} aria-disabled>{l.shortcode}</span>)
|
|
69
|
+
* ```
|
|
70
|
+
*/
|
|
71
|
+
declare function getLanguageLinks(translations: Translation[] | undefined, options?: {
|
|
72
|
+
currentLang?: string;
|
|
73
|
+
locales?: string[];
|
|
74
|
+
}): FlyoLanguageLink[];
|
|
75
|
+
|
|
5
76
|
/**
|
|
6
77
|
* Check if running in production environment
|
|
7
78
|
*/
|
|
@@ -164,4 +235,4 @@ declare function EditableSection({ block, children, className, as: Tag, }: {
|
|
|
164
235
|
as?: React.ElementType;
|
|
165
236
|
}): react_jsx_runtime.JSX.Element;
|
|
166
237
|
|
|
167
|
-
export { type EditableBlock, EditableSection, FlyoCdnLoader, FlyoClientWrapper, FlyoMetric, FlyoWysiwyg, type WysiwygJson, type WysiwygNode, editable, isProd };
|
|
238
|
+
export { type EditableBlock, EditableSection, FlyoCdnLoader, FlyoClientWrapper, type FlyoLanguageLink, FlyoMetric, FlyoWysiwyg, type WysiwygJson, type WysiwygNode, editable, getLanguageLinks, isProd };
|
package/dist/client.js
CHANGED
|
@@ -28,11 +28,38 @@ __export(client_exports, {
|
|
|
28
28
|
FlyoMetric: () => FlyoMetric,
|
|
29
29
|
FlyoWysiwyg: () => FlyoWysiwyg,
|
|
30
30
|
editable: () => editable,
|
|
31
|
+
getLanguageLinks: () => getLanguageLinks,
|
|
31
32
|
isProd: () => isProd
|
|
32
33
|
});
|
|
33
34
|
module.exports = __toCommonJS(client_exports);
|
|
34
35
|
var import_react = require("react");
|
|
35
36
|
var import_nitro_js_bridge = require("@flyo/nitro-js-bridge");
|
|
37
|
+
|
|
38
|
+
// src/i18n.ts
|
|
39
|
+
function getLanguageLinks(translations, options) {
|
|
40
|
+
const currentLang = options?.currentLang;
|
|
41
|
+
const byShortcode = /* @__PURE__ */ new Map();
|
|
42
|
+
for (const t of translations ?? []) {
|
|
43
|
+
const shortcode = t.language?.shortcode;
|
|
44
|
+
if (shortcode) {
|
|
45
|
+
byShortcode.set(shortcode, t);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
const toLink = (shortcode, t) => ({
|
|
49
|
+
shortcode,
|
|
50
|
+
name: t?.language?.name,
|
|
51
|
+
href: t?.href ?? null,
|
|
52
|
+
title: t?.title,
|
|
53
|
+
isCurrent: shortcode === currentLang,
|
|
54
|
+
exists: t?.href != null
|
|
55
|
+
});
|
|
56
|
+
if (options?.locales && options.locales.length > 0) {
|
|
57
|
+
return options.locales.map((shortcode) => toLink(shortcode, byShortcode.get(shortcode)));
|
|
58
|
+
}
|
|
59
|
+
return (translations ?? []).filter((t) => t.language?.shortcode).map((t) => toLink(t.language.shortcode, t));
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// src/client.tsx
|
|
36
63
|
var import_jsx_runtime = require("react/jsx-runtime");
|
|
37
64
|
var FLYO_CDN_HOST = "storage.flyo.cloud";
|
|
38
65
|
var isProd = process.env.NODE_ENV === "production";
|
|
@@ -159,6 +186,7 @@ function EditableSection({
|
|
|
159
186
|
FlyoMetric,
|
|
160
187
|
FlyoWysiwyg,
|
|
161
188
|
editable,
|
|
189
|
+
getLanguageLinks,
|
|
162
190
|
isProd
|
|
163
191
|
});
|
|
164
192
|
//# sourceMappingURL=client.js.map
|
package/dist/client.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/client.tsx"],"sourcesContent":["'use client';\n\nimport { useEffect } from 'react';\nimport { highlightAndClick, wysiwyg, reload, scrollTo} from '@flyo/nitro-js-bridge';\nimport { Block, Entity } from \"@flyo/nitro-typescript\";\nimport type { ImageLoaderProps } from 'next/image';\n\nconst FLYO_CDN_HOST = 'storage.flyo.cloud';\n\n/**\n * Check if running in production environment\n */\nexport const isProd = process.env.NODE_ENV === 'production';\n\n/**\n * Type for WYSIWYG node structure\n */\nexport interface WysiwygNode {\n type: string;\n content?: WysiwygNode[];\n [key: string]: unknown;\n}\n\n/**\n * Type for WYSIWYG JSON that can be a node, array of nodes, or doc structure\n */\nexport type WysiwygJson = WysiwygNode | WysiwygNode[] | { type: 'doc'; content: WysiwygNode[] };\n\n/**\n * The minimal block shape `editable()` needs to wire live-editing.\n *\n * `editable()` reads only `uid`, so it deliberately accepts more than the full\n * {@link Block}. The per-block types generated from a project's OpenAPI schema\n * (e.g. `BlockHero`) are NOT structurally assignable to `Block`: their\n * `content`/`config`/`slots` carry an `_empty` marker that clashes with\n * `Block`'s index signatures (`{ [key: string]: BlockSlotValue }`). Typing\n * against the read-surface keeps both the generic `Block` and those generated\n * subtypes assignable, so callers don't need `as unknown as Block` casts.\n */\nexport type EditableBlock = Pick<Block, 'uid'>;\n\n/**\n * Helper function to get editable props\n */\nexport function editable(block: EditableBlock): { 'data-flyo-uid'?: string } {\n if (typeof block.uid === 'string' && block.uid.trim() !== '') {\n return { 'data-flyo-uid': block.uid };\n }\n return {};\n}\n\n/**\n * Internal client component that sets up live editing functionality\n */\nexport function FlyoClientWrapper({ \n children,\n}: { \n children: React.ReactNode;\n}) {\n useEffect(() => {\n if (typeof window === 'undefined') {\n return;\n }\n\n reload();\n \n scrollTo();\n\n const wireAll = () => {\n const elements = document.querySelectorAll('[data-flyo-uid]');\n elements.forEach((el) => {\n const uid = el.getAttribute('data-flyo-uid');\n if (uid && el instanceof HTMLElement) {\n highlightAndClick(uid, el);\n }\n });\n };\n\n wireAll();\n\n const observer = new MutationObserver((mutations) => {\n const hasRelevantChanges = mutations.some(mutation => \n Array.from(mutation.addedNodes).some(node => {\n if (node.nodeType === Node.ELEMENT_NODE) {\n const element = node as Element;\n return element.hasAttribute('data-flyo-uid') || \n element.querySelector('[data-flyo-uid]');\n }\n return false;\n })\n );\n\n if (hasRelevantChanges) {\n wireAll();\n }\n });\n\n observer.observe(document.body, {\n childList: true,\n subtree: true,\n });\n\n return () => {\n observer.disconnect();\n };\n }, []);\n\n return <>{children}</>;\n}\n\n/**\n * WYSIWYG component for rendering ProseMirror/TipTap JSON content\n * \n * Uses the `wysiwyg()` function from `@flyo/nitro-js-bridge` to convert\n * nodes to HTML. All consecutive non-custom nodes are joined into a single\n * HTML string so no extra wrapper `<div>` elements are added around each node.\n * \n * The component wraps all output in a single `<div>` with an optional\n * `className` (defaults to `\"wysiwyg\"`).\n * \n * @example\n * ```tsx\n * import { FlyoWysiwyg } from '@flyo/nitro-next/client';\n * import CustomImage from './CustomImage';\n * \n * export default function MyComponent({ block }) {\n * return (\n * <FlyoWysiwyg \n * json={block.content.json} \n * className=\"wysiwyg\"\n * components={{\n * image: CustomImage\n * }} \n * />\n * );\n * }\n * ```\n */\nexport function FlyoWysiwyg({\n json,\n className = 'wysiwyg',\n components = {},\n}: {\n json: WysiwygJson;\n className?: string;\n components?: Record<string, React.ComponentType<{ node: WysiwygNode }>>;\n}) {\n let nodes: WysiwygNode[] = [];\n\n if (json) {\n if (Array.isArray(json)) {\n nodes = json;\n } else if ('type' in json && json.type === 'doc' && Array.isArray(json.content)) {\n nodes = json.content;\n } else {\n nodes = [json as WysiwygNode];\n }\n }\n\n // If no custom components are provided, render all nodes as a single HTML block\n const hasCustomComponents = nodes.some((node) => components[node.type]);\n\n if (!hasCustomComponents) {\n const html = nodes.map((node) => wysiwyg(node)).join('');\n return <div className={className} dangerouslySetInnerHTML={{ __html: html }} />;\n }\n\n // When custom components are used, group consecutive non-custom nodes\n // into single HTML blocks to avoid extra wrapper elements.\n const groups: ({ type: 'custom'; component: React.ComponentType<{ node: WysiwygNode }>; node: WysiwygNode } | { type: 'html'; html: string })[] = [];\n\n for (const node of nodes) {\n const Component = components[node.type];\n if (Component) {\n groups.push({ type: 'custom', component: Component, node });\n } else {\n const html = wysiwyg(node);\n const last = groups[groups.length - 1];\n if (last && last.type === 'html') {\n last.html += html;\n } else {\n groups.push({ type: 'html', html });\n }\n }\n }\n\n return (\n <div className={className}>\n {groups.map((group, index) => {\n if (group.type === 'custom') {\n return <group.component key={index} node={group.node} />;\n }\n return <div key={index} dangerouslySetInnerHTML={{ __html: group.html }} />;\n })}\n </div>\n );\n}\n\n/**\n * Image loader for Flyo CDN that automatically handles image transformations.\n * Adds Flyo CDN host if not already present and applies width transformations.\n * \n * @param src - The image source URL (relative or absolute)\n * @param width - The desired width for the image\n * @returns Transformed image URL with Flyo CDN parameters\n * \n * @example\n * ```tsx\n * <Image\n * loader={FlyoCdnLoader}\n * src=\"me.png\"\n * alt=\"Picture\"\n * width={500}\n * height={500}\n * />\n * ```\n */\nexport function FlyoCdnLoader({ src, width }: ImageLoaderProps): string {\n let imageUrl = src;\n\n // If src doesn't contain the Flyo CDN host, prefix it\n if (!src.includes(FLYO_CDN_HOST)) {\n // Remove leading slash if present to avoid double slashes\n const cleanSrc = src.startsWith('/') ? src.slice(1) : src;\n imageUrl = `https://${FLYO_CDN_HOST}/${cleanSrc}`;\n }\n\n // Append Flyo CDN transformation parameters\n return `${imageUrl}/thumb/${width}xnull?format=webp`;\n}\n\n/**\n * FlyoMetric component for tracking entity metrics in production\n * \n * Automatically sends a metric tracking request to the Flyo API when:\n * - The environment is production (NODE_ENV === 'production')\n * - The entity has a metric API URL configured\n * \n * @param entity - The entity object containing entity_metric.api\n * \n * @example\n * ```tsx\n * import { FlyoMetric } from '@flyo/nitro-next/client';\n * \n * export default function BlogPost(props: RouteParams) {\n * return nitroEntityRoute(props, {\n * resolver,\n * render: (entity: Entity) => (\n * <>\n * <FlyoMetric entity={entity} />\n * <article>\n * <h1>{entity.entity?.entity_title}</h1>\n * </article>\n * </>\n * )\n * });\n * }\n * ```\n */\nexport function FlyoMetric({ entity }: { entity: Entity }) {\n useEffect(() => {\n // Only track metrics in production and if API URL is available\n if (isProd && entity?.entity?.entity_metric?.api) {\n fetch(entity.entity.entity_metric.api);\n }\n }, [entity]);\n\n // This component doesn't render anything\n return null;\n}\n\n/**\n * A thin client wrapper that applies `editable()` to a root element while\n * allowing server-rendered children (e.g. `NitroSlot`) to be passed in.\n *\n * In Next.js, a file marked `'use client'` turns all of its imports into\n * client modules, so you cannot import server-only components like\n * `NitroSlot` directly. The workaround is to keep the server part separate\n * and pass it into this client wrapper via `children`.\n *\n * @example\n * ```tsx\n * // components/HeroBanner.tsx (server component – no 'use client')\n * import { Block } from '@flyo/nitro-typescript';\n * import { NitroSlot } from '@flyo/nitro-next/server';\n * import { EditableSection } from '@flyo/nitro-next/client';\n *\n * export function HeroBanner({ block }: { block: Block }) {\n * return (\n * <EditableSection block={block} className=\"hero\">\n * <h2>{block?.content?.title}</h2>\n * <NitroSlot slot={block.slots?.content} />\n * </EditableSection>\n * );\n * }\n * ```\n */\nexport function EditableSection({\n block,\n children,\n className,\n as: Tag = 'section',\n}: {\n block: EditableBlock;\n children: React.ReactNode;\n className?: string;\n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S,UAAU;AAC3B,aAAO,4CAAC,MAAM,WAAN,EAA4B,MAAM,MAAM,QAAnB,KAAyB;AAAA,IACxD;AACA,WAAO,4CAAC,SAAgB,yBAAyB,EAAE,QAAQ,MAAM,KAAK,KAArD,KAAwD;AAAA,EAC3E,CAAC,GACH;AAEJ;AAqBO,SAAS,cAAc,EAAE,KAAK,MAAM,GAA6B;AACtE,MAAI,WAAW;AAGf,MAAI,CAAC,IAAI,SAAS,aAAa,GAAG;AAEhC,UAAM,WAAW,IAAI,WAAW,GAAG,IAAI,IAAI,MAAM,CAAC,IAAI;AACtD,eAAW,WAAW,aAAa,IAAI,QAAQ;AAAA,EACjD;AAGA,SAAO,GAAG,QAAQ,UAAU,KAAK;AACnC;AA8BO,SAAS,WAAW,EAAE,OAAO,GAAuB;AACzD,8BAAU,MAAM;AAEd,QAAI,UAAU,QAAQ,QAAQ,eAAe,KAAK;AAChD,YAAM,OAAO,OAAO,cAAc,GAAG;AAAA,IACvC;AAAA,EACF,GAAG,CAAC,MAAM,CAAC;AAGX,SAAO;AACT;AA4BO,SAAS,gBAAgB;AAAA,EAC9B;AAAA,EACA;AAAA,EACA;AAAA,EACA,IAAI,MAAM;AACZ,GAKG;AACD,SACE,4CAAC,OAAK,GAAG,SAAS,KAAK,GAAG,WACvB,UACH;AAEJ;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../src/client.tsx","../src/i18n.ts"],"sourcesContent":["'use client';\n\nimport { useEffect } from 'react';\nimport { highlightAndClick, wysiwyg, reload, scrollTo} from '@flyo/nitro-js-bridge';\nimport { Block, Entity } from \"@flyo/nitro-typescript\";\nimport type { ImageLoaderProps } from 'next/image';\n\n// Framework-agnostic language-links helper — re-exported here so client\n// components can build a language switcher from a page/entity `translation[]`\n// without importing from `/server` (which would pull server-only code into the\n// client bundle).\nexport { getLanguageLinks } from './i18n';\nexport type { FlyoLanguageLink } from './i18n';\n\nconst FLYO_CDN_HOST = 'storage.flyo.cloud';\n\n/**\n * Check if running in production environment\n */\nexport const isProd = process.env.NODE_ENV === 'production';\n\n/**\n * Type for WYSIWYG node structure\n */\nexport interface WysiwygNode {\n type: string;\n content?: WysiwygNode[];\n [key: string]: unknown;\n}\n\n/**\n * Type for WYSIWYG JSON that can be a node, array of nodes, or doc structure\n */\nexport type WysiwygJson = WysiwygNode | WysiwygNode[] | { type: 'doc'; content: WysiwygNode[] };\n\n/**\n * The minimal block shape `editable()` needs to wire live-editing.\n *\n * `editable()` reads only `uid`, so it deliberately accepts more than the full\n * {@link Block}. The per-block types generated from a project's OpenAPI schema\n * (e.g. `BlockHero`) are NOT structurally assignable to `Block`: their\n * `content`/`config`/`slots` carry an `_empty` marker that clashes with\n * `Block`'s index signatures (`{ [key: string]: BlockSlotValue }`). Typing\n * against the read-surface keeps both the generic `Block` and those generated\n * subtypes assignable, so callers don't need `as unknown as Block` casts.\n */\nexport type EditableBlock = Pick<Block, 'uid'>;\n\n/**\n * Helper function to get editable props\n */\nexport function editable(block: EditableBlock): { 'data-flyo-uid'?: string } {\n if (typeof block.uid === 'string' && block.uid.trim() !== '') {\n return { 'data-flyo-uid': block.uid };\n }\n return {};\n}\n\n/**\n * Internal client component that sets up live editing functionality\n */\nexport function FlyoClientWrapper({ \n children,\n}: { \n children: React.ReactNode;\n}) {\n useEffect(() => {\n if (typeof window === 'undefined') {\n return;\n }\n\n reload();\n \n scrollTo();\n\n const wireAll = () => {\n const elements = document.querySelectorAll('[data-flyo-uid]');\n elements.forEach((el) => {\n const uid = el.getAttribute('data-flyo-uid');\n if (uid && el instanceof HTMLElement) {\n highlightAndClick(uid, el);\n }\n });\n };\n\n wireAll();\n\n const observer = new MutationObserver((mutations) => {\n const hasRelevantChanges = mutations.some(mutation => \n Array.from(mutation.addedNodes).some(node => {\n if (node.nodeType === Node.ELEMENT_NODE) {\n const element = node as Element;\n return element.hasAttribute('data-flyo-uid') || \n element.querySelector('[data-flyo-uid]');\n }\n return false;\n })\n );\n\n if (hasRelevantChanges) {\n wireAll();\n }\n });\n\n observer.observe(document.body, {\n childList: true,\n subtree: true,\n });\n\n return () => {\n observer.disconnect();\n };\n }, []);\n\n return <>{children}</>;\n}\n\n/**\n * WYSIWYG component for rendering ProseMirror/TipTap JSON content\n * \n * Uses the `wysiwyg()` function from `@flyo/nitro-js-bridge` to convert\n * nodes to HTML. All consecutive non-custom nodes are joined into a single\n * HTML string so no extra wrapper `<div>` elements are added around each node.\n * \n * The component wraps all output in a single `<div>` with an optional\n * `className` (defaults to `\"wysiwyg\"`).\n * \n * @example\n * ```tsx\n * import { FlyoWysiwyg } from '@flyo/nitro-next/client';\n * import CustomImage from './CustomImage';\n * \n * export default function MyComponent({ block }) {\n * return (\n * <FlyoWysiwyg \n * json={block.content.json} \n * className=\"wysiwyg\"\n * components={{\n * image: CustomImage\n * }} \n * />\n * );\n * }\n * ```\n */\nexport function FlyoWysiwyg({\n json,\n className = 'wysiwyg',\n components = {},\n}: {\n json: WysiwygJson;\n className?: string;\n components?: Record<string, React.ComponentType<{ node: WysiwygNode }>>;\n}) {\n let nodes: WysiwygNode[] = [];\n\n if (json) {\n if (Array.isArray(json)) {\n nodes = json;\n } else if ('type' in json && json.type === 'doc' && Array.isArray(json.content)) {\n nodes = json.content;\n } else {\n nodes = [json as WysiwygNode];\n }\n }\n\n // If no custom components are provided, render all nodes as a single HTML block\n const hasCustomComponents = nodes.some((node) => components[node.type]);\n\n if (!hasCustomComponents) {\n const html = nodes.map((node) => wysiwyg(node)).join('');\n return <div className={className} dangerouslySetInnerHTML={{ __html: html }} />;\n }\n\n // When custom components are used, group consecutive non-custom nodes\n // into single HTML blocks to avoid extra wrapper elements.\n const groups: ({ type: 'custom'; component: React.ComponentType<{ node: WysiwygNode }>; node: WysiwygNode } | { type: 'html'; html: string })[] = [];\n\n for (const node of nodes) {\n const Component = components[node.type];\n if (Component) {\n groups.push({ type: 'custom', component: Component, node });\n } else {\n const html = wysiwyg(node);\n const last = groups[groups.length - 1];\n if (last && last.type === 'html') {\n last.html += html;\n } else {\n groups.push({ type: 'html', html });\n }\n }\n }\n\n return (\n <div className={className}>\n {groups.map((group, index) => {\n if (group.type === 'custom') {\n return <group.component key={index} node={group.node} />;\n }\n return <div key={index} dangerouslySetInnerHTML={{ __html: group.html }} />;\n })}\n </div>\n );\n}\n\n/**\n * Image loader for Flyo CDN that automatically handles image transformations.\n * Adds Flyo CDN host if not already present and applies width transformations.\n * \n * @param src - The image source URL (relative or absolute)\n * @param width - The desired width for the image\n * @returns Transformed image URL with Flyo CDN parameters\n * \n * @example\n * ```tsx\n * <Image\n * loader={FlyoCdnLoader}\n * src=\"me.png\"\n * alt=\"Picture\"\n * width={500}\n * height={500}\n * />\n * ```\n */\nexport function FlyoCdnLoader({ src, width }: ImageLoaderProps): string {\n let imageUrl = src;\n\n // If src doesn't contain the Flyo CDN host, prefix it\n if (!src.includes(FLYO_CDN_HOST)) {\n // Remove leading slash if present to avoid double slashes\n const cleanSrc = src.startsWith('/') ? src.slice(1) : src;\n imageUrl = `https://${FLYO_CDN_HOST}/${cleanSrc}`;\n }\n\n // Append Flyo CDN transformation parameters\n return `${imageUrl}/thumb/${width}xnull?format=webp`;\n}\n\n/**\n * FlyoMetric component for tracking entity metrics in production\n * \n * Automatically sends a metric tracking request to the Flyo API when:\n * - The environment is production (NODE_ENV === 'production')\n * - The entity has a metric API URL configured\n * \n * @param entity - The entity object containing entity_metric.api\n * \n * @example\n * ```tsx\n * import { FlyoMetric } from '@flyo/nitro-next/client';\n * \n * export default function BlogPost(props: RouteParams) {\n * return nitroEntityRoute(props, {\n * resolver,\n * render: (entity: Entity) => (\n * <>\n * <FlyoMetric entity={entity} />\n * <article>\n * <h1>{entity.entity?.entity_title}</h1>\n * </article>\n * </>\n * )\n * });\n * }\n * ```\n */\nexport function FlyoMetric({ entity }: { entity: Entity }) {\n useEffect(() => {\n // Only track metrics in production and if API URL is available\n if (isProd && entity?.entity?.entity_metric?.api) {\n fetch(entity.entity.entity_metric.api);\n }\n }, [entity]);\n\n // This component doesn't render anything\n return null;\n}\n\n/**\n * A thin client wrapper that applies `editable()` to a root element while\n * allowing server-rendered children (e.g. `NitroSlot`) to be passed in.\n *\n * In Next.js, a file marked `'use client'` turns all of its imports into\n * client modules, so you cannot import server-only components like\n * `NitroSlot` directly. The workaround is to keep the server part separate\n * and pass it into this client wrapper via `children`.\n *\n * @example\n * ```tsx\n * // components/HeroBanner.tsx (server component – no 'use client')\n * import { Block } from '@flyo/nitro-typescript';\n * import { NitroSlot } from '@flyo/nitro-next/server';\n * import { EditableSection } from '@flyo/nitro-next/client';\n *\n * export function HeroBanner({ block }: { block: Block }) {\n * return (\n * <EditableSection block={block} className=\"hero\">\n * <h2>{block?.content?.title}</h2>\n * <NitroSlot slot={block.slots?.content} />\n * </EditableSection>\n * );\n * }\n * ```\n */\nexport function EditableSection({\n block,\n children,\n className,\n as: Tag = 'section',\n}: {\n block: EditableBlock;\n children: React.ReactNode;\n className?: string;\n as?: React.ElementType;\n}) {\n return (\n <Tag {...editable(block)} className={className}>\n {children}\n </Tag>\n );\n}\n","import type { Translation } from '@flyo/nitro-typescript';\n\n/**\n * A single language option for a language switcher, derived from a page's or\n * entity's `translation[]`. Framework-agnostic plain data — render it however\n * you like.\n */\nexport interface FlyoLanguageLink {\n /** Locale shortcode, e.g. `\"de\"`. */\n shortcode: string;\n /** Full language name from the CMS translation, e.g. `\"Deutsch\"` (when available). */\n name?: string;\n /** Fully-resolved localized href, or `null` when this locale has no linked translation. */\n href: string | null;\n /** Localized page/entity title, when available. */\n title?: string;\n /** `true` when this entry is the currently active locale. */\n isCurrent: boolean;\n /** `true` when a linked translation actually exists for this locale. */\n exists: boolean;\n}\n\n/**\n * Map a page's or entity's `translation[]` into a switcher-ready array of typed\n * links — the building block for a language switcher.\n *\n * Flyo only returns `translation` entries for languages that actually have a\n * translation. Pass `options.locales` (e.g. `flyo.state.locales`) to also get an\n * entry for every configured locale that is *missing* a translation — those come\n * back as `{ href: null, exists: false }` so you can render a fallback (a disabled\n * item, a link to the home page, …).\n *\n * Pure — no React or server-only APIs, so it is safe to call from server *or*\n * client components.\n *\n * This is a pure *data* helper — it maps a `translation[]` to switcher links and\n * renders nothing. **The Flyo route helpers already call it for you and publish\n * the result**, so page and entity routes need no switcher code at all; the\n * footer just reads the store (see `publishLanguageLinks` / `readLanguageLinks`\n * in `@flyo/nitro-next/server`). Call this directly only when you publish links\n * by hand from a raw `translation[]` — e.g. on a custom route Flyo doesn't\n * resolve:\n *\n * ```tsx\n * getLanguageLinks(translations, { currentLang, locales });\n * ```\n *\n * **Render each link as a native `<a>`, not `next/link`'s `<Link>`.** A language\n * switch must refresh the shared chrome — the localized nav, footer and\n * `<html lang>` — that lives in the root layout. In the Next.js App Router, soft\n * (client-side) navigation re-renders only the page segment, *not* shared\n * layouts, so a `<Link>` would leave that chrome in the previous language while\n * only the page body updates. A plain `<a>` triggers a full-document navigation,\n * forcing a fresh server render in the new locale so every part updates. (Regular\n * nav links can stay `<Link>` — the nav is identical within a language.)\n *\n * @example\n * ```tsx\n * const links = getLanguageLinks(page.translation, {\n * currentLang: lang,\n * locales: flyo.state.locales,\n * });\n * // Native <a> (full-document nav) — NOT next/link, so shared layout chrome\n * // re-renders in the new locale.\n * // links.map(l => l.exists\n * // ? <a key={l.shortcode} href={l.href!} aria-current={l.isCurrent || undefined}>{l.name ?? l.shortcode}</a>\n * // : <span key={l.shortcode} aria-disabled>{l.shortcode}</span>)\n * ```\n */\nexport function getLanguageLinks(\n translations: Translation[] | undefined,\n options?: { currentLang?: string; locales?: string[] },\n): FlyoLanguageLink[] {\n const currentLang = options?.currentLang;\n\n const byShortcode = new Map<string, Translation>();\n for (const t of translations ?? []) {\n const shortcode = t.language?.shortcode;\n if (shortcode) {\n byShortcode.set(shortcode, t);\n }\n }\n\n const toLink = (shortcode: string, t: Translation | undefined): FlyoLanguageLink => ({\n shortcode,\n name: t?.language?.name,\n href: t?.href ?? null,\n title: t?.title,\n isCurrent: shortcode === currentLang,\n exists: t?.href != null,\n });\n\n // When the full set of locales is known, emit an entry for every one so\n // callers can render fallbacks for languages that have no translation.\n if (options?.locales && options.locales.length > 0) {\n return options.locales.map((shortcode) => toLink(shortcode, byShortcode.get(shortcode)));\n }\n\n // Otherwise, just surface the translations that exist.\n return (translations ?? [])\n .filter((t) => t.language?.shortcode)\n .map((t) => toLink(t.language!.shortcode!, t));\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAEA,mBAA0B;AAC1B,6BAA4D;;;ACkErD,SAAS,iBACd,cACA,SACoB;AACpB,QAAM,cAAc,SAAS;AAE7B,QAAM,cAAc,oBAAI,IAAyB;AACjD,aAAW,KAAK,gBAAgB,CAAC,GAAG;AAClC,UAAM,YAAY,EAAE,UAAU;AAC9B,QAAI,WAAW;AACb,kBAAY,IAAI,WAAW,CAAC;AAAA,IAC9B;AAAA,EACF;AAEA,QAAM,SAAS,CAAC,WAAmB,OAAkD;AAAA,IACnF;AAAA,IACA,MAAM,GAAG,UAAU;AAAA,IACnB,MAAM,GAAG,QAAQ;AAAA,IACjB,OAAO,GAAG;AAAA,IACV,WAAW,cAAc;AAAA,IACzB,QAAQ,GAAG,QAAQ;AAAA,EACrB;AAIA,MAAI,SAAS,WAAW,QAAQ,QAAQ,SAAS,GAAG;AAClD,WAAO,QAAQ,QAAQ,IAAI,CAAC,cAAc,OAAO,WAAW,YAAY,IAAI,SAAS,CAAC,CAAC;AAAA,EACzF;AAGA,UAAQ,gBAAgB,CAAC,GACtB,OAAO,CAAC,MAAM,EAAE,UAAU,SAAS,EACnC,IAAI,CAAC,MAAM,OAAO,EAAE,SAAU,WAAY,CAAC,CAAC;AACjD;;;ADYS;AApGT,IAAM,gBAAgB;AAKf,IAAM,SAAS,QAAQ,IAAI,aAAa;AAgCxC,SAAS,SAAS,OAAoD;AAC3E,MAAI,OAAO,MAAM,QAAQ,YAAY,MAAM,IAAI,KAAK,MAAM,IAAI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AACtD,eAAW,WAAW,aAAa,IAAI,QAAQ;AAAA,EACjD;AAGA,SAAO,GAAG,QAAQ,UAAU,KAAK;AACnC;AA8BO,SAAS,WAAW,EAAE,OAAO,GAAuB;AACzD,8BAAU,MAAM;AAEd,QAAI,UAAU,QAAQ,QAAQ,eAAe,KAAK;AAChD,YAAM,OAAO,OAAO,cAAc,GAAG;AAAA,IACvC;AAAA,EACF,GAAG,CAAC,MAAM,CAAC;AAGX,SAAO;AACT;AA4BO,SAAS,gBAAgB;AAAA,EAC9B;AAAA,EACA;AAAA,EACA;AAAA,EACA,IAAI,MAAM;AACZ,GAKG;AACD,SACE,4CAAC,OAAK,GAAG,SAAS,KAAK,GAAG,WACvB,UACH;AAEJ;","names":[]}
|
package/dist/client.mjs
CHANGED
|
@@ -4,6 +4,32 @@
|
|
|
4
4
|
// src/client.tsx
|
|
5
5
|
import { useEffect } from "react";
|
|
6
6
|
import { highlightAndClick, wysiwyg, reload, scrollTo } from "@flyo/nitro-js-bridge";
|
|
7
|
+
|
|
8
|
+
// src/i18n.ts
|
|
9
|
+
function getLanguageLinks(translations, options) {
|
|
10
|
+
const currentLang = options?.currentLang;
|
|
11
|
+
const byShortcode = /* @__PURE__ */ new Map();
|
|
12
|
+
for (const t of translations ?? []) {
|
|
13
|
+
const shortcode = t.language?.shortcode;
|
|
14
|
+
if (shortcode) {
|
|
15
|
+
byShortcode.set(shortcode, t);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
const toLink = (shortcode, t) => ({
|
|
19
|
+
shortcode,
|
|
20
|
+
name: t?.language?.name,
|
|
21
|
+
href: t?.href ?? null,
|
|
22
|
+
title: t?.title,
|
|
23
|
+
isCurrent: shortcode === currentLang,
|
|
24
|
+
exists: t?.href != null
|
|
25
|
+
});
|
|
26
|
+
if (options?.locales && options.locales.length > 0) {
|
|
27
|
+
return options.locales.map((shortcode) => toLink(shortcode, byShortcode.get(shortcode)));
|
|
28
|
+
}
|
|
29
|
+
return (translations ?? []).filter((t) => t.language?.shortcode).map((t) => toLink(t.language.shortcode, t));
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// src/client.tsx
|
|
7
33
|
import { Fragment, jsx } from "react/jsx-runtime";
|
|
8
34
|
var FLYO_CDN_HOST = "storage.flyo.cloud";
|
|
9
35
|
var isProd = process.env.NODE_ENV === "production";
|
|
@@ -129,6 +155,7 @@ export {
|
|
|
129
155
|
FlyoMetric,
|
|
130
156
|
FlyoWysiwyg,
|
|
131
157
|
editable,
|
|
158
|
+
getLanguageLinks,
|
|
132
159
|
isProd
|
|
133
160
|
};
|
|
134
161
|
//# sourceMappingURL=client.mjs.map
|
package/dist/client.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/client.tsx"],"sourcesContent":["'use client';\n\nimport { useEffect } from 'react';\nimport { highlightAndClick, wysiwyg, reload, scrollTo} from '@flyo/nitro-js-bridge';\nimport { Block, Entity } from \"@flyo/nitro-typescript\";\nimport type { ImageLoaderProps } from 'next/image';\n\nconst FLYO_CDN_HOST = 'storage.flyo.cloud';\n\n/**\n * Check if running in production environment\n */\nexport const isProd = process.env.NODE_ENV === 'production';\n\n/**\n * Type for WYSIWYG node structure\n */\nexport interface WysiwygNode {\n type: string;\n content?: WysiwygNode[];\n [key: string]: unknown;\n}\n\n/**\n * Type for WYSIWYG JSON that can be a node, array of nodes, or doc structure\n */\nexport type WysiwygJson = WysiwygNode | WysiwygNode[] | { type: 'doc'; content: WysiwygNode[] };\n\n/**\n * The minimal block shape `editable()` needs to wire live-editing.\n *\n * `editable()` reads only `uid`, so it deliberately accepts more than the full\n * {@link Block}. The per-block types generated from a project's OpenAPI schema\n * (e.g. `BlockHero`) are NOT structurally assignable to `Block`: their\n * `content`/`config`/`slots` carry an `_empty` marker that clashes with\n * `Block`'s index signatures (`{ [key: string]: BlockSlotValue }`). Typing\n * against the read-surface keeps both the generic `Block` and those generated\n * subtypes assignable, so callers don't need `as unknown as Block` casts.\n */\nexport type EditableBlock = Pick<Block, 'uid'>;\n\n/**\n * Helper function to get editable props\n */\nexport function editable(block: EditableBlock): { 'data-flyo-uid'?: string } {\n if (typeof block.uid === 'string' && block.uid.trim() !== '') {\n return { 'data-flyo-uid': block.uid };\n }\n return {};\n}\n\n/**\n * Internal client component that sets up live editing functionality\n */\nexport function FlyoClientWrapper({ \n children,\n}: { \n children: React.ReactNode;\n}) {\n useEffect(() => {\n if (typeof window === 'undefined') {\n return;\n }\n\n reload();\n \n scrollTo();\n\n const wireAll = () => {\n const elements = document.querySelectorAll('[data-flyo-uid]');\n elements.forEach((el) => {\n const uid = el.getAttribute('data-flyo-uid');\n if (uid && el instanceof HTMLElement) {\n highlightAndClick(uid, el);\n }\n });\n };\n\n wireAll();\n\n const observer = new MutationObserver((mutations) => {\n const hasRelevantChanges = mutations.some(mutation => \n Array.from(mutation.addedNodes).some(node => {\n if (node.nodeType === Node.ELEMENT_NODE) {\n const element = node as Element;\n return element.hasAttribute('data-flyo-uid') || \n element.querySelector('[data-flyo-uid]');\n }\n return false;\n })\n );\n\n if (hasRelevantChanges) {\n wireAll();\n }\n });\n\n observer.observe(document.body, {\n childList: true,\n subtree: true,\n });\n\n return () => {\n observer.disconnect();\n };\n }, []);\n\n return <>{children}</>;\n}\n\n/**\n * WYSIWYG component for rendering ProseMirror/TipTap JSON content\n * \n * Uses the `wysiwyg()` function from `@flyo/nitro-js-bridge` to convert\n * nodes to HTML. All consecutive non-custom nodes are joined into a single\n * HTML string so no extra wrapper `<div>` elements are added around each node.\n * \n * The component wraps all output in a single `<div>` with an optional\n * `className` (defaults to `\"wysiwyg\"`).\n * \n * @example\n * ```tsx\n * import { FlyoWysiwyg } from '@flyo/nitro-next/client';\n * import CustomImage from './CustomImage';\n * \n * export default function MyComponent({ block }) {\n * return (\n * <FlyoWysiwyg \n * json={block.content.json} \n * className=\"wysiwyg\"\n * components={{\n * image: CustomImage\n * }} \n * />\n * );\n * }\n * ```\n */\nexport function FlyoWysiwyg({\n json,\n className = 'wysiwyg',\n components = {},\n}: {\n json: WysiwygJson;\n className?: string;\n components?: Record<string, React.ComponentType<{ node: WysiwygNode }>>;\n}) {\n let nodes: WysiwygNode[] = [];\n\n if (json) {\n if (Array.isArray(json)) {\n nodes = json;\n } else if ('type' in json && json.type === 'doc' && Array.isArray(json.content)) {\n nodes = json.content;\n } else {\n nodes = [json as WysiwygNode];\n }\n }\n\n // If no custom components are provided, render all nodes as a single HTML block\n const hasCustomComponents = nodes.some((node) => components[node.type]);\n\n if (!hasCustomComponents) {\n const html = nodes.map((node) => wysiwyg(node)).join('');\n return <div className={className} dangerouslySetInnerHTML={{ __html: html }} />;\n }\n\n // When custom components are used, group consecutive non-custom nodes\n // into single HTML blocks to avoid extra wrapper elements.\n const groups: ({ type: 'custom'; component: React.ComponentType<{ node: WysiwygNode }>; node: WysiwygNode } | { type: 'html'; html: string })[] = [];\n\n for (const node of nodes) {\n const Component = components[node.type];\n if (Component) {\n groups.push({ type: 'custom', component: Component, node });\n } else {\n const html = wysiwyg(node);\n const last = groups[groups.length - 1];\n if (last && last.type === 'html') {\n last.html += html;\n } else {\n groups.push({ type: 'html', html });\n }\n }\n }\n\n return (\n <div className={className}>\n {groups.map((group, index) => {\n if (group.type === 'custom') {\n return <group.component key={index} node={group.node} />;\n }\n return <div key={index} dangerouslySetInnerHTML={{ __html: group.html }} />;\n })}\n </div>\n );\n}\n\n/**\n * Image loader for Flyo CDN that automatically handles image transformations.\n * Adds Flyo CDN host if not already present and applies width transformations.\n * \n * @param src - The image source URL (relative or absolute)\n * @param width - The desired width for the image\n * @returns Transformed image URL with Flyo CDN parameters\n * \n * @example\n * ```tsx\n * <Image\n * loader={FlyoCdnLoader}\n * src=\"me.png\"\n * alt=\"Picture\"\n * width={500}\n * height={500}\n * />\n * ```\n */\nexport function FlyoCdnLoader({ src, width }: ImageLoaderProps): string {\n let imageUrl = src;\n\n // If src doesn't contain the Flyo CDN host, prefix it\n if (!src.includes(FLYO_CDN_HOST)) {\n // Remove leading slash if present to avoid double slashes\n const cleanSrc = src.startsWith('/') ? src.slice(1) : src;\n imageUrl = `https://${FLYO_CDN_HOST}/${cleanSrc}`;\n }\n\n // Append Flyo CDN transformation parameters\n return `${imageUrl}/thumb/${width}xnull?format=webp`;\n}\n\n/**\n * FlyoMetric component for tracking entity metrics in production\n * \n * Automatically sends a metric tracking request to the Flyo API when:\n * - The environment is production (NODE_ENV === 'production')\n * - The entity has a metric API URL configured\n * \n * @param entity - The entity object containing entity_metric.api\n * \n * @example\n * ```tsx\n * import { FlyoMetric } from '@flyo/nitro-next/client';\n * \n * export default function BlogPost(props: RouteParams) {\n * return nitroEntityRoute(props, {\n * resolver,\n * render: (entity: Entity) => (\n * <>\n * <FlyoMetric entity={entity} />\n * <article>\n * <h1>{entity.entity?.entity_title}</h1>\n * </article>\n * </>\n * )\n * });\n * }\n * ```\n */\nexport function FlyoMetric({ entity }: { entity: Entity }) {\n useEffect(() => {\n // Only track metrics in production and if API URL is available\n if (isProd && entity?.entity?.entity_metric?.api) {\n fetch(entity.entity.entity_metric.api);\n }\n }, [entity]);\n\n // This component doesn't render anything\n return null;\n}\n\n/**\n * A thin client wrapper that applies `editable()` to a root element while\n * allowing server-rendered children (e.g. `NitroSlot`) to be passed in.\n *\n * In Next.js, a file marked `'use client'` turns all of its imports into\n * client modules, so you cannot import server-only components like\n * `NitroSlot` directly. The workaround is to keep the server part separate\n * and pass it into this client wrapper via `children`.\n *\n * @example\n * ```tsx\n * // components/HeroBanner.tsx (server component – no 'use client')\n * import { Block } from '@flyo/nitro-typescript';\n * import { NitroSlot } from '@flyo/nitro-next/server';\n * import { EditableSection } from '@flyo/nitro-next/client';\n *\n * export function HeroBanner({ block }: { block: Block }) {\n * return (\n * <EditableSection block={block} className=\"hero\">\n * <h2>{block?.content?.title}</h2>\n * <NitroSlot slot={block.slots?.content} />\n * </EditableSection>\n * );\n * }\n * ```\n */\nexport function EditableSection({\n block,\n children,\n className,\n as: Tag = 'section',\n}: {\n block: EditableBlock;\n children: React.ReactNode;\n className?: string;\n as?: React.ElementType;\n}) {\n return (\n <Tag {...editable(block)} className={className}>\n {children}\n </Tag>\n );\n}\n"],"mappings":";;;;AAEA,SAAS,iBAAiB;AAC1B,SAAS,mBAAmB,SAAS,QAAQ,gBAAe;AAwGnD;AApGT,IAAM,gBAAgB;AAKf,IAAM,SAAS,QAAQ,IAAI,aAAa;AAgCxC,SAAS,SAAS,OAAoD;AAC3E,MAAI,OAAO,MAAM,QAAQ,YAAY,MAAM,IAAI,KAAK,MAAM,IAAI;AAC5D,WAAO,EAAE,iBAAiB,MAAM,IAAI;AAAA,EACtC;AACA,SAAO,CAAC;AACV;AAKO,SAAS,kBAAkB;AAAA,EAChC;AACF,GAEG;AACD,YAAU,MAAM;AACd,QAAI,OAAO,WAAW,aAAa;AACjC;AAAA,IACF;AAEA,WAAO;AAEP,aAAS;AAET,UAAM,UAAU,MAAM;AACpB,YAAM,WAAW,SAAS,iBAAiB,iBAAiB;AAC5D,eAAS,QAAQ,CAAC,OAAO;AACvB,cAAM,MAAM,GAAG,aAAa,eAAe;AAC3C,YAAI,OAAO,cAAc,aAAa;AACpC,4BAAkB,KAAK,EAAE;AAAA,QAC3B;AAAA,MACF,CAAC;AAAA,IACH;AAEA,YAAQ;AAER,UAAM,WAAW,IAAI,iBAAiB,CAAC,cAAc;AACnD,YAAM,qBAAqB,UAAU;AAAA,QAAK,cACxC,MAAM,KAAK,SAAS,UAAU,EAAE,KAAK,UAAQ;AAC3C,cAAI,KAAK,aAAa,KAAK,cAAc;AACvC,kBAAM,UAAU;AAChB,mBAAO,QAAQ,aAAa,eAAe,KACpC,QAAQ,cAAc,iBAAiB;AAAA,UAChD;AACA,iBAAO;AAAA,QACT,CAAC;AAAA,MACH;AAEA,UAAI,oBAAoB;AACtB,gBAAQ;AAAA,MACV;AAAA,IACF,CAAC;AAED,aAAS,QAAQ,SAAS,MAAM;AAAA,MAC9B,WAAW;AAAA,MACX,SAAS;AAAA,IACX,C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AC,MAAM,WAAN,EAA4B,MAAM,MAAM,QAAnB,KAAyB;AAAA,IACxD;AACA,WAAO,oBAAC,SAAgB,yBAAyB,EAAE,QAAQ,MAAM,KAAK,KAArD,KAAwD;AAAA,EAC3E,CAAC,GACH;AAEJ;AAqBO,SAAS,cAAc,EAAE,KAAK,MAAM,GAA6B;AACtE,MAAI,WAAW;AAGf,MAAI,CAAC,IAAI,SAAS,aAAa,GAAG;AAEhC,UAAM,WAAW,IAAI,WAAW,GAAG,IAAI,IAAI,MAAM,CAAC,IAAI;AACtD,eAAW,WAAW,aAAa,IAAI,QAAQ;AAAA,EACjD;AAGA,SAAO,GAAG,QAAQ,UAAU,KAAK;AACnC;AA8BO,SAAS,WAAW,EAAE,OAAO,GAAuB;AACzD,YAAU,MAAM;AAEd,QAAI,UAAU,QAAQ,QAAQ,eAAe,KAAK;AAChD,YAAM,OAAO,OAAO,cAAc,GAAG;AAAA,IACvC;AAAA,EACF,GAAG,CAAC,MAAM,CAAC;AAGX,SAAO;AACT;AA4BO,SAAS,gBAAgB;AAAA,EAC9B;AAAA,EACA;AAAA,EACA;AAAA,EACA,IAAI,MAAM;AACZ,GAKG;AACD,SACE,oBAAC,OAAK,GAAG,SAAS,KAAK,GAAG,WACvB,UACH;AAEJ;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../src/client.tsx","../src/i18n.ts"],"sourcesContent":["'use client';\n\nimport { useEffect } from 'react';\nimport { highlightAndClick, wysiwyg, reload, scrollTo} from '@flyo/nitro-js-bridge';\nimport { Block, Entity } from \"@flyo/nitro-typescript\";\nimport type { ImageLoaderProps } from 'next/image';\n\n// Framework-agnostic language-links helper — re-exported here so client\n// components can build a language switcher from a page/entity `translation[]`\n// without importing from `/server` (which would pull server-only code into the\n// client bundle).\nexport { getLanguageLinks } from './i18n';\nexport type { FlyoLanguageLink } from './i18n';\n\nconst FLYO_CDN_HOST = 'storage.flyo.cloud';\n\n/**\n * Check if running in production environment\n */\nexport const isProd = process.env.NODE_ENV === 'production';\n\n/**\n * Type for WYSIWYG node structure\n */\nexport interface WysiwygNode {\n type: string;\n content?: WysiwygNode[];\n [key: string]: unknown;\n}\n\n/**\n * Type for WYSIWYG JSON that can be a node, array of nodes, or doc structure\n */\nexport type WysiwygJson = WysiwygNode | WysiwygNode[] | { type: 'doc'; content: WysiwygNode[] };\n\n/**\n * The minimal block shape `editable()` needs to wire live-editing.\n *\n * `editable()` reads only `uid`, so it deliberately accepts more than the full\n * {@link Block}. The per-block types generated from a project's OpenAPI schema\n * (e.g. `BlockHero`) are NOT structurally assignable to `Block`: their\n * `content`/`config`/`slots` carry an `_empty` marker that clashes with\n * `Block`'s index signatures (`{ [key: string]: BlockSlotValue }`). Typing\n * against the read-surface keeps both the generic `Block` and those generated\n * subtypes assignable, so callers don't need `as unknown as Block` casts.\n */\nexport type EditableBlock = Pick<Block, 'uid'>;\n\n/**\n * Helper function to get editable props\n */\nexport function editable(block: EditableBlock): { 'data-flyo-uid'?: string } {\n if (typeof block.uid === 'string' && block.uid.trim() !== '') {\n return { 'data-flyo-uid': block.uid };\n }\n return {};\n}\n\n/**\n * Internal client component that sets up live editing functionality\n */\nexport function FlyoClientWrapper({ \n children,\n}: { \n children: React.ReactNode;\n}) {\n useEffect(() => {\n if (typeof window === 'undefined') {\n return;\n }\n\n reload();\n \n scrollTo();\n\n const wireAll = () => {\n const elements = document.querySelectorAll('[data-flyo-uid]');\n elements.forEach((el) => {\n const uid = el.getAttribute('data-flyo-uid');\n if (uid && el instanceof HTMLElement) {\n highlightAndClick(uid, el);\n }\n });\n };\n\n wireAll();\n\n const observer = new MutationObserver((mutations) => {\n const hasRelevantChanges = mutations.some(mutation => \n Array.from(mutation.addedNodes).some(node => {\n if (node.nodeType === Node.ELEMENT_NODE) {\n const element = node as Element;\n return element.hasAttribute('data-flyo-uid') || \n element.querySelector('[data-flyo-uid]');\n }\n return false;\n })\n );\n\n if (hasRelevantChanges) {\n wireAll();\n }\n });\n\n observer.observe(document.body, {\n childList: true,\n subtree: true,\n });\n\n return () => {\n observer.disconnect();\n };\n }, []);\n\n return <>{children}</>;\n}\n\n/**\n * WYSIWYG component for rendering ProseMirror/TipTap JSON content\n * \n * Uses the `wysiwyg()` function from `@flyo/nitro-js-bridge` to convert\n * nodes to HTML. All consecutive non-custom nodes are joined into a single\n * HTML string so no extra wrapper `<div>` elements are added around each node.\n * \n * The component wraps all output in a single `<div>` with an optional\n * `className` (defaults to `\"wysiwyg\"`).\n * \n * @example\n * ```tsx\n * import { FlyoWysiwyg } from '@flyo/nitro-next/client';\n * import CustomImage from './CustomImage';\n * \n * export default function MyComponent({ block }) {\n * return (\n * <FlyoWysiwyg \n * json={block.content.json} \n * className=\"wysiwyg\"\n * components={{\n * image: CustomImage\n * }} \n * />\n * );\n * }\n * ```\n */\nexport function FlyoWysiwyg({\n json,\n className = 'wysiwyg',\n components = {},\n}: {\n json: WysiwygJson;\n className?: string;\n components?: Record<string, React.ComponentType<{ node: WysiwygNode }>>;\n}) {\n let nodes: WysiwygNode[] = [];\n\n if (json) {\n if (Array.isArray(json)) {\n nodes = json;\n } else if ('type' in json && json.type === 'doc' && Array.isArray(json.content)) {\n nodes = json.content;\n } else {\n nodes = [json as WysiwygNode];\n }\n }\n\n // If no custom components are provided, render all nodes as a single HTML block\n const hasCustomComponents = nodes.some((node) => components[node.type]);\n\n if (!hasCustomComponents) {\n const html = nodes.map((node) => wysiwyg(node)).join('');\n return <div className={className} dangerouslySetInnerHTML={{ __html: html }} />;\n }\n\n // When custom components are used, group consecutive non-custom nodes\n // into single HTML blocks to avoid extra wrapper elements.\n const groups: ({ type: 'custom'; component: React.ComponentType<{ node: WysiwygNode }>; node: WysiwygNode } | { type: 'html'; html: string })[] = [];\n\n for (const node of nodes) {\n const Component = components[node.type];\n if (Component) {\n groups.push({ type: 'custom', component: Component, node });\n } else {\n const html = wysiwyg(node);\n const last = groups[groups.length - 1];\n if (last && last.type === 'html') {\n last.html += html;\n } else {\n groups.push({ type: 'html', html });\n }\n }\n }\n\n return (\n <div className={className}>\n {groups.map((group, index) => {\n if (group.type === 'custom') {\n return <group.component key={index} node={group.node} />;\n }\n return <div key={index} dangerouslySetInnerHTML={{ __html: group.html }} />;\n })}\n </div>\n );\n}\n\n/**\n * Image loader for Flyo CDN that automatically handles image transformations.\n * Adds Flyo CDN host if not already present and applies width transformations.\n * \n * @param src - The image source URL (relative or absolute)\n * @param width - The desired width for the image\n * @returns Transformed image URL with Flyo CDN parameters\n * \n * @example\n * ```tsx\n * <Image\n * loader={FlyoCdnLoader}\n * src=\"me.png\"\n * alt=\"Picture\"\n * width={500}\n * height={500}\n * />\n * ```\n */\nexport function FlyoCdnLoader({ src, width }: ImageLoaderProps): string {\n let imageUrl = src;\n\n // If src doesn't contain the Flyo CDN host, prefix it\n if (!src.includes(FLYO_CDN_HOST)) {\n // Remove leading slash if present to avoid double slashes\n const cleanSrc = src.startsWith('/') ? src.slice(1) : src;\n imageUrl = `https://${FLYO_CDN_HOST}/${cleanSrc}`;\n }\n\n // Append Flyo CDN transformation parameters\n return `${imageUrl}/thumb/${width}xnull?format=webp`;\n}\n\n/**\n * FlyoMetric component for tracking entity metrics in production\n * \n * Automatically sends a metric tracking request to the Flyo API when:\n * - The environment is production (NODE_ENV === 'production')\n * - The entity has a metric API URL configured\n * \n * @param entity - The entity object containing entity_metric.api\n * \n * @example\n * ```tsx\n * import { FlyoMetric } from '@flyo/nitro-next/client';\n * \n * export default function BlogPost(props: RouteParams) {\n * return nitroEntityRoute(props, {\n * resolver,\n * render: (entity: Entity) => (\n * <>\n * <FlyoMetric entity={entity} />\n * <article>\n * <h1>{entity.entity?.entity_title}</h1>\n * </article>\n * </>\n * )\n * });\n * }\n * ```\n */\nexport function FlyoMetric({ entity }: { entity: Entity }) {\n useEffect(() => {\n // Only track metrics in production and if API URL is available\n if (isProd && entity?.entity?.entity_metric?.api) {\n fetch(entity.entity.entity_metric.api);\n }\n }, [entity]);\n\n // This component doesn't render anything\n return null;\n}\n\n/**\n * A thin client wrapper that applies `editable()` to a root element while\n * allowing server-rendered children (e.g. `NitroSlot`) to be passed in.\n *\n * In Next.js, a file marked `'use client'` turns all of its imports into\n * client modules, so you cannot import server-only components like\n * `NitroSlot` directly. The workaround is to keep the server part separate\n * and pass it into this client wrapper via `children`.\n *\n * @example\n * ```tsx\n * // components/HeroBanner.tsx (server component – no 'use client')\n * import { Block } from '@flyo/nitro-typescript';\n * import { NitroSlot } from '@flyo/nitro-next/server';\n * import { EditableSection } from '@flyo/nitro-next/client';\n *\n * export function HeroBanner({ block }: { block: Block }) {\n * return (\n * <EditableSection block={block} className=\"hero\">\n * <h2>{block?.content?.title}</h2>\n * <NitroSlot slot={block.slots?.content} />\n * </EditableSection>\n * );\n * }\n * ```\n */\nexport function EditableSection({\n block,\n children,\n className,\n as: Tag = 'section',\n}: {\n block: EditableBlock;\n children: React.ReactNode;\n className?: string;\n as?: React.ElementType;\n}) {\n return (\n <Tag {...editable(block)} className={className}>\n {children}\n </Tag>\n );\n}\n","import type { Translation } from '@flyo/nitro-typescript';\n\n/**\n * A single language option for a language switcher, derived from a page's or\n * entity's `translation[]`. Framework-agnostic plain data — render it however\n * you like.\n */\nexport interface FlyoLanguageLink {\n /** Locale shortcode, e.g. `\"de\"`. */\n shortcode: string;\n /** Full language name from the CMS translation, e.g. `\"Deutsch\"` (when available). */\n name?: string;\n /** Fully-resolved localized href, or `null` when this locale has no linked translation. */\n href: string | null;\n /** Localized page/entity title, when available. */\n title?: string;\n /** `true` when this entry is the currently active locale. */\n isCurrent: boolean;\n /** `true` when a linked translation actually exists for this locale. */\n exists: boolean;\n}\n\n/**\n * Map a page's or entity's `translation[]` into a switcher-ready array of typed\n * links — the building block for a language switcher.\n *\n * Flyo only returns `translation` entries for languages that actually have a\n * translation. Pass `options.locales` (e.g. `flyo.state.locales`) to also get an\n * entry for every configured locale that is *missing* a translation — those come\n * back as `{ href: null, exists: false }` so you can render a fallback (a disabled\n * item, a link to the home page, …).\n *\n * Pure — no React or server-only APIs, so it is safe to call from server *or*\n * client components.\n *\n * This is a pure *data* helper — it maps a `translation[]` to switcher links and\n * renders nothing. **The Flyo route helpers already call it for you and publish\n * the result**, so page and entity routes need no switcher code at all; the\n * footer just reads the store (see `publishLanguageLinks` / `readLanguageLinks`\n * in `@flyo/nitro-next/server`). Call this directly only when you publish links\n * by hand from a raw `translation[]` — e.g. on a custom route Flyo doesn't\n * resolve:\n *\n * ```tsx\n * getLanguageLinks(translations, { currentLang, locales });\n * ```\n *\n * **Render each link as a native `<a>`, not `next/link`'s `<Link>`.** A language\n * switch must refresh the shared chrome — the localized nav, footer and\n * `<html lang>` — that lives in the root layout. In the Next.js App Router, soft\n * (client-side) navigation re-renders only the page segment, *not* shared\n * layouts, so a `<Link>` would leave that chrome in the previous language while\n * only the page body updates. A plain `<a>` triggers a full-document navigation,\n * forcing a fresh server render in the new locale so every part updates. (Regular\n * nav links can stay `<Link>` — the nav is identical within a language.)\n *\n * @example\n * ```tsx\n * const links = getLanguageLinks(page.translation, {\n * currentLang: lang,\n * locales: flyo.state.locales,\n * });\n * // Native <a> (full-document nav) — NOT next/link, so shared layout chrome\n * // re-renders in the new locale.\n * // links.map(l => l.exists\n * // ? <a key={l.shortcode} href={l.href!} aria-current={l.isCurrent || undefined}>{l.name ?? l.shortcode}</a>\n * // : <span key={l.shortcode} aria-disabled>{l.shortcode}</span>)\n * ```\n */\nexport function getLanguageLinks(\n translations: Translation[] | undefined,\n options?: { currentLang?: string; locales?: string[] },\n): FlyoLanguageLink[] {\n const currentLang = options?.currentLang;\n\n const byShortcode = new Map<string, Translation>();\n for (const t of translations ?? []) {\n const shortcode = t.language?.shortcode;\n if (shortcode) {\n byShortcode.set(shortcode, t);\n }\n }\n\n const toLink = (shortcode: string, t: Translation | undefined): FlyoLanguageLink => ({\n shortcode,\n name: t?.language?.name,\n href: t?.href ?? null,\n title: t?.title,\n isCurrent: shortcode === currentLang,\n exists: t?.href != null,\n });\n\n // When the full set of locales is known, emit an entry for every one so\n // callers can render fallbacks for languages that have no translation.\n if (options?.locales && options.locales.length > 0) {\n return options.locales.map((shortcode) => toLink(shortcode, byShortcode.get(shortcode)));\n }\n\n // Otherwise, just surface the translations that exist.\n return (translations ?? [])\n .filter((t) => t.language?.shortcode)\n .map((t) => toLink(t.language!.shortcode!, t));\n}\n"],"mappings":";;;;AAEA,SAAS,iBAAiB;AAC1B,SAAS,mBAAmB,SAAS,QAAQ,gBAAe;;;ACkErD,SAAS,iBACd,cACA,SACoB;AACpB,QAAM,cAAc,SAAS;AAE7B,QAAM,cAAc,oBAAI,IAAyB;AACjD,aAAW,KAAK,gBAAgB,CAAC,GAAG;AAClC,UAAM,YAAY,EAAE,UAAU;AAC9B,QAAI,WAAW;AACb,kBAAY,IAAI,WAAW,CAAC;AAAA,IAC9B;AAAA,EACF;AAEA,QAAM,SAAS,CAAC,WAAmB,OAAkD;AAAA,IACnF;AAAA,IACA,MAAM,GAAG,UAAU;AAAA,IACnB,MAAM,GAAG,QAAQ;AAAA,IACjB,OAAO,GAAG;AAAA,IACV,WAAW,cAAc;AAAA,IACzB,QAAQ,GAAG,QAAQ;AAAA,EACrB;AAIA,MAAI,SAAS,WAAW,QAAQ,QAAQ,SAAS,GAAG;AAClD,WAAO,QAAQ,QAAQ,IAAI,CAAC,cAAc,OAAO,WAAW,YAAY,IAAI,SAAS,CAAC,CAAC;AAAA,EACzF;AAGA,UAAQ,gBAAgB,CAAC,GACtB,OAAO,CAAC,MAAM,EAAE,UAAU,SAAS,EACnC,IAAI,CAAC,MAAM,OAAO,EAAE,SAAU,WAAY,CAAC,CAAC;AACjD;;;ADYS;AApGT,IAAM,gBAAgB;AAKf,IAAM,SAAS,QAAQ,IAAI,aAAa;AAgCxC,SAAS,SAAS,OAAoD;AAC3E,MAAI,OAAO,MAAM,QAAQ,YAAY,MAAM,IAAI,KAAK,MAAM,IAAI;AAC5D,WAAO,EAAE,iBAAiB,MAAM,IAAI;AAAA,EACtC;AACA,SAAO,CAAC;AACV;AAKO,SAAS,k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A8BO,SAAS,WAAW,EAAE,OAAO,GAAuB;AACzD,YAAU,MAAM;AAEd,QAAI,UAAU,QAAQ,QAAQ,eAAe,KAAK;AAChD,YAAM,OAAO,OAAO,cAAc,GAAG;AAAA,IACvC;AAAA,EACF,GAAG,CAAC,MAAM,CAAC;AAGX,SAAO;AACT;AA4BO,SAAS,gBAAgB;AAAA,EAC9B;AAAA,EACA;AAAA,EACA;AAAA,EACA,IAAI,MAAM;AACZ,GAKG;AACD,SACE,oBAAC,OAAK,GAAG,SAAS,KAAK,GAAG,WACvB,UACH;AAEJ;","names":[]}
|
package/dist/proxy.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { NextResponse } from 'next/server';
|
|
1
|
+
import { NextRequest, NextResponse } from 'next/server';
|
|
2
2
|
import { FlyoInstance } from './server.mjs';
|
|
3
3
|
import 'react';
|
|
4
4
|
import 'react/jsx-runtime';
|
|
@@ -27,7 +27,7 @@ import '@flyo/nitro-typescript';
|
|
|
27
27
|
* };
|
|
28
28
|
* ```
|
|
29
29
|
*/
|
|
30
|
-
declare function createProxy(flyo: FlyoInstance): () => NextResponse<unknown>;
|
|
30
|
+
declare function createProxy(flyo: FlyoInstance): (request: NextRequest) => NextResponse<unknown>;
|
|
31
31
|
/**
|
|
32
32
|
* Proxy matcher configuration
|
|
33
33
|
* Applies to all routes except Next.js internal routes
|
package/dist/proxy.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { NextResponse } from 'next/server';
|
|
1
|
+
import { NextRequest, NextResponse } from 'next/server';
|
|
2
2
|
import { FlyoInstance } from './server.js';
|
|
3
3
|
import 'react';
|
|
4
4
|
import 'react/jsx-runtime';
|
|
@@ -27,7 +27,7 @@ import '@flyo/nitro-typescript';
|
|
|
27
27
|
* };
|
|
28
28
|
* ```
|
|
29
29
|
*/
|
|
30
|
-
declare function createProxy(flyo: FlyoInstance): () => NextResponse<unknown>;
|
|
30
|
+
declare function createProxy(flyo: FlyoInstance): (request: NextRequest) => NextResponse<unknown>;
|
|
31
31
|
/**
|
|
32
32
|
* Proxy matcher configuration
|
|
33
33
|
* Applies to all routes except Next.js internal routes
|
package/dist/proxy.js
CHANGED
|
@@ -27,8 +27,14 @@ module.exports = __toCommonJS(proxy_exports);
|
|
|
27
27
|
var import_server = require("next/server");
|
|
28
28
|
function createProxy(flyo) {
|
|
29
29
|
const { state } = flyo;
|
|
30
|
-
return function proxy() {
|
|
31
|
-
const
|
|
30
|
+
return function proxy(request) {
|
|
31
|
+
const firstSegment = request.nextUrl.pathname.split("/").filter(Boolean)[0];
|
|
32
|
+
const locale = firstSegment && state.locales.includes(firstSegment) ? firstSegment : state.defaultLocale ?? void 0;
|
|
33
|
+
const requestHeaders = new Headers(request.headers);
|
|
34
|
+
if (locale) {
|
|
35
|
+
requestHeaders.set("x-flyo-locale", locale);
|
|
36
|
+
}
|
|
37
|
+
const res = import_server.NextResponse.next({ request: { headers: requestHeaders } });
|
|
32
38
|
if (state.liveEdit) {
|
|
33
39
|
res.headers.set("Vercel-CDN-Cache-Control", "no-store");
|
|
34
40
|
res.headers.set("CDN-Cache-Control", "no-store");
|
package/dist/proxy.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/proxy.ts"],"sourcesContent":["import { NextResponse } from 'next/server';\nimport type { FlyoInstance } from './server';\n\n/**\n * Nitro Next.js Proxy Factory\n *\n * Creates a Next.js middleware that handles cache control headers.\n * Uses cache TTL values from the Flyo instance's configuration state.\n *\n * @param flyo The Flyo instance returned by initNitro()\n * @returns Next.js middleware function\n *\n * @example\n * ```ts\n * // src/middleware.ts\n * import { createProxy } from '@flyo/nitro-next/proxy';\n * import { flyo } from './flyo.config';\n *\n * export default createProxy(flyo);\n *\n * export const config = {\n * matcher: ['/((?!_next/static|_next/image|favicon.ico).*)'],\n * };\n * ```\n */\nexport function createProxy(flyo: FlyoInstance) {\n const { state } = flyo;\n\n return function proxy() {\n const res = NextResponse.next();\n\n if (state.liveEdit) {\n // Development or live edit mode - no caching\n res.headers.set('Vercel-CDN-Cache-Control', 'no-store');\n res.headers.set('CDN-Cache-Control', 'no-store');\n res.headers.set('Cache-Control', 'no-store');\n } else {\n // Production with caching enabled\n const cdn = state.serverCacheTtl > 0 ? `max-age=${state.serverCacheTtl}` : 'no-store';\n res.headers.set('Vercel-CDN-Cache-Control', cdn);\n res.headers.set('CDN-Cache-Control', cdn);\n\n if (state.clientCacheTtl > 0) {\n res.headers.set('Cache-Control', `max-age=${state.clientCacheTtl}`);\n } else {\n res.headers.set('Cache-Control', 'no-store');\n }\n }\n\n return res;\n };\n}\n\n\n/**\n * Proxy matcher configuration\n * Applies to all routes except Next.js internal routes\n */\nexport const config = {\n matcher: ['/((?!_next/static|_next/image|favicon.ico).*)'],\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,
|
|
1
|
+
{"version":3,"sources":["../src/proxy.ts"],"sourcesContent":["import { NextResponse, type NextRequest } from 'next/server';\nimport type { FlyoInstance } from './server';\n\n/**\n * Nitro Next.js Proxy Factory\n *\n * Creates a Next.js middleware that handles cache control headers.\n * Uses cache TTL values from the Flyo instance's configuration state.\n *\n * @param flyo The Flyo instance returned by initNitro()\n * @returns Next.js middleware function\n *\n * @example\n * ```ts\n * // src/middleware.ts\n * import { createProxy } from '@flyo/nitro-next/proxy';\n * import { flyo } from './flyo.config';\n *\n * export default createProxy(flyo);\n *\n * export const config = {\n * matcher: ['/((?!_next/static|_next/image|favicon.ico).*)'],\n * };\n * ```\n */\nexport function createProxy(flyo: FlyoInstance) {\n const { state } = flyo;\n\n return function proxy(request: NextRequest) {\n // Detect the active locale from the first path segment when it is a\n // configured locale, and expose it to Server Components (layout, config,\n // entity resolvers) via a request header. No rewrite: pages are addressed\n // by their full locale-prefixed slug, which stays intact in the URL.\n const firstSegment = request.nextUrl.pathname.split('/').filter(Boolean)[0];\n const locale = firstSegment && state.locales.includes(firstSegment)\n ? firstSegment\n : (state.defaultLocale ?? undefined);\n\n const requestHeaders = new Headers(request.headers);\n if (locale) {\n requestHeaders.set('x-flyo-locale', locale);\n }\n\n const res = NextResponse.next({ request: { headers: requestHeaders } });\n\n if (state.liveEdit) {\n // Development or live edit mode - no caching\n res.headers.set('Vercel-CDN-Cache-Control', 'no-store');\n res.headers.set('CDN-Cache-Control', 'no-store');\n res.headers.set('Cache-Control', 'no-store');\n } else {\n // Production with caching enabled\n const cdn = state.serverCacheTtl > 0 ? `max-age=${state.serverCacheTtl}` : 'no-store';\n res.headers.set('Vercel-CDN-Cache-Control', cdn);\n res.headers.set('CDN-Cache-Control', cdn);\n\n if (state.clientCacheTtl > 0) {\n res.headers.set('Cache-Control', `max-age=${state.clientCacheTtl}`);\n } else {\n res.headers.set('Cache-Control', 'no-store');\n }\n }\n\n return res;\n };\n}\n\n\n/**\n * Proxy matcher configuration\n * Applies to all routes except Next.js internal routes\n */\nexport const config = {\n matcher: ['/((?!_next/static|_next/image|favicon.ico).*)'],\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,oBAA+C;AAyBxC,SAAS,YAAY,MAAoB;AAC9C,QAAM,EAAE,MAAM,IAAI;AAElB,SAAO,SAAS,MAAM,SAAsB;AAK1C,UAAM,eAAe,QAAQ,QAAQ,SAAS,MAAM,GAAG,EAAE,OAAO,OAAO,EAAE,CAAC;AAC1E,UAAM,SAAS,gBAAgB,MAAM,QAAQ,SAAS,YAAY,IAC9D,eACC,MAAM,iBAAiB;AAE5B,UAAM,iBAAiB,IAAI,QAAQ,QAAQ,OAAO;AAClD,QAAI,QAAQ;AACV,qBAAe,IAAI,iBAAiB,MAAM;AAAA,IAC5C;AAEA,UAAM,MAAM,2BAAa,KAAK,EAAE,SAAS,EAAE,SAAS,eAAe,EAAE,CAAC;AAEtE,QAAI,MAAM,UAAU;AAElB,UAAI,QAAQ,IAAI,4BAA4B,UAAU;AACtD,UAAI,QAAQ,IAAI,qBAAqB,UAAU;AAC/C,UAAI,QAAQ,IAAI,iBAAiB,UAAU;AAAA,IAC7C,OAAO;AAEL,YAAM,MAAM,MAAM,iBAAiB,IAAI,WAAW,MAAM,cAAc,KAAK;AAC3E,UAAI,QAAQ,IAAI,4BAA4B,GAAG;AAC/C,UAAI,QAAQ,IAAI,qBAAqB,GAAG;AAExC,UAAI,MAAM,iBAAiB,GAAG;AAC5B,YAAI,QAAQ,IAAI,iBAAiB,WAAW,MAAM,cAAc,EAAE;AAAA,MACpE,OAAO;AACL,YAAI,QAAQ,IAAI,iBAAiB,UAAU;AAAA,MAC7C;AAAA,IACF;AAEA,WAAO;AAAA,EACT;AACF;AAOO,IAAM,SAAS;AAAA,EACpB,SAAS,CAAC,+CAA+C;AAC3D;","names":[]}
|
package/dist/proxy.mjs
CHANGED
|
@@ -2,8 +2,14 @@
|
|
|
2
2
|
import { NextResponse } from "next/server";
|
|
3
3
|
function createProxy(flyo) {
|
|
4
4
|
const { state } = flyo;
|
|
5
|
-
return function proxy() {
|
|
6
|
-
const
|
|
5
|
+
return function proxy(request) {
|
|
6
|
+
const firstSegment = request.nextUrl.pathname.split("/").filter(Boolean)[0];
|
|
7
|
+
const locale = firstSegment && state.locales.includes(firstSegment) ? firstSegment : state.defaultLocale ?? void 0;
|
|
8
|
+
const requestHeaders = new Headers(request.headers);
|
|
9
|
+
if (locale) {
|
|
10
|
+
requestHeaders.set("x-flyo-locale", locale);
|
|
11
|
+
}
|
|
12
|
+
const res = NextResponse.next({ request: { headers: requestHeaders } });
|
|
7
13
|
if (state.liveEdit) {
|
|
8
14
|
res.headers.set("Vercel-CDN-Cache-Control", "no-store");
|
|
9
15
|
res.headers.set("CDN-Cache-Control", "no-store");
|
package/dist/proxy.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/proxy.ts"],"sourcesContent":["import { NextResponse } from 'next/server';\nimport type { FlyoInstance } from './server';\n\n/**\n * Nitro Next.js Proxy Factory\n *\n * Creates a Next.js middleware that handles cache control headers.\n * Uses cache TTL values from the Flyo instance's configuration state.\n *\n * @param flyo The Flyo instance returned by initNitro()\n * @returns Next.js middleware function\n *\n * @example\n * ```ts\n * // src/middleware.ts\n * import { createProxy } from '@flyo/nitro-next/proxy';\n * import { flyo } from './flyo.config';\n *\n * export default createProxy(flyo);\n *\n * export const config = {\n * matcher: ['/((?!_next/static|_next/image|favicon.ico).*)'],\n * };\n * ```\n */\nexport function createProxy(flyo: FlyoInstance) {\n const { state } = flyo;\n\n return function proxy() {\n const res = NextResponse.next();\n\n if (state.liveEdit) {\n // Development or live edit mode - no caching\n res.headers.set('Vercel-CDN-Cache-Control', 'no-store');\n res.headers.set('CDN-Cache-Control', 'no-store');\n res.headers.set('Cache-Control', 'no-store');\n } else {\n // Production with caching enabled\n const cdn = state.serverCacheTtl > 0 ? `max-age=${state.serverCacheTtl}` : 'no-store';\n res.headers.set('Vercel-CDN-Cache-Control', cdn);\n res.headers.set('CDN-Cache-Control', cdn);\n\n if (state.clientCacheTtl > 0) {\n res.headers.set('Cache-Control', `max-age=${state.clientCacheTtl}`);\n } else {\n res.headers.set('Cache-Control', 'no-store');\n }\n }\n\n return res;\n };\n}\n\n\n/**\n * Proxy matcher configuration\n * Applies to all routes except Next.js internal routes\n */\nexport const config = {\n matcher: ['/((?!_next/static|_next/image|favicon.ico).*)'],\n};\n"],"mappings":";AAAA,SAAS,
|
|
1
|
+
{"version":3,"sources":["../src/proxy.ts"],"sourcesContent":["import { NextResponse, type NextRequest } from 'next/server';\nimport type { FlyoInstance } from './server';\n\n/**\n * Nitro Next.js Proxy Factory\n *\n * Creates a Next.js middleware that handles cache control headers.\n * Uses cache TTL values from the Flyo instance's configuration state.\n *\n * @param flyo The Flyo instance returned by initNitro()\n * @returns Next.js middleware function\n *\n * @example\n * ```ts\n * // src/middleware.ts\n * import { createProxy } from '@flyo/nitro-next/proxy';\n * import { flyo } from './flyo.config';\n *\n * export default createProxy(flyo);\n *\n * export const config = {\n * matcher: ['/((?!_next/static|_next/image|favicon.ico).*)'],\n * };\n * ```\n */\nexport function createProxy(flyo: FlyoInstance) {\n const { state } = flyo;\n\n return function proxy(request: NextRequest) {\n // Detect the active locale from the first path segment when it is a\n // configured locale, and expose it to Server Components (layout, config,\n // entity resolvers) via a request header. No rewrite: pages are addressed\n // by their full locale-prefixed slug, which stays intact in the URL.\n const firstSegment = request.nextUrl.pathname.split('/').filter(Boolean)[0];\n const locale = firstSegment && state.locales.includes(firstSegment)\n ? firstSegment\n : (state.defaultLocale ?? undefined);\n\n const requestHeaders = new Headers(request.headers);\n if (locale) {\n requestHeaders.set('x-flyo-locale', locale);\n }\n\n const res = NextResponse.next({ request: { headers: requestHeaders } });\n\n if (state.liveEdit) {\n // Development or live edit mode - no caching\n res.headers.set('Vercel-CDN-Cache-Control', 'no-store');\n res.headers.set('CDN-Cache-Control', 'no-store');\n res.headers.set('Cache-Control', 'no-store');\n } else {\n // Production with caching enabled\n const cdn = state.serverCacheTtl > 0 ? `max-age=${state.serverCacheTtl}` : 'no-store';\n res.headers.set('Vercel-CDN-Cache-Control', cdn);\n res.headers.set('CDN-Cache-Control', cdn);\n\n if (state.clientCacheTtl > 0) {\n res.headers.set('Cache-Control', `max-age=${state.clientCacheTtl}`);\n } else {\n res.headers.set('Cache-Control', 'no-store');\n }\n }\n\n return res;\n };\n}\n\n\n/**\n * Proxy matcher configuration\n * Applies to all routes except Next.js internal routes\n */\nexport const config = {\n matcher: ['/((?!_next/static|_next/image|favicon.ico).*)'],\n};\n"],"mappings":";AAAA,SAAS,oBAAsC;AAyBxC,SAAS,YAAY,MAAoB;AAC9C,QAAM,EAAE,MAAM,IAAI;AAElB,SAAO,SAAS,MAAM,SAAsB;AAK1C,UAAM,eAAe,QAAQ,QAAQ,SAAS,MAAM,GAAG,EAAE,OAAO,OAAO,EAAE,CAAC;AAC1E,UAAM,SAAS,gBAAgB,MAAM,QAAQ,SAAS,YAAY,IAC9D,eACC,MAAM,iBAAiB;AAE5B,UAAM,iBAAiB,IAAI,QAAQ,QAAQ,OAAO;AAClD,QAAI,QAAQ;AACV,qBAAe,IAAI,iBAAiB,MAAM;AAAA,IAC5C;AAEA,UAAM,MAAM,aAAa,KAAK,EAAE,SAAS,EAAE,SAAS,eAAe,EAAE,CAAC;AAEtE,QAAI,MAAM,UAAU;AAElB,UAAI,QAAQ,IAAI,4BAA4B,UAAU;AACtD,UAAI,QAAQ,IAAI,qBAAqB,UAAU;AAC/C,UAAI,QAAQ,IAAI,iBAAiB,UAAU;AAAA,IAC7C,OAAO;AAEL,YAAM,MAAM,MAAM,iBAAiB,IAAI,WAAW,MAAM,cAAc,KAAK;AAC3E,UAAI,QAAQ,IAAI,4BAA4B,GAAG;AAC/C,UAAI,QAAQ,IAAI,qBAAqB,GAAG;AAExC,UAAI,MAAM,iBAAiB,GAAG;AAC5B,YAAI,QAAQ,IAAI,iBAAiB,WAAW,MAAM,cAAc,EAAE;AAAA,MACpE,OAAO;AACL,YAAI,QAAQ,IAAI,iBAAiB,UAAU;AAAA,MAC7C;AAAA,IACF;AAEA,WAAO;AAAA,EACT;AACF;AAOO,IAAM,SAAS;AAAA,EACpB,SAAS,CAAC,+CAA+C;AAC3D;","names":[]}
|