@@ -24,6 +24,8 @@ declare global {
2424/** this is needed, because otherwise the CLI borks while building */
2525const scullyBegin = '<!--scullyContent-begin-->' ;
2626const scullyEnd = '<!--scullyContent-end-->' ;
27+ const dropEndingSlash = ( str : string ) => ( str . endsWith ( '/' ) ? str . slice ( 0 , - 1 ) : str ) ;
28+
2729@Component ( {
2830 // tslint:disable-next-line: component-selector
2931 selector : 'scully-content' ,
@@ -42,7 +44,7 @@ const scullyEnd = '<!--scullyContent-end-->';
4244 encapsulation : ViewEncapsulation . None ,
4345 preserveWhitespaces : true ,
4446} )
45- export class ScullyContentComponent implements OnInit , OnDestroy {
47+ export class ScullyContentComponent implements OnDestroy , OnInit {
4648 elm = this . elmRef . nativeElement as HTMLElement ;
4749 /** placeholder */
4850 lastHandled : string ;
@@ -51,16 +53,18 @@ export class ScullyContentComponent implements OnInit, OnDestroy {
5153 /** monitor the router, so we can update while navigating in the same 'page' see #311 */
5254 routeUpdates$ = this . router . events . pipe (
5355 filter ( ev => ev instanceof NavigationEnd ) ,
56+ /** don't replace if we are already there */
57+ filter ( ( ev : NavigationEnd ) => this . lastHandled && ! this . lastHandled . endsWith ( ev . urlAfterRedirects ) ) ,
5458 tap ( r => this . replaceContent ( ) )
5559 ) ;
5660
5761 routeSub = this . routeUpdates$ . subscribe ( ) ;
5862
59- constructor ( private elmRef : ElementRef , private srs : ScullyRoutesService , private router : Router ) { }
63+ constructor ( private elmRef : ElementRef , private srs : ScullyRoutesService , private router : Router ) {
64+ /** do this from constructor, so it runs ASAP */
65+ }
6066
61- ngOnInit ( ) {
62- // /** make sure the idle-check is loaded. */
63- // this.idle.init();
67+ ngOnInit ( ) : void {
6468 if ( this . elm ) {
6569 /** this will only fire in a browser environment */
6670 this . handlePage ( ) ;
@@ -72,7 +76,7 @@ export class ScullyContentComponent implements OnInit, OnDestroy {
7276 * Will fetch the content from sibling links with xmlHTTPrequest
7377 */
7478 private async handlePage ( ) {
75- const curPage = location . href ;
79+ const curPage = dropEndingSlash ( location . href ) ;
7680 if ( this . lastHandled === curPage ) {
7781 /**
7882 * Due to the fix we needed for #311
@@ -129,8 +133,9 @@ export class ScullyContentComponent implements OnInit, OnDestroy {
129133 parent . insertBefore ( begin , this . elm ) ;
130134 parent . insertBefore ( template . content , this . elm ) ;
131135 parent . insertBefore ( end , this . elm ) ;
132- /** upgrade all hrefs to simulated routelinks */
133- document . querySelectorAll ( '[href]' ) . forEach ( this . upgradeToRoutelink . bind ( this ) ) ;
136+ /** upgrade all hrefs to simulated routelinks (in next microtask) */
137+ setTimeout ( ( ) => document . querySelectorAll ( '[href]' ) . forEach ( this . upgradeToRoutelink . bind ( this ) ) , 10 ) ;
138+ // document.querySelectorAll('[href]').forEach(this.upgradeToRoutelink.bind(this));
134139 }
135140
136141 /**
@@ -141,8 +146,9 @@ export class ScullyContentComponent implements OnInit, OnDestroy {
141146 */
142147 async upgradeToRoutelink ( elm : HTMLElement ) {
143148 const routes = await this . routes ;
144- const lnk = elm . getAttribute ( 'href' ) . toLowerCase ( ) ;
145- const route = routes . find ( r => r . route . toLowerCase ( ) === lnk ) ;
149+ const lnk = dropEndingSlash ( elm . getAttribute ( 'href' ) . toLowerCase ( ) ) ;
150+ const route = routes . find ( r => dropEndingSlash ( r . route . toLowerCase ( ) ) === lnk ) ;
151+
146152 /** only upgrade routes known by scully. */
147153 if ( lnk && route ) {
148154 elm . onclick = async ( ev : MouseEvent ) => {
@@ -175,6 +181,7 @@ export class ScullyContentComponent implements OnInit, OnDestroy {
175181 * the new content. handlePage() takes care of that.
176182 */
177183 /** delete the content, as it is now out of date! */
184+
178185 window . scullyContent = undefined ;
179186 const parent = this . elm . parentElement ;
180187 let cur = findComments ( parent , 'scullyContent-begin' ) [ 0 ] as ChildNode ;
0 commit comments