@parhelia/next 0.0.0-stage → 1.0.12951
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/LICENSE +8 -0
- package/README.md +64 -2
- package/package.json +124 -4
- package/src/config.cjs +128 -0
- package/src/config.d.cts +50 -0
- package/src/contentful.d.ts +1 -0
- package/src/contentful.js +2 -0
- package/src/identity.d.ts +1 -0
- package/src/identity.js +2 -0
- package/src/index.d.ts +4 -0
- package/src/index.js +10 -0
- package/src/magnolia.d.ts +1 -0
- package/src/magnolia.js +2 -0
- package/src/refresh.d.ts +1 -0
- package/src/refresh.js +2 -0
- package/src/rich-text.d.ts +1 -0
- package/src/rich-text.js +2 -0
- package/src/server.d.ts +1 -0
- package/src/server.js +2 -0
- package/src/sitecore.d.ts +23 -0
- package/src/sitecore.js +69 -0
- package/src/typo3.d.ts +1 -0
- package/src/typo3.js +2 -0
- package/src/umbraco.d.ts +1 -0
- package/src/umbraco.js +2 -0
package/LICENSE
ADDED
package/README.md
CHANGED
|
@@ -1,3 +1,65 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @parhelia/next
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Serve a Next.js rendering head in the Parhelia editor. Plain JavaScript, no build step: it can be copied into a head
|
|
4
|
+
that is built elsewhere.
|
|
5
|
+
|
|
6
|
+
```js
|
|
7
|
+
// next.config.js
|
|
8
|
+
const { withParhelia } = require('@parhelia/next/config');
|
|
9
|
+
module.exports = withParhelia({ /* the head's config, or a config function such as next-compose-plugins' */ });
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
```tsx
|
|
13
|
+
// pages/_app.tsx (or a client component of the App Router layout)
|
|
14
|
+
import { ContentfulLivePreviewProvider } from '@contentful/live-preview/react';
|
|
15
|
+
import { ParheliaBridge, withBasePath } from '@parhelia/next';
|
|
16
|
+
import { contentfulLivePreviewOrigins } from '@parhelia/next/contentful'; // a CMS's own helpers: its entry
|
|
17
|
+
|
|
18
|
+
<ContentfulLivePreviewProvider locale={locale} targetOrigin={contentfulLivePreviewOrigins()}>
|
|
19
|
+
<ParheliaBridge />
|
|
20
|
+
{children}
|
|
21
|
+
</ContentfulLivePreviewProvider>;
|
|
22
|
+
|
|
23
|
+
// a root-relative URL below the rendering proxy: a browser fetch, or markup (the server render writes the same URL)
|
|
24
|
+
fetch(withBasePath('/api/content'), { method: 'POST', body });
|
|
25
|
+
<img src={withBasePath('/logo.svg')} alt="" />;
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
| Setting | Read | Effect |
|
|
29
|
+
|---|---|---|
|
|
30
|
+
| `PARHELIA_RENDERING_PROXY_PATH` (`/parhelia/proxy/<head name>`) | at build | the basePath; `NEXT_PUBLIC_BASE_PATH` is read when it is unset, `/` means none |
|
|
31
|
+
| `PARHELIA_ORIGIN` (e.g. `https://cms.example.com`) | at build | the editor may frame the head directly (CSP `frame-ancestors`; `X-Frame-Options` gives way to the same list), and the browser loads the editing bridge from it |
|
|
32
|
+
|
|
33
|
+
Both come from the head's **Head settings** block in Parhelia (Settings › Rendering Hosts).
|
|
34
|
+
|
|
35
|
+
- **Proxy mode** (the editor frames `/parhelia/proxy/<head name>/…` from its own origin) needs only the base path:
|
|
36
|
+
`contentfulLivePreviewOrigins()` adds the page's own origin in the browser, and Parhelia injects the bridge itself.
|
|
37
|
+
Moving the editor to another origin needs no rebuild.
|
|
38
|
+
- **Direct mode** (the editor frames the head's own URL) needs `PARHELIA_ORIGIN`: the framing rule, the Live Preview
|
|
39
|
+
target and the bridge (`ParheliaBridge` loads it after hydration on `?parhelia=1`, only in a cross-origin frame;
|
|
40
|
+
`ParheliaBridgeScript` renders it into the server's HTML instead, with a CSP nonce: see `@parhelia/react`).
|
|
41
|
+
|
|
42
|
+
Without `withParhelia` the helpers fall back to nothing: no basePath, no Parhelia origin.
|
|
43
|
+
|
|
44
|
+
### Server render and browser read the same values
|
|
45
|
+
|
|
46
|
+
`withParhelia` inlines both values into the head's bundles and adds `@parhelia/next` to `transpilePackages` (after the
|
|
47
|
+
head's own entries). That entry matters: Next.js inlines only into code it bundles, and the Pages Router's server loads a
|
|
48
|
+
package from `node_modules` at run time instead (webpack and Turbopack alike). Without it, `withBasePath('/logo.svg')`
|
|
49
|
+
rendered `/logo.svg` on the server and `/parhelia/proxy/<head name>/logo.svg` in the browser: a broken URL in the server's
|
|
50
|
+
HTML and a hydration mismatch. A head that lists the package in `serverExternalPackages` keeps it external (Next.js
|
|
51
|
+
refuses a package in both lists) and renders on the server with what the server's own environment sets.
|
|
52
|
+
|
|
53
|
+
A CMS's own helpers live in its entry (`@parhelia/next/contentful`). The root still re-exports `contentfulLivePreviewOrigins`
|
|
54
|
+
and `CONTENTFUL_APP_ORIGINS` for heads built before the entry existed (deprecated; removed in 2.0).
|
|
55
|
+
|
|
56
|
+
## Everything else: `@parhelia/head` and `@parhelia/react`
|
|
57
|
+
|
|
58
|
+
This package is the Next.js adapter over [`@parhelia/head`](https://www.npmjs.com/package/@parhelia/head), which holds everything a head renders with
|
|
59
|
+
whatever its framework (the frame-mode marker and empty-component hints, the rendered identity `/identity`, the editor signal
|
|
60
|
+
`/server`, rich-text sanitising `/rich-text`, the in-place refresh `/refresh`, the CMS entries `/contentful` and
|
|
61
|
+
`/umbraco`), and [`@parhelia/react`](https://www.npmjs.com/package/@parhelia/react), its React components (`ParheliaBridge`,
|
|
62
|
+
`ParheliaBridgeScript`, `ParheliaSlot`, `useParheliaRefresh`). Every entry of this package but `/config` is the same-named
|
|
63
|
+
entry of `@parhelia/head`, and the root also re-exports `@parhelia/react`: the very same bindings, so existing imports keep
|
|
64
|
+
working; read their documentation there. `withParhelia` transpiles all three packages, so the server render reads the
|
|
65
|
+
build's values as the browser does.
|
package/package.json
CHANGED
|
@@ -1,6 +1,126 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@parhelia/next",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.12951",
|
|
4
|
+
"description": "Serve a Next.js rendering head in the Parhelia editor: withParhelia() for next.config.js (/config), plus the React bindings of @parhelia/react and everything of @parhelia/head under the same entries (the editing bridge, slot markers, the in-place refresh, the rendered-content identity, the editor signal a head's server checks, rich-text sanitising, per-CMS marker helpers), and withParheliaEditingRender() for a SitecoreAI editing host (/sitecore).",
|
|
5
|
+
"license": "SEE LICENSE IN LICENSE",
|
|
6
|
+
"publishConfig": {
|
|
7
|
+
"access": "public"
|
|
8
|
+
},
|
|
9
|
+
"type": "module",
|
|
10
|
+
"main": "./src/index.js",
|
|
11
|
+
"types": "./src/index.d.ts",
|
|
12
|
+
"exports": {
|
|
13
|
+
".": {
|
|
14
|
+
"types": "./src/index.d.ts",
|
|
15
|
+
"default": "./src/index.js"
|
|
16
|
+
},
|
|
17
|
+
"./identity": {
|
|
18
|
+
"types": "./src/identity.d.ts",
|
|
19
|
+
"default": "./src/identity.js"
|
|
20
|
+
},
|
|
21
|
+
"./server": {
|
|
22
|
+
"types": "./src/server.d.ts",
|
|
23
|
+
"default": "./src/server.js"
|
|
24
|
+
},
|
|
25
|
+
"./contentful": {
|
|
26
|
+
"types": "./src/contentful.d.ts",
|
|
27
|
+
"default": "./src/contentful.js"
|
|
28
|
+
},
|
|
29
|
+
"./umbraco": {
|
|
30
|
+
"types": "./src/umbraco.d.ts",
|
|
31
|
+
"default": "./src/umbraco.js"
|
|
32
|
+
},
|
|
33
|
+
"./magnolia": {
|
|
34
|
+
"types": "./src/magnolia.d.ts",
|
|
35
|
+
"default": "./src/magnolia.js"
|
|
36
|
+
},
|
|
37
|
+
"./typo3": {
|
|
38
|
+
"types": "./src/typo3.d.ts",
|
|
39
|
+
"default": "./src/typo3.js"
|
|
40
|
+
},
|
|
41
|
+
"./sitecore": {
|
|
42
|
+
"types": "./src/sitecore.d.ts",
|
|
43
|
+
"default": "./src/sitecore.js"
|
|
44
|
+
},
|
|
45
|
+
"./refresh": {
|
|
46
|
+
"types": "./src/refresh.d.ts",
|
|
47
|
+
"default": "./src/refresh.js"
|
|
48
|
+
},
|
|
49
|
+
"./rich-text": {
|
|
50
|
+
"types": "./src/rich-text.d.ts",
|
|
51
|
+
"default": "./src/rich-text.js"
|
|
52
|
+
},
|
|
53
|
+
"./config": {
|
|
54
|
+
"types": "./src/config.d.cts",
|
|
55
|
+
"default": "./src/config.cjs"
|
|
56
|
+
}
|
|
57
|
+
},
|
|
58
|
+
"typesVersions": {
|
|
59
|
+
"*": {
|
|
60
|
+
"identity": [
|
|
61
|
+
"./src/identity.d.ts"
|
|
62
|
+
],
|
|
63
|
+
"server": [
|
|
64
|
+
"./src/server.d.ts"
|
|
65
|
+
],
|
|
66
|
+
"contentful": [
|
|
67
|
+
"./src/contentful.d.ts"
|
|
68
|
+
],
|
|
69
|
+
"umbraco": [
|
|
70
|
+
"./src/umbraco.d.ts"
|
|
71
|
+
],
|
|
72
|
+
"magnolia": [
|
|
73
|
+
"./src/magnolia.d.ts"
|
|
74
|
+
],
|
|
75
|
+
"typo3": [
|
|
76
|
+
"./src/typo3.d.ts"
|
|
77
|
+
],
|
|
78
|
+
"sitecore": [
|
|
79
|
+
"./src/sitecore.d.ts"
|
|
80
|
+
],
|
|
81
|
+
"refresh": [
|
|
82
|
+
"./src/refresh.d.ts"
|
|
83
|
+
],
|
|
84
|
+
"rich-text": [
|
|
85
|
+
"./src/rich-text.d.ts"
|
|
86
|
+
],
|
|
87
|
+
"config": [
|
|
88
|
+
"./src/config.d.cts"
|
|
89
|
+
]
|
|
90
|
+
}
|
|
91
|
+
},
|
|
92
|
+
"files": [
|
|
93
|
+
"src/*.js",
|
|
94
|
+
"src/*.cjs",
|
|
95
|
+
"src/*.d.ts",
|
|
96
|
+
"src/*.d.cts",
|
|
97
|
+
"README.md",
|
|
98
|
+
"LICENSE"
|
|
99
|
+
],
|
|
100
|
+
"sideEffects": false,
|
|
101
|
+
"scripts": {
|
|
102
|
+
"check-types": "tsc --noEmit -p tsconfig.json",
|
|
103
|
+
"test": "vitest run"
|
|
104
|
+
},
|
|
105
|
+
"dependencies": {
|
|
106
|
+
"@parhelia/head": "1.0.12951",
|
|
107
|
+
"@parhelia/react": "1.0.12951"
|
|
108
|
+
},
|
|
109
|
+
"peerDependencies": {
|
|
110
|
+
"next": ">=15.5",
|
|
111
|
+
"react": ">=18"
|
|
112
|
+
},
|
|
113
|
+
"peerDependenciesMeta": {
|
|
114
|
+
"next": {
|
|
115
|
+
"optional": true
|
|
116
|
+
}
|
|
117
|
+
},
|
|
118
|
+
"devDependencies": {
|
|
119
|
+
"@types/node": "*",
|
|
120
|
+
"@types/react": "19.2.18",
|
|
121
|
+
"jsdom": "^30.0.1",
|
|
122
|
+
"next": "16.4.2",
|
|
123
|
+
"typescript": "npm:@typescript/typescript6@6.0.2",
|
|
124
|
+
"vitest": "^4.1.11"
|
|
125
|
+
}
|
|
126
|
+
}
|
package/src/config.cjs
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* `@parhelia/next/config` — `withParhelia(nextConfig)` for a Next.js rendering head (setup plan E).
|
|
4
|
+
*
|
|
5
|
+
* CommonJS on purpose: most heads have a `next.config.js` that `require`s its plugins. Plain JavaScript on purpose: the
|
|
6
|
+
* package is copied into heads that are built elsewhere (the Marketing starter on its own VM), so it needs no build.
|
|
7
|
+
* Everything a head renders with lives in `@parhelia/head` and `@parhelia/react` (this package re-exports both); this file
|
|
8
|
+
* is the Next.js build adapter, the counterpart of `@parhelia/head/vite`. The settings it reads are `@parhelia/head/settings`'.
|
|
9
|
+
*
|
|
10
|
+
* It reads two settings while Next.js loads the config (Next.js bakes basePath and headers into the build):
|
|
11
|
+
*
|
|
12
|
+
* PARHELIA_RENDERING_PROXY_PATH `/parhelia/proxy/<head name>`: served below Parhelia's same-origin rendering proxy
|
|
13
|
+
* (the basePath). `NEXT_PUBLIC_BASE_PATH` is read when it is unset; `/` means none.
|
|
14
|
+
* PARHELIA_ORIGIN the Parhelia editor's origin. It may frame the head directly (its CSP frame-ancestors;
|
|
15
|
+
* X-Frame-Options cannot name another origin and gives way to the same list) and is
|
|
16
|
+
* handed to the browser for the editing bridge and Contentful Live Preview.
|
|
17
|
+
*
|
|
18
|
+
* A proxied head needs neither origin nor framing rule: the editor frames it from its own origin.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
const { originFromEnv: parheliaOrigin, basePathFromEnv: proxyBasePath } = require("@parhelia/head/settings");
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Wraps a Next.js config: the basePath below the rendering proxy, the editor allowed to frame the head, and both
|
|
25
|
+
* values inlined into the head's bundles as `process.env.PARHELIA_BASE_PATH` / `process.env.PARHELIA_ORIGIN` (read by
|
|
26
|
+
* `withBasePath`, `contentfulLivePreviewOrigins` and `ParheliaBridge`), this package bundled on the server as well. A
|
|
27
|
+
* config function (next-compose-plugins, `(phase, context) => config`) is wrapped as a function. A basePath the config
|
|
28
|
+
* sets itself is kept.
|
|
29
|
+
*/
|
|
30
|
+
function withParhelia(nextConfig, options) {
|
|
31
|
+
const env = (options && options.env) || process.env;
|
|
32
|
+
if (typeof nextConfig === "function")
|
|
33
|
+
return async (phase, context) => applyParhelia(await nextConfig(phase, context), env);
|
|
34
|
+
return applyParhelia(nextConfig || {}, env);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function applyParhelia(config, env) {
|
|
38
|
+
const origin = parheliaOrigin(env);
|
|
39
|
+
const basePath = Object.prototype.hasOwnProperty.call(config, "basePath") ? config.basePath || "" : proxyBasePath(env);
|
|
40
|
+
const result = {
|
|
41
|
+
...config,
|
|
42
|
+
// Next.js inlines `env` into the code it bundles; values must be strings.
|
|
43
|
+
env: { ...config.env, PARHELIA_BASE_PATH: basePath, PARHELIA_ORIGIN: origin },
|
|
44
|
+
};
|
|
45
|
+
// The Pages Router's server does not bundle a package from node_modules (webpack and Turbopack alike): it loads it at
|
|
46
|
+
// run time, where nothing is inlined, and `withBasePath` would answer without the basePath in the server render (a
|
|
47
|
+
// hydration mismatch). Transpiled, the packages are bundled and read the build's values on the server as in the browser:
|
|
48
|
+
// this one, `@parhelia/react` and `@parhelia/head`, where the reads are.
|
|
49
|
+
const missing = packagesToTranspile(config);
|
|
50
|
+
if (missing.length > 0) result.transpilePackages = [...(config.transpilePackages || []), ...missing];
|
|
51
|
+
if (basePath) result.basePath = basePath;
|
|
52
|
+
if (origin) {
|
|
53
|
+
const headers = config.headers;
|
|
54
|
+
result.headers = async () => allowParheliaFrame(typeof headers === "function" ? await headers() : [], origin);
|
|
55
|
+
}
|
|
56
|
+
return result;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const PACKAGE_NAMES = ["@parhelia/next", "@parhelia/react", "@parhelia/head"];
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* The packages the config does not yet say how Next.js loads: neither transpiled nor kept external by the head
|
|
63
|
+
* (`serverExternalPackages`; Next.js refuses a package in both lists). A head that keeps `@parhelia/next` external renders
|
|
64
|
+
* on the server with what the server's own environment sets, and gets neither added.
|
|
65
|
+
*/
|
|
66
|
+
function packagesToTranspile(config) {
|
|
67
|
+
const listed = (name, lists) => lists.some((list) => Array.isArray(list) && list.includes(name));
|
|
68
|
+
const external = [config.serverExternalPackages, config.experimental && config.experimental.serverComponentsExternalPackages];
|
|
69
|
+
if (listed(PACKAGE_NAMES[0], external)) return [];
|
|
70
|
+
return PACKAGE_NAMES.filter((name) => !listed(name, [config.transpilePackages, ...external]));
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// A CSP directive names its sources separated by ASCII whitespace (tab, line feed, form feed, carriage return, space), any
|
|
74
|
+
// of them, any number; other white space is part of a source. The Umbraco package's FrameAncestors reads them by the same set.
|
|
75
|
+
const CSP_WHITESPACE = /[\t\n\f\r ]+/;
|
|
76
|
+
const FRAME_ANCESTORS = /^[\t\n\f\r ]*frame-ancestors([\t\n\f\r ]|$)/i;
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Lets `origin` frame every route a header rule covers: an existing CSP `frame-ancestors` list gains it ('none', in any
|
|
80
|
+
* case, gives way; its sources are read as a browser reads them), and an X-Frame-Options header is replaced by the same rule
|
|
81
|
+
* as a `frame-ancestors` list (`SAMEORIGIN` → 'self' plus the origin, `DENY` → the origin only). A rule with neither leaves
|
|
82
|
+
* framing unrestricted, as it was.
|
|
83
|
+
*/
|
|
84
|
+
function allowParheliaFrame(rules, origin) {
|
|
85
|
+
return (rules || []).map((rule) => {
|
|
86
|
+
if (!rule || !Array.isArray(rule.headers)) return rule;
|
|
87
|
+
let frameOptions = null;
|
|
88
|
+
let listed = false;
|
|
89
|
+
const headers = [];
|
|
90
|
+
for (const header of rule.headers) {
|
|
91
|
+
const key = String(header.key).toLowerCase();
|
|
92
|
+
if (key === "x-frame-options") {
|
|
93
|
+
frameOptions = String(header.value).trim().toUpperCase();
|
|
94
|
+
continue;
|
|
95
|
+
}
|
|
96
|
+
if (key === "content-security-policy") {
|
|
97
|
+
const directives = String(header.value).split(";");
|
|
98
|
+
const index = directives.findIndex((directive) => FRAME_ANCESTORS.test(directive));
|
|
99
|
+
if (index >= 0) {
|
|
100
|
+
listed = true;
|
|
101
|
+
// CSP keywords are case-insensitive: 'NONE' refuses every frame as 'none' does.
|
|
102
|
+
const sources = directives[index]
|
|
103
|
+
.split(CSP_WHITESPACE)
|
|
104
|
+
.filter(Boolean)
|
|
105
|
+
.slice(1)
|
|
106
|
+
.filter((source) => source.toLowerCase() !== "'none'");
|
|
107
|
+
if (!sources.includes(origin)) sources.push(origin);
|
|
108
|
+
directives[index] = `${index === 0 ? "" : " "}frame-ancestors ${sources.join(" ")}`;
|
|
109
|
+
headers.push({ ...header, value: directives.join(";") });
|
|
110
|
+
continue;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
headers.push(header);
|
|
114
|
+
}
|
|
115
|
+
if (frameOptions !== null && !listed) {
|
|
116
|
+
const sources = frameOptions === "DENY" ? [origin] : ["'self'", origin];
|
|
117
|
+
const directive = `frame-ancestors ${sources.join(" ")}`;
|
|
118
|
+
const policy = headers.findIndex((header) => String(header.key).toLowerCase() === "content-security-policy");
|
|
119
|
+
if (policy >= 0) {
|
|
120
|
+
const value = String(headers[policy].value).trim().replace(/;\s*$/, "");
|
|
121
|
+
headers[policy] = { ...headers[policy], value: value ? `${value}; ${directive}` : directive };
|
|
122
|
+
} else headers.push({ key: "Content-Security-Policy", value: directive });
|
|
123
|
+
}
|
|
124
|
+
return { ...rule, headers };
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
module.exports = { withParhelia, allowParheliaFrame, parheliaOrigin, proxyBasePath };
|
package/src/config.d.cts
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/** A Next.js config object or config function; typed loosely so the package needs no `next` types. */
|
|
2
|
+
export type NextConfigLike = Record<string, any>;
|
|
3
|
+
export type NextConfigFunction = (phase: string, context: any) => NextConfigLike | Promise<NextConfigLike>;
|
|
4
|
+
|
|
5
|
+
export interface HeaderRule {
|
|
6
|
+
source: string;
|
|
7
|
+
headers: { key: string; value: string }[];
|
|
8
|
+
[key: string]: unknown;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface WithParheliaOptions {
|
|
12
|
+
/** The environment to read (default `process.env`). */
|
|
13
|
+
env?: Record<string, string | undefined>;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** What `withParhelia` adds to a config. */
|
|
17
|
+
export interface ParheliaConfig {
|
|
18
|
+
basePath?: string;
|
|
19
|
+
/** Inlined into the head's bundles: `PARHELIA_BASE_PATH` and `PARHELIA_ORIGIN` ("" when unset). */
|
|
20
|
+
env: Record<string, string | undefined>;
|
|
21
|
+
/**
|
|
22
|
+
* The config's own list plus `@parhelia/next`, `@parhelia/react` and `@parhelia/head` where it does not name them yet, so the server render
|
|
23
|
+
* reads the inlined values as the browser does; unchanged when it keeps `@parhelia/next` in `serverExternalPackages`.
|
|
24
|
+
*/
|
|
25
|
+
transpilePackages?: string[];
|
|
26
|
+
/** Present when `PARHELIA_ORIGIN` is set: the config's own rules with the editor allowed to frame. */
|
|
27
|
+
headers?: () => Promise<HeaderRule[]>;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Serves the head below Parhelia's rendering proxy (`PARHELIA_RENDERING_PROXY_PATH`, else `NEXT_PUBLIC_BASE_PATH`, as
|
|
32
|
+
* basePath) and lets the Parhelia editor (`PARHELIA_ORIGIN`) frame it; exposes both to the browser bundle.
|
|
33
|
+
*/
|
|
34
|
+
export declare function withParhelia(
|
|
35
|
+
nextConfig: NextConfigFunction,
|
|
36
|
+
options?: WithParheliaOptions,
|
|
37
|
+
): (phase: string, context: any) => Promise<NextConfigLike & ParheliaConfig>;
|
|
38
|
+
export declare function withParhelia<T extends NextConfigLike>(
|
|
39
|
+
nextConfig?: T,
|
|
40
|
+
options?: WithParheliaOptions,
|
|
41
|
+
): Omit<T, keyof ParheliaConfig> & ParheliaConfig;
|
|
42
|
+
|
|
43
|
+
/** Lets `origin` frame every route the rules cover (CSP frame-ancestors; X-Frame-Options gives way). */
|
|
44
|
+
export declare function allowParheliaFrame<T extends HeaderRule>(rules: T[], origin: string): T[];
|
|
45
|
+
|
|
46
|
+
/** `PARHELIA_ORIGIN` trimmed, without a trailing slash; "" when unset. Throws for a value that is not an origin. */
|
|
47
|
+
export declare function parheliaOrigin(env?: Record<string, string | undefined>): string;
|
|
48
|
+
|
|
49
|
+
/** `PARHELIA_RENDERING_PROXY_PATH` (else `NEXT_PUBLIC_BASE_PATH`) without a trailing slash; "" when unset or `/`. */
|
|
50
|
+
export declare function proxyBasePath(env?: Record<string, string | undefined>): string;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "@parhelia/head/contentful";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "@parhelia/head/identity";
|
package/src/identity.js
ADDED
package/src/index.d.ts
ADDED
package/src/index.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@parhelia/next` — Parhelia for a Next.js rendering head: `withParhelia` (`@parhelia/next/config`, the build adapter),
|
|
3
|
+
* the React bindings of `@parhelia/react` and everything of `@parhelia/head`, which this package re-exports at the same
|
|
4
|
+
* paths (`@parhelia/next/server` is `@parhelia/head/server`, and so on). Heads on another framework use those directly.
|
|
5
|
+
*/
|
|
6
|
+
export * from "@parhelia/head";
|
|
7
|
+
export * from "@parhelia/react";
|
|
8
|
+
// Umbraco program thirdcms-frontend: kept on the root only for heads built before the vendor entry existed (the patched
|
|
9
|
+
// Marketing starter imports them from here); new heads import `@parhelia/next/contentful`. Removed with @parhelia/next 2.0.
|
|
10
|
+
export * from "@parhelia/head/contentful";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "@parhelia/head/magnolia";
|
package/src/magnolia.js
ADDED
package/src/refresh.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "@parhelia/head/refresh";
|
package/src/refresh.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "@parhelia/head/rich-text";
|
package/src/rich-text.js
ADDED
package/src/server.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "@parhelia/head/server";
|
package/src/server.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export * from "@parhelia/head/sitecore";
|
|
2
|
+
|
|
3
|
+
/** How Parhelia's canvas render is answered. */
|
|
4
|
+
export type ParheliaEditingRenderOptions = {
|
|
5
|
+
/** The CM's origin, which serves the editing bridge; `PARHELIA_SITECORE_ORIGIN` without one. */
|
|
6
|
+
sitecoreOrigin?: string;
|
|
7
|
+
/** The site when the CM's layout data names none (for example `sitecore.config`'s `defaultSite`). */
|
|
8
|
+
defaultSite?: string;
|
|
9
|
+
/** The language when the CM's request names none; `en` without one. */
|
|
10
|
+
defaultLanguage?: string;
|
|
11
|
+
/** Another address of the editing bridge than the CM's own. */
|
|
12
|
+
bridgeUrl?: string;
|
|
13
|
+
/** The page's name in the bridge's tag (`data-parhelia-source`); `sitecore` without one. */
|
|
14
|
+
source?: string;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
type RequestOf<H> = H extends { GET: (req: infer R) => unknown } ? R : Request;
|
|
18
|
+
|
|
19
|
+
/** The SDK's editing render handlers with a `POST` that also answers the CM's render request for Parhelia's canvas. */
|
|
20
|
+
export declare function withParheliaEditingRender<H extends { GET: (req: never) => Response | Promise<Response> }>(
|
|
21
|
+
handlers: H,
|
|
22
|
+
options?: ParheliaEditingRenderOptions,
|
|
23
|
+
): Omit<H, "POST"> & { POST: (req: RequestOf<H>) => Promise<Response> };
|
package/src/sitecore.js
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@parhelia/next/sitecore` is `@parhelia/head/sitecore`, plus {@link withParheliaEditingRender}: a SitecoreAI editing host's
|
|
3
|
+
* editing render route that also renders Parhelia's canvas. In `src/app/api/editing/render/route.ts`:
|
|
4
|
+
*
|
|
5
|
+
* import { createEditingRenderRouteHandlers } from '@sitecore-content-sdk/nextjs/route-handler';
|
|
6
|
+
* import { withParheliaEditingRender } from '@parhelia/next/sitecore';
|
|
7
|
+
*
|
|
8
|
+
* export const { GET, POST, OPTIONS } = withParheliaEditingRender(createEditingRenderRouteHandlers({}));
|
|
9
|
+
*
|
|
10
|
+
* Pages' requests carry an `Origin` and reach the Content SDK's handlers untouched. The CM's render request for the canvas is a
|
|
11
|
+
* server-to-server `POST` without one: it is rendered through the SDK's own `GET` in edit mode, and the page gets the editing
|
|
12
|
+
* bridge, loaded from the CM that `PARHELIA_SITECORE_ORIGIN` names.
|
|
13
|
+
*/
|
|
14
|
+
import { NextRequest } from "next/server.js";
|
|
15
|
+
|
|
16
|
+
import { addParheliaBridge, isParheliaRender, readSitecoreRenderRequest, sitecoreEditingQuery } from "@parhelia/head/sitecore";
|
|
17
|
+
|
|
18
|
+
export * from "@parhelia/head/sitecore";
|
|
19
|
+
|
|
20
|
+
const MISSING_ORIGIN =
|
|
21
|
+
"<html><body>Set PARHELIA_SITECORE_ORIGIN to the CM's origin, so the page can load Parhelia's editing bridge</body></html>";
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* The SDK's editing render handlers with a `POST` that also answers the CM's render request for Parhelia's canvas.
|
|
25
|
+
* @template {{ GET: (req: any) => Response | Promise<Response>, POST?: (req: any) => Response | Promise<Response> }} H
|
|
26
|
+
* @param {H} handlers
|
|
27
|
+
* @param {{ sitecoreOrigin?: string, defaultSite?: string, defaultLanguage?: string, bridgeUrl?: string, source?: string }} [options]
|
|
28
|
+
*/
|
|
29
|
+
export function withParheliaEditingRender(handlers, options = {}) {
|
|
30
|
+
/** @param {Request} req */
|
|
31
|
+
const POST = async (req) => {
|
|
32
|
+
if (req.headers.get("origin")) return handlers.POST ? handlers.POST(req) : new Response(null, { status: 405 });
|
|
33
|
+
|
|
34
|
+
const body = await req.json().catch(() => undefined);
|
|
35
|
+
const read = readSitecoreRenderRequest(body, {
|
|
36
|
+
fallbackQuery: new URL(req.url).searchParams,
|
|
37
|
+
defaultSite: options.defaultSite,
|
|
38
|
+
defaultLanguage: options.defaultLanguage,
|
|
39
|
+
});
|
|
40
|
+
if (!read.ok) return Response.json({ html: read.html }, { status: read.status });
|
|
41
|
+
|
|
42
|
+
const withBridge = isParheliaRender(read.request);
|
|
43
|
+
const sitecoreOrigin = options.sitecoreOrigin ?? process.env.PARHELIA_SITECORE_ORIGIN;
|
|
44
|
+
if (withBridge && !sitecoreOrigin) return Response.json({ html: MISSING_ORIGIN }, { status: 500 });
|
|
45
|
+
|
|
46
|
+
const url = new URL(req.url);
|
|
47
|
+
url.search = sitecoreEditingQuery(read.request).toString();
|
|
48
|
+
const headers = new Headers(req.headers);
|
|
49
|
+
headers.delete("content-length");
|
|
50
|
+
headers.delete("content-type");
|
|
51
|
+
const rendered = await handlers.GET(new NextRequest(url, { headers }));
|
|
52
|
+
if (!rendered.ok) return rendered;
|
|
53
|
+
|
|
54
|
+
const html = await rendered.text();
|
|
55
|
+
const page =
|
|
56
|
+
withBridge && sitecoreOrigin
|
|
57
|
+
? addParheliaBridge(html, { sitecoreOrigin, bridgeUrl: options.bridgeUrl, source: options.source })
|
|
58
|
+
: html;
|
|
59
|
+
const responseHeaders = new Headers(rendered.headers);
|
|
60
|
+
responseHeaders.delete("content-length");
|
|
61
|
+
responseHeaders.set("content-type", "application/json; charset=utf-8");
|
|
62
|
+
return new Response(JSON.stringify({ html: page }), {
|
|
63
|
+
status: rendered.status,
|
|
64
|
+
statusText: rendered.statusText,
|
|
65
|
+
headers: responseHeaders,
|
|
66
|
+
});
|
|
67
|
+
};
|
|
68
|
+
return { ...handlers, POST };
|
|
69
|
+
}
|
package/src/typo3.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "@parhelia/head/typo3";
|
package/src/typo3.js
ADDED
package/src/umbraco.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "@parhelia/head/umbraco";
|
package/src/umbraco.js
ADDED