remote-components 0.0.21 → 0.0.23

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 (48) hide show
  1. package/dist/html/host.cjs +115 -40
  2. package/dist/html/host.cjs.map +1 -1
  3. package/dist/html/host.js +115 -40
  4. package/dist/html/host.js.map +1 -1
  5. package/dist/internal/next/host/app-router-client.cjs +72 -23
  6. package/dist/internal/next/host/app-router-client.cjs.map +1 -1
  7. package/dist/internal/next/host/app-router-client.d.ts +2 -2
  8. package/dist/internal/next/host/app-router-client.js +73 -24
  9. package/dist/internal/next/host/app-router-client.js.map +1 -1
  10. package/dist/internal/shared/client/remote-component.cjs +43 -16
  11. package/dist/internal/shared/client/remote-component.cjs.map +1 -1
  12. package/dist/internal/shared/client/remote-component.d.ts +2 -0
  13. package/dist/internal/shared/client/remote-component.js +43 -16
  14. package/dist/internal/shared/client/remote-component.js.map +1 -1
  15. package/dist/internal/shared/ssr/dom-flight.cjs +9 -17
  16. package/dist/internal/shared/ssr/dom-flight.cjs.map +1 -1
  17. package/dist/internal/shared/ssr/dom-flight.js +9 -17
  18. package/dist/internal/shared/ssr/dom-flight.js.map +1 -1
  19. package/dist/internal/shared/ssr/fetch-remote-component.cjs +3 -4
  20. package/dist/internal/shared/ssr/fetch-remote-component.cjs.map +1 -1
  21. package/dist/internal/shared/ssr/fetch-remote-component.js +3 -4
  22. package/dist/internal/shared/ssr/fetch-remote-component.js.map +1 -1
  23. package/dist/internal/webpack/next-client-pages-loader.cjs +43 -16
  24. package/dist/internal/webpack/next-client-pages-loader.cjs.map +1 -1
  25. package/dist/internal/webpack/next-client-pages-loader.js +43 -16
  26. package/dist/internal/webpack/next-client-pages-loader.js.map +1 -1
  27. package/dist/next/host/app-router-server.cjs +4 -0
  28. package/dist/next/host/app-router-server.cjs.map +1 -1
  29. package/dist/next/host/app-router-server.d.ts +5 -1
  30. package/dist/next/host/app-router-server.js +4 -0
  31. package/dist/next/host/app-router-server.js.map +1 -1
  32. package/dist/next/host/client/index.d.ts +1 -1
  33. package/dist/next/host/pages-router-server.cjs +11 -4
  34. package/dist/next/host/pages-router-server.cjs.map +1 -1
  35. package/dist/next/host/pages-router-server.d.ts +3 -0
  36. package/dist/next/host/pages-router-server.js +11 -4
  37. package/dist/next/host/pages-router-server.js.map +1 -1
  38. package/dist/next/middleware.cjs +1 -1
  39. package/dist/next/middleware.cjs.map +1 -1
  40. package/dist/next/middleware.js +1 -1
  41. package/dist/next/middleware.js.map +1 -1
  42. package/dist/react/index.cjs +56 -19
  43. package/dist/react/index.cjs.map +1 -1
  44. package/dist/react/index.d.ts +6 -2
  45. package/dist/react/index.js +56 -19
  46. package/dist/react/index.js.map +1 -1
  47. package/dist/{types-7425dfe1.d.ts → types-b8210fd3.d.ts} +2 -0
  48. package/package.json +1 -1
@@ -65,20 +65,28 @@ var init_shared_modules = __esm({
65
65
  // src/shared/webpack/next-client-pages-loader.ts
66
66
  function nextClientPagesLoader(bundle, route, styleContainer = document.head) {
67
67
  const self = globalThis;
68
- const nextCssOriginal = document.getElementById("__next_css__DO_NOT_USE__");
68
+ const nextCssOriginal = document.querySelector(
69
+ `[id="__next_css__DO_NOT_USE__"]:not([data-bundle="${bundle}"][data-route="${route}"])`
70
+ );
69
71
  if (nextCssOriginal) {
70
72
  nextCssOriginal.parentNode?.removeChild(nextCssOriginal);
71
73
  }
72
74
  const nextCss = document.createElement("noscript");
73
75
  nextCss.id = "__next_css__DO_NOT_USE__";
74
- const lastNode = document.head.childNodes[document.head.childNodes.length - 1];
76
+ nextCss.setAttribute("data-bundle", bundle);
77
+ nextCss.setAttribute("data-route", route);
78
+ const nextCssEnd = document.createElement("noscript");
79
+ nextCssEnd.id = "__next_css__DO_NOT_USE_END__";
80
+ nextCssEnd.setAttribute("data-bundle", bundle);
81
+ nextCssEnd.setAttribute("data-route", route);
82
+ document.head.appendChild(nextCssEnd);
75
83
  document.head.appendChild(nextCss);
76
84
  const componentLoaderChunk = Object.keys(self.__remote_webpack_require__?.[bundle]?.m ?? {}).find(
77
- (key) => key.includes("/webpack/loaders/next-client-pages-loader.js") && key.includes(`page=${encodeURIComponent(route)}`)
85
+ (key) => key.includes("/webpack/loaders/next-client-pages-loader.js") && key.includes(`page=${encodeURIComponent(route)}!`)
78
86
  ) ?? Object.keys(self.__remote_webpack_require__?.[bundle]?.m ?? {}).find(
79
87
  (key) => key.includes("/next/dist/client/page-loader.js")
80
88
  ) ?? self.__remote_webpack_module_map__?.[bundle]?.[Object.keys(self.__remote_webpack_module_map__[bundle] ?? {}).find(
81
- (key) => key.includes("/webpack/loaders/next-client-pages-loader.js") && key.includes(`page=${encodeURIComponent(route)}`)
89
+ (key) => key.includes("/webpack/loaders/next-client-pages-loader.js") && key.includes(`page=${encodeURIComponent(route)}!`)
82
90
  ) ?? Object.keys(self.__remote_webpack_module_map__[bundle] ?? {}).find(
83
91
  (key) => key.includes("/next/dist/client/page-loader.js")
84
92
  ) ?? ""] ?? -1;
@@ -120,28 +128,47 @@ function nextClientPagesLoader(bundle, route, styleContainer = document.head) {
120
128
  ];
121
129
  const { default: Component } = componentLoader();
122
130
  const { default: App } = appLoader();
123
- const cssRE = /\.s?css$/;
124
- Object.keys(self.__remote_webpack_require__?.[bundle]?.m ?? {}).filter((id) => cssRE.test(id)).forEach((id) => {
125
- self.__remote_webpack_require__?.[bundle]?.(id);
126
- });
127
- Object.keys(self.__remote_webpack_module_map__?.[bundle] ?? {}).filter((path) => cssRE.test(path)).forEach((path) => {
128
- const id = self.__remote_webpack_module_map__?.[bundle]?.[path];
129
- if (id) {
131
+ if (!self.__remote_next_css__) {
132
+ self.__remote_next_css__ = {};
133
+ }
134
+ if (!self.__remote_next_css__[bundle]) {
135
+ const cssRE = /\.s?css$/;
136
+ Object.keys(self.__remote_webpack_require__?.[bundle]?.m ?? {}).filter((id) => cssRE.test(id)).forEach((id) => {
130
137
  self.__remote_webpack_require__?.[bundle]?.(id);
131
- }
132
- });
133
- if (styleContainer) {
138
+ });
139
+ Object.keys(self.__remote_webpack_module_map__?.[bundle] ?? {}).filter((path) => cssRE.test(path)).forEach((path) => {
140
+ const id = self.__remote_webpack_module_map__?.[bundle]?.[path];
141
+ if (id) {
142
+ self.__remote_webpack_require__?.[bundle]?.(id);
143
+ }
144
+ });
145
+ const elements = [];
134
146
  let node = nextCss.previousSibling;
135
- while (node && node !== lastNode) {
136
- styleContainer.appendChild(node);
147
+ while (node && node !== nextCssEnd) {
148
+ elements.push(node);
149
+ node.remove();
137
150
  node = nextCss.previousSibling;
138
151
  }
152
+ self.__remote_next_css__[bundle] = elements;
153
+ }
154
+ if (styleContainer) {
155
+ const elements = self.__remote_next_css__[bundle];
156
+ elements.forEach((el) => {
157
+ styleContainer.appendChild(el.cloneNode(true));
158
+ });
159
+ } else {
160
+ const elements = self.__remote_next_css__[bundle];
161
+ elements.forEach((el) => {
162
+ document.head.appendChild(el);
163
+ });
139
164
  }
140
165
  delete self.__NEXT_P;
141
166
  self.__NEXT_P = __NEXT_P_ORIGINAL;
142
167
  if (nextCssOriginal) {
143
168
  nextCssOriginal.parentNode?.appendChild(nextCssOriginal);
144
169
  }
170
+ nextCss.remove();
171
+ nextCssEnd.remove();
145
172
  return { Component, App };
146
173
  }
147
174
  return { Component: null, App: null };
@@ -754,6 +781,7 @@ __export(host_exports, {
754
781
  registerSharedModules: () => registerSharedModules
755
782
  });
756
783
  module.exports = __toCommonJS(host_exports);
784
+ var import_react = require("react");
757
785
  var import_client = require("react-dom/client");
758
786
 
759
787
  // src/shared/client/component-loader.ts
@@ -861,9 +889,12 @@ if (typeof HTMLElement !== "undefined") {
861
889
  this.__next = null;
862
890
  this.fouc = null;
863
891
  this.isLoading = false;
864
- this.attachShadow({ mode: "open" });
892
+ this.root = null;
893
+ this.root = this.attachShadow({
894
+ mode: this.getAttribute("mode") === "closed" ? "closed" : "open"
895
+ });
865
896
  this.fallbackSlot = document.createElement("slot");
866
- this.shadowRoot?.appendChild(this.fallbackSlot);
897
+ this.root.appendChild(this.fallbackSlot);
867
898
  this.name = this.getAttribute("name") || "__vercel_remote_component";
868
899
  this.bundle = "default";
869
900
  if (this.hasAttribute("src") || this.querySelector("div#__REMOTE_COMPONENT__") || this.hasAttribute("data-ssr")) {
@@ -873,15 +904,18 @@ if (typeof HTMLElement !== "undefined") {
873
904
  }
874
905
  }
875
906
  static get observedAttributes() {
876
- return ["src"];
907
+ return ["src", "name"];
877
908
  }
878
909
  // watch for src attribute changes
879
910
  // this is required to reload the remote component when the src attribute is added later
880
911
  // this is for rendering the custom element using React
881
912
  attributeChangedCallback(name, oldValue, newValue) {
882
- if (name === "src" && oldValue !== newValue) {
913
+ if ((name === "src" || name === "name") && oldValue !== newValue) {
883
914
  this.load().catch((e) => {
884
- throw new Error(`Failed to load remote component: ${e}`);
915
+ console.error(e);
916
+ throw new Error(
917
+ `Failed to load remote component: ${this.bundle ?? this.name ?? this.getAttribute("src")}`
918
+ );
885
919
  });
886
920
  }
887
921
  }
@@ -907,9 +941,7 @@ if (typeof HTMLElement !== "undefined") {
907
941
  const fetchInit = {
908
942
  method: "GET",
909
943
  headers: {
910
- Accept: "text/html",
911
- // pass the public address of the remote component to the server used for module map mutation
912
- "Vercel-Remote-Component-Url": url.href
944
+ Accept: "text/html"
913
945
  },
914
946
  credentials: this.getAttribute("credentials") || "same-origin"
915
947
  };
@@ -929,10 +961,10 @@ if (typeof HTMLElement !== "undefined") {
929
961
  const nextData = JSON.parse(
930
962
  (doc.querySelector("#__NEXT_DATA__") ?? doc.querySelector("#__REMOTE_NEXT_DATA__"))?.textContent ?? "null"
931
963
  );
932
- if (nextData && nextData.buildId === "development") {
964
+ if (nextData && nextData.buildId === "development" && !this.reactRoot) {
933
965
  this.fouc = document.createElement("style");
934
966
  this.fouc.textContent = `:host { display: none; }`;
935
- this.shadowRoot?.appendChild(this.fouc);
967
+ this.root?.appendChild(this.fouc);
936
968
  }
937
969
  this.name = component?.getAttribute("id")?.replace(/_ssr$/, "") || (nextData ? "__next" : this.name);
938
970
  const rsc = doc.querySelector(`#${this.name}_rsc`);
@@ -983,17 +1015,25 @@ if (typeof HTMLElement !== "undefined") {
983
1015
  newLink.setAttribute(attr.name, attr.value);
984
1016
  }
985
1017
  }
986
- this.shadowRoot?.appendChild(newLink);
1018
+ this.root?.appendChild(newLink);
987
1019
  });
988
1020
  })
989
1021
  );
990
- Array.from(component.children).forEach((el) => {
991
- this.shadowRoot?.appendChild(el);
992
- });
1022
+ if (!this.reactRoot) {
1023
+ Array.from(component.children).forEach((el) => {
1024
+ this.root?.appendChild(el);
1025
+ });
1026
+ }
993
1027
  for (const el of removable) {
994
1028
  el.parentElement?.removeChild(el);
995
1029
  }
996
- this.shadowRoot?.removeChild(this.fallbackSlot);
1030
+ this.fallbackSlot.remove();
1031
+ if (this.getAttribute("reset") !== null && !this.root?.querySelector("style[data-remote-components-reset]")) {
1032
+ const allInitial = document.createElement("style");
1033
+ allInitial.setAttribute("data-remote-components-reset", "");
1034
+ allInitial.textContent = `:host { all: initial; }`;
1035
+ this.root?.prepend(allInitial);
1036
+ }
997
1037
  const {
998
1038
  self,
999
1039
  createFromReadableStream,
@@ -1021,7 +1061,12 @@ if (typeof HTMLElement !== "undefined") {
1021
1061
  window.location.href
1022
1062
  );
1023
1063
  }
1024
- await preloadScripts(Array.from(scripts), url, this.bundle, this.name);
1064
+ await preloadScripts(
1065
+ Array.from(scripts),
1066
+ url,
1067
+ this.bundle ?? "default",
1068
+ this.name ?? "__vercel_remote_component"
1069
+ );
1025
1070
  if (rsc) {
1026
1071
  rsc.parentElement?.removeChild(rsc);
1027
1072
  const rscClone = document.createElement("script");
@@ -1038,28 +1083,58 @@ if (typeof HTMLElement !== "undefined") {
1038
1083
  (cache = createFromReadableStream(stream));
1039
1084
  return Component;
1040
1085
  };
1041
- (0, import_client.hydrateRoot)(
1086
+ if (this.reactRoot) {
1087
+ const root = this.reactRoot;
1088
+ (0, import_react.startTransition)(() => {
1089
+ root.render(
1090
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
1091
+ RemoteComponentFromReadableStream,
1092
+ {
1093
+ name: this.name ?? "__vercel_remote_component"
1094
+ }
1095
+ )
1096
+ );
1097
+ this.isLoading = false;
1098
+ });
1099
+ return;
1100
+ }
1101
+ this.reactRoot = (0, import_client.hydrateRoot)(
1042
1102
  // hydrateRoot expects a document or element, but it works for the shadow DOM too
1043
1103
  // @ts-expect-error support for shadow DOM
1044
- this.shadowRoot,
1045
- /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(RemoteComponentFromReadableStream, { name: this.name })
1104
+ this.root,
1105
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
1106
+ RemoteComponentFromReadableStream,
1107
+ {
1108
+ name: this.name ?? "__vercel_remote_component"
1109
+ }
1110
+ )
1046
1111
  );
1047
1112
  } else if (nextData) {
1048
1113
  const { Component, App } = nextClientPagesLoader2(
1049
- this.bundle,
1114
+ this.bundle ?? "default",
1050
1115
  nextData.page,
1051
- this.shadowRoot
1116
+ this.root
1052
1117
  );
1053
1118
  if (Component) {
1054
- (0, import_client.hydrateRoot)(
1119
+ if (this.reactRoot) {
1120
+ const root = this.reactRoot;
1121
+ (0, import_react.startTransition)(() => {
1122
+ root.render(
1123
+ App ? /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(App, { Component, ...nextData.props }) : /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(Component, { ...nextData.props })
1124
+ );
1125
+ this.isLoading = false;
1126
+ });
1127
+ return;
1128
+ }
1129
+ this.reactRoot = (0, import_client.hydrateRoot)(
1055
1130
  // hydrateRoot expects a document or element, but it works for the shadow DOM too
1056
1131
  // @ts-expect-error support for shadow DOM
1057
- this.shadowRoot,
1132
+ this.root,
1058
1133
  App ? /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(App, { Component, ...nextData.props }) : /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(Component, { ...nextData.props })
1059
1134
  );
1060
1135
  }
1061
1136
  if (this.fouc) {
1062
- this.shadowRoot?.removeChild(this.fouc);
1137
+ this.root?.removeChild(this.fouc);
1063
1138
  }
1064
1139
  }
1065
1140
  this.isLoading = false;