remote-components 0.0.1 → 0.0.3

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.
Files changed (46) hide show
  1. package/dist/html/host.cjs +56 -22
  2. package/dist/html/host.cjs.map +1 -1
  3. package/dist/html/host.js +56 -22
  4. package/dist/html/host.js.map +1 -1
  5. package/dist/next/config.cjs +75 -32
  6. package/dist/next/config.cjs.map +1 -1
  7. package/dist/next/config.d.ts +35 -2
  8. package/dist/next/config.js +75 -35
  9. package/dist/next/config.js.map +1 -1
  10. package/dist/next/host/app-client.cjs +78 -22
  11. package/dist/next/host/app-client.cjs.map +1 -1
  12. package/dist/next/host/app-client.d.ts +13 -1
  13. package/dist/next/host/app-client.js +77 -21
  14. package/dist/next/host/app-client.js.map +1 -1
  15. package/dist/next/host/app-server.cjs +6 -1
  16. package/dist/next/host/app-server.cjs.map +1 -1
  17. package/dist/next/host/app-server.d.ts +28 -1
  18. package/dist/next/host/app-server.js +6 -1
  19. package/dist/next/host/app-server.js.map +1 -1
  20. package/dist/next/host/pages-server.cjs.map +1 -1
  21. package/dist/next/host/pages-server.d.ts +34 -0
  22. package/dist/next/host/pages-server.js.map +1 -1
  23. package/dist/next/middleware.cjs +71 -0
  24. package/dist/next/middleware.cjs.map +1 -0
  25. package/dist/next/middleware.d.ts +28 -0
  26. package/dist/next/middleware.js +45 -0
  27. package/dist/next/middleware.js.map +1 -0
  28. package/dist/next/remote/pages.cjs.map +1 -1
  29. package/dist/next/remote/pages.d.ts +36 -4
  30. package/dist/next/remote/pages.js.map +1 -1
  31. package/dist/next/remote/render-server.cjs +4 -4
  32. package/dist/next/remote/render-server.cjs.map +1 -1
  33. package/dist/next/remote/render-server.d.ts +26 -4
  34. package/dist/next/remote/render-server.js +4 -4
  35. package/dist/next/remote/render-server.js.map +1 -1
  36. package/dist/next/remote/server.d.ts +0 -2
  37. package/dist/shared/client/remote-component.cjs +50 -18
  38. package/dist/shared/client/remote-component.cjs.map +1 -1
  39. package/dist/shared/client/remote-component.d.ts +7 -2
  40. package/dist/shared/client/remote-component.js +50 -18
  41. package/dist/shared/client/remote-component.js.map +1 -1
  42. package/dist/shared/webpack/next-client-pages-loader.cjs +6 -2
  43. package/dist/shared/webpack/next-client-pages-loader.cjs.map +1 -1
  44. package/dist/shared/webpack/next-client-pages-loader.js +6 -2
  45. package/dist/shared/webpack/next-client-pages-loader.js.map +1 -1
  46. package/package.json +9 -1
@@ -24,10 +24,11 @@ __export(app_client_exports, {
24
24
  module.exports = __toCommonJS(app_client_exports);
25
25
  var import_jsx_runtime = require("react/jsx-runtime");
26
26
  var import_react = require("react");
27
- var import_react2 = require("react");
27
+ var import_react_dom = require("react-dom");
28
28
  var import_host = require("@remote-component/shared/host");
29
29
  var import_remote_component = require("../../shared/client/remote-component");
30
30
  function RemoteComponentClient({
31
+ url,
31
32
  name,
32
33
  bundle,
33
34
  route = import_remote_component.DEFAULT_ROUTE,
@@ -37,13 +38,40 @@ function RemoteComponentClient({
37
38
  scripts = [],
38
39
  links = [],
39
40
  remoteShared = {},
41
+ isolate,
40
42
  children
41
43
  }) {
42
- const [component, setComponent] = (0, import_react2.useState)(null);
43
- (0, import_react2.useEffect)(() => {
44
+ const [component, setComponent] = (0, import_react.useState)(null);
45
+ if (component instanceof Error) {
46
+ throw component;
47
+ }
48
+ const shouldUseChildren = (!component || component && !nextData && typeof component.then !== "function") && // if the remote Next.js Pages Router application is in development mode
49
+ // we don't use the provided static HTML
50
+ // to mitigate layout shift when loading CSS using JavaScript on the client
51
+ nextData?.buildId !== "development";
52
+ const [shadowRoot, setShadowRoot] = (0, import_react.useState)(null);
53
+ (0, import_react.useLayoutEffect)(() => {
54
+ if (isolate !== false && typeof document !== "undefined" && !shadowRoot) {
55
+ let shadowRootElement = null;
56
+ const element = document.getElementById(`shadowroot_${name}`);
57
+ shadowRootElement = element?.shadowRoot ?? null;
58
+ if (!shadowRootElement && element) {
59
+ element.attachShadow({ mode: "open" });
60
+ shadowRootElement = element.shadowRoot;
61
+ }
62
+ if (shadowRootElement) {
63
+ shadowRootElement.querySelectorAll("*:not(link)").forEach((node) => {
64
+ node.remove();
65
+ });
66
+ setShadowRoot(shadowRootElement);
67
+ }
68
+ }
69
+ }, [name, isolate, shadowRoot]);
70
+ (0, import_react.useEffect)(() => {
44
71
  let mounted = true;
45
- if (!component) {
72
+ if (!component && (isolate === false || shadowRoot)) {
46
73
  (0, import_remote_component.loadRemoteComponent)({
74
+ url: new URL(url, location.origin),
47
75
  name,
48
76
  bundle,
49
77
  route,
@@ -52,7 +80,8 @@ function RemoteComponentClient({
52
80
  nextData,
53
81
  scripts,
54
82
  shared: import_host.shared,
55
- remoteShared
83
+ remoteShared,
84
+ container: shadowRoot
56
85
  }).then((result) => {
57
86
  if (mounted) {
58
87
  if (result.error) {
@@ -71,6 +100,7 @@ function RemoteComponentClient({
71
100
  mounted = false;
72
101
  };
73
102
  }, [
103
+ url,
74
104
  component,
75
105
  name,
76
106
  bundle,
@@ -81,15 +111,39 @@ function RemoteComponentClient({
81
111
  nextData,
82
112
  remoteShared,
83
113
  children,
84
- links
114
+ links,
115
+ isolate,
116
+ shadowRoot
85
117
  ]);
86
- if (component instanceof Error) {
87
- throw component;
118
+ let componentToRender = /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, { children: shouldUseChildren ? children : component });
119
+ let linksToRender = links.map((link) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
120
+ "link",
121
+ {
122
+ as: link.as,
123
+ href: link.href,
124
+ rel: link.rel
125
+ },
126
+ `${link.href}_${link.rel}`
127
+ ));
128
+ if (isolate !== false) {
129
+ componentToRender = /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { id: `shadowroot_${name}`, children: [
130
+ typeof document === "undefined" ? (
131
+ // eslint-disable-next-line react/no-unknown-property
132
+ /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("template", { shadowrootmode: "open", children: [
133
+ linksToRender,
134
+ componentToRender
135
+ ] })
136
+ ) : null,
137
+ shadowRoot ? (0, import_react_dom.createPortal)(
138
+ shadowRoot.querySelectorAll("link").length !== links.length ? /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [
139
+ linksToRender,
140
+ componentToRender
141
+ ] }) : componentToRender,
142
+ shadowRoot
143
+ ) : null
144
+ ] });
145
+ linksToRender = null;
88
146
  }
89
- const shouldUseChildren = (!component || component && !nextData && typeof component.then !== "function") && // if the remote Next.js Pages Router application is in development mode
90
- // we don't use the provided static HTML
91
- // to mitigate layout shift when loading CSS using JavaScript on the client
92
- nextData?.buildId !== "development";
93
147
  return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [
94
148
  /* @__PURE__ */ (0, import_jsx_runtime.jsx)("script", { "data-remote-component": true, type: "application/json", children: JSON.stringify({
95
149
  name,
@@ -97,7 +151,17 @@ function RemoteComponentClient({
97
151
  route,
98
152
  runtime
99
153
  }) }),
100
- shouldUseChildren ? children : component,
154
+ linksToRender,
155
+ componentToRender,
156
+ links.map((link) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
157
+ "link",
158
+ {
159
+ as: link.as,
160
+ href: link.href,
161
+ rel: link.rel
162
+ },
163
+ `${link.href}_${link.rel}`
164
+ )),
101
165
  data.length > 0 ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)("script", { id: `${name}_rsc`, children: data.join("\n") }) : null,
102
166
  nextData ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
103
167
  "script",
@@ -106,15 +170,7 @@ function RemoteComponentClient({
106
170
  type: "application/json",
107
171
  children: JSON.stringify(nextData)
108
172
  }
109
- ) : null,
110
- links.map((link) => /* @__PURE__ */ (0, import_react.createElement)(
111
- "link",
112
- {
113
- ...link,
114
- key: `${link.href}_${link.rel}`,
115
- precedence: bundle
116
- }
117
- ))
173
+ ) : null
118
174
  ] });
119
175
  }
120
176
  // Annotate the CommonJS export names for ESM import in node:
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/next/host/app-client.tsx"],"sourcesContent":["'use client';\n\nimport { useEffect, useState } from 'react';\nimport { shared } from '@remote-component/shared/host';\nimport {\n loadRemoteComponent,\n DEFAULT_ROUTE,\n RUNTIME_WEBPACK,\n} from '../../shared/client/remote-component';\nimport type { RemoteComponentProps } from '../../shared/client/remote-component';\n\n/**\n * RemoteComponentClient - Main component for rendering remote components\n *\n * This component handles the loading and rendering of remote microfrontends.\n * It supports both RSC (React Server Components) and Next.js Pages Router based components.\n */\nexport function RemoteComponentClient({\n name,\n bundle,\n route = DEFAULT_ROUTE,\n runtime = RUNTIME_WEBPACK,\n data,\n nextData,\n scripts = [],\n links = [],\n remoteShared = {},\n children,\n}: RemoteComponentProps) {\n const [component, setComponent] = useState<React.ReactNode | Error>(null);\n\n useEffect(() => {\n let mounted = true;\n\n // if we have a component, we don't need to load it again\n if (!component) {\n loadRemoteComponent({\n name,\n bundle,\n route,\n runtime,\n data,\n nextData,\n scripts,\n shared,\n remoteShared,\n })\n .then((result) => {\n if (mounted) {\n if (result.error) {\n setComponent(result.error);\n } else {\n setComponent(result.component);\n }\n }\n })\n .catch((error: Error) => {\n if (mounted) {\n setComponent(error);\n }\n });\n }\n\n return () => {\n mounted = false;\n };\n }, [\n component,\n name,\n bundle,\n route,\n runtime,\n scripts,\n data,\n nextData,\n remoteShared,\n children,\n links,\n ]);\n\n // Handle errors by re-throwing them\n if (component instanceof Error) {\n throw component;\n }\n\n // determine whether to use children or loaded component\n const shouldUseChildren =\n (!component ||\n (component &&\n !nextData &&\n typeof (component as unknown as Promise<unknown>).then !==\n 'function')) &&\n // if the remote Next.js Pages Router application is in development mode\n // we don't use the provided static HTML\n // to mitigate layout shift when loading CSS using JavaScript on the client\n nextData?.buildId !== 'development';\n\n return (\n <>\n <script data-remote-component type=\"application/json\">\n {JSON.stringify({\n name,\n bundle,\n route,\n runtime,\n })}\n </script>\n {shouldUseChildren ? children : (component as React.ReactNode)}\n {data.length > 0 ? (\n <script id={`${name}_rsc`}>{data.join('\\n')}</script>\n ) : null}\n {nextData ? (\n <script\n id={`${bundle}_${route.replace(/\\//g, '_')}${name}_next_data`}\n type=\"application/json\"\n >\n {JSON.stringify(nextData)}\n </script>\n ) : null}\n {links.map((link) => (\n <link\n {...link}\n key={`${link.href as string}_${link.rel}`}\n precedence={bundle}\n />\n ))}\n </>\n );\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAkGI;AAsBI;AAtHR,IAAAA,gBAAoC;AACpC,kBAAuB;AACvB,8BAIO;AASA,SAAS,sBAAsB;AAAA,EACpC;AAAA,EACA;AAAA,EACA,QAAQ;AAAA,EACR,UAAU;AAAA,EACV;AAAA,EACA;AAAA,EACA,UAAU,CAAC;AAAA,EACX,QAAQ,CAAC;AAAA,EACT,eAAe,CAAC;AAAA,EAChB;AACF,GAAyB;AACvB,QAAM,CAAC,WAAW,YAAY,QAAI,wBAAkC,IAAI;AAExE,+BAAU,MAAM;AACd,QAAI,UAAU;AAGd,QAAI,CAAC,WAAW;AACd,uDAAoB;AAAA,QAClB;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,MACF,CAAC,EACE,KAAK,CAAC,WAAW;AAChB,YAAI,SAAS;AACX,cAAI,OAAO,OAAO;AAChB,yBAAa,OAAO,KAAK;AAAA,UAC3B,OAAO;AACL,yBAAa,OAAO,SAAS;AAAA,UAC/B;AAAA,QACF;AAAA,MACF,CAAC,EACA,MAAM,CAAC,UAAiB;AACvB,YAAI,SAAS;AACX,uBAAa,KAAK;AAAA,QACpB;AAAA,MACF,CAAC;AAAA,IACL;AAEA,WAAO,MAAM;AACX,gBAAU;AAAA,IACZ;AAAA,EACF,GAAG;AAAA,IACD;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF,CAAC;AAGD,MAAI,qBAAqB,OAAO;AAC9B,UAAM;AAAA,EACR;AAGA,QAAM,qBACH,CAAC,aACC,aACC,CAAC,YACD,OAAQ,UAA0C,SAChD;AAAA;AAAA;AAAA,EAIN,UAAU,YAAY;AAExB,SACE,4EACE;AAAA,gDAAC,YAAO,yBAAqB,MAAC,MAAK,oBAChC,eAAK,UAAU;AAAA,MACd;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,IACF,CAAC,GACH;AAAA,IACC,oBAAoB,WAAY;AAAA,IAChC,KAAK,SAAS,IACb,4CAAC,YAAO,IAAI,GAAG,YAAa,eAAK,KAAK,IAAI,GAAE,IAC1C;AAAA,IACH,WACC;AAAA,MAAC;AAAA;AAAA,QACC,IAAI,GAAG,UAAU,MAAM,QAAQ,OAAO,GAAG,IAAI;AAAA,QAC7C,MAAK;AAAA,QAEJ,eAAK,UAAU,QAAQ;AAAA;AAAA,IAC1B,IACE;AAAA,IACH,MAAM,IAAI,CAAC,SACV;AAAA,MAAC;AAAA;AAAA,QACE,GAAG;AAAA,QACJ,KAAK,GAAG,KAAK,QAAkB,KAAK;AAAA,QACpC,YAAY;AAAA;AAAA,IACd,CACD;AAAA,KACH;AAEJ;","names":["import_react"]}
1
+ {"version":3,"sources":["../../../src/next/host/app-client.tsx"],"sourcesContent":["'use client';\n\nimport { useEffect, useState, useLayoutEffect } from 'react';\nimport { createPortal } from 'react-dom';\nimport { shared } from '@remote-component/shared/host';\nimport {\n loadRemoteComponent,\n DEFAULT_ROUTE,\n RUNTIME_WEBPACK,\n} from '../../shared/client/remote-component';\nimport type { RemoteComponentProps } from '../../shared/client/remote-component';\n\n// patch react/jsx-runtime to support the shadowrootmode attribute on template elements\ndeclare module 'react/jsx-runtime' {\n // eslint-disable-next-line @typescript-eslint/no-namespace\n export namespace JSX {\n interface IntrinsicElements {\n template: {\n shadowrootmode?: 'open' | 'closed';\n children: React.ReactNode;\n };\n }\n }\n}\n\n/**\n * RemoteComponentClient - Main component for rendering remote components\n *\n * This component handles the loading and rendering of remote microfrontends.\n * It supports both RSC (React Server Components) and Next.js Pages Router based components.\n */\nexport function RemoteComponentClient({\n url,\n name,\n bundle,\n route = DEFAULT_ROUTE,\n runtime = RUNTIME_WEBPACK,\n data,\n nextData,\n scripts = [],\n links = [],\n remoteShared = {},\n isolate,\n children,\n}: RemoteComponentProps) {\n const [component, setComponent] = useState<React.ReactNode | Error>(null);\n\n // Handle errors by re-throwing them\n if (component instanceof Error) {\n throw component;\n }\n\n // determine whether to use children or loaded component\n const shouldUseChildren =\n (!component ||\n (component &&\n !nextData &&\n typeof (component as unknown as Promise<unknown>).then !==\n 'function')) &&\n // if the remote Next.js Pages Router application is in development mode\n // we don't use the provided static HTML\n // to mitigate layout shift when loading CSS using JavaScript on the client\n nextData?.buildId !== 'development';\n\n const [shadowRoot, setShadowRoot] = useState<ShadowRoot | null>(null);\n\n useLayoutEffect(() => {\n if (isolate !== false && typeof document !== 'undefined' && !shadowRoot) {\n let shadowRootElement: ShadowRoot | null = null;\n const element = document.getElementById(`shadowroot_${name}`);\n shadowRootElement = element?.shadowRoot ?? null;\n\n if (!shadowRootElement && element) {\n // create a shadow root if it doesn't exist\n // this is a fallback for browsers that don't support declarative shadow DOM\n element.attachShadow({ mode: 'open' });\n shadowRootElement = element.shadowRoot;\n }\n\n if (shadowRootElement) {\n // remove all nodes from the shadow root except links\n shadowRootElement.querySelectorAll('*:not(link)').forEach((node) => {\n node.remove();\n });\n setShadowRoot(shadowRootElement);\n }\n }\n }, [name, isolate, shadowRoot]);\n\n useEffect(() => {\n let mounted = true;\n\n // if we have a component, we don't need to load it again\n if (!component && (isolate === false || shadowRoot)) {\n loadRemoteComponent({\n url: new URL(url, location.origin),\n name,\n bundle,\n route,\n runtime,\n data,\n nextData,\n scripts,\n shared,\n remoteShared,\n container: shadowRoot,\n })\n .then((result) => {\n if (mounted) {\n if (result.error) {\n setComponent(result.error);\n } else {\n setComponent(result.component);\n }\n }\n })\n .catch((error: Error) => {\n if (mounted) {\n setComponent(error);\n }\n });\n }\n\n return () => {\n mounted = false;\n };\n }, [\n url,\n component,\n name,\n bundle,\n route,\n runtime,\n scripts,\n data,\n nextData,\n remoteShared,\n children,\n links,\n isolate,\n shadowRoot,\n ]);\n\n let componentToRender = (\n <>{shouldUseChildren ? children : (component as React.ReactNode)}</>\n );\n let linksToRender: React.ReactNode[] | null = links.map((link) => (\n <link\n as={link.as as string}\n href={link.href as string}\n key={`${link.href as string}_${link.rel}`}\n rel={link.rel as string}\n />\n ));\n\n if (isolate !== false) {\n componentToRender = (\n <div id={`shadowroot_${name}`}>\n {typeof document === 'undefined' ? (\n // eslint-disable-next-line react/no-unknown-property\n <template shadowrootmode=\"open\">\n {linksToRender}\n {componentToRender}\n </template>\n ) : null}\n {shadowRoot\n ? createPortal(\n shadowRoot.querySelectorAll('link').length !== links.length ? (\n <>\n {linksToRender}\n {componentToRender}\n </>\n ) : (\n componentToRender\n ),\n shadowRoot,\n )\n : null}\n </div>\n );\n linksToRender = null;\n }\n\n return (\n <>\n <script data-remote-component type=\"application/json\">\n {JSON.stringify({\n name,\n bundle,\n route,\n runtime,\n })}\n </script>\n {linksToRender}\n {componentToRender}\n {links.map((link) => (\n <link\n as={link.as as string}\n href={link.href as string}\n key={`${link.href as string}_${link.rel}`}\n rel={link.rel as string}\n />\n ))}\n {data.length > 0 ? (\n <script id={`${name}_rsc`}>{data.join('\\n')}</script>\n ) : null}\n {nextData ? (\n <script\n id={`${bundle}_${route.replace(/\\//g, '_')}${name}_next_data`}\n type=\"application/json\"\n >\n {JSON.stringify(nextData)}\n </script>\n ) : null}\n </>\n );\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAgJI;AA9IJ,mBAAqD;AACrD,uBAA6B;AAC7B,kBAAuB;AACvB,8BAIO;AAsBA,SAAS,sBAAsB;AAAA,EACpC;AAAA,EACA;AAA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,MAAC;AAAA;AAAA,QACC,IAAI,KAAK;AAAA,QACT,MAAM,KAAK;AAAA,QAEX,KAAK,KAAK;AAAA;AAAA,MADL,GAAG,KAAK,QAAkB,KAAK;AAAA,IAEtC,CACD;AAAA,IACA,KAAK,SAAS,IACb,4CAAC,YAAO,IAAI,GAAG,YAAa,eAAK,KAAK,IAAI,GAAE,IAC1C;AAAA,IACH,WACC;AAAA,MAAC;AAAA;AAAA,QACC,IAAI,GAAG,UAAU,MAAM,QAAQ,OAAO,GAAG,IAAI;AAAA,QAC7C,MAAK;AAAA,QAEJ,eAAK,UAAU,QAAQ;AAAA;AAAA,IAC1B,IACE;AAAA,KACN;AAEJ;","names":[]}
@@ -3,6 +3,7 @@ import * as react from 'react';
3
3
  import { R as RemoteComponentMetadata } from '../../types-280a3640.js';
4
4
 
5
5
  interface RemoteComponentProps {
6
+ url: string;
6
7
  name: string;
7
8
  bundle: string;
8
9
  route?: string;
@@ -19,15 +20,26 @@ interface RemoteComponentProps {
19
20
  }[];
20
21
  links?: Record<string, string | boolean>[];
21
22
  remoteShared?: Record<string, string>;
23
+ isolate?: boolean;
22
24
  children: react.ReactNode;
23
25
  }
24
26
 
27
+ declare module 'react/jsx-runtime' {
28
+ namespace JSX {
29
+ interface IntrinsicElements {
30
+ template: {
31
+ shadowrootmode?: 'open' | 'closed';
32
+ children: React.ReactNode;
33
+ };
34
+ }
35
+ }
36
+ }
25
37
  /**
26
38
  * RemoteComponentClient - Main component for rendering remote components
27
39
  *
28
40
  * This component handles the loading and rendering of remote microfrontends.
29
41
  * It supports both RSC (React Server Components) and Next.js Pages Router based components.
30
42
  */
31
- declare function RemoteComponentClient({ name, bundle, route, runtime, data, nextData, scripts, links, remoteShared, children, }: RemoteComponentProps): react_jsx_runtime.JSX.Element;
43
+ declare function RemoteComponentClient({ url, name, bundle, route, runtime, data, nextData, scripts, links, remoteShared, isolate, children, }: RemoteComponentProps): react_jsx_runtime.JSX.Element;
32
44
 
33
45
  export { RemoteComponentClient };
@@ -1,7 +1,7 @@
1
1
  "use client";
2
2
  import { Fragment, jsx, jsxs } from "react/jsx-runtime";
3
- import { createElement } from "react";
4
- import { useEffect, useState } from "react";
3
+ import { useEffect, useState, useLayoutEffect } from "react";
4
+ import { createPortal } from "react-dom";
5
5
  import { shared } from "@remote-component/shared/host";
6
6
  import {
7
7
  loadRemoteComponent,
@@ -9,6 +9,7 @@ import {
9
9
  RUNTIME_WEBPACK
10
10
  } from "../../shared/client/remote-component";
11
11
  function RemoteComponentClient({
12
+ url,
12
13
  name,
13
14
  bundle,
14
15
  route = DEFAULT_ROUTE,
@@ -18,13 +19,40 @@ function RemoteComponentClient({
18
19
  scripts = [],
19
20
  links = [],
20
21
  remoteShared = {},
22
+ isolate,
21
23
  children
22
24
  }) {
23
25
  const [component, setComponent] = useState(null);
26
+ if (component instanceof Error) {
27
+ throw component;
28
+ }
29
+ const shouldUseChildren = (!component || component && !nextData && typeof component.then !== "function") && // if the remote Next.js Pages Router application is in development mode
30
+ // we don't use the provided static HTML
31
+ // to mitigate layout shift when loading CSS using JavaScript on the client
32
+ nextData?.buildId !== "development";
33
+ const [shadowRoot, setShadowRoot] = useState(null);
34
+ useLayoutEffect(() => {
35
+ if (isolate !== false && typeof document !== "undefined" && !shadowRoot) {
36
+ let shadowRootElement = null;
37
+ const element = document.getElementById(`shadowroot_${name}`);
38
+ shadowRootElement = element?.shadowRoot ?? null;
39
+ if (!shadowRootElement && element) {
40
+ element.attachShadow({ mode: "open" });
41
+ shadowRootElement = element.shadowRoot;
42
+ }
43
+ if (shadowRootElement) {
44
+ shadowRootElement.querySelectorAll("*:not(link)").forEach((node) => {
45
+ node.remove();
46
+ });
47
+ setShadowRoot(shadowRootElement);
48
+ }
49
+ }
50
+ }, [name, isolate, shadowRoot]);
24
51
  useEffect(() => {
25
52
  let mounted = true;
26
- if (!component) {
53
+ if (!component && (isolate === false || shadowRoot)) {
27
54
  loadRemoteComponent({
55
+ url: new URL(url, location.origin),
28
56
  name,
29
57
  bundle,
30
58
  route,
@@ -33,7 +61,8 @@ function RemoteComponentClient({
33
61
  nextData,
34
62
  scripts,
35
63
  shared,
36
- remoteShared
64
+ remoteShared,
65
+ container: shadowRoot
37
66
  }).then((result) => {
38
67
  if (mounted) {
39
68
  if (result.error) {
@@ -52,6 +81,7 @@ function RemoteComponentClient({
52
81
  mounted = false;
53
82
  };
54
83
  }, [
84
+ url,
55
85
  component,
56
86
  name,
57
87
  bundle,
@@ -62,15 +92,39 @@ function RemoteComponentClient({
62
92
  nextData,
63
93
  remoteShared,
64
94
  children,
65
- links
95
+ links,
96
+ isolate,
97
+ shadowRoot
66
98
  ]);
67
- if (component instanceof Error) {
68
- throw component;
99
+ let componentToRender = /* @__PURE__ */ jsx(Fragment, { children: shouldUseChildren ? children : component });
100
+ let linksToRender = links.map((link) => /* @__PURE__ */ jsx(
101
+ "link",
102
+ {
103
+ as: link.as,
104
+ href: link.href,
105
+ rel: link.rel
106
+ },
107
+ `${link.href}_${link.rel}`
108
+ ));
109
+ if (isolate !== false) {
110
+ componentToRender = /* @__PURE__ */ jsxs("div", { id: `shadowroot_${name}`, children: [
111
+ typeof document === "undefined" ? (
112
+ // eslint-disable-next-line react/no-unknown-property
113
+ /* @__PURE__ */ jsxs("template", { shadowrootmode: "open", children: [
114
+ linksToRender,
115
+ componentToRender
116
+ ] })
117
+ ) : null,
118
+ shadowRoot ? createPortal(
119
+ shadowRoot.querySelectorAll("link").length !== links.length ? /* @__PURE__ */ jsxs(Fragment, { children: [
120
+ linksToRender,
121
+ componentToRender
122
+ ] }) : componentToRender,
123
+ shadowRoot
124
+ ) : null
125
+ ] });
126
+ linksToRender = null;
69
127
  }
70
- const shouldUseChildren = (!component || component && !nextData && typeof component.then !== "function") && // if the remote Next.js Pages Router application is in development mode
71
- // we don't use the provided static HTML
72
- // to mitigate layout shift when loading CSS using JavaScript on the client
73
- nextData?.buildId !== "development";
74
128
  return /* @__PURE__ */ jsxs(Fragment, { children: [
75
129
  /* @__PURE__ */ jsx("script", { "data-remote-component": true, type: "application/json", children: JSON.stringify({
76
130
  name,
@@ -78,7 +132,17 @@ function RemoteComponentClient({
78
132
  route,
79
133
  runtime
80
134
  }) }),
81
- shouldUseChildren ? children : component,
135
+ linksToRender,
136
+ componentToRender,
137
+ links.map((link) => /* @__PURE__ */ jsx(
138
+ "link",
139
+ {
140
+ as: link.as,
141
+ href: link.href,
142
+ rel: link.rel
143
+ },
144
+ `${link.href}_${link.rel}`
145
+ )),
82
146
  data.length > 0 ? /* @__PURE__ */ jsx("script", { id: `${name}_rsc`, children: data.join("\n") }) : null,
83
147
  nextData ? /* @__PURE__ */ jsx(
84
148
  "script",
@@ -87,15 +151,7 @@ function RemoteComponentClient({
87
151
  type: "application/json",
88
152
  children: JSON.stringify(nextData)
89
153
  }
90
- ) : null,
91
- links.map((link) => /* @__PURE__ */ createElement(
92
- "link",
93
- {
94
- ...link,
95
- key: `${link.href}_${link.rel}`,
96
- precedence: bundle
97
- }
98
- ))
154
+ ) : null
99
155
  ] });
100
156
  }
101
157
  export {
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/next/host/app-client.tsx"],"sourcesContent":["'use client';\n\nimport { useEffect, useState } from 'react';\nimport { shared } from '@remote-component/shared/host';\nimport {\n loadRemoteComponent,\n DEFAULT_ROUTE,\n RUNTIME_WEBPACK,\n} from '../../shared/client/remote-component';\nimport type { RemoteComponentProps } from '../../shared/client/remote-component';\n\n/**\n * RemoteComponentClient - Main component for rendering remote components\n *\n * This component handles the loading and rendering of remote microfrontends.\n * It supports both RSC (React Server Components) and Next.js Pages Router based components.\n */\nexport function RemoteComponentClient({\n name,\n bundle,\n route = DEFAULT_ROUTE,\n runtime = RUNTIME_WEBPACK,\n data,\n nextData,\n scripts = [],\n links = [],\n remoteShared = {},\n children,\n}: RemoteComponentProps) {\n const [component, setComponent] = useState<React.ReactNode | Error>(null);\n\n useEffect(() => {\n let mounted = true;\n\n // if we have a component, we don't need to load it again\n if (!component) {\n loadRemoteComponent({\n name,\n bundle,\n route,\n runtime,\n data,\n nextData,\n scripts,\n shared,\n remoteShared,\n })\n .then((result) => {\n if (mounted) {\n if (result.error) {\n setComponent(result.error);\n } else {\n setComponent(result.component);\n }\n }\n })\n .catch((error: Error) => {\n if (mounted) {\n setComponent(error);\n }\n });\n }\n\n return () => {\n mounted = false;\n };\n }, [\n component,\n name,\n bundle,\n route,\n runtime,\n scripts,\n data,\n nextData,\n remoteShared,\n children,\n links,\n ]);\n\n // Handle errors by re-throwing them\n if (component instanceof Error) {\n throw component;\n }\n\n // determine whether to use children or loaded component\n const shouldUseChildren =\n (!component ||\n (component &&\n !nextData &&\n typeof (component as unknown as Promise<unknown>).then !==\n 'function')) &&\n // if the remote Next.js Pages Router application is in development mode\n // we don't use the provided static HTML\n // to mitigate layout shift when loading CSS using JavaScript on the client\n nextData?.buildId !== 'development';\n\n return (\n <>\n <script data-remote-component type=\"application/json\">\n {JSON.stringify({\n name,\n bundle,\n route,\n runtime,\n })}\n </script>\n {shouldUseChildren ? children : (component as React.ReactNode)}\n {data.length > 0 ? (\n <script id={`${name}_rsc`}>{data.join('\\n')}</script>\n ) : null}\n {nextData ? (\n <script\n id={`${bundle}_${route.replace(/\\//g, '_')}${name}_next_data`}\n type=\"application/json\"\n >\n {JSON.stringify(nextData)}\n </script>\n ) : null}\n {links.map((link) => (\n <link\n {...link}\n key={`${link.href as string}_${link.rel}`}\n precedence={bundle}\n />\n ))}\n </>\n );\n}\n"],"mappings":";AAkGI,mBACE,KADF;AAsBI;AAtHR,SAAS,WAAW,gBAAgB;AACpC,SAAS,cAAc;AACvB;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OACK;AASA,SAAS,sBAAsB;AAAA,EACpC;AAAA,EACA;AAAA,EACA,QAAQ;AAAA,EACR,UAAU;AAAA,EACV;AAAA,EACA;AAAA,EACA,UAAU,CAAC;AAAA,EACX,QAAQ,CAAC;AAAA,EACT,eAAe,CAAC;AAAA,EAChB;AACF,GAAyB;AACvB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J,eAAK,UAAU,QAAQ;AAAA;AAAA,IAC1B,IACE;AAAA,IACH,MAAM,IAAI,CAAC,SACV;AAAA,MAAC;AAAA;AAAA,QACE,GAAG;AAAA,QACJ,KAAK,GAAG,KAAK,QAAkB,KAAK;AAAA,QACpC,YAAY;AAAA;AAAA,IACd,CACD;AAAA,KACH;AAEJ;","names":[]}
1
+ {"version":3,"sources":["../../../src/next/host/app-client.tsx"],"sourcesContent":["'use client';\n\nimport { useEffect, useState, useLayoutEffect } from 'react';\nimport { createPortal } from 'react-dom';\nimport { shared } from '@remote-component/shared/host';\nimport {\n loadRemoteComponent,\n DEFAULT_ROUTE,\n RUNTIME_WEBPACK,\n} from '../../shared/client/remote-component';\nimport type { RemoteComponentProps } from '../../shared/client/remote-component';\n\n// patch react/jsx-runtime to support the shadowrootmode attribute on template elements\ndeclare module 'react/jsx-runtime' {\n // eslint-disable-next-line @typescript-eslint/no-namespace\n export namespace JSX {\n interface IntrinsicElements {\n template: {\n shadowrootmode?: 'open' | 'closed';\n children: React.ReactNode;\n };\n }\n }\n}\n\n/**\n * RemoteComponentClient - Main component for rendering remote components\n *\n * This component handles the loading and rendering of remote microfrontends.\n * It supports both RSC (React Server Components) and Next.js Pages Router based components.\n */\nexport function RemoteComponentClient({\n url,\n name,\n bundle,\n route = DEFAULT_ROUTE,\n runtime = RUNTIME_WEBPACK,\n data,\n nextData,\n scripts = [],\n links = [],\n remoteShared = {},\n isolate,\n children,\n}: RemoteComponentProps) {\n const [component, setComponent] = useState<React.ReactNode | Error>(null);\n\n // Handle errors by re-throwing them\n if (component instanceof Error) {\n throw component;\n }\n\n // determine whether to use children or loaded component\n const shouldUseChildren =\n (!component ||\n (component &&\n !nextData &&\n typeof (component as unknown as Promise<unknown>).then !==\n 'function')) &&\n // if the remote Next.js Pages Router application is in development mode\n // we don't use the provided static HTML\n // to mitigate layout shift when loading CSS using JavaScript on the client\n nextData?.buildId !== 'development';\n\n const [shadowRoot, setShadowRoot] = useState<ShadowRoot | null>(null);\n\n useLayoutEffect(() => {\n if (isolate !== false && typeof document !== 'undefined' && !shadowRoot) {\n let shadowRootElement: ShadowRoot | null = null;\n const element = document.getElementById(`shadowroot_${name}`);\n shadowRootElement = element?.shadowRoot ?? null;\n\n if (!shadowRootElement && element) {\n // create a shadow root if it doesn't exist\n // this is a fallback for browsers that don't support declarative shadow DOM\n element.attachShadow({ mode: 'open' });\n shadowRootElement = element.shadowRoot;\n }\n\n if (shadowRootElement) {\n // remove all nodes from the shadow root except links\n shadowRootElement.querySelectorAll('*:not(link)').forEach((node) => {\n node.remove();\n });\n setShadowRoot(shadowRootElement);\n }\n }\n }, [name, isolate, shadowRoot]);\n\n useEffect(() => {\n let mounted = true;\n\n // if we have a component, we don't need to load it again\n if (!component && (isolate === false || shadowRoot)) {\n loadRemoteComponent({\n url: new URL(url, location.origin),\n name,\n bundle,\n route,\n runtime,\n data,\n nextData,\n scripts,\n shared,\n remoteShared,\n container: shadowRoot,\n })\n .then((result) => {\n if (mounted) {\n if (result.error) {\n setComponent(result.error);\n } else {\n setComponent(result.component);\n }\n }\n })\n .catch((error: Error) => {\n if (mounted) {\n setComponent(error);\n }\n });\n }\n\n return () => {\n mounted = false;\n };\n }, [\n url,\n component,\n name,\n bundle,\n route,\n runtime,\n scripts,\n data,\n nextData,\n remoteShared,\n children,\n links,\n isolate,\n shadowRoot,\n ]);\n\n let componentToRender = (\n <>{shouldUseChildren ? children : (component as React.ReactNode)}</>\n );\n let linksToRender: React.ReactNode[] | null = links.map((link) => (\n <link\n as={link.as as string}\n href={link.href as string}\n key={`${link.href as string}_${link.rel}`}\n rel={link.rel as string}\n />\n ));\n\n if (isolate !== false) {\n componentToRender = (\n <div id={`shadowroot_${name}`}>\n {typeof document === 'undefined' ? (\n // eslint-disable-next-line react/no-unknown-property\n <template shadowrootmode=\"open\">\n {linksToRender}\n {componentToRender}\n </template>\n ) : null}\n {shadowRoot\n ? createPortal(\n shadowRoot.querySelectorAll('link').length !== links.length ? (\n <>\n {linksToRender}\n {componentToRender}\n </>\n ) : (\n componentToRender\n ),\n shadowRoot,\n )\n : null}\n </div>\n );\n linksToRender = null;\n }\n\n return (\n <>\n <script data-remote-component type=\"application/json\">\n {JSON.stringify({\n name,\n bundle,\n route,\n runtime,\n })}\n </script>\n {linksToRender}\n {componentToRender}\n {links.map((link) => (\n <link\n as={link.as as string}\n href={link.href as string}\n key={`${link.href as string}_${link.rel}`}\n rel={link.rel as string}\n />\n ))}\n {data.length > 0 ? (\n <script id={`${name}_rsc`}>{data.join('\\n')}</script>\n ) : null}\n {nextData ? (\n <script\n id={`${bundle}_${route.replace(/\\//g, '_')}${name}_next_data`}\n type=\"application/json\"\n >\n {JSON.stringify(nextData)}\n </script>\n ) : null}\n </>\n );\n}\n"],"mappings":";AAgJI,wBAgBM,YAhBN;AA9IJ,SAAS,WAAW,UAAU,uBAAuB;AACrD,SAAS,oBAAoB;AAC7B,SAAS,cAAc;AACvB;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAsBA,SAAS,sBAAsB;AAAA,EACpC;AAAA,EACA;AAAA,EACA;AAAA,EACA,QAAQ;AAAA,EACR,UAAU;AAAA,EACV;AAAA,EACA;AAAA,EACA,UAAU,CAAC;AAAA,EACX,QAAQ,CAAC;AAAA,EACT,eAAe,CAAC;AAAA,EAChB;AAAA,EACA;AACF,GAAyB;AACvB,QAAM,CAAC,WAAW,YAAY,IAAI,SAAkC,IAAI;AAGxE,MAAI,qBAAqB,OAAO;AAC9B,UAAM;AAAA,EACR;AAGA,QAAM,qBACH,CAAC,aACC,aACC,CAAC,YACD,OAAQ,UAA0C,SAChD;AAAA;AAAA;AAAA,EAIN,UAAU,YAAY;AAExB,QAAM,CAAC,YAAY,aAAa,IAAI,SAA4B,IAAI;AAEpE,kBAAgB,MAAM;AACpB,QAAI,YAAY,SAAS,OAAO,aAAa,eAAe,CAAC,YAAY;AACvE,UAAI,oBAAuC;AAC3C,YAAM,UAAU,SAAS,eAAe,cAAc,MA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A,MACC,IAAI,KAAK;AAAA,MACT,MAAM,KAAK;AAAA,MAEX,KAAK,KAAK;AAAA;AAAA,IADL,GAAG,KAAK,QAAkB,KAAK;AAAA,EAEtC,CACD;AAED,MAAI,YAAY,OAAO;AACrB,wBACE,qBAAC,SAAI,IAAI,cAAc,QACpB;AAAA,aAAO,aAAa;AAAA;AAAA,QAEnB,qBAAC,cAAS,gBAAe,QACtB;AAAA;AAAA,UACA;AAAA,WACH;AAAA,UACE;AAAA,MACH,aACG;AAAA,QACE,WAAW,iBAAiB,MAAM,EAAE,WAAW,MAAM,SACnD,iCACG;AAAA;AAAA,UACA;AAAA,WACH,IAEA;AAAA,QAEF;AAAA,MACF,IACA;AAAA,OACN;AAEF,oBAAgB;AAAA,EAClB;AAEA,SACE,iCACE;AAAA,wBAAC,YAAO,yBAAqB,MAAC,MAAK,oBAChC,eAAK,UAAU;AAAA,MACd;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,IACF,CAAC,GACH;AAAA,IACC;AAAA,IACA;AAAA,IACA,MAAM,IAAI,CAAC,SACV;AAAA,MAAC;AAAA;AAAA,QACC,IAAI,KAAK;AAAA,QACT,MAAM,KAAK;AAAA,QAEX,KAAK,KAAK;AAAA;AAAA,MADL,GAAG,KAAK,QAAkB,KAAK;AAAA,IAEtC,CACD;AAAA,IACA,KAAK,SAAS,IACb,oBAAC,YAAO,IAAI,GAAG,YAAa,eAAK,KAAK,IAAI,GAAE,IAC1C;AAAA,IACH,WACC;AAAA,MAAC;AAAA;AAAA,QACC,IAAI,GAAG,UAAU,MAAM,QAAQ,OAAO,GAAG,IAAI;AAAA,QAC7C,MAAK;AAAA,QAEJ,eAAK,UAAU,QAAQ;AAAA;AAAA,IAC1B,IACE;AAAA,KACN;AAEJ;","names":[]}
@@ -26,7 +26,10 @@ var import_headers = require("next/headers");
26
26
  var import_fetch_remote_component = require("../../shared/ssr/fetch-remote-component");
27
27
  var import_app_client = require("./app-client");
28
28
  const CURRENT_ZONE = process.env.NEXT_PUBLIC_MFE_CURRENT_APPLICATION;
29
- async function RemoteComponent({ src }) {
29
+ async function RemoteComponent({
30
+ src,
31
+ isolate
32
+ }) {
30
33
  const headerList = await (0, import_headers.headers)();
31
34
  const {
32
35
  metadata,
@@ -43,6 +46,7 @@ async function RemoteComponent({ src }) {
43
46
  {
44
47
  bundle: metadata.bundle || (CURRENT_ZONE ?? name),
45
48
  data: hydrationData,
49
+ isolate,
46
50
  links,
47
51
  name,
48
52
  nextData,
@@ -50,6 +54,7 @@ async function RemoteComponent({ src }) {
50
54
  route: metadata.route,
51
55
  runtime: metadata.runtime,
52
56
  scripts,
57
+ url: typeof src === "string" ? src : src.href,
53
58
  children: component
54
59
  }
55
60
  );
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/next/host/app-server.tsx"],"sourcesContent":["import { headers } from 'next/headers';\nimport { fetchRemoteComponent } from '../../shared/ssr/fetch-remote-component';\nimport { RemoteComponentClient } from './app-client';\n\nconst CURRENT_ZONE = process.env.NEXT_PUBLIC_MFE_CURRENT_APPLICATION;\n\nexport async function RemoteComponent({ src }: { src: string | URL }) {\n // get the headers from the request\n const headerList = await headers();\n\n const {\n metadata,\n scripts,\n links,\n hydrationData,\n nextData,\n component,\n remoteShared,\n } = await fetchRemoteComponent(src, headerList, true);\n\n // pass all remote component data to the SSR/client layer\n // render remote component static HTML as children\n // remote _ssr suffix from remote component id\n const name = metadata.id.replace(/_ssr$/, '');\n return (\n <RemoteComponentClient\n bundle={metadata.bundle || (CURRENT_ZONE ?? name)}\n data={hydrationData}\n links={links}\n name={name}\n nextData={nextData}\n remoteShared={remoteShared}\n route={metadata.route}\n runtime={metadata.runtime}\n scripts={scripts}\n >\n {component}\n </RemoteComponentClient>\n );\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAyBI;AAzBJ,qBAAwB;AACxB,oCAAqC;AACrC,wBAAsC;AAEtC,MAAM,eAAe,QAAQ,IAAI;AAEjC,eAAsB,gBAAgB,EAAE,IAAI,GAA0B;AAEpE,QAAM,aAAa,UAAM,wBAAQ;AAEjC,QAAM;AAAA,IACJ;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF,IAAI,UAAM,oDAAqB,KAAK,YAAY,IAAI;AAKpD,QAAM,OAAO,SAAS,GAAG,QAAQ,SAAS,EAAE;AAC5C,SACE;AAAA,IAAC;AAAA;AAAA,MACC,QAAQ,SAAS,WAAW,gBAAgB;AAAA,MAC5C,MAAM;AAAA,MACN;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA,OAAO,SAAS;AAAA,MAChB,SAAS,SAAS;AAAA,MAClB;AAAA,MAEC;AAAA;AAAA,EACH;AAEJ;","names":[]}
1
+ {"version":3,"sources":["../../../src/next/host/app-server.tsx"],"sourcesContent":["import { headers } from 'next/headers';\nimport { fetchRemoteComponent } from '../../shared/ssr/fetch-remote-component';\nimport { RemoteComponentClient } from './app-client';\n\nconst CURRENT_ZONE = process.env.NEXT_PUBLIC_MFE_CURRENT_APPLICATION;\n\n/**\n * RemoteComponent is a Next.js component that fetches and renders a remote component.\n * It supports SSR and can isolate the remote component in a shadow DOM.\n *\n * @param src - The source URL of the remote component. When using Vercel Microfrontends, you can use relative paths, e.g. `/nextjs-app-remote/components/header`. Absolute URLs are also supported.\n * @param isolate - Whether to isolate the remote component using a Shadow DOM wrapper. Defaults to `true`. Use `false` explicitly to disable isolation.\n * @returns A React component that renders the remote component.\n *\n * @example\n *\n * Use the `<RemoteComponent>` in your Next.js App Router application to consume a remote component from a remote application:\n *\n * ```tsx\n * import { RemoteComponent } from 'remote-components/next/host';\n *\n * export default function MyPage() {\n * return (\n * <>\n * <h1>Welcome to My Page</h1>\n * <p>This page consumes a remote component from another application.</p>\n * <RemoteComponent src=\"/nextjs-app-remote/components/header\" />\n * </>\n * );\n * }\n * ```\n */\nexport async function RemoteComponent({\n src,\n isolate,\n}: {\n src: string | URL;\n isolate?: boolean;\n}) {\n // get the headers from the request\n const headerList = await headers();\n\n const {\n metadata,\n scripts,\n links,\n hydrationData,\n nextData,\n component,\n remoteShared,\n } = await fetchRemoteComponent(src, headerList, true);\n\n // pass all remote component data to the SSR/client layer\n // render remote component static HTML as children\n // remote _ssr suffix from remote component id\n const name = metadata.id.replace(/_ssr$/, '');\n return (\n <RemoteComponentClient\n bundle={metadata.bundle || (CURRENT_ZONE ?? name)}\n data={hydrationData}\n isolate={isolate}\n links={links}\n name={name}\n nextData={nextData}\n remoteShared={remoteShared}\n route={metadata.route}\n runtime={metadata.runtime}\n scripts={scripts}\n url={typeof src === 'string' ? src : src.href}\n >\n {component}\n </RemoteComponentClient>\n );\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAyDI;AAzDJ,qBAAwB;AACxB,oCAAqC;AACrC,wBAAsC;AAEtC,MAAM,eAAe,QAAQ,IAAI;AA4BjC,eAAsB,gBAAgB;AAAA,EACpC;AAAA,EACA;AACF,GAGG;AAED,QAAM,aAAa,UAAM,wBAAQ;AAEjC,QAAM;AAAA,IACJ;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF,IAAI,UAAM,oDAAqB,KAAK,YAAY,IAAI;AAKpD,QAAM,OAAO,SAAS,GAAG,QAAQ,SAAS,EAAE;AAC5C,SACE;AAAA,IAAC;AAAA;AAAA,MACC,QAAQ,SAAS,WAAW,gBAAgB;AAAA,MAC5C,MAAM;AAAA,MACN;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA,OAAO,SAAS;AAAA,MAChB,SAAS,SAAS;AAAA,MAClB;AAAA,MACA,KAAK,OAAO,QAAQ,WAAW,MAAM,IAAI;AAAA,MAExC;AAAA;AAAA,EACH;AAEJ;","names":[]}
@@ -1,7 +1,34 @@
1
1
  import * as react_jsx_runtime from 'react/jsx-runtime';
2
2
 
3
- declare function RemoteComponent({ src }: {
3
+ /**
4
+ * RemoteComponent is a Next.js component that fetches and renders a remote component.
5
+ * It supports SSR and can isolate the remote component in a shadow DOM.
6
+ *
7
+ * @param src - The source URL of the remote component. When using Vercel Microfrontends, you can use relative paths, e.g. `/nextjs-app-remote/components/header`. Absolute URLs are also supported.
8
+ * @param isolate - Whether to isolate the remote component using a Shadow DOM wrapper. Defaults to `true`. Use `false` explicitly to disable isolation.
9
+ * @returns A React component that renders the remote component.
10
+ *
11
+ * @example
12
+ *
13
+ * Use the `<RemoteComponent>` in your Next.js App Router application to consume a remote component from a remote application:
14
+ *
15
+ * ```tsx
16
+ * import { RemoteComponent } from 'remote-components/next/host';
17
+ *
18
+ * export default function MyPage() {
19
+ * return (
20
+ * <>
21
+ * <h1>Welcome to My Page</h1>
22
+ * <p>This page consumes a remote component from another application.</p>
23
+ * <RemoteComponent src="/nextjs-app-remote/components/header" />
24
+ * </>
25
+ * );
26
+ * }
27
+ * ```
28
+ */
29
+ declare function RemoteComponent({ src, isolate, }: {
4
30
  src: string | URL;
31
+ isolate?: boolean;
5
32
  }): Promise<react_jsx_runtime.JSX.Element>;
6
33
 
7
34
  export { RemoteComponent };
@@ -3,7 +3,10 @@ import { headers } from "next/headers";
3
3
  import { fetchRemoteComponent } from "../../shared/ssr/fetch-remote-component";
4
4
  import { RemoteComponentClient } from "./app-client";
5
5
  const CURRENT_ZONE = process.env.NEXT_PUBLIC_MFE_CURRENT_APPLICATION;
6
- async function RemoteComponent({ src }) {
6
+ async function RemoteComponent({
7
+ src,
8
+ isolate
9
+ }) {
7
10
  const headerList = await headers();
8
11
  const {
9
12
  metadata,
@@ -20,6 +23,7 @@ async function RemoteComponent({ src }) {
20
23
  {
21
24
  bundle: metadata.bundle || (CURRENT_ZONE ?? name),
22
25
  data: hydrationData,
26
+ isolate,
23
27
  links,
24
28
  name,
25
29
  nextData,
@@ -27,6 +31,7 @@ async function RemoteComponent({ src }) {
27
31
  route: metadata.route,
28
32
  runtime: metadata.runtime,
29
33
  scripts,
34
+ url: typeof src === "string" ? src : src.href,
30
35
  children: component
31
36
  }
32
37
  );
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/next/host/app-server.tsx"],"sourcesContent":["import { headers } from 'next/headers';\nimport { fetchRemoteComponent } from '../../shared/ssr/fetch-remote-component';\nimport { RemoteComponentClient } from './app-client';\n\nconst CURRENT_ZONE = process.env.NEXT_PUBLIC_MFE_CURRENT_APPLICATION;\n\nexport async function RemoteComponent({ src }: { src: string | URL }) {\n // get the headers from the request\n const headerList = await headers();\n\n const {\n metadata,\n scripts,\n links,\n hydrationData,\n nextData,\n component,\n remoteShared,\n } = await fetchRemoteComponent(src, headerList, true);\n\n // pass all remote component data to the SSR/client layer\n // render remote component static HTML as children\n // remote _ssr suffix from remote component id\n const name = metadata.id.replace(/_ssr$/, '');\n return (\n <RemoteComponentClient\n bundle={metadata.bundle || (CURRENT_ZONE ?? name)}\n data={hydrationData}\n links={links}\n name={name}\n nextData={nextData}\n remoteShared={remoteShared}\n route={metadata.route}\n runtime={metadata.runtime}\n scripts={scripts}\n >\n {component}\n </RemoteComponentClient>\n );\n}\n"],"mappings":"AAyBI;AAzBJ,SAAS,eAAe;AACxB,SAAS,4BAA4B;AACrC,SAAS,6BAA6B;AAEtC,MAAM,eAAe,QAAQ,IAAI;AAEjC,eAAsB,gBAAgB,EAAE,IAAI,GAA0B;AAEpE,QAAM,aAAa,MAAM,QAAQ;AAEjC,QAAM;AAAA,IACJ;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF,IAAI,MAAM,qBAAqB,KAAK,YAAY,IAAI;AAKpD,QAAM,OAAO,SAAS,GAAG,QAAQ,SAAS,EAAE;AAC5C,SACE;AAAA,IAAC;AAAA;AAAA,MACC,QAAQ,SAAS,WAAW,gBAAgB;AAAA,MAC5C,MAAM;AAAA,MACN;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA,OAAO,SAAS;AAAA,MAChB,SAAS,SAAS;AAAA,MAClB;AAAA,MAEC;AAAA;AAAA,EACH;AAEJ;","names":[]}
1
+ {"version":3,"sources":["../../../src/next/host/app-server.tsx"],"sourcesContent":["import { headers } from 'next/headers';\nimport { fetchRemoteComponent } from '../../shared/ssr/fetch-remote-component';\nimport { RemoteComponentClient } from './app-client';\n\nconst CURRENT_ZONE = process.env.NEXT_PUBLIC_MFE_CURRENT_APPLICATION;\n\n/**\n * RemoteComponent is a Next.js component that fetches and renders a remote component.\n * It supports SSR and can isolate the remote component in a shadow DOM.\n *\n * @param src - The source URL of the remote component. When using Vercel Microfrontends, you can use relative paths, e.g. `/nextjs-app-remote/components/header`. Absolute URLs are also supported.\n * @param isolate - Whether to isolate the remote component using a Shadow DOM wrapper. Defaults to `true`. Use `false` explicitly to disable isolation.\n * @returns A React component that renders the remote component.\n *\n * @example\n *\n * Use the `<RemoteComponent>` in your Next.js App Router application to consume a remote component from a remote application:\n *\n * ```tsx\n * import { RemoteComponent } from 'remote-components/next/host';\n *\n * export default function MyPage() {\n * return (\n * <>\n * <h1>Welcome to My Page</h1>\n * <p>This page consumes a remote component from another application.</p>\n * <RemoteComponent src=\"/nextjs-app-remote/components/header\" />\n * </>\n * );\n * }\n * ```\n */\nexport async function RemoteComponent({\n src,\n isolate,\n}: {\n src: string | URL;\n isolate?: boolean;\n}) {\n // get the headers from the request\n const headerList = await headers();\n\n const {\n metadata,\n scripts,\n links,\n hydrationData,\n nextData,\n component,\n remoteShared,\n } = await fetchRemoteComponent(src, headerList, true);\n\n // pass all remote component data to the SSR/client layer\n // render remote component static HTML as children\n // remote _ssr suffix from remote component id\n const name = metadata.id.replace(/_ssr$/, '');\n return (\n <RemoteComponentClient\n bundle={metadata.bundle || (CURRENT_ZONE ?? name)}\n data={hydrationData}\n isolate={isolate}\n links={links}\n name={name}\n nextData={nextData}\n remoteShared={remoteShared}\n route={metadata.route}\n runtime={metadata.runtime}\n scripts={scripts}\n url={typeof src === 'string' ? src : src.href}\n >\n {component}\n </RemoteComponentClient>\n );\n}\n"],"mappings":"AAyDI;AAzDJ,SAAS,eAAe;AACxB,SAAS,4BAA4B;AACrC,SAAS,6BAA6B;AAEtC,MAAM,eAAe,QAAQ,IAAI;AA4BjC,eAAsB,gBAAgB;AAAA,EACpC;AAAA,EACA;AACF,GAGG;AAED,QAAM,aAAa,MAAM,QAAQ;AAEjC,QAAM;AAAA,IACJ;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF,IAAI,MAAM,qBAAqB,KAAK,YAAY,IAAI;AAKpD,QAAM,OAAO,SAAS,GAAG,QAAQ,SAAS,EAAE;AAC5C,SACE;AAAA,IAAC;AAAA;AAAA,MACC,QAAQ,SAAS,WAAW,gBAAgB;AAAA,MAC5C,MAAM;AAAA,MACN;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA,OAAO,SAAS;AAAA,MAChB,SAAS,SAAS;AAAA,MAClB;AAAA,MACA,KAAK,OAAO,QAAQ,WAAW,MAAM,IAAI;AAAA,MAExC;AAAA;AAAA,EACH;AAEJ;","names":[]}
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/next/host/pages-server.tsx"],"sourcesContent":["import type { IncomingHttpHeaders } from 'node:http';\nimport { useEffect } from 'react';\nimport { shared } from '@remote-component/shared/host';\nimport { fetchRemoteComponent } from '../../shared/ssr/fetch-remote-component';\n\n// patch react/jsx-runtime to support the remote-component custom element\ndeclare module 'react/jsx-runtime' {\n // eslint-disable-next-line @typescript-eslint/no-namespace\n export namespace JSX {\n interface IntrinsicElements {\n 'remote-component': {\n src?: string;\n children: React.ReactNode;\n };\n }\n }\n}\n\n// internal symbols to access global store\nconst REMOTE_COMPONENT_STORE = Symbol('REMOTE_COMPONENT_STORE');\nconst REMOTE_COMPONENT_KEY = '__REMOTE_COMPONENT_KEY__';\n\n// temporary global store for remote component HTML\n// the store is used to save the HTML of remote components for SSR without sending the content to the client\nconst self = globalThis as typeof globalThis & {\n [REMOTE_COMPONENT_STORE]?: Map<string, string>;\n};\n\nfunction getKey({\n bundle,\n route,\n name,\n}: {\n bundle?: string;\n route?: string;\n name?: string;\n}): string {\n return `${bundle ?? '__next'}:${route ?? '/'}:${name ?? '__vercel_remote_component'}__${crypto.randomUUID()}`;\n}\n\nfunction setComponent(key: string, html: string): void {\n if (!self[REMOTE_COMPONENT_STORE]) {\n self[REMOTE_COMPONENT_STORE] = new Map();\n }\n self[REMOTE_COMPONENT_STORE].set(key, html);\n}\n\nfunction getComponent(key: string): string | undefined {\n const component = self[REMOTE_COMPONENT_STORE]?.get(key);\n // remove the component from the store after retrieving it to prevent memory leaks\n // storing the HTML in the global store is only needed for SSR and it's temporary only used for a single render\n self[REMOTE_COMPONENT_STORE]?.delete(key);\n return component;\n}\n\nexport interface RemoteComponentProps {\n src: string;\n bundle?: string;\n route?: string;\n name?: string;\n [REMOTE_COMPONENT_KEY]?: string;\n children?: React.ReactNode;\n}\n\nexport function RemoteComponent(props: RemoteComponentProps): JSX.Element {\n const remoteComponent =\n typeof document !== 'undefined'\n ? null\n : // retrieve the HTML from the global store\n getComponent(\n props[REMOTE_COMPONENT_KEY] ?? '__vercel_remote_component',\n );\n\n useEffect(() => {\n const clientSelf = globalThis as typeof globalThis & {\n __remote_component_shared__?: Record<string, () => Promise<unknown>>;\n };\n // eslint-disable-next-line camelcase\n clientSelf.__remote_component_shared__ = shared;\n import('remote-components/html');\n }, []);\n\n if (!props[REMOTE_COMPONENT_KEY]) {\n return (\n <remote-component src={props.src}>{props.children}</remote-component>\n );\n }\n\n return (\n <remote-component src={props.src}>\n <div\n dangerouslySetInnerHTML={{ __html: remoteComponent ?? '' }}\n id=\"__REMOTE_COMPONENT__\"\n suppressHydrationWarning\n />\n </remote-component>\n );\n}\n\nexport async function getRemoteComponentProps(\n src: string,\n headers: IncomingHttpHeaders,\n): Promise<RemoteComponentProps> {\n if (typeof document !== 'undefined') {\n throw new Error(\n 'getRemoteComponentProps can only be used on the server side.',\n );\n }\n\n const {\n metadata: { bundle, route, runtime },\n name,\n html,\n nextData,\n } = await fetchRemoteComponent(\n src,\n headers instanceof Headers\n ? headers\n : // convert IncomingHttpHeaders to web standard Headers\n Object.entries(headers).reduce((acc, [key, value]) => {\n if (value) {\n if (Array.isArray(value)) {\n value.forEach((v) => acc.append(key, v));\n } else {\n acc.append(key, value);\n }\n }\n return acc;\n }, new Headers()),\n );\n\n const props = {\n src,\n bundle,\n name,\n route,\n runtime,\n };\n\n // do not render the HTML in development mode when remote is using Next.js Pages Router\n // this behavior is emulating the Next.js Pages Router FOUC as the styles are only applied on the client when running in development mode\n if (nextData?.buildId === 'development') {\n return props;\n }\n\n const key = getKey(props);\n // store the HTML in a global store\n setComponent(key, html);\n\n return {\n ...props,\n // add remote component key to the props\n [REMOTE_COMPONENT_KEY]: key,\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAoFM;AAnFN,mBAA0B;AAC1B,kBAAuB;AACvB,oCAAqC;AAgBrC,MAAM,yBAAyB,OAAO,wBAAwB;AAC9D,MAAM,uBAAuB;AAI7B,MAAM,OAAO;AAIb,SAAS,OAAO;AAAA,EACd;AAAA,EACA;AAAA,EACA;AACF,GAIW;AACT,SAAO,GAAG,UAAU,YAAY,SAAS,OAAO,QAAQ,gCAAgC,OAAO,WAAW;AAC5G;AAEA,SAAS,aAAa,KAAa,MAAoB;AACrD,MAAI,CAAC,KAAK,sBAAsB,GAAG;AACjC,SAAK,sBAAsB,IAAI,oBAAI,IAAI;AAAA,EACzC;AACA,OAAK,sBAAsB,EAAE,IAAI,KAAK,IAAI;AAC5C;AAEA,SAAS,aAAa,KAAiC;AACrD,QAAM,YAAY,KAAK,sBAAsB,GAAG,IAAI,GAAG;AAGvD,OAAK,sBAAsB,GAAG,OAAO,GAAG;AACxC,SAAO;AACT;AAWO,SAAS,g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;AACvC,WAAO;AAAA,EACT;AAEA,QAAM,MAAM,OAAO,KAAK;AAExB,eAAa,KAAK,IAAI;AAEtB,SAAO;AAAA,IACL,GAAG;AAAA;AAAA,IAEH,CAAC,oBAAoB,GAAG;AAAA,EAC1B;AACF;","names":["key"]}
1
+ {"version":3,"sources":["../../../src/next/host/pages-server.tsx"],"sourcesContent":["import type { IncomingHttpHeaders } from 'node:http';\nimport { useEffect } from 'react';\nimport { shared } from '@remote-component/shared/host';\nimport { fetchRemoteComponent } from '../../shared/ssr/fetch-remote-component';\n\n// patch react/jsx-runtime to support the remote-component custom element\ndeclare module 'react/jsx-runtime' {\n // eslint-disable-next-line @typescript-eslint/no-namespace\n export namespace JSX {\n interface IntrinsicElements {\n 'remote-component': {\n src?: string;\n children: React.ReactNode;\n };\n }\n }\n}\n\n// internal symbols to access global store\nconst REMOTE_COMPONENT_STORE = Symbol('REMOTE_COMPONENT_STORE');\nconst REMOTE_COMPONENT_KEY = '__REMOTE_COMPONENT_KEY__';\n\n// temporary global store for remote component HTML\n// the store is used to save the HTML of remote components for SSR without sending the content to the client\nconst self = globalThis as typeof globalThis & {\n [REMOTE_COMPONENT_STORE]?: Map<string, string>;\n};\n\nfunction getKey({\n bundle,\n route,\n name,\n}: {\n bundle?: string;\n route?: string;\n name?: string;\n}): string {\n return `${bundle ?? '__next'}:${route ?? '/'}:${name ?? '__vercel_remote_component'}__${crypto.randomUUID()}`;\n}\n\nfunction setComponent(key: string, html: string): void {\n if (!self[REMOTE_COMPONENT_STORE]) {\n self[REMOTE_COMPONENT_STORE] = new Map();\n }\n self[REMOTE_COMPONENT_STORE].set(key, html);\n}\n\nfunction getComponent(key: string): string | undefined {\n const component = self[REMOTE_COMPONENT_STORE]?.get(key);\n // remove the component from the store after retrieving it to prevent memory leaks\n // storing the HTML in the global store is only needed for SSR and it's temporary only used for a single render\n self[REMOTE_COMPONENT_STORE]?.delete(key);\n return component;\n}\n\nexport interface RemoteComponentProps {\n src: string;\n bundle?: string;\n route?: string;\n name?: string;\n [REMOTE_COMPONENT_KEY]?: string;\n children?: React.ReactNode;\n}\n\n/**\n * This component handles the rendering of remote microfrontends.\n *\n * @param props - The properties for the remote component.\n * @returns A React component that renders the remote component.\n */\nexport function RemoteComponent(props: RemoteComponentProps): JSX.Element {\n const remoteComponent =\n typeof document !== 'undefined'\n ? null\n : // retrieve the HTML from the global store\n getComponent(\n props[REMOTE_COMPONENT_KEY] ?? '__vercel_remote_component',\n );\n\n useEffect(() => {\n const clientSelf = globalThis as typeof globalThis & {\n __remote_component_shared__?: Record<string, () => Promise<unknown>>;\n };\n // eslint-disable-next-line camelcase\n clientSelf.__remote_component_shared__ = shared;\n import('remote-components/html');\n }, []);\n\n if (!props[REMOTE_COMPONENT_KEY]) {\n return (\n <remote-component src={props.src}>{props.children}</remote-component>\n );\n }\n\n return (\n <remote-component src={props.src}>\n <div\n dangerouslySetInnerHTML={{ __html: remoteComponent ?? '' }}\n id=\"__REMOTE_COMPONENT__\"\n suppressHydrationWarning\n />\n </remote-component>\n );\n}\n\n/**\n * Fetches the remote component properties from the server. You need to pass these properties to the `<RemoteComponent>` component to render the fetched remote component.\n *\n * @param src - The source URL of the remote component. When using the Vercel Microfrontends solution, you can use relative paths, e.g. `/nextjs-app-remote/components/header`. Absolute URLs are also supported.\n * @param headers - The HTTP headers used for supporting the Vercel Microfrontends proxy.\n * @returns The properties of the remote component.\n *\n * @example\n *\n * ```tsx\n * import { getRemoteComponentProps } from 'remote-components/next/host/pages';\n * import type { GetServerSideProps } from 'next';\n *\n * export const getServerSideProps: GetServerSideProps<PageProps> = async function getServerSideProps({ req }) {\n * const myRemoteComponent = await getRemoteComponentProps(\n * '/nextjs-app-remote/components/header',\n * req.headers,\n * );\n * return {\n * props: {\n * remoteComponents: {\n * myRemoteComponent,\n * },\n * },\n * };\n * }\n * ```\n */\nexport async function getRemoteComponentProps(\n src: string,\n headers: IncomingHttpHeaders,\n): Promise<RemoteComponentProps> {\n if (typeof document !== 'undefined') {\n throw new Error(\n 'getRemoteComponentProps can only be used on the server side.',\n );\n }\n\n const {\n metadata: { bundle, route, runtime },\n name,\n html,\n nextData,\n } = await fetchRemoteComponent(\n src,\n headers instanceof Headers\n ? headers\n : // convert IncomingHttpHeaders to web standard Headers\n Object.entries(headers).reduce((acc, [key, value]) => {\n if (value) {\n if (Array.isArray(value)) {\n value.forEach((v) => acc.append(key, v));\n } else {\n acc.append(key, value);\n }\n }\n return acc;\n }, new Headers()),\n );\n\n const props = {\n src,\n bundle,\n name,\n route,\n runtime,\n };\n\n // do not render the HTML in development mode when remote is using Next.js Pages Router\n // this behavior is emulating the Next.js Pages Router FOUC as the styles are only applied on the client when running in development mode\n if (nextData?.buildId === 'development') {\n return props;\n }\n\n const key = getKey(props);\n // store the HTML in a global store\n setComponent(key, html);\n\n return {\n ...props,\n // add remote component key to the props\n [REMOTE_COMPONENT_KEY]: key,\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AA0FM;AAzFN,mBAA0B;AAC1B,kBAAuB;AACvB,oCAAqC;AAgBrC,MAAM,yBAAyB,OAAO,wBAAwB;AAC9D,MAAM,uBAAuB;AAI7B,MAAM,OAAO;AAIb,SAAS,OAAO;AAAA,EACd;AAAA,EACA;AAAA,EACA;AACF,GAIW;AACT,SAAO,GAAG,UAAU,YAAY,SAAS,OAAO,QAAQ,gCAAgC,OAAO,WAAW;AAC5G;AAEA,SAAS,aAAa,KAAa,MAAoB;AACrD,MAAI,CAAC,KAAK,sBAAsB,GAAG;AACjC,SAAK,sBAAsB,IAAI,oBAAI,IAAI;AAAA,EACzC;AACA,OAAK,sBAAsB,EAAE,IAAI,KAAK,IAAI;AAC5C;AAEA,SAAS,aAAa,KAAiC;AACrD,QAAM,YAAY,KAAK,sBAAsB,GAAG,IAAI,GAAG;AAGvD,OAAK,sBAAsB,GAAG,OAAO,GAAG;AACxC,SAAO;AACT;AAiBO,SAAS,gBAA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ACvC,WAAO;AAAA,EACT;AAEA,QAAM,MAAM,OAAO,KAAK;AAExB,eAAa,KAAK,IAAI;AAEtB,SAAO;AAAA,IACL,GAAG;AAAA;AAAA,IAEH,CAAC,oBAAoB,GAAG;AAAA,EAC1B;AACF;","names":["key"]}
@@ -19,7 +19,41 @@ interface RemoteComponentProps {
19
19
  [REMOTE_COMPONENT_KEY]?: string;
20
20
  children?: React.ReactNode;
21
21
  }
22
+ /**
23
+ * This component handles the rendering of remote microfrontends.
24
+ *
25
+ * @param props - The properties for the remote component.
26
+ * @returns A React component that renders the remote component.
27
+ */
22
28
  declare function RemoteComponent(props: RemoteComponentProps): JSX.Element;
29
+ /**
30
+ * Fetches the remote component properties from the server. You need to pass these properties to the `<RemoteComponent>` component to render the fetched remote component.
31
+ *
32
+ * @param src - The source URL of the remote component. When using the Vercel Microfrontends solution, you can use relative paths, e.g. `/nextjs-app-remote/components/header`. Absolute URLs are also supported.
33
+ * @param headers - The HTTP headers used for supporting the Vercel Microfrontends proxy.
34
+ * @returns The properties of the remote component.
35
+ *
36
+ * @example
37
+ *
38
+ * ```tsx
39
+ * import { getRemoteComponentProps } from 'remote-components/next/host/pages';
40
+ * import type { GetServerSideProps } from 'next';
41
+ *
42
+ * export const getServerSideProps: GetServerSideProps<PageProps> = async function getServerSideProps({ req }) {
43
+ * const myRemoteComponent = await getRemoteComponentProps(
44
+ * '/nextjs-app-remote/components/header',
45
+ * req.headers,
46
+ * );
47
+ * return {
48
+ * props: {
49
+ * remoteComponents: {
50
+ * myRemoteComponent,
51
+ * },
52
+ * },
53
+ * };
54
+ * }
55
+ * ```
56
+ */
23
57
  declare function getRemoteComponentProps(src: string, headers: IncomingHttpHeaders): Promise<RemoteComponentProps>;
24
58
 
25
59
  export { RemoteComponent, RemoteComponentProps, getRemoteComponentProps };