Skip to content

Commit e53541d

Browse files
fix(lib): scully-content-component didn't update links in all cases (#417)
* fix(lib): scully-conent-component didn't update links in all cases fiex the case where links wheren't updated properly in content pages, and there was an extra load of an index.html on first visit * fix(lib): scullycontent don't load index on intial page load * fix(lib): move back to init * test(snapshots): update snapshot to reflect new situation Co-authored-by: Jorge Cano <jorgeucano@gmail.com>
1 parent ddf793c commit e53541d

2 files changed

Lines changed: 36 additions & 10 deletions

File tree

projects/scullyio/ng-lib/src/lib/scully-content/scully-content.component.ts

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ declare global {
2424
/** this is needed, because otherwise the CLI borks while building */
2525
const scullyBegin = '<!--scullyContent-begin-->';
2626
const 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;

src/__tests__/__snapshots__/blog-index.spec.ts.snap

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,25 @@ console.log('yah');</code></pre>
3434
<hr =\\"\\">
3535
<h4 =\\"\\">End of blog content</h4>
3636
37+
<span =\\"\\">
38+
<a =\\"\\" href=\\"/blog/2014/2/12/asdf/\\">/blog/2014/2/12/asdf</a>
39+
<br =\\"\\">
40+
</span><span =\\"\\">
41+
<a =\\"\\" href=\\"/blog/page-1/\\">/blog/page-1</a>
42+
<br =\\"\\">
43+
</span><span =\\"\\">
44+
<a =\\"\\" href=\\"/blog/page-3/\\">/blog/page-3</a>
45+
<br =\\"\\">
46+
</span><span =\\"\\">
47+
<a =\\"\\" href=\\"/blog/page-4/\\">/blog/page-4</a>
48+
<br =\\"\\">
49+
</span><span =\\"\\">
50+
<a =\\"\\" href=\\"/blog/page-5/\\">/blog/page-5</a>
51+
<br =\\"\\">
52+
</span><span =\\"\\">
53+
<a =\\"\\" href=\\"/blog/test/testing/\\">/blog/test/testing</a>
54+
<br =\\"\\">
55+
</span><!---->
3756
</app-blog><!----></main><footer =\\"\\"><h3 =\\"\\">Made with ❤️ <strong =\\"\\">@HeroDevs</strong></h3></footer></app-root>
3857
<script id=\\"ScullyIO-transfer-state\\"></script><script src=\\"runtime-es2015.js\\" type=\\"module\\"></script><script src=\\"runtime-es5.js\\" nomodule=\\"\\" defer=\\"\\"></script><script src=\\"polyfills-es5.js\\" nomodule=\\"\\" defer=\\"\\"></script><script src=\\"polyfills-es2015.js\\" type=\\"module\\"></script><script src=\\"main-es2015.js\\" type=\\"module\\"></script><script src=\\"main-es5.js\\" nomodule=\\"\\" defer=\\"\\"></script>
3958

0 commit comments

Comments
 (0)