@pantheon-systems/p1-next-sdk 0.23.0 → 0.25.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 +99 -1
- package/bin/lib/cli.js +143 -25
- package/bin/lib/detect.js +14 -2
- package/bin/lib/fs-ops.js +2 -1
- package/bin/lib/localized-pages.js +522 -0
- package/bin/lib/messages.js +38 -23
- package/bin/templates/localized-pages/app/[[...puckPath]]/layout.tsx.template +29 -0
- package/bin/templates/localized-pages/app/[[...puckPath]]/page.tsx.template +24 -0
- package/bin/templates/localized-pages/app/p1/layout.tsx.template +11 -0
- package/bin/templates/localized-pages/components/html-shell.tsx.template +28 -0
- package/bin/templates/localized-pages/components/locale-suggestion.module.css.template +74 -0
- package/bin/templates/localized-pages/components/locale-suggestion.tsx.template +94 -0
- package/bin/templates/localized-pages/legacy/app/[...puckPath]/page.tsx.template +23 -0
- package/bin/templates/localized-pages/legacy/app/layout.tsx.template +37 -0
- package/bin/templates/localized-pages/legacy/app/layout.v1.tsx.template +27 -0
- package/bin/templates/localized-pages/legacy/app/page.tsx.template +19 -0
- package/bin/templates/localized-pages/lib/locale-name.ts.template +9 -0
- package/dist/chatbot/chatbot-plugin.js +3 -0
- package/dist/create-published-page.d.ts +22 -9
- package/dist/create-published-page.js +59 -8
- package/dist/editor-client/editor-content.js +3 -0
- package/dist/editor-client/use-business-account-id.d.ts +5 -0
- package/dist/editor-client/use-business-account-id.js +7 -0
- package/dist/experimental-features/features.d.ts +16 -5
- package/dist/experimental-features/features.js +6 -5
- package/dist/experimental-features/resolve-features.d.ts +5 -2
- package/dist/experimental-features/resolve-features.js +7 -4
- package/dist/experimental-features/session-cache.d.ts +1 -1
- package/dist/experimental-features/session-cache.js +2 -2
- package/dist/experimental-features/use-p1-experimental-features.d.ts +10 -3
- package/dist/experimental-features/use-p1-experimental-features.js +29 -11
- package/dist/handler.d.ts +2 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/localization/index.d.ts +4 -0
- package/dist/localization/index.js +3 -0
- package/dist/localization/saved-locale.d.ts +13 -0
- package/dist/localization/saved-locale.js +20 -0
- package/dist/localization-react.d.ts +40 -0
- package/dist/localization-react.js +15 -0
- package/dist/page-localization.d.ts +12 -0
- package/dist/page-localization.js +9 -0
- package/dist/pages-handler.js +2 -2
- package/dist/routes/publish.d.ts +0 -2
- package/dist/routes/publish.js +2 -3
- package/dist/routes/revalidate.d.ts +4 -4
- package/dist/routes/revalidate.js +10 -4
- package/dist/server.d.ts +2 -0
- package/dist/server.js +1 -0
- package/docs/migrations/editor-layout.md +213 -0
- package/docs/migrations/localized-pages.md +208 -0
- package/package.json +9 -3
- package/dist/chatbot/flag-context.d.ts +0 -15
- package/dist/chatbot/flag-context.js +0 -13
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import "../styles.css";
|
|
2
|
+
import { HtmlShell } from "../../components/html-shell";
|
|
3
|
+
|
|
4
|
+
/** Root layout for the editor and its auth and API routes. */
|
|
5
|
+
export default function EditorRootLayout({
|
|
6
|
+
children,
|
|
7
|
+
}: {
|
|
8
|
+
children: React.ReactNode;
|
|
9
|
+
}) {
|
|
10
|
+
return <HtmlShell lang="en">{children}</HtmlShell>;
|
|
11
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The document around every route. The site and the editor are separate root
|
|
3
|
+
* layouts, and both render through this.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
export function HtmlShell({
|
|
7
|
+
lang,
|
|
8
|
+
children,
|
|
9
|
+
}: {
|
|
10
|
+
lang: string;
|
|
11
|
+
children: React.ReactNode;
|
|
12
|
+
}) {
|
|
13
|
+
return (
|
|
14
|
+
<html lang={lang}>
|
|
15
|
+
{/* Puck's canvas-preview iframe copies this document's <body> attributes
|
|
16
|
+
onto its own iframe <body> (@puckeditor/core's CopyHostStyles
|
|
17
|
+
syncAttributes()), so a class/attribute placed directly on <body>
|
|
18
|
+
cannot be used to keep a rule out of the iframe. `.p1-app-shell` is
|
|
19
|
+
a child of <body> instead — Puck's canvas iframe never contains it
|
|
20
|
+
(the iframe only ever renders the Puck root/block tree into its own
|
|
21
|
+
#frame-root, not this layout). See styles.css for the scoped reset
|
|
22
|
+
this enables. */}
|
|
23
|
+
<body data-rm-theme="light">
|
|
24
|
+
<div className="p1-app-shell">{children}</div>
|
|
25
|
+
</body>
|
|
26
|
+
</html>
|
|
27
|
+
);
|
|
28
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/* The saved-language prompt. Every colour, space, radius and shadow routes
|
|
2
|
+
through a `--p1-locale-prompt-*` property so a site can retheme it without
|
|
3
|
+
touching these rules; each resolves to the matching PDS token, then to a
|
|
4
|
+
literal for pages that don't load PDS. Type inherits from the site. */
|
|
5
|
+
|
|
6
|
+
.prompt {
|
|
7
|
+
--p1-locale-prompt-color-text: var(--pds-color-foreground-default, #111827);
|
|
8
|
+
--p1-locale-prompt-color-surface: var(--pds-color-surface-default, #ffffff);
|
|
9
|
+
--p1-locale-prompt-color-border: var(--pds-color-border-default, #e5e7eb);
|
|
10
|
+
--p1-locale-prompt-color-hover: var(--pds-color-surface-default-secondary, #f3f4f6);
|
|
11
|
+
--p1-locale-prompt-color-accept: var(--pds-color-foreground-default, #111827);
|
|
12
|
+
--p1-locale-prompt-color-accept-hover: #374151;
|
|
13
|
+
--p1-locale-prompt-color-accept-text: var(--pds-color-foreground-reverse, #ffffff);
|
|
14
|
+
--p1-locale-prompt-space-edge: var(--pds-spacing-m, 1rem);
|
|
15
|
+
--p1-locale-prompt-space-gap: var(--pds-spacing-s, 0.75rem);
|
|
16
|
+
--p1-locale-prompt-space-button-block: 0.375rem;
|
|
17
|
+
--p1-locale-prompt-space-button-inline: var(--pds-spacing-s, 0.75rem);
|
|
18
|
+
--p1-locale-prompt-radius: var(--pds-border-radius-card, 0.5rem);
|
|
19
|
+
--p1-locale-prompt-radius-button: var(--pds-border-radius-default, 0.25rem);
|
|
20
|
+
--p1-locale-prompt-shadow: 0 10px 15px -3px rgb(0 0 0 / 0.1), 0 4px 6px -4px rgb(0 0 0 / 0.1);
|
|
21
|
+
|
|
22
|
+
position: fixed;
|
|
23
|
+
inset-inline: var(--p1-locale-prompt-space-edge);
|
|
24
|
+
bottom: var(--p1-locale-prompt-space-edge);
|
|
25
|
+
z-index: 50;
|
|
26
|
+
display: flex;
|
|
27
|
+
flex-wrap: wrap;
|
|
28
|
+
align-items: center;
|
|
29
|
+
gap: var(--p1-locale-prompt-space-gap);
|
|
30
|
+
max-width: 28rem;
|
|
31
|
+
margin-inline: auto;
|
|
32
|
+
padding: var(--p1-locale-prompt-space-edge);
|
|
33
|
+
border: 1px solid var(--p1-locale-prompt-color-border);
|
|
34
|
+
border-radius: var(--p1-locale-prompt-radius);
|
|
35
|
+
background: var(--p1-locale-prompt-color-surface);
|
|
36
|
+
color: var(--p1-locale-prompt-color-text);
|
|
37
|
+
box-shadow: var(--p1-locale-prompt-shadow);
|
|
38
|
+
font-size: 0.875rem;
|
|
39
|
+
line-height: 1.25rem;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
.message {
|
|
43
|
+
flex: 1;
|
|
44
|
+
margin: 0;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
.accept,
|
|
48
|
+
.dismiss {
|
|
49
|
+
padding: var(--p1-locale-prompt-space-button-block) var(--p1-locale-prompt-space-button-inline);
|
|
50
|
+
border-radius: var(--p1-locale-prompt-radius-button);
|
|
51
|
+
font: inherit;
|
|
52
|
+
font-weight: 500;
|
|
53
|
+
cursor: pointer;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
.accept {
|
|
57
|
+
background: var(--p1-locale-prompt-color-accept);
|
|
58
|
+
color: var(--p1-locale-prompt-color-accept-text);
|
|
59
|
+
text-decoration: none;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
.accept:hover {
|
|
63
|
+
background: var(--p1-locale-prompt-color-accept-hover);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
.dismiss {
|
|
67
|
+
border: 1px solid var(--p1-locale-prompt-color-border);
|
|
68
|
+
background: transparent;
|
|
69
|
+
color: inherit;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
.dismiss:hover {
|
|
73
|
+
background: var(--p1-locale-prompt-color-hover);
|
|
74
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Offers the page in the visitor's saved locale when it is in another one or
|
|
5
|
+
* has none. Either choice saves a locale, so the offer is not repeated; on a
|
|
6
|
+
* page with no locale, "Stay here" only closes it.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { useState, useSyncExternalStore } from "react";
|
|
10
|
+
import { useP1Localization } from "@pantheon-systems/p1-next-sdk";
|
|
11
|
+
import {
|
|
12
|
+
getSavedLocale,
|
|
13
|
+
getSavedLocaleSuggestion,
|
|
14
|
+
saveLocale,
|
|
15
|
+
} from "@pantheon-systems/p1-next-sdk/localization";
|
|
16
|
+
import { localeName } from "../lib/locale-name";
|
|
17
|
+
import styles from "./locale-suggestion.module.css";
|
|
18
|
+
|
|
19
|
+
export function LocaleSuggestionPrompt({
|
|
20
|
+
locale,
|
|
21
|
+
path,
|
|
22
|
+
onAccept,
|
|
23
|
+
onDismiss,
|
|
24
|
+
}: {
|
|
25
|
+
locale: string;
|
|
26
|
+
path: string;
|
|
27
|
+
onAccept: () => void;
|
|
28
|
+
onDismiss: () => void;
|
|
29
|
+
}) {
|
|
30
|
+
return (
|
|
31
|
+
<aside
|
|
32
|
+
lang="en"
|
|
33
|
+
aria-label="Language"
|
|
34
|
+
className={styles.prompt}
|
|
35
|
+
>
|
|
36
|
+
<p className={styles.message}>
|
|
37
|
+
This page is available in <span lang={locale}>{localeName(locale)}</span>.
|
|
38
|
+
</p>
|
|
39
|
+
<a
|
|
40
|
+
href={path}
|
|
41
|
+
hrefLang={locale}
|
|
42
|
+
onClick={onAccept}
|
|
43
|
+
className={styles.accept}
|
|
44
|
+
>
|
|
45
|
+
<span lang={locale}>{localeName(locale)}</span>
|
|
46
|
+
</a>
|
|
47
|
+
<button
|
|
48
|
+
type="button"
|
|
49
|
+
onClick={onDismiss}
|
|
50
|
+
className={styles.dismiss}
|
|
51
|
+
>
|
|
52
|
+
Stay here
|
|
53
|
+
</button>
|
|
54
|
+
</aside>
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const subscribe = () => () => {};
|
|
59
|
+
|
|
60
|
+
/** The visitor's saved locale, or `null` on the server and while hydrating. */
|
|
61
|
+
function useSavedLocale(): string | null {
|
|
62
|
+
return useSyncExternalStore(subscribe, getSavedLocale, () => null);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export function LocaleSuggestion() {
|
|
66
|
+
// The live region is rendered empty first, so screen readers announce the offer when it fills.
|
|
67
|
+
return (
|
|
68
|
+
<div aria-live="polite">
|
|
69
|
+
<LocaleOffer savedLocale={useSavedLocale()} />
|
|
70
|
+
</div>
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export function LocaleOffer({ savedLocale }: { savedLocale: string | null }) {
|
|
75
|
+
const { locale, siteLocales, variants } = useP1Localization();
|
|
76
|
+
const [dismissed, setDismissed] = useState(false);
|
|
77
|
+
|
|
78
|
+
const suggestion = siteLocales
|
|
79
|
+
? getSavedLocaleSuggestion({ variants, siteLocales, savedLocale })
|
|
80
|
+
: null;
|
|
81
|
+
if (!suggestion || dismissed) return null;
|
|
82
|
+
|
|
83
|
+
return (
|
|
84
|
+
<LocaleSuggestionPrompt
|
|
85
|
+
locale={suggestion.locale}
|
|
86
|
+
path={suggestion.path}
|
|
87
|
+
onAccept={() => saveLocale(suggestion.locale)}
|
|
88
|
+
onDismiss={() => {
|
|
89
|
+
if (locale) saveLocale(locale);
|
|
90
|
+
setDismissed(true);
|
|
91
|
+
}}
|
|
92
|
+
/>
|
|
93
|
+
);
|
|
94
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Catch-all route that renders user-facing pages generated by Puck.
|
|
3
|
+
*
|
|
4
|
+
* The pipeline lives in the SDK — see createPublishedPage in
|
|
5
|
+
* app/published-pages.tsx for what this route is made of.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { published } from "../published-pages";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Backstop only: publishing calls revalidatePath for the affected routes, so
|
|
12
|
+
* cached pages normally refresh the moment their content changes. This bounds
|
|
13
|
+
* how long an edit made outside that path (a direct API write, a restored
|
|
14
|
+
* branch) can stay stale.
|
|
15
|
+
*
|
|
16
|
+
* Must stay a literal here: Next.js statically analyzes segment-config exports,
|
|
17
|
+
* so a value re-exported from the factory would go undetected.
|
|
18
|
+
*/
|
|
19
|
+
export const revalidate = 300;
|
|
20
|
+
|
|
21
|
+
export const generateStaticParams = published.generateStaticParams;
|
|
22
|
+
export const generateMetadata = published.generateMetadata;
|
|
23
|
+
export default published.Page;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import "./styles.css";
|
|
2
|
+
import type { Metadata } from "next";
|
|
3
|
+
|
|
4
|
+
const siteUrl = process.env.NEXT_PUBLIC_SITE_URL;
|
|
5
|
+
|
|
6
|
+
// A page-level openGraph replaces (not merges) this one, so buildPageMetadata
|
|
7
|
+
// re-declares og:type and the env site-name fallback; this covers routes that
|
|
8
|
+
// return no openGraph (e.g. not-found early returns).
|
|
9
|
+
export const metadata: Metadata = {
|
|
10
|
+
...(siteUrl ? { metadataBase: new URL(siteUrl) } : {}),
|
|
11
|
+
openGraph: {
|
|
12
|
+
type: "website",
|
|
13
|
+
siteName: process.env.NEXT_PUBLIC_SITE_NAME,
|
|
14
|
+
},
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export default function RootLayout({
|
|
18
|
+
children,
|
|
19
|
+
}: {
|
|
20
|
+
children: React.ReactNode;
|
|
21
|
+
}) {
|
|
22
|
+
return (
|
|
23
|
+
<html lang="en">
|
|
24
|
+
{/* Puck's canvas-preview iframe copies this document's <body> attributes
|
|
25
|
+
onto its own iframe <body> (@puckeditor/core's CopyHostStyles
|
|
26
|
+
syncAttributes()), so a class/attribute placed directly on <body>
|
|
27
|
+
cannot be used to keep a rule out of the iframe. `.p1-app-shell` is
|
|
28
|
+
a child of <body> instead — Puck's canvas iframe never contains it
|
|
29
|
+
(the iframe only ever renders the Puck root/block tree into its own
|
|
30
|
+
#frame-root, not this layout). See styles.css for the scoped reset
|
|
31
|
+
this enables. */}
|
|
32
|
+
<body data-rm-theme="light">
|
|
33
|
+
<div className="p1-app-shell">{children}</div>
|
|
34
|
+
</body>
|
|
35
|
+
</html>
|
|
36
|
+
);
|
|
37
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import "./styles.css";
|
|
2
|
+
import type { Metadata } from "next";
|
|
3
|
+
|
|
4
|
+
const siteUrl = process.env.NEXT_PUBLIC_SITE_URL;
|
|
5
|
+
|
|
6
|
+
// A page-level openGraph replaces (not merges) this one, so buildPageMetadata
|
|
7
|
+
// re-declares og:type and the env site-name fallback; this covers routes that
|
|
8
|
+
// return no openGraph (e.g. not-found early returns).
|
|
9
|
+
export const metadata: Metadata = {
|
|
10
|
+
...(siteUrl ? { metadataBase: new URL(siteUrl) } : {}),
|
|
11
|
+
openGraph: {
|
|
12
|
+
type: "website",
|
|
13
|
+
siteName: process.env.NEXT_PUBLIC_SITE_NAME,
|
|
14
|
+
},
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export default function RootLayout({
|
|
18
|
+
children,
|
|
19
|
+
}: {
|
|
20
|
+
children: React.ReactNode;
|
|
21
|
+
}) {
|
|
22
|
+
return (
|
|
23
|
+
<html lang="en">
|
|
24
|
+
<body data-rm-theme="light">{children}</body>
|
|
25
|
+
</html>
|
|
26
|
+
);
|
|
27
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Home route. The pipeline lives in the SDK — see createPublishedPage in
|
|
3
|
+
* app/published-pages.tsx for what this route is made of.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { published } from "./published-pages";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Backstop only: publishing calls revalidatePath("/"), so the home page
|
|
10
|
+
* normally refreshes the moment its content changes. This bounds how long an
|
|
11
|
+
* edit made outside that path can stay stale.
|
|
12
|
+
*
|
|
13
|
+
* Must stay a literal here: Next.js statically analyzes segment-config exports,
|
|
14
|
+
* so a value re-exported from the factory would go undetected.
|
|
15
|
+
*/
|
|
16
|
+
export const revalidate = 300;
|
|
17
|
+
|
|
18
|
+
export const generateMetadata = published.generateHomeMetadata;
|
|
19
|
+
export default published.HomePage;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The name of a locale in its own language, such as "Français" for `fr`, for
|
|
3
|
+
* offering a visitor a language they read. Throws a `RangeError` for a value
|
|
4
|
+
* that is not a language tag.
|
|
5
|
+
*/
|
|
6
|
+
export function localeName(locale: string): string {
|
|
7
|
+
const name = new Intl.DisplayNames([locale], { type: "language" }).of(locale) ?? locale;
|
|
8
|
+
return name.charAt(0).toLocaleUpperCase(locale) + name.slice(1);
|
|
9
|
+
}
|
|
@@ -3,12 +3,15 @@ import { Fragment as _Fragment, jsx as _jsx } from "react/jsx-runtime";
|
|
|
3
3
|
import { useP1Auth } from "@pantheon-systems/puck-css";
|
|
4
4
|
import { createAIChatPlugin } from "@pantheon-systems/p1-ai-chat";
|
|
5
5
|
import { useP1ExperimentalFeatures } from "../experimental-features";
|
|
6
|
+
import { useBusinessAccountId } from "../editor-client/use-business-account-id";
|
|
6
7
|
import { p1UserFlagKey } from "../launchdarkly/user-key";
|
|
7
8
|
export function useChatbotEnabled() {
|
|
8
9
|
const { user } = useP1Auth();
|
|
10
|
+
const businessAccountId = useBusinessAccountId();
|
|
9
11
|
const features = useP1ExperimentalFeatures({
|
|
10
12
|
userId: p1UserFlagKey(user),
|
|
11
13
|
siteId: process.env.NEXT_PUBLIC_CSS_SITE_ID || undefined,
|
|
14
|
+
businessAccountId,
|
|
12
15
|
});
|
|
13
16
|
return features.isEnabled("chatbot");
|
|
14
17
|
}
|
|
@@ -18,16 +18,17 @@
|
|
|
18
18
|
* import { createPublishedPage } from "@pantheon-systems/p1-next-sdk/server";
|
|
19
19
|
* export const published = createPublishedPage({ ... });
|
|
20
20
|
*
|
|
21
|
-
* // app/[...puckPath]/page.tsx
|
|
21
|
+
* // app/[[...puckPath]]/page.tsx — "/" and every other page
|
|
22
22
|
* export const revalidate = 300;
|
|
23
23
|
* export default published.Page;
|
|
24
24
|
* export const generateMetadata = published.generateMetadata;
|
|
25
25
|
* export const generateStaticParams = published.generateStaticParams;
|
|
26
26
|
*
|
|
27
|
-
* // app/
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
*
|
|
27
|
+
* // app/[[...puckPath]]/layout.tsx — the root layout
|
|
28
|
+
* const lang = (await published.getDocumentLanguage({ params })) ?? "en";
|
|
29
|
+
*
|
|
30
|
+
* `HomePage` and `generateHomeMetadata` serve a separate `app/page.tsx` for an
|
|
31
|
+
* app whose catch-all is `[...puckPath]` rather than optional.
|
|
31
32
|
*
|
|
32
33
|
* `revalidate` must stay a literal in the route file. Next.js statically
|
|
33
34
|
* analyzes segment-config exports, so re-exporting one through the factory
|
|
@@ -37,6 +38,7 @@
|
|
|
37
38
|
import type { Metadata } from "next";
|
|
38
39
|
import { type RemoteDatasourceFetcher } from "@pantheon-systems/puck-css/server";
|
|
39
40
|
import type { Data } from "@puckeditor/core";
|
|
41
|
+
import { type PublishedPageLocalization } from "./localization-react";
|
|
40
42
|
/**
|
|
41
43
|
* Describes the rendered document to the render client, which uses it for the
|
|
42
44
|
* edit affordances that only make sense once a real document is on screen.
|
|
@@ -49,6 +51,12 @@ export type PublishedPageMetadata = {
|
|
|
49
51
|
export type PublishedPageClientProps = {
|
|
50
52
|
data: Data;
|
|
51
53
|
pageMetadata: PublishedPageMetadata;
|
|
54
|
+
/**
|
|
55
|
+
* The page's locale and its locale variants. Present only on a site with
|
|
56
|
+
* locales. The same value is available to any component below the client
|
|
57
|
+
* through `useP1Localization`.
|
|
58
|
+
*/
|
|
59
|
+
localization?: PublishedPageLocalization;
|
|
52
60
|
};
|
|
53
61
|
export type CreatePublishedPageConfig = {
|
|
54
62
|
/**
|
|
@@ -96,15 +104,20 @@ export type CreatePublishedPageConfig = {
|
|
|
96
104
|
internalPathPrefixes?: readonly string[];
|
|
97
105
|
};
|
|
98
106
|
export declare function createPublishedPage(config: CreatePublishedPageConfig): {
|
|
99
|
-
Page: ({ params
|
|
107
|
+
Page: ({ params }: {
|
|
100
108
|
params: Promise<{
|
|
101
|
-
puckPath
|
|
109
|
+
puckPath?: string[];
|
|
102
110
|
}>;
|
|
103
111
|
}) => Promise<import("react").JSX.Element>;
|
|
112
|
+
getDocumentLanguage: ({ params, }: {
|
|
113
|
+
params: Promise<{
|
|
114
|
+
puckPath?: string[];
|
|
115
|
+
}>;
|
|
116
|
+
}) => Promise<string | null>;
|
|
104
117
|
HomePage: () => Promise<import("react").JSX.Element>;
|
|
105
|
-
generateMetadata: ({ params
|
|
118
|
+
generateMetadata: ({ params }: {
|
|
106
119
|
params: Promise<{
|
|
107
|
-
puckPath
|
|
120
|
+
puckPath?: string[];
|
|
108
121
|
}>;
|
|
109
122
|
}) => Promise<Metadata>;
|
|
110
123
|
generateHomeMetadata: () => Promise<Metadata>;
|
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
2
|
import { cache } from "react";
|
|
3
3
|
import { notFound } from "next/navigation";
|
|
4
|
+
import { getHreflangAlternates, getLocaleVariants, } from "@pantheon-systems/css-client/localization";
|
|
4
5
|
import { loadRemoteDatasourceContext, extractReferencedDatasourceIds, resolveDataTemplates, pagePathFromCatchAllSegments, } from "@pantheon-systems/puck-css/server";
|
|
5
6
|
import { createCssQueryFetchers } from "./css-query-fetchers";
|
|
6
7
|
import { loadPublishedPage, loadRouteTemplateKeys, } from "./published-page";
|
|
7
8
|
import { resolvePageMetadata } from "./resolve-page-metadata";
|
|
9
|
+
import { P1LocalizationProvider, } from "./localization-react";
|
|
10
|
+
import { getPageLocalization } from "./page-localization";
|
|
8
11
|
export function createPublishedPage(config) {
|
|
9
12
|
const { Client, Unavailable, Fallback, fetchers = [], resolveMetadata = ({ pageData, path }) => resolvePageMetadata({ pageData, path, fetchers }), titles, internalPathPrefixes, } = config;
|
|
10
13
|
const loadOptions = internalPathPrefixes ? { internalPathPrefixes } : undefined;
|
|
@@ -23,13 +26,48 @@ export function createPublishedPage(config) {
|
|
|
23
26
|
});
|
|
24
27
|
return resolveDataTemplates(data, context);
|
|
25
28
|
}
|
|
29
|
+
function localize(path, data) {
|
|
30
|
+
const found = getPageLocalization(data);
|
|
31
|
+
if (!found)
|
|
32
|
+
return null;
|
|
33
|
+
const { localization, siteLocales } = found;
|
|
34
|
+
return {
|
|
35
|
+
locale: localization.locale,
|
|
36
|
+
path,
|
|
37
|
+
siteLocales,
|
|
38
|
+
localization,
|
|
39
|
+
variants: getLocaleVariants(localization),
|
|
40
|
+
};
|
|
41
|
+
}
|
|
26
42
|
async function render(path, data) {
|
|
27
43
|
const resolvedData = await resolve(path, data);
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
44
|
+
const localization = localize(path, data);
|
|
45
|
+
const pageMetadata = {
|
|
46
|
+
route: path,
|
|
47
|
+
documentName: data.root.props?.title,
|
|
48
|
+
pageType: "page",
|
|
49
|
+
};
|
|
50
|
+
if (!localization) {
|
|
51
|
+
return _jsx(Client, { data: resolvedData, pageMetadata: pageMetadata });
|
|
52
|
+
}
|
|
53
|
+
return (_jsx(P1LocalizationProvider, { value: localization, children: _jsx(Client, { data: resolvedData, pageMetadata: pageMetadata, localization: localization }) }));
|
|
54
|
+
}
|
|
55
|
+
function withAlternates(metadata, data) {
|
|
56
|
+
const siteUrl = process.env.NEXT_PUBLIC_SITE_URL?.trim();
|
|
57
|
+
if (!siteUrl)
|
|
58
|
+
return metadata;
|
|
59
|
+
const found = getPageLocalization(data);
|
|
60
|
+
const alternates = found && getHreflangAlternates(found.localization);
|
|
61
|
+
if (!alternates)
|
|
62
|
+
return metadata;
|
|
63
|
+
const languages = Object.fromEntries(Object.entries(alternates).map(([locale, path]) => [locale, new URL(path, siteUrl).href]));
|
|
64
|
+
return {
|
|
65
|
+
...metadata,
|
|
66
|
+
alternates: {
|
|
67
|
+
...metadata.alternates,
|
|
68
|
+
languages: { ...languages, ...metadata.alternates?.languages },
|
|
69
|
+
},
|
|
70
|
+
};
|
|
33
71
|
}
|
|
34
72
|
async function metadataFor(result, path) {
|
|
35
73
|
if (result.status === "missing") {
|
|
@@ -38,18 +76,23 @@ export function createPublishedPage(config) {
|
|
|
38
76
|
if (result.status === "unavailable") {
|
|
39
77
|
return { title: titles?.unavailable ?? "Temporarily unavailable" };
|
|
40
78
|
}
|
|
41
|
-
|
|
79
|
+
const metadata = await resolveMetadata({ pageData: result.data, path });
|
|
80
|
+
return withAlternates(metadata, result.data);
|
|
42
81
|
}
|
|
43
82
|
function generateStaticParams() {
|
|
44
83
|
return [];
|
|
45
84
|
}
|
|
46
|
-
async function generateMetadata({ params
|
|
85
|
+
async function generateMetadata({ params }) {
|
|
47
86
|
const { puckPath = [] } = await params;
|
|
87
|
+
if (puckPath.length === 0)
|
|
88
|
+
return generateHomeMetadata();
|
|
48
89
|
const path = pagePathFromCatchAllSegments(puckPath);
|
|
49
90
|
return metadataFor(await loadPublishedPage(path, loadOptions), path);
|
|
50
91
|
}
|
|
51
|
-
async function Page({ params
|
|
92
|
+
async function Page({ params }) {
|
|
52
93
|
const { puckPath = [] } = await params;
|
|
94
|
+
if (puckPath.length === 0)
|
|
95
|
+
return HomePage();
|
|
53
96
|
const path = pagePathFromCatchAllSegments(puckPath);
|
|
54
97
|
const result = await loadPublishedPage(path, loadOptions);
|
|
55
98
|
if (result.status === "missing") {
|
|
@@ -74,8 +117,16 @@ export function createPublishedPage(config) {
|
|
|
74
117
|
}
|
|
75
118
|
return render("/", result.data);
|
|
76
119
|
}
|
|
120
|
+
async function getDocumentLanguage({ params, }) {
|
|
121
|
+
const { puckPath = [] } = await params;
|
|
122
|
+
const result = await loadPublishedPage(pagePathFromCatchAllSegments(puckPath), loadOptions);
|
|
123
|
+
if (result.status !== "ok")
|
|
124
|
+
return null;
|
|
125
|
+
return getPageLocalization(result.data)?.localization.locale ?? null;
|
|
126
|
+
}
|
|
77
127
|
return {
|
|
78
128
|
Page,
|
|
129
|
+
getDocumentLanguage,
|
|
79
130
|
HomePage,
|
|
80
131
|
generateMetadata,
|
|
81
132
|
generateHomeMetadata,
|
|
@@ -7,6 +7,7 @@ import { useP1ExperimentalFeatures } from "../experimental-features";
|
|
|
7
7
|
import { p1UserFlagKey } from "../launchdarkly/user-key";
|
|
8
8
|
import { EditorCanvas } from "./editor-canvas";
|
|
9
9
|
import { useReturnToRedirect } from "./return-to";
|
|
10
|
+
import { useBusinessAccountId } from "./use-business-account-id";
|
|
10
11
|
export function EditorContent({ runtime, path, }) {
|
|
11
12
|
const router = useRouter();
|
|
12
13
|
const { data: editorCtx } = useEditorContext(path);
|
|
@@ -14,9 +15,11 @@ export function EditorContent({ runtime, path, }) {
|
|
|
14
15
|
const { context: datasourceContext } = useRemoteDatasourceContext(path, datasourceRegistry);
|
|
15
16
|
const p1Plugins = useP1Plugins(path, runtime.puckConfig);
|
|
16
17
|
const { user } = useP1Auth();
|
|
18
|
+
const businessAccountId = useBusinessAccountId();
|
|
17
19
|
const features = useP1ExperimentalFeatures({
|
|
18
20
|
userId: p1UserFlagKey(user),
|
|
19
21
|
siteId: runtime.config?.siteId,
|
|
22
|
+
businessAccountId,
|
|
20
23
|
});
|
|
21
24
|
const openDocument = useCallback((documentPath) => {
|
|
22
25
|
router.push(`${editorPathHref(documentPath)}${window.location.search}`);
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The Business account of the current site: undefined while it is still loading, null
|
|
3
|
+
* once it is settled as unknown (or there is no provider, or the provider does not carry it), otherwise its id.
|
|
4
|
+
*/
|
|
5
|
+
export declare function useBusinessAccountId(): string | null | undefined;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { useContext } from "react";
|
|
3
|
+
import { P1PuckContext } from "@pantheon-systems/puck-css";
|
|
4
|
+
export function useBusinessAccountId() {
|
|
5
|
+
const puck = useContext(P1PuckContext);
|
|
6
|
+
return puck && "businessAccountId" in puck ? puck.businessAccountId : null;
|
|
7
|
+
}
|
|
@@ -17,14 +17,25 @@ export declare const P1_EXPERIMENTAL_FEATURE_FLAGS: {
|
|
|
17
17
|
};
|
|
18
18
|
export type P1ExperimentalFeature = keyof typeof P1_EXPERIMENTAL_FEATURE_FLAGS;
|
|
19
19
|
export declare const P1_EXPERIMENTAL_FEATURES: P1ExperimentalFeature[];
|
|
20
|
+
/**
|
|
21
|
+
* The features whose flag has a rule on the Business account. Only these wait for the
|
|
22
|
+
* Business account and re-evaluate once it arrives; every other feature is answered
|
|
23
|
+
* from the user and site alone and is never changed afterwards. A feature belongs here
|
|
24
|
+
* before a rule on `business-account` is added to its flag.
|
|
25
|
+
*/
|
|
26
|
+
export declare const BUSINESS_ACCOUNT_TARGETED_FEATURES: readonly P1ExperimentalFeature[];
|
|
20
27
|
/** Every experimental feature off. What an unanswered LaunchDarkly resolves to. */
|
|
21
28
|
export type P1ExperimentalFeatureSet = Record<P1ExperimentalFeature, boolean>;
|
|
22
29
|
export declare const ALL_OFF: P1ExperimentalFeatureSet;
|
|
23
30
|
/**
|
|
24
|
-
*
|
|
31
|
+
* Whether one experimental feature is on, reading its flag through `valueOf`.
|
|
25
32
|
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
|
|
33
|
+
* Only a literal `true` turns a feature on, so a missing flag, a null and a non-boolean
|
|
34
|
+
* all read as off.
|
|
35
|
+
*/
|
|
36
|
+
export declare function readFeature(feature: P1ExperimentalFeature, valueOf: (flagKey: string) => unknown): boolean;
|
|
37
|
+
/**
|
|
38
|
+
* Every experimental feature's state. Reads only the flags that gate a feature, one
|
|
39
|
+
* `readFeature` per feature.
|
|
29
40
|
*/
|
|
30
|
-
export declare function readFeatures(
|
|
41
|
+
export declare function readFeatures(valueOf: (flagKey: string) => unknown): P1ExperimentalFeatureSet;
|
|
@@ -5,10 +5,11 @@ export const P1_EXPERIMENTAL_FEATURE_FLAGS = {
|
|
|
5
5
|
sectionNesting: "p1-section-nesting",
|
|
6
6
|
};
|
|
7
7
|
export const P1_EXPERIMENTAL_FEATURES = Object.keys(P1_EXPERIMENTAL_FEATURE_FLAGS);
|
|
8
|
+
export const BUSINESS_ACCOUNT_TARGETED_FEATURES = [];
|
|
8
9
|
export const ALL_OFF = Object.freeze(Object.fromEntries(P1_EXPERIMENTAL_FEATURES.map((feature) => [feature, false])));
|
|
9
|
-
export function
|
|
10
|
-
return
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
]));
|
|
10
|
+
export function readFeature(feature, valueOf) {
|
|
11
|
+
return valueOf(P1_EXPERIMENTAL_FEATURE_FLAGS[feature]) === true;
|
|
12
|
+
}
|
|
13
|
+
export function readFeatures(valueOf) {
|
|
14
|
+
return Object.fromEntries(P1_EXPERIMENTAL_FEATURES.map((feature) => [feature, readFeature(feature, valueOf)]));
|
|
14
15
|
}
|
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
import { type LDContext } from "launchdarkly-js-client-sdk";
|
|
2
2
|
import { type P1ExperimentalFeatureSet } from "./features";
|
|
3
|
-
/**
|
|
4
|
-
|
|
3
|
+
/**
|
|
4
|
+
* The LaunchDarkly evaluation context: who is editing, which site they are in, and which
|
|
5
|
+
* Business account owns it. Each is identified by key alone.
|
|
6
|
+
*/
|
|
7
|
+
export declare function buildFeatureContext(userId: string, siteId: string | undefined, businessAccountId?: string | null): LDContext;
|
|
5
8
|
/**
|
|
6
9
|
* Evaluate every experimental feature once for one context.
|
|
7
10
|
*
|
|
@@ -2,14 +2,15 @@ import { initialize } from "launchdarkly-js-client-sdk";
|
|
|
2
2
|
import { ALL_OFF, readFeatures } from "./features";
|
|
3
3
|
import { localFlagOverrides, overridesEveryFeature } from "./local-overrides";
|
|
4
4
|
const INITIALIZATION_TIMEOUT_SECONDS = 5;
|
|
5
|
-
export function buildFeatureContext(userId, siteId) {
|
|
6
|
-
if (!siteId) {
|
|
5
|
+
export function buildFeatureContext(userId, siteId, businessAccountId) {
|
|
6
|
+
if (!siteId && !businessAccountId) {
|
|
7
7
|
return { kind: "user", key: userId };
|
|
8
8
|
}
|
|
9
9
|
return {
|
|
10
10
|
kind: "multi",
|
|
11
11
|
user: { key: userId },
|
|
12
|
-
site: { key: siteId },
|
|
12
|
+
...(siteId ? { site: { key: siteId } } : {}),
|
|
13
|
+
...(businessAccountId ? { "business-account": { key: businessAccountId } } : {}),
|
|
13
14
|
};
|
|
14
15
|
}
|
|
15
16
|
export async function resolveFeatures(clientSideId, context) {
|
|
@@ -22,9 +23,11 @@ export async function resolveFeatures(clientSideId, context) {
|
|
|
22
23
|
client = initialize(clientSideId, context, {
|
|
23
24
|
streaming: false,
|
|
24
25
|
diagnosticOptOut: true,
|
|
26
|
+
sendEventsOnlyForVariation: true,
|
|
25
27
|
});
|
|
26
28
|
await client.waitForInitialization(INITIALIZATION_TIMEOUT_SECONDS);
|
|
27
|
-
|
|
29
|
+
const ldClient = client;
|
|
30
|
+
return { ...readFeatures((key) => ldClient.variation(key, false)), ...overrides };
|
|
28
31
|
}
|
|
29
32
|
catch {
|
|
30
33
|
return { ...ALL_OFF, ...overrides };
|