@module-federation/bridge-react 0.0.0-next-20240909092952 → 0.0.0-next-20240909101028
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 +9 -2
- package/dist/index.cjs.js +33 -1
- package/dist/index.d.ts +5 -0
- package/dist/index.es.js +34 -2
- package/package.json +2 -2
- package/src/hooks.tsx +44 -0
- package/src/index.ts +1 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,10 +1,17 @@
|
|
|
1
1
|
# @module-federation/bridge-react
|
|
2
2
|
|
|
3
|
-
## 0.0.0-next-
|
|
3
|
+
## 0.0.0-next-20240909101028
|
|
4
4
|
|
|
5
5
|
### Patch Changes
|
|
6
6
|
|
|
7
|
-
-
|
|
7
|
+
- chore: bump next
|
|
8
|
+
- @module-federation/bridge-shared@0.0.0-next-20240909101028
|
|
9
|
+
|
|
10
|
+
## 0.6.1
|
|
11
|
+
|
|
12
|
+
### Patch Changes
|
|
13
|
+
|
|
14
|
+
- @module-federation/bridge-shared@0.6.1
|
|
8
15
|
|
|
9
16
|
## 0.6.0
|
|
10
17
|
|
package/dist/index.cjs.js
CHANGED
|
@@ -177,7 +177,7 @@ const RemoteAppWrapper = React.forwardRef(function(props, ref) {
|
|
|
177
177
|
}
|
|
178
178
|
);
|
|
179
179
|
};
|
|
180
|
-
RemoteApp2["__APP_VERSION__"] = "0.6.
|
|
180
|
+
RemoteApp2["__APP_VERSION__"] = "0.6.1";
|
|
181
181
|
return /* @__PURE__ */ React.createElement(RemoteApp2, null);
|
|
182
182
|
});
|
|
183
183
|
function withRouterData(WrappedComponent) {
|
|
@@ -399,5 +399,37 @@ function createBridgeComponent(bridgeInfo) {
|
|
|
399
399
|
};
|
|
400
400
|
};
|
|
401
401
|
}
|
|
402
|
+
function useBridgeRouteChange(onRouteChange) {
|
|
403
|
+
const previousUrlRef = React.useRef(window.location.pathname);
|
|
404
|
+
React.useEffect(() => {
|
|
405
|
+
const handleRouteChange = () => {
|
|
406
|
+
const currentUrl = window.location.pathname;
|
|
407
|
+
const from = previousUrlRef.current;
|
|
408
|
+
const to = currentUrl;
|
|
409
|
+
if (from !== to) {
|
|
410
|
+
onRouteChange({ from, to });
|
|
411
|
+
previousUrlRef.current = currentUrl;
|
|
412
|
+
}
|
|
413
|
+
};
|
|
414
|
+
const originalPushState = window.history.pushState;
|
|
415
|
+
const originalReplaceState = window.history.replaceState;
|
|
416
|
+
window.history.pushState = function(...args) {
|
|
417
|
+
originalPushState.apply(window.history, args);
|
|
418
|
+
handleRouteChange();
|
|
419
|
+
};
|
|
420
|
+
window.history.replaceState = function(...args) {
|
|
421
|
+
console.log("<<<<< replaceState called >>>>>>>", args);
|
|
422
|
+
originalReplaceState.apply(window.history, args);
|
|
423
|
+
handleRouteChange();
|
|
424
|
+
};
|
|
425
|
+
window.addEventListener("popstate", handleRouteChange);
|
|
426
|
+
return () => {
|
|
427
|
+
window.history.pushState = originalPushState;
|
|
428
|
+
window.history.replaceState = originalReplaceState;
|
|
429
|
+
window.removeEventListener("popstate", handleRouteChange);
|
|
430
|
+
};
|
|
431
|
+
}, [onRouteChange]);
|
|
432
|
+
}
|
|
402
433
|
exports.createBridgeComponent = createBridgeComponent;
|
|
403
434
|
exports.createRemoteComponent = createRemoteComponent;
|
|
435
|
+
exports.useBridgeRouteChange = useBridgeRouteChange;
|
package/dist/index.d.ts
CHANGED
|
@@ -66,4 +66,9 @@ export declare interface RenderFnParams extends ProviderParams {
|
|
|
66
66
|
|
|
67
67
|
declare type RootType = HTMLElement | default_3.Root;
|
|
68
68
|
|
|
69
|
+
export declare function useBridgeRouteChange(onRouteChange: ({ from, to }: {
|
|
70
|
+
from: string;
|
|
71
|
+
to: string;
|
|
72
|
+
}) => void): void;
|
|
73
|
+
|
|
69
74
|
export { }
|
package/dist/index.es.js
CHANGED
|
@@ -158,7 +158,7 @@ const RemoteAppWrapper = forwardRef(function(props, ref) {
|
|
|
158
158
|
}
|
|
159
159
|
);
|
|
160
160
|
};
|
|
161
|
-
RemoteApp2["__APP_VERSION__"] = "0.6.
|
|
161
|
+
RemoteApp2["__APP_VERSION__"] = "0.6.1";
|
|
162
162
|
return /* @__PURE__ */ React__default.createElement(RemoteApp2, null);
|
|
163
163
|
});
|
|
164
164
|
function withRouterData(WrappedComponent) {
|
|
@@ -380,7 +380,39 @@ function createBridgeComponent(bridgeInfo) {
|
|
|
380
380
|
};
|
|
381
381
|
};
|
|
382
382
|
}
|
|
383
|
+
function useBridgeRouteChange(onRouteChange) {
|
|
384
|
+
const previousUrlRef = useRef(window.location.pathname);
|
|
385
|
+
useEffect(() => {
|
|
386
|
+
const handleRouteChange = () => {
|
|
387
|
+
const currentUrl = window.location.pathname;
|
|
388
|
+
const from = previousUrlRef.current;
|
|
389
|
+
const to = currentUrl;
|
|
390
|
+
if (from !== to) {
|
|
391
|
+
onRouteChange({ from, to });
|
|
392
|
+
previousUrlRef.current = currentUrl;
|
|
393
|
+
}
|
|
394
|
+
};
|
|
395
|
+
const originalPushState = window.history.pushState;
|
|
396
|
+
const originalReplaceState = window.history.replaceState;
|
|
397
|
+
window.history.pushState = function(...args) {
|
|
398
|
+
originalPushState.apply(window.history, args);
|
|
399
|
+
handleRouteChange();
|
|
400
|
+
};
|
|
401
|
+
window.history.replaceState = function(...args) {
|
|
402
|
+
console.log("<<<<< replaceState called >>>>>>>", args);
|
|
403
|
+
originalReplaceState.apply(window.history, args);
|
|
404
|
+
handleRouteChange();
|
|
405
|
+
};
|
|
406
|
+
window.addEventListener("popstate", handleRouteChange);
|
|
407
|
+
return () => {
|
|
408
|
+
window.history.pushState = originalPushState;
|
|
409
|
+
window.history.replaceState = originalReplaceState;
|
|
410
|
+
window.removeEventListener("popstate", handleRouteChange);
|
|
411
|
+
};
|
|
412
|
+
}, [onRouteChange]);
|
|
413
|
+
}
|
|
383
414
|
export {
|
|
384
415
|
createBridgeComponent,
|
|
385
|
-
createRemoteComponent
|
|
416
|
+
createRemoteComponent,
|
|
417
|
+
useBridgeRouteChange
|
|
386
418
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@module-federation/bridge-react",
|
|
3
|
-
"version": "0.0.0-next-
|
|
3
|
+
"version": "0.0.0-next-20240909101028",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public"
|
|
6
6
|
},
|
|
@@ -35,7 +35,7 @@
|
|
|
35
35
|
"dependencies": {
|
|
36
36
|
"@loadable/component": "^5.16.4",
|
|
37
37
|
"react-error-boundary": "^4.0.13",
|
|
38
|
-
"@module-federation/bridge-shared": "0.0.0-next-
|
|
38
|
+
"@module-federation/bridge-shared": "0.0.0-next-20240909101028"
|
|
39
39
|
},
|
|
40
40
|
"peerDependencies": {
|
|
41
41
|
"react": ">=16.9.0",
|
package/src/hooks.tsx
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { useEffect, useRef } from 'react';
|
|
2
|
+
|
|
3
|
+
function useBridgeRouteChange(
|
|
4
|
+
onRouteChange: ({ from, to }: { from: string; to: string }) => void,
|
|
5
|
+
) {
|
|
6
|
+
const previousUrlRef = useRef(window.location.pathname);
|
|
7
|
+
|
|
8
|
+
useEffect(() => {
|
|
9
|
+
const handleRouteChange = () => {
|
|
10
|
+
const currentUrl = window.location.pathname;
|
|
11
|
+
const from = previousUrlRef.current;
|
|
12
|
+
const to = currentUrl;
|
|
13
|
+
|
|
14
|
+
if (from !== to) {
|
|
15
|
+
onRouteChange({ from, to });
|
|
16
|
+
previousUrlRef.current = currentUrl;
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
const originalPushState = window.history.pushState;
|
|
21
|
+
const originalReplaceState = window.history.replaceState;
|
|
22
|
+
|
|
23
|
+
window.history.pushState = function (...args) {
|
|
24
|
+
originalPushState.apply(window.history, args);
|
|
25
|
+
handleRouteChange();
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
window.history.replaceState = function (...args) {
|
|
29
|
+
console.log('<<<<< replaceState called >>>>>>>', args);
|
|
30
|
+
originalReplaceState.apply(window.history, args);
|
|
31
|
+
handleRouteChange(); // Manually trigger route change handler
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
window.addEventListener('popstate', handleRouteChange);
|
|
35
|
+
|
|
36
|
+
return () => {
|
|
37
|
+
window.history.pushState = originalPushState;
|
|
38
|
+
window.history.replaceState = originalReplaceState;
|
|
39
|
+
window.removeEventListener('popstate', handleRouteChange);
|
|
40
|
+
};
|
|
41
|
+
}, [onRouteChange]);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export { useBridgeRouteChange };
|