I am working on an Angular Blog app, I have implemented Server Side Rendering in my blog application. Problem I have encountered is, on my blog home page I can see all the blogs with its Source Code content by Clicking Ctrl + u that is coming from Node.js API, after clicking on selected blog it will redirect to that blog and show its content but not the HTML Source Code (which is stored in the Local Storage for reducing API calls, and I fetch it from Local Storage to show the selected Blog content).
I also read that SSR doesn't support window,document and location objects, and Local Storage is a window property so for using that i have follow this link https://stackoverflow.com/a/57781883.
After following all the tutorials and docs, I have tried every possible way but didn't worked for me. Help will be appreciated. Thanks in advance.
Below is my code attached
Home Component.html
<owl-carousel-o [options]="bannerSlider">
<ng-template carouselSlide>
<div class="sliderblogbox">
<img class="penci-image-holder" src="{{recentPost[0]?.featured_image}}">
<div class="sliderblogbox-text">
<h3><a (click)="showDetails(recentPost[0])">{{recentPost[0]?.title}}</a></h3> <span
class="daytime">{{recentPost[0]?.post_date * 1000 | date:'mediumDate'}}</span>
</div>
</div>
</ng-template>
<ng-template carouselSlide>
<div class="sliderblogbox">
<img class=" penci-image-holder" src={{recentPost[1]?.featured_image}}>
<div class="sliderblogbox-text">
<h3><a (click)="showDetails(recentPost[1])">{{recentPost[1]?.title}}</a></h3> <span
class="daytime">{{recentPost[1]?.post_date * 1000 | date:'mediumDate'}}</span>
</div>
</div>
</ng-template>
<ng-template carouselSlide>
<div class="sliderblogbox">
<img class=" penci-image-holder" src={{recentPost[0]?.featured_image}}>
<div class="sliderblogbox-text">
<h3><a (click)="showDetails(recentPost[0])">{{recentPost[0]?.title}}</a></h3> <span
class="daytime">{{recentPost[0]?.post_date * 1000 | date:'mediumDate'}}</span>
</div>
</div>
</ng-template>
<ng-template carouselSlide>
<div class="sliderblogbox">
<img class=" penci-image-holder" src={{recentPost[1]?.featured_image}}>
<div class="sliderblogbox-text">
<h3><a (click)="showDetails(recentPost[1])">{{recentPost[1]?.title}}</a></h3> <span
class="daytime">{{recentPost[1]?.post_date * 1000 | date:'mediumDate'}}</span>
</div>
</div>
</ng-template>
</owl-carousel-o>
Home Component.ts
bannerSlider: OwlOptions = {
loop: false,
navText: [''],
nav: true,
margin: 10,
dots: false,
responsive: {
0: {
items: 1
},
600: {
items: 2
},
1000: {
items: 2
}
}
};
relatedPost: OwlOptions = {
loop: true,
navText: [''],
nav: true,
margin: 15,
dots: false,
responsive: {
0: {
items: 1
},
600: {
items: 2
},
1000: {
items: 3
}
}
};
constructor(private _service: ServiceService, private router: Router) { }
recentPost = [];
ngOnInit(): void {
this.getRecentPost()
}
getRecentPost() {
let key = 'slider';
this._service.getRecentPostSlider(key).subscribe(res => {
if (!res.error) {
this.recentPost = res.data.data;
}
})
}
showDetails(post) {
localStorage.setItem('post', JSON.stringify(post));
this.router.navigate([`/${post.title_abbr}/`]);
}
BlogPost component.html
<div class="largeblog-area">
<div class="largeblog-content blogdetailtop">
<h6><a href=""> {{post?.category}} </a> </h6>
<h2>{{post?.title}}</h2>
<h5><span>written by {{post?.author}} </span> |
<span>{{post?.post_date * 1000 | date:'mediumDate'}}</span> </h5>
</div>
<div class="largeblogimg"><img alt="img" src="{{post?.featured_image}}"></div>
<div class="largeblog-content">
<p [innerHTML]="post?.description"></p>
<div class="post-tags">
<a href="#">fresh</a>
<a href="#">life</a>
<a href="#">style</a>
<a href="#">travel</a>
</div>
</div>
<div class="largeblog-bottombox">
<div class="largeblog-date">
<span><i class="fa fa-clock-o" aria-hidden="true"></i> July 5, 2017</span>
</div>
<div class="largeblog-social">
<ul>
<li><a href="" title="Like"><i class="fa fa-heart-o" aria-hidden="true"></i></a>
</li>
<li><a href="" title="Facebook"><i class="fa fa-facebook"
aria-hidden="true"></i></a> </li>
<li><a href="" title="Twitter"><i class="fa fa-twitter" aria-hidden="true"></i></a>
</li>
<li><a href="" title="Pinterest"><i class="fa fa-pinterest"
aria-hidden="true"></i></a> </li>
</ul>
</div>
</div>
</div>
BlogPost Component.ts
post: any;
constructor(private meta: Meta, private router: Router, private titleService: Title,
private globle: Globals, private _interaction: CommunicationService, private route: ActivatedRoute) {
this.post = JSON.parse(localStorage.getItem('post'));
if (this.post && this.post.meta_description !== '') {
this.meta.addTag({ name: 'title:', content: this.post.meta_description });
}
}
ngOnInit(): void {
console.log(this.post);
this.router.events.subscribe(event => {
if (event instanceof NavigationEnd) {
this.post = JSON.parse(localStorage.getItem('post'));
}
});
}