@decocms/tanstack 8.14.0 → 8.16.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 +2 -2
- package/dist/react/DecoDocument.d.ts +3 -3
- package/dist/react/DecoDocument.d.ts.map +1 -1
- package/dist/react/DecoDocument.js +21 -12
- package/dist/react/DecoDocument.js.map +1 -1
- package/dist/react/StudioRefresh.d.ts +2 -0
- package/dist/react/StudioRefresh.d.ts.map +1 -0
- package/dist/react/StudioRefresh.js +66 -0
- package/dist/react/StudioRefresh.js.map +1 -0
- package/dist/studio.d.ts +6 -2
- package/dist/studio.d.ts.map +1 -1
- package/dist/studio.js +6 -2
- package/dist/studio.js.map +1 -1
- package/dist/studioShortcut.d.ts +52 -0
- package/dist/studioShortcut.d.ts.map +1 -0
- package/dist/studioShortcut.js +74 -0
- package/dist/studioShortcut.js.map +1 -0
- package/package.json +2 -2
- package/src/react/DecoDocument.tsx +23 -14
- package/src/react/StudioRefresh.ts +66 -0
- package/src/studio.ts +7 -2
- package/src/studioShortcut.ts +99 -0
package/README.md
CHANGED
|
@@ -58,7 +58,7 @@ A complete worker entry with a page cache is [`examples/tanstack-worker-smoke/sr
|
|
|
58
58
|
|
|
59
59
|
### Names and Studio's origins
|
|
60
60
|
|
|
61
|
-
The root export holds what the worker, `/server` and `DecoDocument` share, for a site's own code: the draft names (`DRAFT_PARAM` `__draft`, `DRAFT_COOKIE` `__deco_draft`), the headers the worker hands the app (`VERSION_HEADER` `x-deco-version`, `VISITOR_HEADER` `x-deco-visitor`, `ASSIGNMENTS_HEADER` `x-deco-assignments`), `draftResponseHeaders(preview)` (`max-age=0, no-cache, must-revalidate`, `Vary: Cookie`, `noindex`; pass the version, or `true` for any request with a draft signal), and Studio's origins: `STUDIO_ORIGINS`, `STUDIO_FRAME_ANCESTORS` (what the worker's `frame-ancestors` starts from) and `isStudioOrigin`; the Studio channel's exact list, `DEFAULT_STUDIO_ORIGINS` and `studioOrigins(option?)`; and `withStudioCsp(directives)`, what the worker does to the site's `csp` (Studio's origins in `script-src` and `frame-ancestors`), for a policy the site sends some other way.
|
|
61
|
+
The root export holds what the worker, `/server` and `DecoDocument` share, for a site's own code: the draft names (`DRAFT_PARAM` `__draft`, `DRAFT_COOKIE` `__deco_draft`), the headers the worker hands the app (`VERSION_HEADER` `x-deco-version`, `VISITOR_HEADER` `x-deco-visitor`, `ASSIGNMENTS_HEADER` `x-deco-assignments`), `draftResponseHeaders(preview)` (`max-age=0, no-cache, must-revalidate`, `Vary: Cookie`, `noindex`; pass the version, or `true` for any request with a draft signal), and Studio's origins: `STUDIO_ORIGINS`, `STUDIO_FRAME_ANCESTORS` (what the worker's `frame-ancestors` starts from) and `isStudioOrigin`; the Studio channel's exact list, `DEFAULT_STUDIO_ORIGINS` and `studioOrigins(option?)`; and `withStudioCsp(directives)`, what the worker does to the site's `csp` (Studio's origins in `script-src` and `frame-ancestors`), for a policy the site sends some other way.
|
|
62
62
|
|
|
63
63
|
```ts
|
|
64
64
|
import { STUDIO_ORIGINS, VERSION_HEADER } from "@decocms/tanstack";
|
|
@@ -81,7 +81,7 @@ export const startInstance = createStart(() => ({ serverFns: { fetch: decoServer
|
|
|
81
81
|
export const Route = createRootRoute({ component: () => <DecoDocument site="my-store" /> });
|
|
82
82
|
```
|
|
83
83
|
|
|
84
|
-
`DecoDocument` mounts
|
|
84
|
+
`DecoDocument` mounts v7's "." edit shortcut, the Studio channel and the draft badge. It is v8 only: v7's framed handler (`scrollToComponent`, `editor::rerender`) is gone, so no script on the page compares the window with `top` or `parent`, and a test runner that rewrites "frame-busting" code (Cypress's `modifyObstructiveCode`) serves the page byte for byte. The Studio channel is on every page, production included: a small inline script (about 0.7 kB gzipped) through which Studio loads its own editor code (the section wireframe, the page swap after a save) into the page when it frames it. It is the only way Studio runs code in the page: the bridge evaluates no script Studio sends. The page trusts a list of exact Studio origins: the `studioOrigins` prop when given, else the `DECO_STUDIO_ORIGIN` environment variable, comma-separated (read on the server; a local Studio, such as `http://localhost:3000`), else Studio's production and staging origins (`DEFAULT_STUDIO_ORIGINS`). Framed, the page posts `{ type: "deco:ready" }` with each of them as the target origin (only a parent at one of them receives it) and imports a module only when Studio's answer, `{ type: "deco:load", url }`, comes from exactly one of them and names a URL on the origin of that message; not framed, or framed by any other origin, it does nothing. The worker adds the same list to every HTML response's CSP: to `frame-ancestors`, and to `script-src` (or `script-src-elem`; a policy that restricts scripts only through `default-src` gets a `script-src` with its sources plus Studio's). A site that sets `studioOrigins` on `DecoDocument` sets the worker's `studioOrigins` option too; `DECO_STUDIO_ORIGIN` sets both. The inline scripts take the site's `nonce`. The message and module types are in `@decocms/blocks/protocol`. A page route returns `pathTemplate` (the page block's `path`, as `matchRoute` matched it) in its loader data, so "." on the store page opens that page in Studio's editor. A site that keeps its own document (a skip link before `<main>`, say) renders `<DecoPreview site="my-store" />` in its `<body>` instead: the shortcut, the channel, the badge and the draft they read.
|
|
85
85
|
|
|
86
86
|
## Images
|
|
87
87
|
|
|
@@ -35,12 +35,12 @@ export interface DecoPreviewProps {
|
|
|
35
35
|
/**
|
|
36
36
|
* What `DecoDocument` mounts for Studio and draft previews, for a site that
|
|
37
37
|
* keeps its own document (a skip link before `<main>`, say): the request's
|
|
38
|
-
* draft and CDN segment token published to the browser,
|
|
39
|
-
* `DraftBadge`. Render it once, in the `<body>`.
|
|
38
|
+
* draft and CDN segment token published to the browser, the "." shortcut,
|
|
39
|
+
* `StudioChannel` and `DraftBadge`. Render it once, in the `<body>`.
|
|
40
40
|
*/
|
|
41
41
|
export declare function DecoPreview({ site, editShortcut, nonce, studioOrigins }: DecoPreviewProps): import("react").JSX.Element;
|
|
42
42
|
export interface DecoDocumentProps {
|
|
43
|
-
/** The site's name in Deco (the
|
|
43
|
+
/** The site's name in Deco (the "." shortcut opens the page under it). */
|
|
44
44
|
site: string;
|
|
45
45
|
/** `<html lang>`. Default `"en"`. */
|
|
46
46
|
lang?: string;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"DecoDocument.d.ts","sourceRoot":"","sources":["../../src/react/DecoDocument.tsx"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"DecoDocument.d.ts","sourceRoot":"","sources":["../../src/react/DecoDocument.tsx"],"names":[],"mappings":"AAgCA,OAAO,EAAE,KAAK,SAAS,EAA+B,MAAM,OAAO,CAAC;AAOpE,OAAO,CAAC,MAAM,CAAC;IACb,UAAU,MAAM;QACd,YAAY,CAAC,EAAE,OAAO,CAAC;KACxB;CACF;AAED,MAAM,WAAW,uBAAuB;IACtC,2FAA2F;IAC3F,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAED,0EAA0E;AAC1E,wBAAgB,kBAAkB,CAAC,EACjC,KAAsD,GACvD,EAAE,uBAAuB,sCAmBzB;AAED,6FAA6F;AAC7F,wBAAgB,YAAY,gCAa3B;AAqDD,MAAM,WAAW,gBAAgB;IAC/B,0EAA0E;IAC1E,IAAI,EAAE,MAAM,CAAC;IACb;;;;;OAKG;IACH,YAAY,CAAC,EAAE,OAAO,CAAC;IACvB,wEAAwE;IACxE,KAAK,CAAC,EAAE,MAAM,CAAC;IACf;;;;;OAKG;IACH,aAAa,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;CACnC;AAED;;;;;GAKG;AACH,wBAAgB,WAAW,CAAC,EAAE,IAAI,EAAE,YAAY,EAAE,KAAK,EAAE,aAAa,EAAE,EAAE,gBAAgB,+BAsBzF;AAED,MAAM,WAAW,iBAAiB;IAChC,0EAA0E;IAC1E,IAAI,EAAE,MAAM,CAAC;IACb,qCAAqC;IACrC,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,iEAAiE;IACjE,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,kFAAkF;IAClF,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,2EAA2E;IAC3E,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,0GAA0G;IAC1G,MAAM,CAAC,EAAE,MAAM,GAAG,KAAK,CAAC;IACxB;;;;;OAKG;IACH,YAAY,CAAC,EAAE,OAAO,CAAC;IACvB,wEAAwE;IACxE,KAAK,CAAC,EAAE,MAAM,CAAC;IACf;;;;;OAKG;IACH,aAAa,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;IAClC,4DAA4D;IAC5D,QAAQ,CAAC,EAAE,SAAS,CAAC;CACtB;AAED,wBAAgB,YAAY,CAAC,EAC3B,IAAI,EACJ,IAAW,EACX,aAA+C,EAC/C,OAAY,EACZ,UAAgB,EAChB,MAAe,EACf,YAAY,EACZ,KAAK,EACL,aAAa,EACb,QAAQ,GACT,EAAE,iBAAiB,+BA8CnB"}
|
|
@@ -9,24 +9,33 @@ import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-run
|
|
|
9
9
|
* keeps its height while the next page loads;
|
|
10
10
|
* - the CDN segment token and the draft this request renders, for
|
|
11
11
|
* `decoServerFnFetch` and the draft badge;
|
|
12
|
-
* - `StudioBridge` (in Studio's frame it answers Studio's origins,
|
|
13
|
-
* `STUDIO_ORIGINS`; outside it, "." opens the page in Studio's editor, with
|
|
14
|
-
* the page's `pathTemplate` from the route's loader data) and `DraftBadge`
|
|
15
|
-
* (only on a draft page outside Studio's frame), from `@decocms/blocks/preview`;
|
|
16
12
|
* - `StudioChannel` (every page, production included: framed by one of
|
|
17
13
|
* Studio's origins, `studioOrigins` / `DECO_STUDIO_ORIGIN` / Studio's
|
|
18
14
|
* production and staging origins, each exact, it loads the editor's own
|
|
19
|
-
* modules from that origin; otherwise it does nothing)
|
|
20
|
-
*
|
|
15
|
+
* modules from that origin; otherwise it does nothing) and `DraftBadge`
|
|
16
|
+
* (only on a draft page outside Studio's frame), from
|
|
17
|
+
* `@decocms/blocks/preview`. The worker adds the same origins to the
|
|
18
|
+
* page's CSP `script-src` and `frame-ancestors`;
|
|
19
|
+
* - `StudioRefresh`: after a save, Studio shows the new content by rerunning
|
|
20
|
+
* the loaders (`router.invalidate()`), with React still attached, instead
|
|
21
|
+
* of replacing the document;
|
|
22
|
+
* - v7's "." edit shortcut (`../studioShortcut.ts`: outside Studio's frame,
|
|
23
|
+
* "." opens the page in Studio's editor, with the page's `pathTemplate`
|
|
24
|
+
* from the route's loader data). v8 only: v7's framed handler
|
|
25
|
+
* (`scrollToComponent`, `editor::rerender`) is gone, so no script compares
|
|
26
|
+
* the window with `top` or `parent` in a form a test runner's
|
|
27
|
+
* "frame-busting" rewrite matches (Cypress's `modifyObstructiveCode`).
|
|
21
28
|
*
|
|
22
29
|
* A site that keeps its own document renders `DecoPreview` in its `<body>`
|
|
23
30
|
* for the bridge, the badge and the draft they read.
|
|
24
31
|
*/
|
|
25
|
-
import { DraftBadge,
|
|
32
|
+
import { DraftBadge, StudioChannel } from "@decocms/blocks/preview";
|
|
26
33
|
import { HeadContent, Outlet, ScriptOnce, Scripts, useRouterState } from "@tanstack/react-router";
|
|
27
34
|
import { useEffect, useRef, useState } from "react";
|
|
28
|
-
import {
|
|
35
|
+
import { studioOrigins } from "../studio.js";
|
|
36
|
+
import { StudioShortcut } from "../studioShortcut.js";
|
|
29
37
|
import { pageValues, publishScript } from "./pageValues.js";
|
|
38
|
+
import { StudioRefresh } from "./StudioRefresh.js";
|
|
30
39
|
import { DATA_EVENT_OBSERVER, eventsBootstrap, PROGRESS_CSS } from "./scripts.js";
|
|
31
40
|
/** A loading bar at the top of the page during client-side navigation. */
|
|
32
41
|
export function NavigationProgress({ color = "var(--color-brand-primary-500, currentColor)", }) {
|
|
@@ -66,18 +75,18 @@ function studioPageOf(matches) {
|
|
|
66
75
|
}
|
|
67
76
|
return undefined;
|
|
68
77
|
}
|
|
69
|
-
/** The
|
|
78
|
+
/** The "." shortcut, with the page on screen, the Studio channel, and the badge for the request's draft. */
|
|
70
79
|
function StudioPieces({ site, editShortcut, nonce, studioOrigins: origins, draft, draftUnavailable, }) {
|
|
71
80
|
// Two primitives, so a navigation that keeps the page doesn't re-render the document.
|
|
72
81
|
const pathTemplate = useRouterState({ select: (s) => studioPageOf(s.matches)?.pathTemplate });
|
|
73
82
|
const pageId = useRouterState({ select: (s) => studioPageOf(s.matches)?.id });
|
|
74
|
-
return (_jsxs(_Fragment, { children: [_jsx(
|
|
83
|
+
return (_jsxs(_Fragment, { children: [_jsx(StudioShortcut, { site: site, editShortcut: editShortcut, page: { pathTemplate, id: pageId }, nonce: nonce }), _jsx(StudioChannel, { studioOrigins: studioOrigins(origins), nonce: nonce }), _jsx(StudioRefresh, {}), _jsx(DraftBadge, { pointer: draft ?? null, unavailable: draftUnavailable })] }));
|
|
75
84
|
}
|
|
76
85
|
/**
|
|
77
86
|
* What `DecoDocument` mounts for Studio and draft previews, for a site that
|
|
78
87
|
* keeps its own document (a skip link before `<main>`, say): the request's
|
|
79
|
-
* draft and CDN segment token published to the browser,
|
|
80
|
-
* `DraftBadge`. Render it once, in the `<body>`.
|
|
88
|
+
* draft and CDN segment token published to the browser, the "." shortcut,
|
|
89
|
+
* `StudioChannel` and `DraftBadge`. Render it once, in the `<body>`.
|
|
81
90
|
*/
|
|
82
91
|
export function DecoPreview({ site, editShortcut, nonce, studioOrigins }) {
|
|
83
92
|
const values = pageValues();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"DecoDocument.js","sourceRoot":"","sources":["../../src/react/DecoDocument.tsx"],"names":[],"mappings":";AAAA
|
|
1
|
+
{"version":3,"file":"DecoDocument.js","sourceRoot":"","sources":["../../src/react/DecoDocument.tsx"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH,OAAO,EAAE,UAAU,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAC;AACpE,OAAO,EAAE,WAAW,EAAE,MAAM,EAAE,UAAU,EAAE,OAAO,EAAE,cAAc,EAAE,MAAM,wBAAwB,CAAC;AAClG,OAAO,EAAkB,SAAS,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AACpE,OAAO,EAAE,aAAa,EAAE,MAA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,gBAAgB,GACzC,EACF,KAAC,UAAU,cAAE,mBAAmB,GAAc,EAC9C,KAAC,OAAO,KAAG,IACN,IACF,CACR,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"StudioRefresh.d.ts","sourceRoot":"","sources":["../../src/react/StudioRefresh.ts"],"names":[],"mappings":"AAyBA,wBAAgB,aAAa,IAAI,IAAI,CAwCpC"}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The page's own refresh for Studio's HTML swap (`STUDIO_PAGE_REFRESH` of
|
|
3
|
+
* `@decocms/blocks/protocol`): after a save, Studio asks the page to show its
|
|
4
|
+
* new content. Replacing the document would leave React holding the old nodes,
|
|
5
|
+
* so every client component would stop at its server markup. Here the router
|
|
6
|
+
* does it instead and stays attached: `router.invalidate()` reruns the loaders
|
|
7
|
+
* and resolves once the page shows their data.
|
|
8
|
+
*
|
|
9
|
+
* A new URL of the page (a save moved the draft pointer in `?__draft=`) first
|
|
10
|
+
* goes to the worker as a document request, only for its draft cookie: the
|
|
11
|
+
* loaders' server-function calls carry the cookie, not the page's query. Then
|
|
12
|
+
* the router moves to the URL. A route whose loader deps include `__draft`
|
|
13
|
+
* loads again on that move; only the matches it didn't load are invalidated,
|
|
14
|
+
* so the page renders the save once.
|
|
15
|
+
*
|
|
16
|
+
* Set on every page, framed or not: Studio's module is the only caller, and
|
|
17
|
+
* comparing the window with its parent here is what a test runner's
|
|
18
|
+
* frame-busting rewrite matches (`tests/studio-shortcut.test.ts`).
|
|
19
|
+
*/
|
|
20
|
+
import { useRouter } from "@tanstack/react-router";
|
|
21
|
+
import { useEffect } from "react";
|
|
22
|
+
import { STUDIO_PAGE_REFRESH } from "../studio.js";
|
|
23
|
+
export function StudioRefresh() {
|
|
24
|
+
const router = useRouter();
|
|
25
|
+
useEffect(() => {
|
|
26
|
+
const refresh = async (url) => {
|
|
27
|
+
const target = new URL(url, location.href);
|
|
28
|
+
if (target.origin !== location.origin) {
|
|
29
|
+
throw new Error(`Not this page's origin: ${target.origin}`);
|
|
30
|
+
}
|
|
31
|
+
let loadedSince = null;
|
|
32
|
+
if (target.href !== location.href) {
|
|
33
|
+
const res = await fetch(target.href, {
|
|
34
|
+
headers: { accept: "text/html" },
|
|
35
|
+
credentials: "same-origin",
|
|
36
|
+
cache: "no-store",
|
|
37
|
+
});
|
|
38
|
+
await res.body?.cancel();
|
|
39
|
+
if (!res.ok)
|
|
40
|
+
throw new Error(`The page answered ${res.status}`);
|
|
41
|
+
loadedSince = Date.now();
|
|
42
|
+
await router.navigate({
|
|
43
|
+
href: `${target.pathname}${target.search}${target.hash}`,
|
|
44
|
+
replace: true,
|
|
45
|
+
resetScroll: false,
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
if (loadedSince === null)
|
|
49
|
+
return router.invalidate();
|
|
50
|
+
const since = loadedSince;
|
|
51
|
+
const stale = router.state.matches.filter((m) => m.updatedAt < since);
|
|
52
|
+
if (stale.length === 0)
|
|
53
|
+
return;
|
|
54
|
+
const ids = new Set(stale.map((m) => m.id));
|
|
55
|
+
await router.invalidate({ filter: (m) => ids.has(m.id) });
|
|
56
|
+
};
|
|
57
|
+
const slot = window;
|
|
58
|
+
slot[STUDIO_PAGE_REFRESH] = refresh;
|
|
59
|
+
return () => {
|
|
60
|
+
if (slot[STUDIO_PAGE_REFRESH] === refresh)
|
|
61
|
+
delete slot[STUDIO_PAGE_REFRESH];
|
|
62
|
+
};
|
|
63
|
+
}, [router]);
|
|
64
|
+
return null;
|
|
65
|
+
}
|
|
66
|
+
//# sourceMappingURL=StudioRefresh.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"StudioRefresh.js","sourceRoot":"","sources":["../../src/react/StudioRefresh.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AACH,OAAO,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAC;AACnD,OAAO,EAAE,SA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AE,OAAO,MAAM,CAAC,UAAU,EAAE,CAAC;YACrD,MAAM,KAAK,GAAG,WAAW,CAAC;YAC1B,MAAM,KAAK,GAAG,MAAM,CAAC,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,SAAS,GAAG,KAAK,CAAC,CAAC;YACtE,IAAI,KAAK,CAAC,MAAM,KAAK,CAAC;gBAAE,OAAO;YAC/B,MAAM,GAAG,GAAG,IAAI,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;YAC5C,MAAM,MAAM,CAAC,UAAU,CAAC,EAAE,MAAM,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC;QAC5D,CAAC,CAAC;QACF,MAAM,IAAI,GAAG,MAA4C,CAAC;QAC1D,IAAI,CAAC,mBAAmB,CAAC,GAAG,OAAO,CAAC;QACpC,OAAO,GAAG,EAAE;YACV,IAAI,IAAI,CAAC,mBAAmB,CAAC,KAAK,OAAO;gBAAE,OAAO,IAAI,CAAC,mBAAmB,CAAC,CAAC;QAC9E,CAAC,CAAC;IACJ,CAAC,EAAE,CAAC,MAAM,CAAC,CAAC,CAAC;IAEb,OAAO,IAAI,CAAC;AACd,CAAC"}
|
package/dist/studio.d.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Where Deco Studio runs: the origins its site editor frames a site from and
|
|
3
3
|
* talks to it from (`postMessage`). One list, so the worker's
|
|
4
|
-
* `frame-ancestors` and
|
|
5
|
-
* apart.
|
|
4
|
+
* `frame-ancestors` and a site's own never drift apart.
|
|
6
5
|
*
|
|
7
6
|
* No module state and no framework code: safe in a browser bundle.
|
|
8
7
|
*/
|
|
@@ -27,6 +26,11 @@ export declare const STUDIO_ORIGIN = "https://studio.decocms.com";
|
|
|
27
26
|
* staging origins. No wildcard: each one is compared exactly.
|
|
28
27
|
*/
|
|
29
28
|
export declare const DEFAULT_STUDIO_ORIGINS: readonly string[];
|
|
29
|
+
/**
|
|
30
|
+
* Where `StudioRefresh` puts the page's own refresh for Studio's HTML swap
|
|
31
|
+
* (`STUDIO_PAGE_REFRESH` of `@decocms/blocks/protocol`, held equal by a test).
|
|
32
|
+
*/
|
|
33
|
+
export declare const STUDIO_PAGE_REFRESH = "__decoStudioRefresh";
|
|
30
34
|
/**
|
|
31
35
|
* The Studio origins the page trusts: the Studio channel accepts messages and
|
|
32
36
|
* modules from them, and the binding adds them to the page's CSP `script-src`
|
package/dist/studio.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"studio.d.ts","sourceRoot":"","sources":["../src/studio.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"studio.d.ts","sourceRoot":"","sources":["../src/studio.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH;;;;GAIG;AACH,eAAO,MAAM,cAAc,EAAE,SAAS,MAAM,EAK1C,CAAC;AAEH;;;;;GAKG;AACH,eAAO,MAAM,sBAAsB,EAAE,SAAS,MAAM,EAIlD,CAAC;AAEH,kCAAkC;AAClC,eAAO,MAAM,aAAa,+BAA+B,CAAC;AAE1D;;;;GAIG;AACH,eAAO,MAAM,sBAAsB,EAAE,SAAS,MAAM,EAGlD,CAAC;AAEH;;;GAGG;AACH,eAAO,MAAM,mBAAmB,wBAAwB,CAAC;AAkBzD;;;;;;;;;;GAUG;AACH,wBAAgB,aAAa,CAAC,MAAM,CAAC,EAAE,SAAS,MAAM,EAAE,GAAG,SAAS,MAAM,EAAE,CAK3E;AAED;;;;GAIG;AACH,wBAAgB,cAAc,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,GAAG,OAAO,CAazE"}
|
package/dist/studio.js
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Where Deco Studio runs: the origins its site editor frames a site from and
|
|
3
3
|
* talks to it from (`postMessage`). One list, so the worker's
|
|
4
|
-
* `frame-ancestors` and
|
|
5
|
-
* apart.
|
|
4
|
+
* `frame-ancestors` and a site's own never drift apart.
|
|
6
5
|
*
|
|
7
6
|
* No module state and no framework code: safe in a browser bundle.
|
|
8
7
|
*/
|
|
@@ -39,6 +38,11 @@ export const DEFAULT_STUDIO_ORIGINS = Object.freeze([
|
|
|
39
38
|
STUDIO_ORIGIN,
|
|
40
39
|
"https://studio-stg.decocms.com",
|
|
41
40
|
]);
|
|
41
|
+
/**
|
|
42
|
+
* Where `StudioRefresh` puts the page's own refresh for Studio's HTML swap
|
|
43
|
+
* (`STUDIO_PAGE_REFRESH` of `@decocms/blocks/protocol`, held equal by a test).
|
|
44
|
+
*/
|
|
45
|
+
export const STUDIO_PAGE_REFRESH = "__decoStudioRefresh";
|
|
42
46
|
/** `value` as an exact http(s) origin (`https://host[:port]`, a trailing `/` dropped; no wildcard), or `""`. */
|
|
43
47
|
function asOrigin(value) {
|
|
44
48
|
if (typeof value !== "string")
|
package/dist/studio.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"studio.js","sourceRoot":"","sources":["../src/studio.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"studio.js","sourceRoot":"","sources":["../src/studio.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH;;;;GAIG;AACH,MAAM,CAAC,MAAM,cAAc,GAAsB,MAAM,CAAC,MAAM,CAAC;IAC7D,4BAA4B;IAC5B,gCAAgC;IAChC,qBAAqB;IACrB,uBAAuB;CACxB,CAAC,CAAC;AAEH;;;;;GAKG;AACH,MAAM,CAAC,MAAM,sBAAsB,GAAsB,MAAM,CAAC,MAAM,CAAC;IACrE,QAAQ;IACR,GAAG,cAAc;IACjB,uBAAuB;CACxB,CAAC,CAAC;AAEH,kCAAkC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B,CAAC,CAAC;IAC/E,OAAO,CAAC,GAAG,IAAI,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC;AAC1D,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,cAAc,CAAC,MAAiC;IAC9D,IAAI,OAAO,MAAM,KAAK,QAAQ,IAAI,MAAM,KAAK,EAAE;QAAE,OAAO,KAAK,CAAC;IAC9D,IAAI,GAAQ,CAAC;IACb,IAAI,CAAC;QACH,GAAG,GAAG,IAAI,GAAG,CAAC,MAAM,CAAC,CAAC;IACxB,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,KAAK,CAAC;IACf,CAAC;IACD,8EAA8E;IAC9E,yBAAyB;IACzB,IAAI,GAAG,CAAC,MAAM,KAAK,MAAM,IAAI,GAAG,CAAC,QAAQ,KAAK,QAAQ,IAAI,GAAG,CAAC,IAAI,KAAK,EAAE;QAAE,OAAO,KAAK,CAAC;IACxF,MAAM,IAAI,GAAG,GAAG,CAAC,QAAQ,CAAC;IAC1B,OAAO,IAAI,KAAK,aAAa,IAAI,IAAI,CAAC,QAAQ,CAAC,cAAc,CAAC,CAAC;AACjE,CAAC"}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* v7's "." edit shortcut, alone: on a page that isn't framed, "." (or
|
|
3
|
+
* Ctrl+Shift+E) opens the page in Studio's editor (`/choose-editor`, which
|
|
4
|
+
* signs a visitor who isn't signed in in first), and Ctrl or Cmd+"." opens it
|
|
5
|
+
* in a new tab. In a frame it does nothing: Studio's editor reaches a framed
|
|
6
|
+
* page through the Studio channel (`StudioChannel`) only.
|
|
7
|
+
*
|
|
8
|
+
* Safe under a test runner that rewrites "frame-busting" code in the HTML it
|
|
9
|
+
* proxies (Cypress's `modifyObstructiveCode`): the script compares no window
|
|
10
|
+
* with `top` or `parent` in a form those rewrites match, so the HTML reaches
|
|
11
|
+
* the browser byte for byte (a rewrite changes the length of a React Server
|
|
12
|
+
* Components payload that carries the script and breaks the page).
|
|
13
|
+
*
|
|
14
|
+
* A key typed in a field is left alone, also in a field inside an open shadow
|
|
15
|
+
* root (a third-party widget's form): its events reach the document with the
|
|
16
|
+
* host as their target, so the script reads the field from the event's
|
|
17
|
+
* composed path.
|
|
18
|
+
*
|
|
19
|
+
* The page it sends: the last `StudioPage` marker the page rendered, else the
|
|
20
|
+
* `page` the binding passed, read at the keypress. An inline script with no
|
|
21
|
+
* hooks, so a Server Component renders it; its settings travel in `data-*`
|
|
22
|
+
* attributes (the same as v7's bridge's: `data-deco-studio-bridge`,
|
|
23
|
+
* `data-site`, `data-shortcut`, `data-path-template`, `data-page-id`) and the
|
|
24
|
+
* script itself is static.
|
|
25
|
+
*
|
|
26
|
+
* `@decocms/nextjs` and `@decocms/tanstack` each carry this file (neither
|
|
27
|
+
* imports the other); tests/studio-shortcut.test.ts keeps the two the same.
|
|
28
|
+
*/
|
|
29
|
+
import { type ReactNode } from "react";
|
|
30
|
+
/** The page the "." shortcut opens in Studio. */
|
|
31
|
+
interface ShortcutPage {
|
|
32
|
+
/** The page block's `path`, as `matchRoute` matched it (`/products/:slug/p`, `/*`). */
|
|
33
|
+
pathTemplate?: string;
|
|
34
|
+
/** The page block's id, when the site knows it. */
|
|
35
|
+
id?: string;
|
|
36
|
+
}
|
|
37
|
+
export interface StudioShortcutProps {
|
|
38
|
+
/** The site's name in Deco (the shortcut sends it to Studio). */
|
|
39
|
+
site: string;
|
|
40
|
+
/** On by default; `false` turns it off. */
|
|
41
|
+
editShortcut?: boolean;
|
|
42
|
+
/** The page on screen, when the component that renders the shortcut knows it. */
|
|
43
|
+
page?: ShortcutPage;
|
|
44
|
+
/** The CSP nonce for the inline script, when the site sends one. */
|
|
45
|
+
nonce?: string;
|
|
46
|
+
}
|
|
47
|
+
/** The browser side. Static: everything it needs comes from the script element's attributes. */
|
|
48
|
+
export declare const STUDIO_SHORTCUT_SCRIPT = "(function(){\nvar s=document.currentScript,w=window,p=w.parent;\nif(!s||p!==w||w.__decoStudioShortcut||!s.hasAttribute(\"data-shortcut\"))return;\nw.__decoStudioShortcut=true;\nvar site=s.getAttribute(\"data-site\")||\"\";\nfunction page(){\n var all=document.querySelectorAll(\"[data-deco-page]\"),el=all.length?all[all.length-1]:null;\n if(!el||!el.getAttribute(\"data-path-template\")){el=document.querySelector(\"script[data-deco-studio-bridge]\")||s}\n return{t:el.getAttribute(\"data-path-template\")||\"\",id:el.getAttribute(\"data-page-id\")||\"\"};\n}\ndocument.addEventListener(\"keydown\",function(e){\n var t=e.composedPath?e.composedPath()[0]:e.target;\n if(e.defaultPrevented||(t&&(t.tagName===\"INPUT\"||t.tagName===\"TEXTAREA\"||t.tagName===\"SELECT\"||t.isContentEditable)))return;\n if(e.key!==\".\"&&!(e.ctrlKey&&e.shiftKey&&e.key===\"E\"))return;\n e.preventDefault();\n e.stopPropagation();\n var g=page(),h=new URL(\"https://studio.decocms.com/choose-editor\");\n h.searchParams.set(\"site\",site);\n h.searchParams.set(\"domain\",location.origin);\n if(g.id)h.searchParams.set(\"pageId\",g.id);\n h.searchParams.set(\"path\",location.pathname+location.search);\n h.searchParams.set(\"pathTemplate\",g.t||\"/*\");\n if((e.ctrlKey||e.metaKey)&&e.key===\".\"){w.open(h.toString(),\"_blank\");return}\n location.href=h.toString();\n});\n})();";
|
|
49
|
+
/** The "." shortcut's script. Render it once, in the `<body>`. */
|
|
50
|
+
export declare function StudioShortcut({ site, editShortcut, page, nonce, }: StudioShortcutProps): ReactNode;
|
|
51
|
+
export {};
|
|
52
|
+
//# sourceMappingURL=studioShortcut.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"studioShortcut.d.ts","sourceRoot":"","sources":["../src/studioShortcut.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,OAAO,EAAiB,KAAK,SAAS,EAAE,MAAM,OAAO,CAAC;AAEtD,iDAAiD;AACjD,UAAU,YAAY;IACpB,uFAAuF;IACvF,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,mDAAmD;IACnD,EAAE,CAAC,EAAE,MAAM,CAAC;CACb;AAED,MAAM,WAAW,mBAAmB;IAClC,iEAAiE;IACjE,IAAI,EAAE,MAAM,CAAC;IACb,2CAA2C;IAC3C,YAAY,CAAC,EAAE,OAAO,CAAC;IACvB,iFAAiF;IACjF,IAAI,CAAC,EAAE,YAAY,CAAC;IACpB,oEAAoE;IACpE,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAED,gGAAgG;AAChG,eAAO,MAAM,sBAAsB,s2CAyB7B,CAAC;AAEP,kEAAkE;AAClE,wBAAgB,cAAc,CAAC,EAC7B,IAAI,EACJ,YAAmB,EACnB,IAAI,EACJ,KAAK,GACN,EAAE,mBAAmB,GAAG,SAAS,CAejC"}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* v7's "." edit shortcut, alone: on a page that isn't framed, "." (or
|
|
3
|
+
* Ctrl+Shift+E) opens the page in Studio's editor (`/choose-editor`, which
|
|
4
|
+
* signs a visitor who isn't signed in in first), and Ctrl or Cmd+"." opens it
|
|
5
|
+
* in a new tab. In a frame it does nothing: Studio's editor reaches a framed
|
|
6
|
+
* page through the Studio channel (`StudioChannel`) only.
|
|
7
|
+
*
|
|
8
|
+
* Safe under a test runner that rewrites "frame-busting" code in the HTML it
|
|
9
|
+
* proxies (Cypress's `modifyObstructiveCode`): the script compares no window
|
|
10
|
+
* with `top` or `parent` in a form those rewrites match, so the HTML reaches
|
|
11
|
+
* the browser byte for byte (a rewrite changes the length of a React Server
|
|
12
|
+
* Components payload that carries the script and breaks the page).
|
|
13
|
+
*
|
|
14
|
+
* A key typed in a field is left alone, also in a field inside an open shadow
|
|
15
|
+
* root (a third-party widget's form): its events reach the document with the
|
|
16
|
+
* host as their target, so the script reads the field from the event's
|
|
17
|
+
* composed path.
|
|
18
|
+
*
|
|
19
|
+
* The page it sends: the last `StudioPage` marker the page rendered, else the
|
|
20
|
+
* `page` the binding passed, read at the keypress. An inline script with no
|
|
21
|
+
* hooks, so a Server Component renders it; its settings travel in `data-*`
|
|
22
|
+
* attributes (the same as v7's bridge's: `data-deco-studio-bridge`,
|
|
23
|
+
* `data-site`, `data-shortcut`, `data-path-template`, `data-page-id`) and the
|
|
24
|
+
* script itself is static.
|
|
25
|
+
*
|
|
26
|
+
* `@decocms/nextjs` and `@decocms/tanstack` each carry this file (neither
|
|
27
|
+
* imports the other); tests/studio-shortcut.test.ts keeps the two the same.
|
|
28
|
+
*/
|
|
29
|
+
import { createElement } from "react";
|
|
30
|
+
/** The browser side. Static: everything it needs comes from the script element's attributes. */
|
|
31
|
+
export const STUDIO_SHORTCUT_SCRIPT = `(function(){
|
|
32
|
+
var s=document.currentScript,w=window,p=w.parent;
|
|
33
|
+
if(!s||p!==w||w.__decoStudioShortcut||!s.hasAttribute("data-shortcut"))return;
|
|
34
|
+
w.__decoStudioShortcut=true;
|
|
35
|
+
var site=s.getAttribute("data-site")||"";
|
|
36
|
+
function page(){
|
|
37
|
+
var all=document.querySelectorAll("[data-deco-page]"),el=all.length?all[all.length-1]:null;
|
|
38
|
+
if(!el||!el.getAttribute("data-path-template")){el=document.querySelector("script[data-deco-studio-bridge]")||s}
|
|
39
|
+
return{t:el.getAttribute("data-path-template")||"",id:el.getAttribute("data-page-id")||""};
|
|
40
|
+
}
|
|
41
|
+
document.addEventListener("keydown",function(e){
|
|
42
|
+
var t=e.composedPath?e.composedPath()[0]:e.target;
|
|
43
|
+
if(e.defaultPrevented||(t&&(t.tagName==="INPUT"||t.tagName==="TEXTAREA"||t.tagName==="SELECT"||t.isContentEditable)))return;
|
|
44
|
+
if(e.key!=="."&&!(e.ctrlKey&&e.shiftKey&&e.key==="E"))return;
|
|
45
|
+
e.preventDefault();
|
|
46
|
+
e.stopPropagation();
|
|
47
|
+
var g=page(),h=new URL("https://studio.decocms.com/choose-editor");
|
|
48
|
+
h.searchParams.set("site",site);
|
|
49
|
+
h.searchParams.set("domain",location.origin);
|
|
50
|
+
if(g.id)h.searchParams.set("pageId",g.id);
|
|
51
|
+
h.searchParams.set("path",location.pathname+location.search);
|
|
52
|
+
h.searchParams.set("pathTemplate",g.t||"/*");
|
|
53
|
+
if((e.ctrlKey||e.metaKey)&&e.key==="."){w.open(h.toString(),"_blank");return}
|
|
54
|
+
location.href=h.toString();
|
|
55
|
+
});
|
|
56
|
+
})();`;
|
|
57
|
+
/** The "." shortcut's script. Render it once, in the `<body>`. */
|
|
58
|
+
export function StudioShortcut({ site, editShortcut = true, page, nonce, }) {
|
|
59
|
+
return createElement("script", {
|
|
60
|
+
"data-deco-studio-bridge": "",
|
|
61
|
+
"data-site": site,
|
|
62
|
+
"data-shortcut": editShortcut ? "" : undefined,
|
|
63
|
+
// Read on each keypress, so a client-side navigation that re-renders the
|
|
64
|
+
// script with the next page updates them.
|
|
65
|
+
"data-path-template": page?.pathTemplate || undefined,
|
|
66
|
+
"data-page-id": page?.id || undefined,
|
|
67
|
+
nonce,
|
|
68
|
+
// Browsers hide a nonce from the DOM after parsing, which React would
|
|
69
|
+
// report as a hydration mismatch.
|
|
70
|
+
suppressHydrationWarning: true,
|
|
71
|
+
dangerouslySetInnerHTML: { __html: STUDIO_SHORTCUT_SCRIPT },
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
//# sourceMappingURL=studioShortcut.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"studioShortcut.js","sourceRoot":"","sources":["../src/studioShortcut.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,OAAO,EAAE,aAAa,EAAkB,MAAM,OAAO,CAAC;AAqBtD,gGAAgG;AAChG,MAAM,CAAC,MAAM,sBAAsB,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;MAyBhC,CAAC;AAEP,kEAAkE;AAClE,MAAM,UAAU,cAAc,CAAC,EAC7B,IAAI,EACJ,YAAY,GAAG,IAAI,EACnB,IAAI,EACJ,KAAK,GACe;IACpB,OAAO,aAAa,CAAC,QAAQ,EAAE;QAC7B,yBAAyB,EAAE,EAAE;QAC7B,WAAW,EAAE,IAAI;QACjB,eAAe,EAAE,YAAY,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,SAAS;QAC9C,yEAAyE;QACzE,0CAA0C;QAC1C,oBAAoB,EAAE,IAAI,EAAE,YAAY,IAAI,SAAS;QACrD,cAAc,EAAE,IAAI,EAAE,EAAE,IAAI,SAAS;QACrC,KAAK;QACL,sEAAsE;QACtE,kCAAkC;QAClC,wBAAwB,EAAE,IAAI;QAC9B,uBAAuB,EAAE,EAAE,MAAM,EAAE,sBAAsB,EAAE;KAC5D,CAAC,CAAC;AACL,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@decocms/tanstack",
|
|
3
|
-
"version": "8.
|
|
3
|
+
"version": "8.16.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"engines": {
|
|
6
6
|
"node": ">=24"
|
|
@@ -94,7 +94,7 @@
|
|
|
94
94
|
}
|
|
95
95
|
},
|
|
96
96
|
"devDependencies": {
|
|
97
|
-
"@decocms/blocks": "8.
|
|
97
|
+
"@decocms/blocks": "8.16.0",
|
|
98
98
|
"@tanstack/react-router": "1.170.39",
|
|
99
99
|
"@tanstack/react-start": "1.168.58",
|
|
100
100
|
"@types/react": "^19.0.0",
|
|
@@ -8,24 +8,33 @@
|
|
|
8
8
|
* keeps its height while the next page loads;
|
|
9
9
|
* - the CDN segment token and the draft this request renders, for
|
|
10
10
|
* `decoServerFnFetch` and the draft badge;
|
|
11
|
-
* - `StudioBridge` (in Studio's frame it answers Studio's origins,
|
|
12
|
-
* `STUDIO_ORIGINS`; outside it, "." opens the page in Studio's editor, with
|
|
13
|
-
* the page's `pathTemplate` from the route's loader data) and `DraftBadge`
|
|
14
|
-
* (only on a draft page outside Studio's frame), from `@decocms/blocks/preview`;
|
|
15
11
|
* - `StudioChannel` (every page, production included: framed by one of
|
|
16
12
|
* Studio's origins, `studioOrigins` / `DECO_STUDIO_ORIGIN` / Studio's
|
|
17
13
|
* production and staging origins, each exact, it loads the editor's own
|
|
18
|
-
* modules from that origin; otherwise it does nothing)
|
|
19
|
-
*
|
|
14
|
+
* modules from that origin; otherwise it does nothing) and `DraftBadge`
|
|
15
|
+
* (only on a draft page outside Studio's frame), from
|
|
16
|
+
* `@decocms/blocks/preview`. The worker adds the same origins to the
|
|
17
|
+
* page's CSP `script-src` and `frame-ancestors`;
|
|
18
|
+
* - `StudioRefresh`: after a save, Studio shows the new content by rerunning
|
|
19
|
+
* the loaders (`router.invalidate()`), with React still attached, instead
|
|
20
|
+
* of replacing the document;
|
|
21
|
+
* - v7's "." edit shortcut (`../studioShortcut.ts`: outside Studio's frame,
|
|
22
|
+
* "." opens the page in Studio's editor, with the page's `pathTemplate`
|
|
23
|
+
* from the route's loader data). v8 only: v7's framed handler
|
|
24
|
+
* (`scrollToComponent`, `editor::rerender`) is gone, so no script compares
|
|
25
|
+
* the window with `top` or `parent` in a form a test runner's
|
|
26
|
+
* "frame-busting" rewrite matches (Cypress's `modifyObstructiveCode`).
|
|
20
27
|
*
|
|
21
28
|
* A site that keeps its own document renders `DecoPreview` in its `<body>`
|
|
22
29
|
* for the bridge, the badge and the draft they read.
|
|
23
30
|
*/
|
|
24
|
-
import { DraftBadge,
|
|
31
|
+
import { DraftBadge, StudioChannel } from "@decocms/blocks/preview";
|
|
25
32
|
import { HeadContent, Outlet, ScriptOnce, Scripts, useRouterState } from "@tanstack/react-router";
|
|
26
33
|
import { type ReactNode, useEffect, useRef, useState } from "react";
|
|
27
|
-
import {
|
|
34
|
+
import { studioOrigins } from "../studio.ts";
|
|
35
|
+
import { StudioShortcut } from "../studioShortcut.ts";
|
|
28
36
|
import { pageValues, publishScript } from "./pageValues.ts";
|
|
37
|
+
import { StudioRefresh } from "./StudioRefresh.ts";
|
|
29
38
|
import { DATA_EVENT_OBSERVER, eventsBootstrap, PROGRESS_CSS } from "./scripts.ts";
|
|
30
39
|
|
|
31
40
|
declare global {
|
|
@@ -100,7 +109,7 @@ function studioPageOf(
|
|
|
100
109
|
return undefined;
|
|
101
110
|
}
|
|
102
111
|
|
|
103
|
-
/** The
|
|
112
|
+
/** The "." shortcut, with the page on screen, the Studio channel, and the badge for the request's draft. */
|
|
104
113
|
function StudioPieces({
|
|
105
114
|
site,
|
|
106
115
|
editShortcut,
|
|
@@ -117,14 +126,14 @@ function StudioPieces({
|
|
|
117
126
|
const pageId = useRouterState({ select: (s) => studioPageOf(s.matches)?.id });
|
|
118
127
|
return (
|
|
119
128
|
<>
|
|
120
|
-
<
|
|
129
|
+
<StudioShortcut
|
|
121
130
|
site={site}
|
|
122
|
-
trustedOrigins={STUDIO_ORIGINS}
|
|
123
131
|
editShortcut={editShortcut}
|
|
124
132
|
page={{ pathTemplate, id: pageId }}
|
|
125
133
|
nonce={nonce}
|
|
126
134
|
/>
|
|
127
135
|
<StudioChannel studioOrigins={studioOrigins(origins)} nonce={nonce} />
|
|
136
|
+
<StudioRefresh />
|
|
128
137
|
<DraftBadge pointer={draft ?? null} unavailable={draftUnavailable} />
|
|
129
138
|
</>
|
|
130
139
|
);
|
|
@@ -154,8 +163,8 @@ export interface DecoPreviewProps {
|
|
|
154
163
|
/**
|
|
155
164
|
* What `DecoDocument` mounts for Studio and draft previews, for a site that
|
|
156
165
|
* keeps its own document (a skip link before `<main>`, say): the request's
|
|
157
|
-
* draft and CDN segment token published to the browser,
|
|
158
|
-
* `DraftBadge`. Render it once, in the `<body>`.
|
|
166
|
+
* draft and CDN segment token published to the browser, the "." shortcut,
|
|
167
|
+
* `StudioChannel` and `DraftBadge`. Render it once, in the `<body>`.
|
|
159
168
|
*/
|
|
160
169
|
export function DecoPreview({ site, editShortcut, nonce, studioOrigins }: DecoPreviewProps) {
|
|
161
170
|
const values = pageValues();
|
|
@@ -182,7 +191,7 @@ export function DecoPreview({ site, editShortcut, nonce, studioOrigins }: DecoPr
|
|
|
182
191
|
}
|
|
183
192
|
|
|
184
193
|
export interface DecoDocumentProps {
|
|
185
|
-
/** The site's name in Deco (the
|
|
194
|
+
/** The site's name in Deco (the "." shortcut opens the page under it). */
|
|
186
195
|
site: string;
|
|
187
196
|
/** `<html lang>`. Default `"en"`. */
|
|
188
197
|
lang?: string;
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The page's own refresh for Studio's HTML swap (`STUDIO_PAGE_REFRESH` of
|
|
3
|
+
* `@decocms/blocks/protocol`): after a save, Studio asks the page to show its
|
|
4
|
+
* new content. Replacing the document would leave React holding the old nodes,
|
|
5
|
+
* so every client component would stop at its server markup. Here the router
|
|
6
|
+
* does it instead and stays attached: `router.invalidate()` reruns the loaders
|
|
7
|
+
* and resolves once the page shows their data.
|
|
8
|
+
*
|
|
9
|
+
* A new URL of the page (a save moved the draft pointer in `?__draft=`) first
|
|
10
|
+
* goes to the worker as a document request, only for its draft cookie: the
|
|
11
|
+
* loaders' server-function calls carry the cookie, not the page's query. Then
|
|
12
|
+
* the router moves to the URL. A route whose loader deps include `__draft`
|
|
13
|
+
* loads again on that move; only the matches it didn't load are invalidated,
|
|
14
|
+
* so the page renders the save once.
|
|
15
|
+
*
|
|
16
|
+
* Set on every page, framed or not: Studio's module is the only caller, and
|
|
17
|
+
* comparing the window with its parent here is what a test runner's
|
|
18
|
+
* frame-busting rewrite matches (`tests/studio-shortcut.test.ts`).
|
|
19
|
+
*/
|
|
20
|
+
import { useRouter } from "@tanstack/react-router";
|
|
21
|
+
import { useEffect } from "react";
|
|
22
|
+
import { STUDIO_PAGE_REFRESH } from "../studio.ts";
|
|
23
|
+
|
|
24
|
+
type StudioPageRefresh = (url: string) => Promise<void>;
|
|
25
|
+
|
|
26
|
+
export function StudioRefresh(): null {
|
|
27
|
+
const router = useRouter();
|
|
28
|
+
|
|
29
|
+
useEffect(() => {
|
|
30
|
+
const refresh: StudioPageRefresh = async (url) => {
|
|
31
|
+
const target = new URL(url, location.href);
|
|
32
|
+
if (target.origin !== location.origin) {
|
|
33
|
+
throw new Error(`Not this page's origin: ${target.origin}`);
|
|
34
|
+
}
|
|
35
|
+
let loadedSince: number | null = null;
|
|
36
|
+
if (target.href !== location.href) {
|
|
37
|
+
const res = await fetch(target.href, {
|
|
38
|
+
headers: { accept: "text/html" },
|
|
39
|
+
credentials: "same-origin",
|
|
40
|
+
cache: "no-store",
|
|
41
|
+
});
|
|
42
|
+
await res.body?.cancel();
|
|
43
|
+
if (!res.ok) throw new Error(`The page answered ${res.status}`);
|
|
44
|
+
loadedSince = Date.now();
|
|
45
|
+
await router.navigate({
|
|
46
|
+
href: `${target.pathname}${target.search}${target.hash}`,
|
|
47
|
+
replace: true,
|
|
48
|
+
resetScroll: false,
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
if (loadedSince === null) return router.invalidate();
|
|
52
|
+
const since = loadedSince;
|
|
53
|
+
const stale = router.state.matches.filter((m) => m.updatedAt < since);
|
|
54
|
+
if (stale.length === 0) return;
|
|
55
|
+
const ids = new Set(stale.map((m) => m.id));
|
|
56
|
+
await router.invalidate({ filter: (m) => ids.has(m.id) });
|
|
57
|
+
};
|
|
58
|
+
const slot = window as unknown as Record<string, unknown>;
|
|
59
|
+
slot[STUDIO_PAGE_REFRESH] = refresh;
|
|
60
|
+
return () => {
|
|
61
|
+
if (slot[STUDIO_PAGE_REFRESH] === refresh) delete slot[STUDIO_PAGE_REFRESH];
|
|
62
|
+
};
|
|
63
|
+
}, [router]);
|
|
64
|
+
|
|
65
|
+
return null;
|
|
66
|
+
}
|
package/src/studio.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Where Deco Studio runs: the origins its site editor frames a site from and
|
|
3
3
|
* talks to it from (`postMessage`). One list, so the worker's
|
|
4
|
-
* `frame-ancestors` and
|
|
5
|
-
* apart.
|
|
4
|
+
* `frame-ancestors` and a site's own never drift apart.
|
|
6
5
|
*
|
|
7
6
|
* No module state and no framework code: safe in a browser bundle.
|
|
8
7
|
*/
|
|
@@ -44,6 +43,12 @@ export const DEFAULT_STUDIO_ORIGINS: readonly string[] = Object.freeze([
|
|
|
44
43
|
"https://studio-stg.decocms.com",
|
|
45
44
|
]);
|
|
46
45
|
|
|
46
|
+
/**
|
|
47
|
+
* Where `StudioRefresh` puts the page's own refresh for Studio's HTML swap
|
|
48
|
+
* (`STUDIO_PAGE_REFRESH` of `@decocms/blocks/protocol`, held equal by a test).
|
|
49
|
+
*/
|
|
50
|
+
export const STUDIO_PAGE_REFRESH = "__decoStudioRefresh";
|
|
51
|
+
|
|
47
52
|
/** `value` as an exact http(s) origin (`https://host[:port]`, a trailing `/` dropped; no wildcard), or `""`. */
|
|
48
53
|
function asOrigin(value: unknown): string {
|
|
49
54
|
if (typeof value !== "string") return "";
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* v7's "." edit shortcut, alone: on a page that isn't framed, "." (or
|
|
3
|
+
* Ctrl+Shift+E) opens the page in Studio's editor (`/choose-editor`, which
|
|
4
|
+
* signs a visitor who isn't signed in in first), and Ctrl or Cmd+"." opens it
|
|
5
|
+
* in a new tab. In a frame it does nothing: Studio's editor reaches a framed
|
|
6
|
+
* page through the Studio channel (`StudioChannel`) only.
|
|
7
|
+
*
|
|
8
|
+
* Safe under a test runner that rewrites "frame-busting" code in the HTML it
|
|
9
|
+
* proxies (Cypress's `modifyObstructiveCode`): the script compares no window
|
|
10
|
+
* with `top` or `parent` in a form those rewrites match, so the HTML reaches
|
|
11
|
+
* the browser byte for byte (a rewrite changes the length of a React Server
|
|
12
|
+
* Components payload that carries the script and breaks the page).
|
|
13
|
+
*
|
|
14
|
+
* A key typed in a field is left alone, also in a field inside an open shadow
|
|
15
|
+
* root (a third-party widget's form): its events reach the document with the
|
|
16
|
+
* host as their target, so the script reads the field from the event's
|
|
17
|
+
* composed path.
|
|
18
|
+
*
|
|
19
|
+
* The page it sends: the last `StudioPage` marker the page rendered, else the
|
|
20
|
+
* `page` the binding passed, read at the keypress. An inline script with no
|
|
21
|
+
* hooks, so a Server Component renders it; its settings travel in `data-*`
|
|
22
|
+
* attributes (the same as v7's bridge's: `data-deco-studio-bridge`,
|
|
23
|
+
* `data-site`, `data-shortcut`, `data-path-template`, `data-page-id`) and the
|
|
24
|
+
* script itself is static.
|
|
25
|
+
*
|
|
26
|
+
* `@decocms/nextjs` and `@decocms/tanstack` each carry this file (neither
|
|
27
|
+
* imports the other); tests/studio-shortcut.test.ts keeps the two the same.
|
|
28
|
+
*/
|
|
29
|
+
import { createElement, type ReactNode } from "react";
|
|
30
|
+
|
|
31
|
+
/** The page the "." shortcut opens in Studio. */
|
|
32
|
+
interface ShortcutPage {
|
|
33
|
+
/** The page block's `path`, as `matchRoute` matched it (`/products/:slug/p`, `/*`). */
|
|
34
|
+
pathTemplate?: string;
|
|
35
|
+
/** The page block's id, when the site knows it. */
|
|
36
|
+
id?: string;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface StudioShortcutProps {
|
|
40
|
+
/** The site's name in Deco (the shortcut sends it to Studio). */
|
|
41
|
+
site: string;
|
|
42
|
+
/** On by default; `false` turns it off. */
|
|
43
|
+
editShortcut?: boolean;
|
|
44
|
+
/** The page on screen, when the component that renders the shortcut knows it. */
|
|
45
|
+
page?: ShortcutPage;
|
|
46
|
+
/** The CSP nonce for the inline script, when the site sends one. */
|
|
47
|
+
nonce?: string;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** The browser side. Static: everything it needs comes from the script element's attributes. */
|
|
51
|
+
export const STUDIO_SHORTCUT_SCRIPT = `(function(){
|
|
52
|
+
var s=document.currentScript,w=window,p=w.parent;
|
|
53
|
+
if(!s||p!==w||w.__decoStudioShortcut||!s.hasAttribute("data-shortcut"))return;
|
|
54
|
+
w.__decoStudioShortcut=true;
|
|
55
|
+
var site=s.getAttribute("data-site")||"";
|
|
56
|
+
function page(){
|
|
57
|
+
var all=document.querySelectorAll("[data-deco-page]"),el=all.length?all[all.length-1]:null;
|
|
58
|
+
if(!el||!el.getAttribute("data-path-template")){el=document.querySelector("script[data-deco-studio-bridge]")||s}
|
|
59
|
+
return{t:el.getAttribute("data-path-template")||"",id:el.getAttribute("data-page-id")||""};
|
|
60
|
+
}
|
|
61
|
+
document.addEventListener("keydown",function(e){
|
|
62
|
+
var t=e.composedPath?e.composedPath()[0]:e.target;
|
|
63
|
+
if(e.defaultPrevented||(t&&(t.tagName==="INPUT"||t.tagName==="TEXTAREA"||t.tagName==="SELECT"||t.isContentEditable)))return;
|
|
64
|
+
if(e.key!=="."&&!(e.ctrlKey&&e.shiftKey&&e.key==="E"))return;
|
|
65
|
+
e.preventDefault();
|
|
66
|
+
e.stopPropagation();
|
|
67
|
+
var g=page(),h=new URL("https://studio.decocms.com/choose-editor");
|
|
68
|
+
h.searchParams.set("site",site);
|
|
69
|
+
h.searchParams.set("domain",location.origin);
|
|
70
|
+
if(g.id)h.searchParams.set("pageId",g.id);
|
|
71
|
+
h.searchParams.set("path",location.pathname+location.search);
|
|
72
|
+
h.searchParams.set("pathTemplate",g.t||"/*");
|
|
73
|
+
if((e.ctrlKey||e.metaKey)&&e.key==="."){w.open(h.toString(),"_blank");return}
|
|
74
|
+
location.href=h.toString();
|
|
75
|
+
});
|
|
76
|
+
})();`;
|
|
77
|
+
|
|
78
|
+
/** The "." shortcut's script. Render it once, in the `<body>`. */
|
|
79
|
+
export function StudioShortcut({
|
|
80
|
+
site,
|
|
81
|
+
editShortcut = true,
|
|
82
|
+
page,
|
|
83
|
+
nonce,
|
|
84
|
+
}: StudioShortcutProps): ReactNode {
|
|
85
|
+
return createElement("script", {
|
|
86
|
+
"data-deco-studio-bridge": "",
|
|
87
|
+
"data-site": site,
|
|
88
|
+
"data-shortcut": editShortcut ? "" : undefined,
|
|
89
|
+
// Read on each keypress, so a client-side navigation that re-renders the
|
|
90
|
+
// script with the next page updates them.
|
|
91
|
+
"data-path-template": page?.pathTemplate || undefined,
|
|
92
|
+
"data-page-id": page?.id || undefined,
|
|
93
|
+
nonce,
|
|
94
|
+
// Browsers hide a nonce from the DOM after parsing, which React would
|
|
95
|
+
// report as a hydration mismatch.
|
|
96
|
+
suppressHydrationWarning: true,
|
|
97
|
+
dangerouslySetInnerHTML: { __html: STUDIO_SHORTCUT_SCRIPT },
|
|
98
|
+
});
|
|
99
|
+
}
|