Skip to content

Commit

Permalink
fix: fixed route matcher for webcomponents (#376)
Browse files Browse the repository at this point in the history
 to ensure that routing is working for routes with empty prefix
  • Loading branch information
anninowak authored Aug 8, 2024
1 parent 9509cfc commit cadbc6a
Showing 1 changed file with 15 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,25 +1,33 @@
import { Route, UrlMatcher, UrlSegment, UrlSegmentGroup } from '@angular/router'

export function startsWith(prefix: string): UrlMatcher {
return (url: UrlSegment[], UrlSegmentGroup: UrlSegmentGroup, route: Route) => {
const urlWithoutBaseHref = sliceBaseHref(route, url)
return (url: UrlSegment[], _urlSegmentGroup: UrlSegmentGroup, route: Route) => {
const baseHref = getBaseHrefOfRoute(route)
if (`/${url.map((s) => s.path).join('/')}/`.startsWith(baseHref)) {
const urlWithoutBaseHref = sliceBaseHref(route, url)

const fullUrl = urlWithoutBaseHref.map((u) => u.path).join('/')
if (fullUrl.startsWith(prefix)) {
const prefixLength = prefix.split('/').filter((value) => value).length
return { consumed: url.slice(0, baseHrefSegmentAmount(url, urlWithoutBaseHref) + prefixLength) }
const fullUrl = urlWithoutBaseHref.map((u) => u.path).join('/')
if (fullUrl.startsWith(prefix)) {
const prefixLength = prefix.split('/').filter((value) => value).length
return { consumed: url.slice(0, baseHrefSegmentAmount(url, urlWithoutBaseHref) + prefixLength) }
}
}
return null
}
}

export function sliceBaseHref(route: Route, url: UrlSegment[]): UrlSegment[] {
export function getBaseHrefOfRoute(route: Route): string {
const mfeBaseHref: string = route?.data?.['mfeInfo']?.baseHref
if (!mfeBaseHref) {
console.warn(
'mfeInfo was not provided for route. initializeRouter function is required to be registered as app initializer.'
)
}
return mfeBaseHref
}

export function sliceBaseHref(route: Route, url: UrlSegment[]): UrlSegment[] {
const mfeBaseHref = getBaseHrefOfRoute(route)

const baseHrefSegmentAmount = mfeBaseHref.split('/').filter((value) => value).length
return url.slice(baseHrefSegmentAmount)
Expand Down

0 comments on commit cadbc6a

Please sign in to comment.