广告
返回顶部
首页 > 资讯 > 服务器 >JS Angular 服务器端渲染应用设置渲染超时时间​​​​​​​
  • 528
分享到

JS Angular 服务器端渲染应用设置渲染超时时间​​​​​​​

2024-04-02 19:04:59 528人浏览 八月长安
摘要

我们用 setTimeout 模拟一个需要 5 秒钟才能完成调用的 api: const express = require('express'); const

我们用 setTimeout 模拟一个需要 5 秒钟才能完成调用的 api

const express = require('express');
const app = express();
app.get('/api/fast', (req, res) => {
  console.log('fast endpoint hit');
  res.send({response: 'fast'});
});
app.get('/api/slow', (req, res) => {
  setTimeout(() => {
      console.log('slow endpoint hit');
      res.send({response: 'slow'});
  }, 5000);
});
app.listen(8081, () => {
  console.log('Listening');
});

然后新建一个 angular service,调用这个 /api/slow:

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
@Injectable({
 providedIn: 'root'
})
export class CustomService {
 constructor(private http: HttpClient) {}
 public getFast(): Observable<any> {
   return this.http.get<any>('http://localhost:8081/api/fast');
 }
 public getSlow(): Observable<any> {
   return this.http.get<any>('http://localhost:8081/api/slow');
 }
}

服务器端渲染模式下,等待这个 API 调用的返回,至少需要花费 5 秒钟。我们可以给这个 API 调用设置一个超时机制。如果服务器端渲染时超过我们指定的超时间隔,还没有得到 API 响应,我们就放弃这次 API 调用,让其在客户端渲染模式下继续进行。

我们使用 RouteResolver 来实现。

从 Angular route 框架导入 router

import { Resolve } from '@angular/router';

从 Angular common 开发包导入 Angular 运行环境监测的 API:

import { isPlatfORMBrowser } from '@angular/common';

导入 injection token,获得当前运行的 platform id:

import { Injectable, Inject, PLATFORM_ID } from '@angular/core';

新建一个 service class:

import { Injectable, Inject, PLATFORM_ID } from '@angular/core';
import { Resolve } from '@angular/router';
import { Observable, timer } from 'rxjs';
import { isPlatformBrowser } from '@angular/common';
import { CustomService } from './custom.service';
import { takeUntil } from 'rxjs/operators';
@Injectable({
 providedIn: 'root'
})
export class SlowComponentResolverService implements Resolve<any> {
 constructor(private service: CustomService, @Inject(PLATFORM_ID) private platformId: any) { }
 public resolve(): Observable<any> {
   if (isPlatformBrowser(this.platformId)) {
     return this.service.getSlow();
   }

如果当前应用运行于浏览器端,上图的 isPlatformBrowser(this.platformId) 返回 true,因此直接调用慢速 API.

否则创建一个 Observable,500 毫秒后发射值:

const watchdog: Observable<number> = timer(500);

我们将这个 watchDog Observable 通过 pipe 设置到 this.service.getSlow 的下游。这样,如果 this.service.getSlow() 返回的 Observable 在 500 毫秒之内不 emit 值的话,watchdog 就会向 Component push null 值,否则,API 的真实 response 会推送给 Component.

我们需要更新应用相关的 routing 代码来消费这个 Resolver.

给 slowComponent 分配一个 resolver:

const routes: Routes = [
{path: '', redirectTo: 'fast', pathMatch: 'full'},
{path: 'fast', component: FastComponent},
{path: 'slow', component: SlowComponent, resolve: {response: SlowComponentResolverService}}
];

在 slowComponent 的实现代码里,从分配的 Route resolver 里读取 API response 数据:

import { Component } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
@Component({
selector: 'app-slow',
template: `
  <p>
    Response is: {{response | JSON}}
  </p>
`,
styles: []
})
export class SlowComponent {

public response: any = this.router.snapshot.data.response;
constructor(private router: ActivatedRoute) {}
}

注意这里并没有直接访问 Route Resolver:this.router.snapshot.data.response

当 API 在 500 毫秒之内返回时,所有的 slowComponent 源代码都由服务器端生成:

当 API 500 毫秒未能成功返回数据,则客户端会再次调用该 API,然后在客户端完成渲染:

到此这篇关于JS Angular 服务器端渲染应用设置渲染超时时间的文章就介绍到这了,更多相关JS Angular 服务器端渲染内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: JS Angular 服务器端渲染应用设置渲染超时时间​​​​​​​

本文链接: https://www.lsjlt.com/news/152699.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作