>(
WrappedComponent: RouteComponent
) {
return function WithDomainRedirectWrapper(props: P) {
const {customerDomain, links} = window.__initialData;
const {sentryUrl} = links;
const currentOrganization = useOrganization({allowNull: true});
if (customerDomain) {
// Customer domain is being used on a route that has an :orgId parameter.
const redirectPath = `${window.location.pathname}${window.location.search}${window.location.hash}`;
const redirectURL = `${trimEnd(sentryUrl, '/')}/${trimStart(redirectPath, '/')}`;
// If we have domain information, but the subdomain and slug are different
// redirect to the slug path and let django decide what happens next.
if (
currentOrganization &&
customerDomain.subdomain &&
(currentOrganization.slug !== customerDomain.subdomain ||
!currentOrganization.features.includes('customer-domains'))
) {
window.location.replace(redirectURL);
return null;
}
const {params, routes} = props;
// Regenerate the full route with the :orgId parameter omitted.
const newParams = {...params};
Object.keys(params).forEach(param => {
newParams[param] = `:${param}`;
});
const fullRoute = recreateRoute('', {routes, params: newParams});
const orglessSlugRoute = normalizeUrl(fullRoute, {forceCustomerDomain: true});
if (orglessSlugRoute === fullRoute) {
// :orgId is not present in the route, so we do not need to perform a redirect here.
return