remote-components 0.0.33 → 0.0.34
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{component-loader-28ad0083.d.ts → component-loader-8951c052.d.ts} +6 -4
- package/dist/html/host.cjs +227 -13
- package/dist/html/host.cjs.map +1 -1
- package/dist/html/host.js +227 -13
- package/dist/html/host.js.map +1 -1
- package/dist/html/remote.cjs +198 -0
- package/dist/html/remote.cjs.map +1 -0
- package/dist/html/remote.d.ts +2 -0
- package/dist/html/remote.js +197 -0
- package/dist/html/remote.js.map +1 -0
- package/dist/internal/next/host/app-router-client.cjs +56 -4
- package/dist/internal/next/host/app-router-client.cjs.map +1 -1
- package/dist/internal/next/host/app-router-client.d.ts +3 -3
- package/dist/internal/next/host/app-router-client.js +58 -5
- package/dist/internal/next/host/app-router-client.js.map +1 -1
- package/dist/internal/shared/client/remote-component.cjs +99 -0
- package/dist/internal/shared/client/remote-component.cjs.map +1 -1
- package/dist/internal/shared/client/remote-component.d.ts +11 -4
- package/dist/internal/shared/client/remote-component.js +97 -0
- package/dist/internal/shared/client/remote-component.js.map +1 -1
- package/dist/internal/shared/error.cjs.map +1 -1
- package/dist/internal/shared/error.d.ts +1 -1
- package/dist/internal/shared/error.js.map +1 -1
- package/dist/internal/shared/ssr/dom-flight.cjs +17 -5
- package/dist/internal/shared/ssr/dom-flight.cjs.map +1 -1
- package/dist/internal/shared/ssr/dom-flight.d.ts +1 -1
- package/dist/internal/shared/ssr/dom-flight.js +17 -5
- package/dist/internal/shared/ssr/dom-flight.js.map +1 -1
- package/dist/internal/shared/ssr/fetch-remote-component.cjs +7 -3
- package/dist/internal/shared/ssr/fetch-remote-component.cjs.map +1 -1
- package/dist/internal/shared/ssr/fetch-remote-component.d.ts +2 -1
- package/dist/internal/shared/ssr/fetch-remote-component.js +7 -3
- package/dist/internal/shared/ssr/fetch-remote-component.js.map +1 -1
- package/dist/next/host/app-router-server.cjs +1 -0
- package/dist/next/host/app-router-server.cjs.map +1 -1
- package/dist/next/host/app-router-server.js +1 -0
- package/dist/next/host/app-router-server.js.map +1 -1
- package/dist/next/host/client/index.cjs +292 -103
- package/dist/next/host/client/index.cjs.map +1 -1
- package/dist/next/host/client/index.d.ts +1 -1
- package/dist/next/host/client/index.js +289 -102
- package/dist/next/host/client/index.js.map +1 -1
- package/dist/react/index.cjs +197 -103
- package/dist/react/index.cjs.map +1 -1
- package/dist/react/index.d.ts +2 -2
- package/dist/react/index.js +196 -102
- package/dist/react/index.js.map +1 -1
- package/dist/{types-7c207455.d.ts → types-4e7dea94.d.ts} +2 -1
- package/dist/{types-e4a3fa37.d.ts → types-cbf6c34f.d.ts} +2 -2
- package/package.json +8 -1
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __esm = (fn, res) => function __init() {
|
|
5
|
+
return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
|
|
6
|
+
};
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
// src/shared/error.ts
|
|
13
|
+
var RemoteComponentsError;
|
|
14
|
+
var init_error = __esm({
|
|
15
|
+
"src/shared/error.ts"() {
|
|
16
|
+
"use strict";
|
|
17
|
+
RemoteComponentsError = class extends Error {
|
|
18
|
+
constructor(message, options) {
|
|
19
|
+
super(message, options);
|
|
20
|
+
this.code = "REMOTE_COMPONENTS_ERROR";
|
|
21
|
+
this.name = "RemoteComponentsError";
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
// src/shared/client/static-loader.ts
|
|
28
|
+
var static_loader_exports = {};
|
|
29
|
+
__export(static_loader_exports, {
|
|
30
|
+
loadStaticRemoteComponent: () => loadStaticRemoteComponent
|
|
31
|
+
});
|
|
32
|
+
async function loadStaticRemoteComponent(scripts, url) {
|
|
33
|
+
const self = globalThis;
|
|
34
|
+
if (self.__remote_script_entrypoint_mount__?.[url.href]) {
|
|
35
|
+
self.__remote_script_entrypoint_mount__[url.href] = /* @__PURE__ */ new Set();
|
|
36
|
+
}
|
|
37
|
+
if (self.__remote_script_entrypoint_unmount__?.[url.href]) {
|
|
38
|
+
self.__remote_script_entrypoint_unmount__[url.href] = /* @__PURE__ */ new Set();
|
|
39
|
+
}
|
|
40
|
+
const mountUnmountSets = await Promise.all(
|
|
41
|
+
scripts.map(async (script) => {
|
|
42
|
+
try {
|
|
43
|
+
let src = typeof script.getAttribute === "function" ? script.getAttribute("src") ?? script.src : script.src;
|
|
44
|
+
if (!src && script.textContent) {
|
|
45
|
+
const blob = new Blob(
|
|
46
|
+
[
|
|
47
|
+
script.textContent.replace(
|
|
48
|
+
/import\.meta\.url/g,
|
|
49
|
+
JSON.stringify(url)
|
|
50
|
+
)
|
|
51
|
+
],
|
|
52
|
+
{
|
|
53
|
+
type: "text/javascript"
|
|
54
|
+
}
|
|
55
|
+
);
|
|
56
|
+
src = URL.createObjectURL(blob);
|
|
57
|
+
}
|
|
58
|
+
const mod = await import(
|
|
59
|
+
/* @vite-ignore */
|
|
60
|
+
/* webpackIgnore: true */
|
|
61
|
+
new URL(src, url).href
|
|
62
|
+
);
|
|
63
|
+
if (src.startsWith("blob:")) {
|
|
64
|
+
URL.revokeObjectURL(src);
|
|
65
|
+
}
|
|
66
|
+
if (typeof mod.mount === "function" || typeof mod.default?.mount === "function") {
|
|
67
|
+
if (!self.__remote_script_entrypoint_mount__) {
|
|
68
|
+
self.__remote_script_entrypoint_mount__ = {};
|
|
69
|
+
}
|
|
70
|
+
if (!self.__remote_script_entrypoint_mount__[url.href]) {
|
|
71
|
+
self.__remote_script_entrypoint_mount__[url.href] = /* @__PURE__ */ new Set();
|
|
72
|
+
}
|
|
73
|
+
self.__remote_script_entrypoint_mount__[url.href]?.add(
|
|
74
|
+
mod.mount || mod.default?.mount || (() => {
|
|
75
|
+
})
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
if (typeof mod.unmount === "function" || typeof mod.default?.unmount === "function") {
|
|
79
|
+
if (!self.__remote_script_entrypoint_unmount__) {
|
|
80
|
+
self.__remote_script_entrypoint_unmount__ = {};
|
|
81
|
+
}
|
|
82
|
+
if (!self.__remote_script_entrypoint_unmount__[url.href]) {
|
|
83
|
+
self.__remote_script_entrypoint_unmount__[url.href] = /* @__PURE__ */ new Set();
|
|
84
|
+
}
|
|
85
|
+
self.__remote_script_entrypoint_unmount__[url.href]?.add(
|
|
86
|
+
mod.unmount || mod.default?.unmount || (() => {
|
|
87
|
+
})
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
return {
|
|
91
|
+
mount: mod.mount || mod.default?.mount,
|
|
92
|
+
unmount: mod.unmount || mod.default?.unmount
|
|
93
|
+
};
|
|
94
|
+
} catch (e) {
|
|
95
|
+
console.error(
|
|
96
|
+
new RemoteComponentsError(
|
|
97
|
+
`Error loading remote component script from "${script.src || url.href}".`,
|
|
98
|
+
{ cause: e }
|
|
99
|
+
)
|
|
100
|
+
);
|
|
101
|
+
return {
|
|
102
|
+
mount: void 0,
|
|
103
|
+
unmount: void 0
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
})
|
|
107
|
+
);
|
|
108
|
+
return mountUnmountSets.reduce(
|
|
109
|
+
(acc, { mount, unmount }) => {
|
|
110
|
+
if (typeof mount === "function") {
|
|
111
|
+
acc.mount.add(mount);
|
|
112
|
+
}
|
|
113
|
+
if (typeof unmount === "function") {
|
|
114
|
+
acc.unmount.add(unmount);
|
|
115
|
+
}
|
|
116
|
+
return acc;
|
|
117
|
+
},
|
|
118
|
+
{
|
|
119
|
+
mount: /* @__PURE__ */ new Set(),
|
|
120
|
+
unmount: /* @__PURE__ */ new Set()
|
|
121
|
+
}
|
|
122
|
+
);
|
|
123
|
+
}
|
|
124
|
+
var init_static_loader = __esm({
|
|
125
|
+
"src/shared/client/static-loader.ts"() {
|
|
126
|
+
"use strict";
|
|
127
|
+
init_error();
|
|
128
|
+
}
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
// src/html/remote/index.tsx
|
|
132
|
+
init_error();
|
|
133
|
+
if (typeof HTMLElement !== "undefined") {
|
|
134
|
+
class RemoteComponent extends HTMLElement {
|
|
135
|
+
constructor() {
|
|
136
|
+
super();
|
|
137
|
+
this.root = null;
|
|
138
|
+
this.unmount = null;
|
|
139
|
+
const html = this.innerHTML;
|
|
140
|
+
this.root = this.attachShadow({
|
|
141
|
+
mode: this.getAttribute("mode") === "closed" ? "closed" : "open"
|
|
142
|
+
});
|
|
143
|
+
this.root.innerHTML = html;
|
|
144
|
+
(async () => {
|
|
145
|
+
const { loadStaticRemoteComponent: loadStaticRemoteComponent2 } = await Promise.resolve().then(() => (init_static_loader(), static_loader_exports));
|
|
146
|
+
loadStaticRemoteComponent2(
|
|
147
|
+
Array.from(this.root?.querySelectorAll("script") ?? []),
|
|
148
|
+
new URL(location.href)
|
|
149
|
+
).then(({ mount, unmount }) => {
|
|
150
|
+
this.unmount = unmount;
|
|
151
|
+
return Promise.all(
|
|
152
|
+
Array.from(mount).map((mountFn) => mountFn(this.root))
|
|
153
|
+
);
|
|
154
|
+
}).catch((e) => {
|
|
155
|
+
console.error(
|
|
156
|
+
new RemoteComponentsError("Error mounting remote component.", {
|
|
157
|
+
cause: e
|
|
158
|
+
})
|
|
159
|
+
);
|
|
160
|
+
});
|
|
161
|
+
})().catch((e) => {
|
|
162
|
+
console.error(
|
|
163
|
+
new RemoteComponentsError("Error mounting remote component.", {
|
|
164
|
+
cause: e
|
|
165
|
+
})
|
|
166
|
+
);
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
disconnectedCallback() {
|
|
170
|
+
(async () => {
|
|
171
|
+
if (this.unmount) {
|
|
172
|
+
await Promise.all(
|
|
173
|
+
Array.from(this.unmount).map(async (unmount) => {
|
|
174
|
+
try {
|
|
175
|
+
await unmount(this.root);
|
|
176
|
+
} catch (e) {
|
|
177
|
+
console.error(
|
|
178
|
+
new RemoteComponentsError(
|
|
179
|
+
"Error unmounting remote component.",
|
|
180
|
+
{ cause: e }
|
|
181
|
+
)
|
|
182
|
+
);
|
|
183
|
+
}
|
|
184
|
+
})
|
|
185
|
+
);
|
|
186
|
+
}
|
|
187
|
+
})().catch((e) => {
|
|
188
|
+
console.error(
|
|
189
|
+
new RemoteComponentsError("Error unmounting remote component.", {
|
|
190
|
+
cause: e
|
|
191
|
+
})
|
|
192
|
+
);
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
customElements.define("remote-component", RemoteComponent);
|
|
197
|
+
}
|
|
198
|
+
//# sourceMappingURL=remote.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/shared/error.ts","../../src/shared/client/static-loader.ts","../../src/html/remote/index.tsx"],"sourcesContent":["export class RemoteComponentsError extends Error {\n code = 'REMOTE_COMPONENTS_ERROR';\n\n constructor(message: string, options?: { cause?: unknown }) {\n super(message, options);\n this.name = 'RemoteComponentsError';\n }\n}\n\nexport function multipleRemoteComponentsError(url: string | undefined) {\n return new Error(\n `Multiple Remote Components found at \"${url}\". When a page exposes multiple Remote Components you must specify the \"name\" prop to select which one to load.`,\n );\n}\n\nexport function failedToFetchRemoteComponentError(url: string, error: unknown) {\n return new RemoteComponentsError(\n `Failed to fetch Remote Component from \"${url}\". Is the URL correct and accessible?`,\n { cause: error instanceof Error ? error : new Error(String(error)) },\n );\n}\n","import { RemoteComponentsError } from '../error';\nimport type {\n MountOrUnmountFunction,\n RemoteComponentMountUnmount,\n} from './types';\n\nexport async function loadStaticRemoteComponent(\n scripts: HTMLScriptElement[],\n url: URL,\n) {\n const self = globalThis as typeof globalThis & RemoteComponentMountUnmount;\n if (self.__remote_script_entrypoint_mount__?.[url.href]) {\n self.__remote_script_entrypoint_mount__[url.href] = new Set();\n }\n if (self.__remote_script_entrypoint_unmount__?.[url.href]) {\n self.__remote_script_entrypoint_unmount__[url.href] = new Set();\n }\n const mountUnmountSets = await Promise.all(\n scripts.map(async (script) => {\n try {\n let src =\n typeof script.getAttribute === 'function'\n ? (script.getAttribute('src') ?? script.src)\n : script.src;\n // if there's no src attribute but there is inline script content, create a blob URL for it\n if (!src && script.textContent) {\n const blob = new Blob(\n [\n script.textContent.replace(\n /import\\.meta\\.url/g,\n JSON.stringify(url),\n ),\n ],\n {\n type: 'text/javascript',\n },\n );\n src = URL.createObjectURL(blob);\n }\n const mod = (await import(\n /* @vite-ignore */\n /* webpackIgnore: true */ new URL(src, url).href\n )) as {\n mount?: MountOrUnmountFunction;\n unmount?: MountOrUnmountFunction;\n default?: {\n mount?: MountOrUnmountFunction;\n unmount?: MountOrUnmountFunction;\n };\n };\n // revoke the object URL if we created one\n if (src.startsWith('blob:')) {\n URL.revokeObjectURL(src);\n }\n if (\n typeof mod.mount === 'function' ||\n typeof mod.default?.mount === 'function'\n ) {\n if (!self.__remote_script_entrypoint_mount__) {\n // eslint-disable-next-line camelcase\n self.__remote_script_entrypoint_mount__ = {};\n }\n if (!self.__remote_script_entrypoint_mount__[url.href]) {\n self.__remote_script_entrypoint_mount__[url.href] = new Set();\n }\n self.__remote_script_entrypoint_mount__[url.href]?.add(\n mod.mount ||\n mod.default?.mount ||\n (() => {\n // noop\n }),\n );\n }\n if (\n typeof mod.unmount === 'function' ||\n typeof mod.default?.unmount === 'function'\n ) {\n if (!self.__remote_script_entrypoint_unmount__) {\n // eslint-disable-next-line camelcase\n self.__remote_script_entrypoint_unmount__ = {};\n }\n if (!self.__remote_script_entrypoint_unmount__[url.href]) {\n self.__remote_script_entrypoint_unmount__[url.href] = new Set();\n }\n self.__remote_script_entrypoint_unmount__[url.href]?.add(\n mod.unmount ||\n mod.default?.unmount ||\n (() => {\n // noop\n }),\n );\n }\n return {\n mount: mod.mount || mod.default?.mount,\n unmount: mod.unmount || mod.default?.unmount,\n };\n } catch (e) {\n // eslint-disable-next-line no-console\n console.error(\n new RemoteComponentsError(\n `Error loading remote component script from \"${script.src || url.href}\".`,\n { cause: e },\n ),\n );\n return {\n mount: undefined,\n unmount: undefined,\n };\n }\n }),\n );\n return mountUnmountSets.reduce(\n (acc, { mount, unmount }) => {\n if (typeof mount === 'function') {\n acc.mount.add(mount);\n }\n if (typeof unmount === 'function') {\n acc.unmount.add(unmount);\n }\n return acc;\n },\n {\n mount: new Set<MountOrUnmountFunction>(),\n unmount: new Set<MountOrUnmountFunction>(),\n },\n );\n}\n","import type { MountOrUnmountFunction } from '../../shared/client/types';\nimport { RemoteComponentsError } from '../../shared/error';\n\nif (typeof HTMLElement !== 'undefined') {\n class RemoteComponent extends HTMLElement {\n name?: string;\n root?: ShadowRoot | null = null;\n unmount?: Set<MountOrUnmountFunction> | null = null;\n\n constructor() {\n super();\n\n const html = this.innerHTML;\n this.root = this.attachShadow({\n mode: this.getAttribute('mode') === 'closed' ? 'closed' : 'open',\n });\n this.root.innerHTML = html;\n\n // run the mount functions for any scripts inside the shadow root asynchronously\n (async () => {\n const { loadStaticRemoteComponent } = await import(\n '../../shared/client/static-loader'\n );\n loadStaticRemoteComponent(\n Array.from(this.root?.querySelectorAll('script') ?? []),\n new URL(location.href),\n )\n .then(({ mount, unmount }) => {\n this.unmount = unmount;\n return Promise.all(\n Array.from(mount).map((mountFn) => mountFn(this.root)),\n );\n })\n .catch((e) => {\n // eslint-disable-next-line no-console\n console.error(\n new RemoteComponentsError('Error mounting remote component.', {\n cause: e,\n }),\n );\n });\n })().catch((e) => {\n // eslint-disable-next-line no-console\n console.error(\n new RemoteComponentsError('Error mounting remote component.', {\n cause: e,\n }),\n );\n });\n }\n\n disconnectedCallback() {\n // run the unmount functions for any scripts inside the shadow root asynchronously\n (async () => {\n if (this.unmount) {\n await Promise.all(\n Array.from(this.unmount).map(async (unmount) => {\n try {\n await unmount(this.root);\n } catch (e) {\n // eslint-disable-next-line no-console\n console.error(\n new RemoteComponentsError(\n 'Error unmounting remote component.',\n { cause: e },\n ),\n );\n }\n }),\n );\n }\n })().catch((e) => {\n // eslint-disable-next-line no-console\n console.error(\n new RemoteComponentsError('Error unmounting remote component.', {\n cause: e,\n }),\n );\n });\n }\n }\n\n // register the custom element\n customElements.define('remote-component', RemoteComponent);\n}\n"],"mappings":";;;;;;;;;;;;AAAA,IAAa;AAAb;AAAA;AAAA;AAAO,IAAM,wBAAN,cAAoC,MAAM;AAAA,MAG/C,YAAY,SAAiB,SAA+B;AAC1D,cAAM,SAAS,OAAO;AAHxB,oBAAO;AAIL,aAAK,OAAO;AAAA,MACd;AAAA,IACF;AAAA;AAAA;;;ACPA;AAAA;AAAA;AAAA;AAMA,eAAsB,0BACpB,SACA,KACA;AACA,QAAM,OAAO;AACb,MAAI,KAAK,qCAAqC,IAAI,IAAI,GAAG;AACvD,SAAK,mCAAmC,IAAI,IAAI,IAAI,oBAAI,IAAI;AAAA,EAC9D;AACA,MAAI,KAAK,uCAAuC,IAAI,IAAI,GAAG;AACzD,SAAK,qCAAqC,IAAI,IAAI,IAAI,oBAAI,IAAI;AAAA,EAChE;AACA,QAAM,mBAAmB,MAAM,QAAQ;AAAA,IACrC,QAAQ,IAAI,OAAO,WAAW;AAC5B,UAAI;AACF,YAAI,MACF,OAAO,OAAO,iBAAiB,aAC1B,OAAO,aAAa,KAAK,KAAK,OAAO,MACtC,OAAO;AAEb,YAAI,CAAC,OAAO,OAAO,aAAa;AAC9B,gBAAM,OAAO,IAAI;AAAA,YACf;AAAA,cACE,OAAO,YAAY;AAAA,gBACjB;AAAA,gBACA,KAAK,UAAU,GAAG;AAAA,cACpB;AAAA,YACF;AAAA,YACA;AAAA,cACE,MAAM;AAAA,YACR;AAAA,UACF;AACA,gBAAM,IAAI,gBAAgB,IAAI;AAAA,QAChC;AACA,cAAM,MAAO,MAAM;AAAA;AAAA;AAAA,UAES,IAAI,IAA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,YAC5D,OAAO;AAAA,UACT,CAAC;AAAA,QACH;AAAA,MACF,CAAC;AAAA,IACH;AAAA,IAEA,uBAAuB;AAErB,OAAC,YAAY;AACX,YAAI,KAAK,SAAS;AAChB,gBAAM,QAAQ;AAAA,YACZ,MAAM,KAAK,KAAK,OAAO,EAAE,IAAI,OAAO,YAAY;AAC9C,kBAAI;AACF,sBAAM,QAAQ,KAAK,IAAI;AAAA,cACzB,SAAS,GAAP;AAEA,wBAAQ;AAAA,kBACN,IAAI;AAAA,oBACF;AAAA,oBACA,EAAE,OAAO,EAAE;AAAA,kBACb;AAAA,gBACF;AAAA,cACF;AAAA,YACF,CAAC;AAAA,UACH;AAAA,QACF;AAAA,MACF,GAAG,EAAE,MAAM,CAAC,MAAM;AAEhB,gBAAQ;AAAA,UACN,IAAI,sBAAsB,sCAAsC;AAAA,YAC9D,OAAO;AAAA,UACT,CAAC;AAAA,QACH;AAAA,MACF,CAAC;AAAA,IACH;AAAA,EACF;AAGA,iBAAe,OAAO,oBAAoB,eAAe;AAC3D;","names":["loadStaticRemoteComponent"]}
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
3
|
+
var __esm = (fn, res) => function __init() {
|
|
4
|
+
return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
|
|
5
|
+
};
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
// src/shared/error.ts
|
|
12
|
+
var RemoteComponentsError;
|
|
13
|
+
var init_error = __esm({
|
|
14
|
+
"src/shared/error.ts"() {
|
|
15
|
+
"use strict";
|
|
16
|
+
RemoteComponentsError = class extends Error {
|
|
17
|
+
constructor(message, options) {
|
|
18
|
+
super(message, options);
|
|
19
|
+
this.code = "REMOTE_COMPONENTS_ERROR";
|
|
20
|
+
this.name = "RemoteComponentsError";
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
// src/shared/client/static-loader.ts
|
|
27
|
+
var static_loader_exports = {};
|
|
28
|
+
__export(static_loader_exports, {
|
|
29
|
+
loadStaticRemoteComponent: () => loadStaticRemoteComponent
|
|
30
|
+
});
|
|
31
|
+
async function loadStaticRemoteComponent(scripts, url) {
|
|
32
|
+
const self = globalThis;
|
|
33
|
+
if (self.__remote_script_entrypoint_mount__?.[url.href]) {
|
|
34
|
+
self.__remote_script_entrypoint_mount__[url.href] = /* @__PURE__ */ new Set();
|
|
35
|
+
}
|
|
36
|
+
if (self.__remote_script_entrypoint_unmount__?.[url.href]) {
|
|
37
|
+
self.__remote_script_entrypoint_unmount__[url.href] = /* @__PURE__ */ new Set();
|
|
38
|
+
}
|
|
39
|
+
const mountUnmountSets = await Promise.all(
|
|
40
|
+
scripts.map(async (script) => {
|
|
41
|
+
try {
|
|
42
|
+
let src = typeof script.getAttribute === "function" ? script.getAttribute("src") ?? script.src : script.src;
|
|
43
|
+
if (!src && script.textContent) {
|
|
44
|
+
const blob = new Blob(
|
|
45
|
+
[
|
|
46
|
+
script.textContent.replace(
|
|
47
|
+
/import\.meta\.url/g,
|
|
48
|
+
JSON.stringify(url)
|
|
49
|
+
)
|
|
50
|
+
],
|
|
51
|
+
{
|
|
52
|
+
type: "text/javascript"
|
|
53
|
+
}
|
|
54
|
+
);
|
|
55
|
+
src = URL.createObjectURL(blob);
|
|
56
|
+
}
|
|
57
|
+
const mod = await import(
|
|
58
|
+
/* @vite-ignore */
|
|
59
|
+
/* webpackIgnore: true */
|
|
60
|
+
new URL(src, url).href
|
|
61
|
+
);
|
|
62
|
+
if (src.startsWith("blob:")) {
|
|
63
|
+
URL.revokeObjectURL(src);
|
|
64
|
+
}
|
|
65
|
+
if (typeof mod.mount === "function" || typeof mod.default?.mount === "function") {
|
|
66
|
+
if (!self.__remote_script_entrypoint_mount__) {
|
|
67
|
+
self.__remote_script_entrypoint_mount__ = {};
|
|
68
|
+
}
|
|
69
|
+
if (!self.__remote_script_entrypoint_mount__[url.href]) {
|
|
70
|
+
self.__remote_script_entrypoint_mount__[url.href] = /* @__PURE__ */ new Set();
|
|
71
|
+
}
|
|
72
|
+
self.__remote_script_entrypoint_mount__[url.href]?.add(
|
|
73
|
+
mod.mount || mod.default?.mount || (() => {
|
|
74
|
+
})
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
if (typeof mod.unmount === "function" || typeof mod.default?.unmount === "function") {
|
|
78
|
+
if (!self.__remote_script_entrypoint_unmount__) {
|
|
79
|
+
self.__remote_script_entrypoint_unmount__ = {};
|
|
80
|
+
}
|
|
81
|
+
if (!self.__remote_script_entrypoint_unmount__[url.href]) {
|
|
82
|
+
self.__remote_script_entrypoint_unmount__[url.href] = /* @__PURE__ */ new Set();
|
|
83
|
+
}
|
|
84
|
+
self.__remote_script_entrypoint_unmount__[url.href]?.add(
|
|
85
|
+
mod.unmount || mod.default?.unmount || (() => {
|
|
86
|
+
})
|
|
87
|
+
);
|
|
88
|
+
}
|
|
89
|
+
return {
|
|
90
|
+
mount: mod.mount || mod.default?.mount,
|
|
91
|
+
unmount: mod.unmount || mod.default?.unmount
|
|
92
|
+
};
|
|
93
|
+
} catch (e) {
|
|
94
|
+
console.error(
|
|
95
|
+
new RemoteComponentsError(
|
|
96
|
+
`Error loading remote component script from "${script.src || url.href}".`,
|
|
97
|
+
{ cause: e }
|
|
98
|
+
)
|
|
99
|
+
);
|
|
100
|
+
return {
|
|
101
|
+
mount: void 0,
|
|
102
|
+
unmount: void 0
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
})
|
|
106
|
+
);
|
|
107
|
+
return mountUnmountSets.reduce(
|
|
108
|
+
(acc, { mount, unmount }) => {
|
|
109
|
+
if (typeof mount === "function") {
|
|
110
|
+
acc.mount.add(mount);
|
|
111
|
+
}
|
|
112
|
+
if (typeof unmount === "function") {
|
|
113
|
+
acc.unmount.add(unmount);
|
|
114
|
+
}
|
|
115
|
+
return acc;
|
|
116
|
+
},
|
|
117
|
+
{
|
|
118
|
+
mount: /* @__PURE__ */ new Set(),
|
|
119
|
+
unmount: /* @__PURE__ */ new Set()
|
|
120
|
+
}
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
var init_static_loader = __esm({
|
|
124
|
+
"src/shared/client/static-loader.ts"() {
|
|
125
|
+
"use strict";
|
|
126
|
+
init_error();
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
// src/html/remote/index.tsx
|
|
131
|
+
init_error();
|
|
132
|
+
if (typeof HTMLElement !== "undefined") {
|
|
133
|
+
class RemoteComponent extends HTMLElement {
|
|
134
|
+
constructor() {
|
|
135
|
+
super();
|
|
136
|
+
this.root = null;
|
|
137
|
+
this.unmount = null;
|
|
138
|
+
const html = this.innerHTML;
|
|
139
|
+
this.root = this.attachShadow({
|
|
140
|
+
mode: this.getAttribute("mode") === "closed" ? "closed" : "open"
|
|
141
|
+
});
|
|
142
|
+
this.root.innerHTML = html;
|
|
143
|
+
(async () => {
|
|
144
|
+
const { loadStaticRemoteComponent: loadStaticRemoteComponent2 } = await Promise.resolve().then(() => (init_static_loader(), static_loader_exports));
|
|
145
|
+
loadStaticRemoteComponent2(
|
|
146
|
+
Array.from(this.root?.querySelectorAll("script") ?? []),
|
|
147
|
+
new URL(location.href)
|
|
148
|
+
).then(({ mount, unmount }) => {
|
|
149
|
+
this.unmount = unmount;
|
|
150
|
+
return Promise.all(
|
|
151
|
+
Array.from(mount).map((mountFn) => mountFn(this.root))
|
|
152
|
+
);
|
|
153
|
+
}).catch((e) => {
|
|
154
|
+
console.error(
|
|
155
|
+
new RemoteComponentsError("Error mounting remote component.", {
|
|
156
|
+
cause: e
|
|
157
|
+
})
|
|
158
|
+
);
|
|
159
|
+
});
|
|
160
|
+
})().catch((e) => {
|
|
161
|
+
console.error(
|
|
162
|
+
new RemoteComponentsError("Error mounting remote component.", {
|
|
163
|
+
cause: e
|
|
164
|
+
})
|
|
165
|
+
);
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
disconnectedCallback() {
|
|
169
|
+
(async () => {
|
|
170
|
+
if (this.unmount) {
|
|
171
|
+
await Promise.all(
|
|
172
|
+
Array.from(this.unmount).map(async (unmount) => {
|
|
173
|
+
try {
|
|
174
|
+
await unmount(this.root);
|
|
175
|
+
} catch (e) {
|
|
176
|
+
console.error(
|
|
177
|
+
new RemoteComponentsError(
|
|
178
|
+
"Error unmounting remote component.",
|
|
179
|
+
{ cause: e }
|
|
180
|
+
)
|
|
181
|
+
);
|
|
182
|
+
}
|
|
183
|
+
})
|
|
184
|
+
);
|
|
185
|
+
}
|
|
186
|
+
})().catch((e) => {
|
|
187
|
+
console.error(
|
|
188
|
+
new RemoteComponentsError("Error unmounting remote component.", {
|
|
189
|
+
cause: e
|
|
190
|
+
})
|
|
191
|
+
);
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
customElements.define("remote-component", RemoteComponent);
|
|
196
|
+
}
|
|
197
|
+
//# sourceMappingURL=remote.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/shared/error.ts","../../src/shared/client/static-loader.ts","../../src/html/remote/index.tsx"],"sourcesContent":["export class RemoteComponentsError extends Error {\n code = 'REMOTE_COMPONENTS_ERROR';\n\n constructor(message: string, options?: { cause?: unknown }) {\n super(message, options);\n this.name = 'RemoteComponentsError';\n }\n}\n\nexport function multipleRemoteComponentsError(url: string | undefined) {\n return new Error(\n `Multiple Remote Components found at \"${url}\". When a page exposes multiple Remote Components you must specify the \"name\" prop to select which one to load.`,\n );\n}\n\nexport function failedToFetchRemoteComponentError(url: string, error: unknown) {\n return new RemoteComponentsError(\n `Failed to fetch Remote Component from \"${url}\". Is the URL correct and accessible?`,\n { cause: error instanceof Error ? error : new Error(String(error)) },\n );\n}\n","import { RemoteComponentsError } from '../error';\nimport type {\n MountOrUnmountFunction,\n RemoteComponentMountUnmount,\n} from './types';\n\nexport async function loadStaticRemoteComponent(\n scripts: HTMLScriptElement[],\n url: URL,\n) {\n const self = globalThis as typeof globalThis & RemoteComponentMountUnmount;\n if (self.__remote_script_entrypoint_mount__?.[url.href]) {\n self.__remote_script_entrypoint_mount__[url.href] = new Set();\n }\n if (self.__remote_script_entrypoint_unmount__?.[url.href]) {\n self.__remote_script_entrypoint_unmount__[url.href] = new Set();\n }\n const mountUnmountSets = await Promise.all(\n scripts.map(async (script) => {\n try {\n let src =\n typeof script.getAttribute === 'function'\n ? (script.getAttribute('src') ?? script.src)\n : script.src;\n // if there's no src attribute but there is inline script content, create a blob URL for it\n if (!src && script.textContent) {\n const blob = new Blob(\n [\n script.textContent.replace(\n /import\\.meta\\.url/g,\n JSON.stringify(url),\n ),\n ],\n {\n type: 'text/javascript',\n },\n );\n src = URL.createObjectURL(blob);\n }\n const mod = (await import(\n /* @vite-ignore */\n /* webpackIgnore: true */ new URL(src, url).href\n )) as {\n mount?: MountOrUnmountFunction;\n unmount?: MountOrUnmountFunction;\n default?: {\n mount?: MountOrUnmountFunction;\n unmount?: MountOrUnmountFunction;\n };\n };\n // revoke the object URL if we created one\n if (src.startsWith('blob:')) {\n URL.revokeObjectURL(src);\n }\n if (\n typeof mod.mount === 'function' ||\n typeof mod.default?.mount === 'function'\n ) {\n if (!self.__remote_script_entrypoint_mount__) {\n // eslint-disable-next-line camelcase\n self.__remote_script_entrypoint_mount__ = {};\n }\n if (!self.__remote_script_entrypoint_mount__[url.href]) {\n self.__remote_script_entrypoint_mount__[url.href] = new Set();\n }\n self.__remote_script_entrypoint_mount__[url.href]?.add(\n mod.mount ||\n mod.default?.mount ||\n (() => {\n // noop\n }),\n );\n }\n if (\n typeof mod.unmount === 'function' ||\n typeof mod.default?.unmount === 'function'\n ) {\n if (!self.__remote_script_entrypoint_unmount__) {\n // eslint-disable-next-line camelcase\n self.__remote_script_entrypoint_unmount__ = {};\n }\n if (!self.__remote_script_entrypoint_unmount__[url.href]) {\n self.__remote_script_entrypoint_unmount__[url.href] = new Set();\n }\n self.__remote_script_entrypoint_unmount__[url.href]?.add(\n mod.unmount ||\n mod.default?.unmount ||\n (() => {\n // noop\n }),\n );\n }\n return {\n mount: mod.mount || mod.default?.mount,\n unmount: mod.unmount || mod.default?.unmount,\n };\n } catch (e) {\n // eslint-disable-next-line no-console\n console.error(\n new RemoteComponentsError(\n `Error loading remote component script from \"${script.src || url.href}\".`,\n { cause: e },\n ),\n );\n return {\n mount: undefined,\n unmount: undefined,\n };\n }\n }),\n );\n return mountUnmountSets.reduce(\n (acc, { mount, unmount }) => {\n if (typeof mount === 'function') {\n acc.mount.add(mount);\n }\n if (typeof unmount === 'function') {\n acc.unmount.add(unmount);\n }\n return acc;\n },\n {\n mount: new Set<MountOrUnmountFunction>(),\n unmount: new Set<MountOrUnmountFunction>(),\n },\n );\n}\n","import type { MountOrUnmountFunction } from '../../shared/client/types';\nimport { RemoteComponentsError } from '../../shared/error';\n\nif (typeof HTMLElement !== 'undefined') {\n class RemoteComponent extends HTMLElement {\n name?: string;\n root?: ShadowRoot | null = null;\n unmount?: Set<MountOrUnmountFunction> | null = null;\n\n constructor() {\n super();\n\n const html = this.innerHTML;\n this.root = this.attachShadow({\n mode: this.getAttribute('mode') === 'closed' ? 'closed' : 'open',\n });\n this.root.innerHTML = html;\n\n // run the mount functions for any scripts inside the shadow root asynchronously\n (async () => {\n const { loadStaticRemoteComponent } = await import(\n '../../shared/client/static-loader'\n );\n loadStaticRemoteComponent(\n Array.from(this.root?.querySelectorAll('script') ?? []),\n new URL(location.href),\n )\n .then(({ mount, unmount }) => {\n this.unmount = unmount;\n return Promise.all(\n Array.from(mount).map((mountFn) => mountFn(this.root)),\n );\n })\n .catch((e) => {\n // eslint-disable-next-line no-console\n console.error(\n new RemoteComponentsError('Error mounting remote component.', {\n cause: e,\n }),\n );\n });\n })().catch((e) => {\n // eslint-disable-next-line no-console\n console.error(\n new RemoteComponentsError('Error mounting remote component.', {\n cause: e,\n }),\n );\n });\n }\n\n disconnectedCallback() {\n // run the unmount functions for any scripts inside the shadow root asynchronously\n (async () => {\n if (this.unmount) {\n await Promise.all(\n Array.from(this.unmount).map(async (unmount) => {\n try {\n await unmount(this.root);\n } catch (e) {\n // eslint-disable-next-line no-console\n console.error(\n new RemoteComponentsError(\n 'Error unmounting remote component.',\n { cause: e },\n ),\n );\n }\n }),\n );\n }\n })().catch((e) => {\n // eslint-disable-next-line no-console\n console.error(\n new RemoteComponentsError('Error unmounting remote component.', {\n cause: e,\n }),\n );\n });\n }\n }\n\n // register the custom element\n customElements.define('remote-component', RemoteComponent);\n}\n"],"mappings":";;;;;;;;;;;AAAA,IAAa;AAAb;AAAA;AAAA;AAAO,IAAM,wBAAN,cAAoC,MAAM;AAAA,MAG/C,YAAY,SAAiB,SAA+B;AAC1D,cAAM,SAAS,OAAO;AAHxB,oBAAO;AAIL,aAAK,OAAO;AAAA,MACd;AAAA,IACF;AAAA;AAAA;;;ACPA;AAAA;AAAA;AAAA;AAMA,eAAsB,0BACpB,SACA,KACA;AACA,QAAM,OAAO;AACb,MAAI,KAAK,qCAAqC,IAAI,IAAI,GAAG;AACvD,SAAK,mCAAmC,IAAI,IAAI,IAAI,oBAAI,IAAI;AAAA,EAC9D;AACA,MAAI,KAAK,uCAAuC,IAAI,IAAI,GAAG;AACzD,SAAK,qCAAqC,IAAI,IAAI,IAAI,oBAAI,IAAI;AAAA,EAChE;AACA,QAAM,mBAAmB,MAAM,QAAQ;AAAA,IACrC,QAAQ,IAAI,OAAO,WAAW;AAC5B,UAAI;AACF,YAAI,MACF,OAAO,OAAO,iBAAiB,aAC1B,OAAO,aAAa,KAAK,KAAK,OAAO,MACtC,OAAO;AAEb,YAAI,CAAC,OAAO,OAAO,aAAa;AAC9B,gBAAM,OAAO,IAAI;AAAA,YACf;AAAA,cACE,OAAO,YAAY;AAAA,gBACjB;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,kBAAQ;AAAA,YACN,IAAI,sBAAsB,oCAAoC;AAAA,cAC5D,OAAO;AAAA,YACT,CAAC;AAAA,UACH;AAAA,QACF,CAAC;AAAA,MACL,GAAG,EAAE,MAAM,CAAC,MAAM;AAEhB,gBAAQ;AAAA,UACN,IAAI,sBAAsB,oCAAoC;AAAA,YAC5D,OAAO;AAAA,UACT,CAAC;AAAA,QACH;AAAA,MACF,CAAC;AAAA,IACH;AAAA,IAEA,uBAAuB;AAErB,OAAC,YAAY;AACX,YAAI,KAAK,SAAS;AAChB,gBAAM,QAAQ;AAAA,YACZ,MAAM,KAAK,KAAK,OAAO,EAAE,IAAI,OAAO,YAAY;AAC9C,kBAAI;AACF,sBAAM,QAAQ,KAAK,IAAI;AAAA,cACzB,SAAS,GAAP;AAEA,wBAAQ;AAAA,kBACN,IAAI;AAAA,oBACF;AAAA,oBACA,EAAE,OAAO,EAAE;AAAA,kBACb;AAAA,gBACF;AAAA,cACF;AAAA,YACF,CAAC;AAAA,UACH;AAAA,QACF;AAAA,MACF,GAAG,EAAE,MAAM,CAAC,MAAM;AAEhB,gBAAQ;AAAA,UACN,IAAI,sBAAsB,sCAAsC;AAAA,YAC9D,OAAO;AAAA,UACT,CAAC;AAAA,QACH;AAAA,MACF,CAAC;AAAA,IACH;AAAA,EACF;AAGA,iBAAe,OAAO,oBAAoB,eAAe;AAC3D;","names":["loadStaticRemoteComponent"]}
|
|
@@ -61,6 +61,7 @@ function RemoteComponentClient({
|
|
|
61
61
|
isolate,
|
|
62
62
|
mode = "open",
|
|
63
63
|
reset,
|
|
64
|
+
type,
|
|
64
65
|
children
|
|
65
66
|
}) {
|
|
66
67
|
const remoteComponentId = (0, import_react.useId)();
|
|
@@ -72,10 +73,13 @@ function RemoteComponentClient({
|
|
|
72
73
|
url,
|
|
73
74
|
loading: false
|
|
74
75
|
});
|
|
76
|
+
const startRef = (0, import_react.useRef)(null);
|
|
77
|
+
const endRef = (0, import_react.useRef)(null);
|
|
78
|
+
const prevRemoteComponentUnmountRef = (0, import_react.useRef)(null);
|
|
75
79
|
if (component instanceof Error) {
|
|
76
80
|
throw component;
|
|
77
81
|
}
|
|
78
|
-
const shouldUseChildren = (!component || component && !nextData && typeof component.then !== "function") && // if the remote Next.js Pages Router application is in development mode
|
|
82
|
+
const shouldUseChildren = (!component || component && !nextData && type !== "remote-component" && typeof component.then !== "function") && // if the remote Next.js Pages Router application is in development mode
|
|
79
83
|
// we don't use the provided static HTML
|
|
80
84
|
// to mitigate layout shift when loading CSS using JavaScript on the client
|
|
81
85
|
nextData?.buildId !== "development";
|
|
@@ -161,6 +165,23 @@ function RemoteComponentClient({
|
|
|
161
165
|
shouldUseChildren,
|
|
162
166
|
remoteComponentId
|
|
163
167
|
]);
|
|
168
|
+
(0, import_react.useEffect)(() => {
|
|
169
|
+
if (prevRemoteComponentUnmountRef.current && component !== children) {
|
|
170
|
+
const { unmount, root } = prevRemoteComponentUnmountRef.current;
|
|
171
|
+
prevRemoteComponentUnmountRef.current = null;
|
|
172
|
+
Promise.all(
|
|
173
|
+
Array.from(unmount).map(async (unmountFn) => {
|
|
174
|
+
try {
|
|
175
|
+
await unmountFn(root);
|
|
176
|
+
} catch (e) {
|
|
177
|
+
console.error(e);
|
|
178
|
+
}
|
|
179
|
+
})
|
|
180
|
+
).catch((e) => {
|
|
181
|
+
console.error(e);
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
}, [component, children]);
|
|
164
185
|
(0, import_react.useEffect)(() => {
|
|
165
186
|
(0, import_react.startTransition)(async () => {
|
|
166
187
|
try {
|
|
@@ -172,6 +193,36 @@ function RemoteComponentClient({
|
|
|
172
193
|
url,
|
|
173
194
|
loading: true
|
|
174
195
|
};
|
|
196
|
+
if (type === "remote-component") {
|
|
197
|
+
setComponent(children);
|
|
198
|
+
(0, import_remote_component.loadStaticRemoteComponent)(
|
|
199
|
+
scripts,
|
|
200
|
+
new URL(url, location.origin)
|
|
201
|
+
).then(({ mount, unmount }) => {
|
|
202
|
+
if (mount.size > 0) {
|
|
203
|
+
let root = shadowRootRef.current;
|
|
204
|
+
try {
|
|
205
|
+
if (!root && startRef.current && endRef.current) {
|
|
206
|
+
root = document.querySelectorAll(
|
|
207
|
+
`#${startRef.current.id} ~ *:not(#${endRef.current.id}):has(~ #${endRef.current.id})`
|
|
208
|
+
);
|
|
209
|
+
}
|
|
210
|
+
} catch (e) {
|
|
211
|
+
console.error(e);
|
|
212
|
+
}
|
|
213
|
+
if (unmount.size > 0) {
|
|
214
|
+
prevRemoteComponentUnmountRef.current = { unmount, root };
|
|
215
|
+
}
|
|
216
|
+
return Promise.all(
|
|
217
|
+
Array.from(mount).map((mountFn) => mountFn(root))
|
|
218
|
+
);
|
|
219
|
+
}
|
|
220
|
+
}).catch((e) => {
|
|
221
|
+
console.error(e);
|
|
222
|
+
setComponent(e);
|
|
223
|
+
});
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
175
226
|
const rscName = `__remote_components_rsc_${(0, import_utils.escapeString)(new URL(url).href)}_${(0, import_utils.escapeString)(name)}`;
|
|
176
227
|
const result = await (0, import_remote_component.loadRemoteComponent)({
|
|
177
228
|
url: new URL(url, location.origin),
|
|
@@ -223,7 +274,8 @@ function RemoteComponentClient({
|
|
|
223
274
|
remoteShared,
|
|
224
275
|
children,
|
|
225
276
|
links,
|
|
226
|
-
isolate
|
|
277
|
+
isolate,
|
|
278
|
+
type
|
|
227
279
|
]);
|
|
228
280
|
if (nextData?.buildId === "development" && shadowRootRef.current && isolate !== false && reset && !shadowRootRef.current.querySelector("[data-remote-components-reset]")) {
|
|
229
281
|
const style = document.createElement("style");
|
|
@@ -323,7 +375,7 @@ function RemoteComponentClient({
|
|
|
323
375
|
);
|
|
324
376
|
}
|
|
325
377
|
return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [
|
|
326
|
-
isolate === false ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)("template", { id: `${name}_start
|
|
378
|
+
isolate === false ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)("template", { id: `${name}_start`, ref: startRef }) : null,
|
|
327
379
|
/* @__PURE__ */ (0, import_jsx_runtime.jsx)("script", { "data-remote-component": true, type: "application/json", children: JSON.stringify({
|
|
328
380
|
name,
|
|
329
381
|
bundle,
|
|
@@ -340,7 +392,7 @@ function RemoteComponentClient({
|
|
|
340
392
|
children: JSON.stringify(nextData)
|
|
341
393
|
}
|
|
342
394
|
) : null,
|
|
343
|
-
isolate === false ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)("template", { id: `${name}_end
|
|
395
|
+
isolate === false ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)("template", { id: `${name}_end`, ref: endRef }) : null
|
|
344
396
|
] });
|
|
345
397
|
}
|
|
346
398
|
// Annotate the CommonJS export names for ESM import in node:
|