Skip to content

Commit

Permalink
feat: pass origin to component
Browse files Browse the repository at this point in the history
  • Loading branch information
JulianWielga committed Nov 19, 2024
1 parent 555830f commit 88af7d1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
8 changes: 4 additions & 4 deletions src/FederatedComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,16 @@ function FederatedComponentRender<P extends NonNullable<unknown>, T>(
}: FederatedComponentProps<P> & Pick<FederatedModuleProviderProps, "fallback" | "buildHash">,
ref: React.ForwardedRef<T>,
) {
const [, scopeValue, , , , query] = splitUrl(url as ModuleUrl);
const [, scopeValue, , , , , u] = useMemo(() => splitUrl(url as ModuleUrl), [url]);

const params = useMemo(() => {
const searchParams = new URLSearchParams(query);
const searchParams = u.searchParams;
return Object.fromEntries(searchParams.entries()) as P;
}, [query]);
}, [u]);

return (
<FederatedModuleProvider url={url} fallback={fallback} buildHash={buildHash}>
<Component ref={ref} {...params} {...props} scope={scope || scopeValue} />
<Component ref={ref} {...params} {...props} scope={scope || scopeValue} moduleOrigin={u.origin} />
</FederatedModuleProvider>
);
}
Expand Down
12 changes: 10 additions & 2 deletions src/tools/splitUrl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ModuleString, ModuleUrl, PathString, QueryString, ScopeString, ScriptUr
* Split FederatedModule url to url and module parts.
* @param fullModuleUrl
*/
export function splitUrl(fullModuleUrl: ModuleUrl): [ModuleUrl, ModuleString, ScriptUrl, ScopeString, PathString, QueryString] {
export function splitUrl(fullModuleUrl: ModuleUrl): [ModuleUrl, ModuleString, ScriptUrl, ScopeString, PathString, QueryString, URL] {
const [module, url] = fullModuleUrl.split("@");
const [scope] = module.split("/");
const path = module.replace(scope, ".");
Expand All @@ -14,5 +14,13 @@ export function splitUrl(fullModuleUrl: ModuleUrl): [ModuleUrl, ModuleString, Sc
throw new Error("invalid remote module url");
}

return [fullModuleUrl, module as ModuleString, script as ScriptUrl, scope as ScopeString, path as PathString, query as QueryString];
return [
fullModuleUrl,
module as ModuleString,
script as ScriptUrl,
scope as ScopeString,
path as PathString,
query as QueryString,
new URL(script, window.location.origin),
];
}

0 comments on commit 88af7d1

Please sign in to comment.