简体   繁体   English

如何有条件地禁用客户端

[英]How to conditionally disable client side

I have a server-side rendered app (with transition) and I have a 404 page that I've put in a lazy module (so I don't increase loaded Javascript size). 我有一个服务器端呈现的应用程序(有过渡),我有一个404页面,我已经放入一个懒惰的模块(所以我不增加加载的Javascript大小)。

This is working fine, but there is some flickering when the client side takes over, which I think is due to the JS chunk loading after the main chunk has finished loading. 这工作正常,但是当客户端接管时有一些闪烁,我认为这是由于主块完成加载后的JS块加载。

Given this page only contains static links and one routerLink , the client side is not needed at all and I'm wondering if there's a way to disable the client side on certain URLs 鉴于此页面只包含静态链接和一个routerLink ,根本不需要客户端,我想知道是否有办法在某些URL上禁用客户端

Edit: as suggested in the comments I tried to conditionally bootstrap the application. 编辑:正如评论中建议的那样,我试图有条件地引导应用程序。 I can't use the URL as a condition because the server responds 404 on the non-existing URL (there is no redirect at this point). 我不能将URL用作条件,因为服务器对不存在的URL响应404(此时没有重定向)。 So the most appropriated thing to use is the TransferState which i successfully set up on server side and it generates some output just before the closing body tag. 因此,最适合使用的是我在服务器端成功设置的TransferState ,它在关闭body标记之前生成一些输出。

Unfortunately I didn't find any way to properly instantiate TransferState in main.ts because it's supposed to be imported via BrowserTransferStateModule and I see no possible way to import an Angular module in the main.ts file. 不幸的是我没有找到任何方法在main.ts正确实例化TransferState ,因为它应该通过BrowserTransferStateModule导入,我看不到在main.ts文件中导入Angular模块的可行方法。

I tried to instantiate it directly with the initTransferState function but it appears it's not designed to be used outside the module since i get an error : 我尝试使用initTransferState函数直接实例化它,但它似乎不是设计为在模块外部使用,因为我收到一个错误:

ERROR in ./src/main.ts
Module not found: Error: Can't resolve '@angular/platform-browser/src/browser/transfer_state' in 'G:\Documents\Projets\Myapp\Sources\master\frontend\src'

Any idea other than doing some dirty deserialization? 除了做一些肮脏的反序列化之外的任何想法?

I think this is what you are looking for: 我认为这就是你要找的东西:

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

export class MyComponent implements OnInit{
    constructor(@Inject(PLATFORM_ID) private platform: any) { }

    ngOnInit() {
        if (isPlatformBrowser(this.platform)) {
            // use clientside stuff
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM