Skip to content

Commit 4b3113d

Browse files
authored
fix(site-selector) prevent set current site undefined #32019 (#33204)
This pull request improves the robustness of the `SiteService` in `core-web/libs/dotcms-js/src/lib/core/site.service.ts` by ensuring that only valid (non-null/undefined) site objects are processed in key methods. This helps prevent potential errors caused by invalid site data being emitted or used. **Improvements for site data validation and type safety:** * Added the `filter` operator to RxJS pipelines in several methods (`requestCurrentSite`, `getCurrentSite`, and `loadCurrentSite`) to ensure only truthy (valid) `site` objects are processed. [[1]](diffhunk://#diff-ebec7159cb4a8660492c16c6797f79029bb5fedea93d4bbad8f44c84729ca61cR193) [[2]](diffhunk://#diff-ebec7159cb4a8660492c16c6797f79029bb5fedea93d4bbad8f44c84729ca61cL206-R207) [[3]](diffhunk://#diff-ebec7159cb4a8660492c16c6797f79029bb5fedea93d4bbad8f44c84729ca61cL225-R229) * Updated the type of the `urls` property to a more specific object structure for better type safety. * Included the `filter` operator in the RxJS import statement.
1 parent ddd0579 commit 4b3113d

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

core-web/libs/dotcms-js/src/lib/core/site.service.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Observable, Subject, merge, of } from 'rxjs';
22

33
import { Injectable, inject } from '@angular/core';
44

5-
import { map, pluck, startWith, switchMap, take, tap } from 'rxjs/operators';
5+
import { filter, map, pluck, startWith, switchMap, take, tap } from 'rxjs/operators';
66

77
import { CoreWebService } from './core-web.service';
88
import { DotcmsEventsService } from './dotcms-events.service';
@@ -26,7 +26,7 @@ export class SiteService {
2626
private loggerService = inject(LoggerService);
2727

2828
private selectedSite: Site;
29-
private urls: any;
29+
private urls: { currentSiteUrl: string; sitesUrl: string; switchSiteUrl: string };
3030
private events: string[] = [
3131
'SAVE_SITE',
3232
'PUBLISH_SITE',
@@ -203,7 +203,7 @@ export class SiteService {
203203
getCurrentSite(): Observable<Site> {
204204
return merge(
205205
this.selectedSite ? of(this.selectedSite) : this.requestCurrentSite(),
206-
this.switchSite$
206+
this.switchSite$.pipe(filter((site) => !!site))
207207
);
208208
}
209209

@@ -222,7 +222,10 @@ export class SiteService {
222222

223223
private loadCurrentSite(): void {
224224
this.getCurrentSite()
225-
.pipe(take(1))
225+
.pipe(
226+
take(1),
227+
filter((site) => !!site)
228+
)
226229
.subscribe((currentSite: Site) => {
227230
this.setCurrentSite(currentSite);
228231
});

0 commit comments

Comments
 (0)