Skip to content

Commit

Permalink
feat: read scope from module url, deprecate prop
Browse files Browse the repository at this point in the history
  • Loading branch information
JulianWielga committed Nov 19, 2024
1 parent add389e commit 91b2598
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions src/FederatedComponent.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { ModuleString, ModuleUrl, ScriptUrl } from "./types";
import { useFederatedModule } from "./hooks";
import React, { Fragment } from "react";
import { splitUrl } from "./tools";
import ReactDOM from "react-dom";
import { FederatedModuleProvider, FederatedModuleProviderProps } from "./FederatedModuleProvider";
import { useFederatedModule } from "./hooks";
import { splitUrl } from "./tools";
import { ModuleString, ModuleUrl, ScriptUrl } from "./types";

function Component<P>({
scope,
Expand All @@ -19,18 +19,24 @@ function Component<P>({

export type FederatedComponentProps<P extends NonNullable<unknown>> = P & {
url: ModuleUrl;
scope: ModuleString;
/**
* @deprecated better use scope from ModuleUrl
*/
scope?: ModuleString;
};

export function FederatedComponent<P extends NonNullable<unknown>>({
url,
buildHash,
fallback,
scope,
...props
}: FederatedComponentProps<P> & Pick<FederatedModuleProviderProps, "fallback" | "buildHash">) {
const [, scopeValue] = splitUrl(url as ModuleUrl);

return (
<FederatedModuleProvider url={url} fallback={fallback} buildHash={buildHash}>
<Component {...props} />
<Component scope={scope || scopeValue} {...props} />
</FederatedModuleProvider>
);
}
Expand All @@ -44,7 +50,7 @@ export function getFederatedComponentLoader({ Wrapper = Fragment, ...options }:
return <P extends NonNullable<unknown>>(url: string, props: P) => {
const rootContainer = document.createElement(`div`);
document.body.appendChild(rootContainer);
const [urlValue, scopeValue, scriptValue] = splitUrl(url as ModuleUrl);
const [urlValue, , scriptValue] = splitUrl(url as ModuleUrl);
ReactDOM.render(
<Wrapper>
<FederatedComponent<
Expand All @@ -54,7 +60,6 @@ export function getFederatedComponentLoader({ Wrapper = Fragment, ...options }:
}
>
url={urlValue}
scope={scopeValue}
scriptOrigin={scriptValue}
getAuthToken={options.getAuthToken}
fallback={null}
Expand Down

0 comments on commit 91b2598

Please sign in to comment.