@module-federation/bridge-react 0.0.0-next-20240619100937 → 0.0.0-next-20240620034636

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/CHANGELOG.md CHANGED
@@ -1,7 +1,7 @@
1
1
  # @module-federation/bridge-react
2
2
 
3
- ## 0.0.0-next-20240619100937
3
+ ## 0.0.0-next-20240620034636
4
4
 
5
5
  ### Patch Changes
6
6
 
7
- - @module-federation/bridge-shared@0.0.0-next-20240619100937
7
+ - @module-federation/bridge-shared@0.0.0-next-20240620034636
@@ -63,6 +63,7 @@ describe('bridge', () => {
63
63
  expect(getHtml(container)).toMatch('loading');
64
64
 
65
65
  await sleep(200);
66
- expect(getHtml(container)).toMatch('life cycle render hello world');
66
+ expect(getHtml(container)).toMatch('life cycle render');
67
+ expect(getHtml(container)).toMatch('hello world');
67
68
  });
68
69
  });
package/dist/index.cjs.js CHANGED
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
3
  const React = require("react");
4
- const reactRouterDom = require("react-router-dom");
4
+ const ReactRouterDOM = require("react-router-dom");
5
5
  const context = require("./context-ePt4wynZ.cjs");
6
6
  const ReactDOM = require("react-dom");
7
7
  function _interopNamespaceDefault(e) {
@@ -21,28 +21,32 @@ function _interopNamespaceDefault(e) {
21
21
  return Object.freeze(n);
22
22
  }
23
23
  const React__namespace = /* @__PURE__ */ _interopNamespaceDefault(React);
24
+ const ReactRouterDOM__namespace = /* @__PURE__ */ _interopNamespaceDefault(ReactRouterDOM);
24
25
  const RemoteApp = ({
25
26
  name,
26
27
  memoryRoute,
27
28
  basename,
28
29
  providerInfo,
30
+ dispathPopstate,
29
31
  ...resProps
30
32
  }) => {
31
33
  const rootRef = React.useRef(null);
32
34
  const renderDom = React.useRef(null);
33
- const location = reactRouterDom.useLocation();
34
- const [pathname, setPathname] = React.useState(location.pathname);
35
35
  const providerInfoRef = React.useRef(null);
36
- React.useEffect(() => {
37
- if (pathname !== "" && pathname !== location.pathname) {
38
- context.LoggerInstance.log(`createRemoteComponent dispatchPopstateEnv >>>`, {
39
- name,
40
- pathname: location.pathname
41
- });
42
- context.f();
43
- }
44
- setPathname(location.pathname);
45
- }, [location]);
36
+ if (dispathPopstate) {
37
+ const location = ReactRouterDOM__namespace.useLocation();
38
+ const [pathname, setPathname] = React.useState(location.pathname);
39
+ React.useEffect(() => {
40
+ if (pathname !== "" && pathname !== location.pathname) {
41
+ context.LoggerInstance.log(`createRemoteComponent dispatchPopstateEnv >>>`, {
42
+ name,
43
+ pathname: location.pathname
44
+ });
45
+ context.f();
46
+ }
47
+ setPathname(location.pathname);
48
+ }, [location]);
49
+ }
46
50
  React.useEffect(() => {
47
51
  const renderTimeout = setTimeout(() => {
48
52
  const providerReturn = providerInfo();
@@ -83,10 +87,21 @@ RemoteApp["__APP_VERSION__"] = "0.0.1";
83
87
  function createRemoteComponent(lazyComponent, info) {
84
88
  return (props) => {
85
89
  const exportName = (info == null ? void 0 : info.export) || "default";
86
- const routerContextVal = React.useContext(reactRouterDom.UNSAFE_RouteContext);
87
90
  let basename = "/";
88
- if (routerContextVal.matches[0] && routerContextVal.matches[0].pathnameBase) {
89
- basename = routerContextVal.matches[0].pathnameBase;
91
+ let enableDispathPopstate = false;
92
+ let routerContextVal;
93
+ try {
94
+ ReactRouterDOM__namespace.useLocation();
95
+ enableDispathPopstate = true;
96
+ } catch {
97
+ enableDispathPopstate = false;
98
+ }
99
+ if (ReactRouterDOM__namespace.UNSAFE_RouteContext && enableDispathPopstate) {
100
+ routerContextVal = React.useContext(ReactRouterDOM__namespace.UNSAFE_RouteContext);
101
+ if (routerContextVal && routerContextVal.matches && routerContextVal.matches[0] && routerContextVal.matches[0].pathnameBase) {
102
+ basename = routerContextVal.matches[0].pathnameBase;
103
+ }
104
+ enableDispathPopstate = (routerContextVal == null ? void 0 : routerContextVal.matches) && (routerContextVal == null ? void 0 : routerContextVal.matches.length) > 0;
90
105
  }
91
106
  const LazyComponent = React.useMemo(() => {
92
107
  return React.lazy(async () => {
@@ -94,7 +109,8 @@ function createRemoteComponent(lazyComponent, info) {
94
109
  basename,
95
110
  lazyComponent,
96
111
  exportName,
97
- props
112
+ props,
113
+ routerContextVal
98
114
  });
99
115
  const m2 = await lazyComponent();
100
116
  const moduleName = m2 && m2[Symbol.for("mf_module_id")];
@@ -109,6 +125,7 @@ function createRemoteComponent(lazyComponent, info) {
109
125
  RemoteApp,
110
126
  {
111
127
  name: moduleName,
128
+ dispathPopstate: enableDispathPopstate,
112
129
  ...info,
113
130
  ...props,
114
131
  providerInfo: exportFn,
package/dist/index.es.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import * as React from "react";
2
2
  import React__default, { useContext, useMemo, useRef, useState, useEffect } from "react";
3
- import { UNSAFE_RouteContext, useLocation } from "react-router-dom";
3
+ import * as ReactRouterDOM from "react-router-dom";
4
4
  import { L as LoggerInstance, f, a as atLeastReact18, R as RouterContext } from "./context-CPtN38Ur.js";
5
5
  import ReactDOM from "react-dom";
6
6
  const RemoteApp = ({
@@ -8,23 +8,26 @@ const RemoteApp = ({
8
8
  memoryRoute,
9
9
  basename,
10
10
  providerInfo,
11
+ dispathPopstate,
11
12
  ...resProps
12
13
  }) => {
13
14
  const rootRef = useRef(null);
14
15
  const renderDom = useRef(null);
15
- const location = useLocation();
16
- const [pathname, setPathname] = useState(location.pathname);
17
16
  const providerInfoRef = useRef(null);
18
- useEffect(() => {
19
- if (pathname !== "" && pathname !== location.pathname) {
20
- LoggerInstance.log(`createRemoteComponent dispatchPopstateEnv >>>`, {
21
- name,
22
- pathname: location.pathname
23
- });
24
- f();
25
- }
26
- setPathname(location.pathname);
27
- }, [location]);
17
+ if (dispathPopstate) {
18
+ const location = ReactRouterDOM.useLocation();
19
+ const [pathname, setPathname] = useState(location.pathname);
20
+ useEffect(() => {
21
+ if (pathname !== "" && pathname !== location.pathname) {
22
+ LoggerInstance.log(`createRemoteComponent dispatchPopstateEnv >>>`, {
23
+ name,
24
+ pathname: location.pathname
25
+ });
26
+ f();
27
+ }
28
+ setPathname(location.pathname);
29
+ }, [location]);
30
+ }
28
31
  useEffect(() => {
29
32
  const renderTimeout = setTimeout(() => {
30
33
  const providerReturn = providerInfo();
@@ -65,10 +68,21 @@ RemoteApp["__APP_VERSION__"] = "0.0.1";
65
68
  function createRemoteComponent(lazyComponent, info) {
66
69
  return (props) => {
67
70
  const exportName = (info == null ? void 0 : info.export) || "default";
68
- const routerContextVal = useContext(UNSAFE_RouteContext);
69
71
  let basename = "/";
70
- if (routerContextVal.matches[0] && routerContextVal.matches[0].pathnameBase) {
71
- basename = routerContextVal.matches[0].pathnameBase;
72
+ let enableDispathPopstate = false;
73
+ let routerContextVal;
74
+ try {
75
+ ReactRouterDOM.useLocation();
76
+ enableDispathPopstate = true;
77
+ } catch {
78
+ enableDispathPopstate = false;
79
+ }
80
+ if (ReactRouterDOM.UNSAFE_RouteContext && enableDispathPopstate) {
81
+ routerContextVal = useContext(ReactRouterDOM.UNSAFE_RouteContext);
82
+ if (routerContextVal && routerContextVal.matches && routerContextVal.matches[0] && routerContextVal.matches[0].pathnameBase) {
83
+ basename = routerContextVal.matches[0].pathnameBase;
84
+ }
85
+ enableDispathPopstate = (routerContextVal == null ? void 0 : routerContextVal.matches) && (routerContextVal == null ? void 0 : routerContextVal.matches.length) > 0;
72
86
  }
73
87
  const LazyComponent = useMemo(() => {
74
88
  return React__default.lazy(async () => {
@@ -76,7 +90,8 @@ function createRemoteComponent(lazyComponent, info) {
76
90
  basename,
77
91
  lazyComponent,
78
92
  exportName,
79
- props
93
+ props,
94
+ routerContextVal
80
95
  });
81
96
  const m2 = await lazyComponent();
82
97
  const moduleName = m2 && m2[Symbol.for("mf_module_id")];
@@ -91,6 +106,7 @@ function createRemoteComponent(lazyComponent, info) {
91
106
  RemoteApp,
92
107
  {
93
108
  name: moduleName,
109
+ dispathPopstate: enableDispathPopstate,
94
110
  ...info,
95
111
  ...props,
96
112
  providerInfo: exportFn,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@module-federation/bridge-react",
3
- "version": "0.0.0-next-20240619100937",
3
+ "version": "0.0.0-next-20240620034636",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
@@ -25,7 +25,7 @@
25
25
  "dependencies": {
26
26
  "@loadable/component": "^5.16.4",
27
27
  "react-error-boundary": "^4.0.13",
28
- "@module-federation/bridge-shared": "0.0.0-next-20240619100937"
28
+ "@module-federation/bridge-shared": "0.0.0-next-20240620034636"
29
29
  },
30
30
  "peerDependencies": {
31
31
  "react": ">=16.9.0",
package/src/create.tsx CHANGED
@@ -6,7 +6,7 @@ import React, {
6
6
  useRef,
7
7
  useState,
8
8
  } from 'react';
9
- import { UNSAFE_RouteContext, useLocation } from 'react-router-dom';
9
+ import * as ReactRouterDOM from 'react-router-dom';
10
10
  import type { ProviderParams } from '@module-federation/bridge-shared';
11
11
  import { LoggerInstance } from './utils';
12
12
  import { dispatchPopstateEnv } from '@module-federation/bridge-shared';
@@ -31,6 +31,7 @@ interface RemoteModule {
31
31
  interface RemoteAppParams {
32
32
  name: string;
33
33
  providerInfo: NonNullable<RemoteModule['provider']>;
34
+ dispathPopstate: boolean;
34
35
  }
35
36
 
36
37
  const RemoteApp = ({
@@ -38,24 +39,27 @@ const RemoteApp = ({
38
39
  memoryRoute,
39
40
  basename,
40
41
  providerInfo,
42
+ dispathPopstate,
41
43
  ...resProps
42
44
  }: RemoteAppParams & ProviderParams) => {
43
45
  const rootRef = useRef(null);
44
46
  const renderDom = useRef(null);
45
- const location = useLocation();
46
- const [pathname, setPathname] = useState(location.pathname);
47
47
  const providerInfoRef = useRef<any>(null);
48
-
49
- useEffect(() => {
50
- if (pathname !== '' && pathname !== location.pathname) {
51
- LoggerInstance.log(`createRemoteComponent dispatchPopstateEnv >>>`, {
52
- name,
53
- pathname: location.pathname,
54
- });
55
- dispatchPopstateEnv();
56
- }
57
- setPathname(location.pathname);
58
- }, [location]);
48
+ if (dispathPopstate) {
49
+ const location = ReactRouterDOM.useLocation();
50
+ const [pathname, setPathname] = useState(location.pathname);
51
+
52
+ useEffect(() => {
53
+ if (pathname !== '' && pathname !== location.pathname) {
54
+ LoggerInstance.log(`createRemoteComponent dispatchPopstateEnv >>>`, {
55
+ name,
56
+ pathname: location.pathname,
57
+ });
58
+ dispatchPopstateEnv();
59
+ }
60
+ setPathname(location.pathname);
61
+ }, [location]);
62
+ }
59
63
 
60
64
  useEffect(() => {
61
65
  const renderTimeout = setTimeout(() => {
@@ -121,13 +125,28 @@ export function createRemoteComponent<T, E extends keyof T>(
121
125
  } & RawComponentType,
122
126
  ) => {
123
127
  const exportName = info?.export || 'default';
124
- const routerContextVal = useContext(UNSAFE_RouteContext);
125
128
  let basename = '/';
126
- if (
127
- routerContextVal.matches[0] &&
128
- routerContextVal.matches[0].pathnameBase
129
- ) {
130
- basename = routerContextVal.matches[0].pathnameBase;
129
+ let enableDispathPopstate = false;
130
+ let routerContextVal: any;
131
+ try {
132
+ ReactRouterDOM.useLocation();
133
+ enableDispathPopstate = true;
134
+ } catch {
135
+ enableDispathPopstate = false;
136
+ }
137
+
138
+ if (ReactRouterDOM.UNSAFE_RouteContext && enableDispathPopstate) {
139
+ routerContextVal = useContext(ReactRouterDOM.UNSAFE_RouteContext);
140
+ if (
141
+ routerContextVal &&
142
+ routerContextVal.matches &&
143
+ routerContextVal.matches[0] &&
144
+ routerContextVal.matches[0].pathnameBase
145
+ ) {
146
+ basename = routerContextVal.matches[0].pathnameBase;
147
+ }
148
+ enableDispathPopstate =
149
+ routerContextVal?.matches && routerContextVal?.matches.length > 0;
131
150
  }
132
151
 
133
152
  const LazyComponent = useMemo(() => {
@@ -138,6 +157,7 @@ export function createRemoteComponent<T, E extends keyof T>(
138
157
  lazyComponent,
139
158
  exportName,
140
159
  props,
160
+ routerContextVal,
141
161
  });
142
162
  const m = (await lazyComponent()) as RemoteModule;
143
163
  // @ts-ignore
@@ -155,6 +175,7 @@ export function createRemoteComponent<T, E extends keyof T>(
155
175
  default: () => (
156
176
  <RemoteApp
157
177
  name={moduleName}
178
+ dispathPopstate={enableDispathPopstate}
158
179
  {...info}
159
180
  {...props}
160
181
  providerInfo={exportFn}