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
package/dist/html/host.js CHANGED
@@ -43,20 +43,28 @@ var init_shared_modules = __esm({
43
43
  // src/shared/webpack/next-client-pages-loader.ts
44
44
  function nextClientPagesLoader(bundle, route, styleContainer = document.head) {
45
45
  const self = globalThis;
46
- const nextCssOriginal = document.getElementById("__next_css__DO_NOT_USE__");
46
+ const nextCssOriginal = document.querySelector(
47
+ `[id="__next_css__DO_NOT_USE__"]:not([data-bundle="${bundle}"][data-route="${route}"])`
48
+ );
47
49
  if (nextCssOriginal) {
48
50
  nextCssOriginal.parentNode?.removeChild(nextCssOriginal);
49
51
  }
50
52
  const nextCss = document.createElement("noscript");
51
53
  nextCss.id = "__next_css__DO_NOT_USE__";
52
- const lastNode = document.head.childNodes[document.head.childNodes.length - 1];
54
+ nextCss.setAttribute("data-bundle", bundle);
55
+ nextCss.setAttribute("data-route", route);
56
+ const nextCssEnd = document.createElement("noscript");
57
+ nextCssEnd.id = "__next_css__DO_NOT_USE_END__";
58
+ nextCssEnd.setAttribute("data-bundle", bundle);
59
+ nextCssEnd.setAttribute("data-route", route);
60
+ document.head.appendChild(nextCssEnd);
53
61
  document.head.appendChild(nextCss);
54
62
  const componentLoaderChunk = Object.keys(self.__remote_webpack_require__?.[bundle]?.m ?? {}).find(
55
- (key) => key.includes("/webpack/loaders/next-client-pages-loader.js") && key.includes(`page=${encodeURIComponent(route)}`)
63
+ (key) => key.includes("/webpack/loaders/next-client-pages-loader.js") && key.includes(`page=${encodeURIComponent(route)}!`)
56
64
  ) ?? Object.keys(self.__remote_webpack_require__?.[bundle]?.m ?? {}).find(
57
65
  (key) => key.includes("/next/dist/client/page-loader.js")
58
66
  ) ?? self.__remote_webpack_module_map__?.[bundle]?.[Object.keys(self.__remote_webpack_module_map__[bundle] ?? {}).find(
59
- (key) => key.includes("/webpack/loaders/next-client-pages-loader.js") && key.includes(`page=${encodeURIComponent(route)}`)
67
+ (key) => key.includes("/webpack/loaders/next-client-pages-loader.js") && key.includes(`page=${encodeURIComponent(route)}!`)
60
68
  ) ?? Object.keys(self.__remote_webpack_module_map__[bundle] ?? {}).find(
61
69
  (key) => key.includes("/next/dist/client/page-loader.js")
62
70
  ) ?? ""] ?? -1;
@@ -98,28 +106,47 @@ function nextClientPagesLoader(bundle, route, styleContainer = document.head) {
98
106
  ];
99
107
  const { default: Component } = componentLoader();
100
108
  const { default: App } = appLoader();
101
- const cssRE = /\.s?css$/;
102
- Object.keys(self.__remote_webpack_require__?.[bundle]?.m ?? {}).filter((id) => cssRE.test(id)).forEach((id) => {
103
- self.__remote_webpack_require__?.[bundle]?.(id);
104
- });
105
- Object.keys(self.__remote_webpack_module_map__?.[bundle] ?? {}).filter((path) => cssRE.test(path)).forEach((path) => {
106
- const id = self.__remote_webpack_module_map__?.[bundle]?.[path];
107
- if (id) {
109
+ if (!self.__remote_next_css__) {
110
+ self.__remote_next_css__ = {};
111
+ }
112
+ if (!self.__remote_next_css__[bundle]) {
113
+ const cssRE = /\.s?css$/;
114
+ Object.keys(self.__remote_webpack_require__?.[bundle]?.m ?? {}).filter((id) => cssRE.test(id)).forEach((id) => {
108
115
  self.__remote_webpack_require__?.[bundle]?.(id);
109
- }
110
- });
111
- if (styleContainer) {
116
+ });
117
+ Object.keys(self.__remote_webpack_module_map__?.[bundle] ?? {}).filter((path) => cssRE.test(path)).forEach((path) => {
118
+ const id = self.__remote_webpack_module_map__?.[bundle]?.[path];
119
+ if (id) {
120
+ self.__remote_webpack_require__?.[bundle]?.(id);
121
+ }
122
+ });
123
+ const elements = [];
112
124
  let node = nextCss.previousSibling;
113
- while (node && node !== lastNode) {
114
- styleContainer.appendChild(node);
125
+ while (node && node !== nextCssEnd) {
126
+ elements.push(node);
127
+ node.remove();
115
128
  node = nextCss.previousSibling;
116
129
  }
130
+ self.__remote_next_css__[bundle] = elements;
131
+ }
132
+ if (styleContainer) {
133
+ const elements = self.__remote_next_css__[bundle];
134
+ elements.forEach((el) => {
135
+ styleContainer.appendChild(el.cloneNode(true));
136
+ });
137
+ } else {
138
+ const elements = self.__remote_next_css__[bundle];
139
+ elements.forEach((el) => {
140
+ document.head.appendChild(el);
141
+ });
117
142
  }
118
143
  delete self.__NEXT_P;
119
144
  self.__NEXT_P = __NEXT_P_ORIGINAL;
120
145
  if (nextCssOriginal) {
121
146
  nextCssOriginal.parentNode?.appendChild(nextCssOriginal);
122
147
  }
148
+ nextCss.remove();
149
+ nextCssEnd.remove();
123
150
  return { Component, App };
124
151
  }
125
152
  return { Component: null, App: null };
@@ -726,6 +753,7 @@ var init_turbopack = __esm({
726
753
  });
727
754
 
728
755
  // src/html/host/index.tsx
756
+ import { startTransition } from "react";
729
757
  import { hydrateRoot } from "react-dom/client";
730
758
 
731
759
  // src/shared/client/component-loader.ts
@@ -833,9 +861,12 @@ if (typeof HTMLElement !== "undefined") {
833
861
  this.__next = null;
834
862
  this.fouc = null;
835
863
  this.isLoading = false;
836
- this.attachShadow({ mode: "open" });
864
+ this.root = null;
865
+ this.root = this.attachShadow({
866
+ mode: this.getAttribute("mode") === "closed" ? "closed" : "open"
867
+ });
837
868
  this.fallbackSlot = document.createElement("slot");
838
- this.shadowRoot?.appendChild(this.fallbackSlot);
869
+ this.root.appendChild(this.fallbackSlot);
839
870
  this.name = this.getAttribute("name") || "__vercel_remote_component";
840
871
  this.bundle = "default";
841
872
  if (this.hasAttribute("src") || this.querySelector("div#__REMOTE_COMPONENT__") || this.hasAttribute("data-ssr")) {
@@ -845,15 +876,18 @@ if (typeof HTMLElement !== "undefined") {
845
876
  }
846
877
  }
847
878
  static get observedAttributes() {
848
- return ["src"];
879
+ return ["src", "name"];
849
880
  }
850
881
  // watch for src attribute changes
851
882
  // this is required to reload the remote component when the src attribute is added later
852
883
  // this is for rendering the custom element using React
853
884
  attributeChangedCallback(name, oldValue, newValue) {
854
- if (name === "src" && oldValue !== newValue) {
885
+ if ((name === "src" || name === "name") && oldValue !== newValue) {
855
886
  this.load().catch((e) => {
856
- throw new Error(`Failed to load remote component: ${e}`);
887
+ console.error(e);
888
+ throw new Error(
889
+ `Failed to load remote component: ${this.bundle ?? this.name ?? this.getAttribute("src")}`
890
+ );
857
891
  });
858
892
  }
859
893
  }
@@ -879,9 +913,7 @@ if (typeof HTMLElement !== "undefined") {
879
913
  const fetchInit = {
880
914
  method: "GET",
881
915
  headers: {
882
- Accept: "text/html",
883
- // pass the public address of the remote component to the server used for module map mutation
884
- "Vercel-Remote-Component-Url": url.href
916
+ Accept: "text/html"
885
917
  },
886
918
  credentials: this.getAttribute("credentials") || "same-origin"
887
919
  };
@@ -901,10 +933,10 @@ if (typeof HTMLElement !== "undefined") {
901
933
  const nextData = JSON.parse(
902
934
  (doc.querySelector("#__NEXT_DATA__") ?? doc.querySelector("#__REMOTE_NEXT_DATA__"))?.textContent ?? "null"
903
935
  );
904
- if (nextData && nextData.buildId === "development") {
936
+ if (nextData && nextData.buildId === "development" && !this.reactRoot) {
905
937
  this.fouc = document.createElement("style");
906
938
  this.fouc.textContent = `:host { display: none; }`;
907
- this.shadowRoot?.appendChild(this.fouc);
939
+ this.root?.appendChild(this.fouc);
908
940
  }
909
941
  this.name = component?.getAttribute("id")?.replace(/_ssr$/, "") || (nextData ? "__next" : this.name);
910
942
  const rsc = doc.querySelector(`#${this.name}_rsc`);
@@ -955,17 +987,25 @@ if (typeof HTMLElement !== "undefined") {
955
987
  newLink.setAttribute(attr.name, attr.value);
956
988
  }
957
989
  }
958
- this.shadowRoot?.appendChild(newLink);
990
+ this.root?.appendChild(newLink);
959
991
  });
960
992
  })
961
993
  );
962
- Array.from(component.children).forEach((el) => {
963
- this.shadowRoot?.appendChild(el);
964
- });
994
+ if (!this.reactRoot) {
995
+ Array.from(component.children).forEach((el) => {
996
+ this.root?.appendChild(el);
997
+ });
998
+ }
965
999
  for (const el of removable) {
966
1000
  el.parentElement?.removeChild(el);
967
1001
  }
968
- this.shadowRoot?.removeChild(this.fallbackSlot);
1002
+ this.fallbackSlot.remove();
1003
+ if (this.getAttribute("reset") !== null && !this.root?.querySelector("style[data-remote-components-reset]")) {
1004
+ const allInitial = document.createElement("style");
1005
+ allInitial.setAttribute("data-remote-components-reset", "");
1006
+ allInitial.textContent = `:host { all: initial; }`;
1007
+ this.root?.prepend(allInitial);
1008
+ }
969
1009
  const {
970
1010
  self,
971
1011
  createFromReadableStream,
@@ -993,7 +1033,12 @@ if (typeof HTMLElement !== "undefined") {
993
1033
  window.location.href
994
1034
  );
995
1035
  }
996
- await preloadScripts(Array.from(scripts), url, this.bundle, this.name);
1036
+ await preloadScripts(
1037
+ Array.from(scripts),
1038
+ url,
1039
+ this.bundle ?? "default",
1040
+ this.name ?? "__vercel_remote_component"
1041
+ );
997
1042
  if (rsc) {
998
1043
  rsc.parentElement?.removeChild(rsc);
999
1044
  const rscClone = document.createElement("script");
@@ -1010,28 +1055,58 @@ if (typeof HTMLElement !== "undefined") {
1010
1055
  (cache = createFromReadableStream(stream));
1011
1056
  return Component;
1012
1057
  };
1013
- hydrateRoot(
1058
+ if (this.reactRoot) {
1059
+ const root = this.reactRoot;
1060
+ startTransition(() => {
1061
+ root.render(
1062
+ /* @__PURE__ */ jsx2(
1063
+ RemoteComponentFromReadableStream,
1064
+ {
1065
+ name: this.name ?? "__vercel_remote_component"
1066
+ }
1067
+ )
1068
+ );
1069
+ this.isLoading = false;
1070
+ });
1071
+ return;
1072
+ }
1073
+ this.reactRoot = hydrateRoot(
1014
1074
  // hydrateRoot expects a document or element, but it works for the shadow DOM too
1015
1075
  // @ts-expect-error support for shadow DOM
1016
- this.shadowRoot,
1017
- /* @__PURE__ */ jsx2(RemoteComponentFromReadableStream, { name: this.name })
1076
+ this.root,
1077
+ /* @__PURE__ */ jsx2(
1078
+ RemoteComponentFromReadableStream,
1079
+ {
1080
+ name: this.name ?? "__vercel_remote_component"
1081
+ }
1082
+ )
1018
1083
  );
1019
1084
  } else if (nextData) {
1020
1085
  const { Component, App } = nextClientPagesLoader2(
1021
- this.bundle,
1086
+ this.bundle ?? "default",
1022
1087
  nextData.page,
1023
- this.shadowRoot
1088
+ this.root
1024
1089
  );
1025
1090
  if (Component) {
1026
- hydrateRoot(
1091
+ if (this.reactRoot) {
1092
+ const root = this.reactRoot;
1093
+ startTransition(() => {
1094
+ root.render(
1095
+ App ? /* @__PURE__ */ jsx2(App, { Component, ...nextData.props }) : /* @__PURE__ */ jsx2(Component, { ...nextData.props })
1096
+ );
1097
+ this.isLoading = false;
1098
+ });
1099
+ return;
1100
+ }
1101
+ this.reactRoot = hydrateRoot(
1027
1102
  // hydrateRoot expects a document or element, but it works for the shadow DOM too
1028
1103
  // @ts-expect-error support for shadow DOM
1029
- this.shadowRoot,
1104
+ this.root,
1030
1105
  App ? /* @__PURE__ */ jsx2(App, { Component, ...nextData.props }) : /* @__PURE__ */ jsx2(Component, { ...nextData.props })
1031
1106
  );
1032
1107
  }
1033
1108
  if (this.fouc) {
1034
- this.shadowRoot?.removeChild(this.fouc);
1109
+ this.root?.removeChild(this.fouc);
1035
1110
  }
1036
1111
  }
1037
1112
  this.isLoading = false;