Skip to content

Commit df2beb0

Browse files
committed
Always load NodeHttpClient to fix SSR
1 parent 19fa263 commit df2beb0

File tree

4 files changed

+36
-17
lines changed

4 files changed

+36
-17
lines changed

src/SignalR/clients/ts/signalr/src/DefaultHttpClient.ts

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,9 @@
44
import { AbortError } from "./Errors";
55
import { HttpClient, HttpRequest, HttpResponse } from "./HttpClient";
66
import { ILogger } from "./ILogger";
7+
import { NodeHttpClient } from "./NodeHttpClient";
78
import { XhrHttpClient } from "./XhrHttpClient";
89

9-
let nodeHttpClientModule: any;
10-
if (typeof XMLHttpRequest === "undefined") {
11-
// In order to ignore the dynamic require in webpack builds we need to do this magic
12-
// @ts-ignore: TS doesn't know about these names
13-
const requireFunc = typeof __webpack_require__ === "function" ? __non_webpack_require__ : require;
14-
nodeHttpClientModule = requireFunc("./NodeHttpClient");
15-
}
16-
1710
/** Default implementation of {@link @aspnet/signalr.HttpClient}. */
1811
export class DefaultHttpClient extends HttpClient {
1912
private readonly httpClient: HttpClient;
@@ -24,10 +17,8 @@ export class DefaultHttpClient extends HttpClient {
2417

2518
if (typeof XMLHttpRequest !== "undefined") {
2619
this.httpClient = new XhrHttpClient(logger);
27-
} else if (typeof nodeHttpClientModule !== "undefined") {
28-
this.httpClient = new nodeHttpClientModule.NodeHttpClient(logger);
2920
} else {
30-
throw new Error("No HttpClient could be created.");
21+
this.httpClient = new NodeHttpClient(logger);
3122
}
3223
}
3324

src/SignalR/clients/ts/signalr/src/NodeHttpClient.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,36 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4-
import * as Request from "request";
4+
// @ts-ignore: This will be removed from built files and is here to make the types available during dev work
5+
import * as Request from "@types/request";
56

67
import { AbortError, HttpError, TimeoutError } from "./Errors";
78
import { HttpClient, HttpRequest, HttpResponse } from "./HttpClient";
89
import { ILogger, LogLevel } from "./ILogger";
910
import { isArrayBuffer } from "./Utils";
1011

12+
let requestModule: Request.RequestAPI<Request.Request, Request.CoreOptions, Request.RequiredUriUrl>;
13+
if (typeof XMLHttpRequest === "undefined") {
14+
// In order to ignore the dynamic require in webpack builds we need to do this magic
15+
// @ts-ignore: TS doesn't know about these names
16+
const requireFunc = typeof __webpack_require__ === "function" ? __non_webpack_require__ : require;
17+
requestModule = requireFunc("request");
18+
}
19+
1120
export class NodeHttpClient extends HttpClient {
1221
private readonly logger: ILogger;
13-
private readonly request: Request.RequestAPI<Request.Request, Request.CoreOptions, Request.RequiredUriUrl>;
22+
private readonly request: typeof requestModule;
1423
private readonly cookieJar: Request.CookieJar;
1524

1625
public constructor(logger: ILogger) {
1726
super();
27+
if (typeof requestModule === "undefined") {
28+
throw new Error("error");
29+
}
30+
1831
this.logger = logger;
19-
this.cookieJar = Request.jar();
20-
this.request = Request.defaults({ jar: this.cookieJar });
32+
this.cookieJar = requestModule.jar();
33+
this.request = requestModule.defaults({ jar: this.cookieJar });
2134
}
2235

2336
public send(httpRequest: HttpRequest): Promise<HttpResponse> {
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { HttpClient, HttpResponse } from "./HttpClient";
2+
import { ILogger } from "./ILogger";
3+
4+
export class NodeHttpClient extends HttpClient {
5+
// @ts-ignore
6+
public constructor(logger: ILogger) {
7+
super();
8+
}
9+
10+
public send(): Promise<HttpResponse> {
11+
throw new Error("Method not implemented.");
12+
}
13+
}

src/SignalR/clients/ts/webpack.config.base.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@ module.exports = function (modulePath, browserBaseName, options) {
4040
},
4141
resolve: {
4242
extensions: [".ts", ".js"],
43-
alias: options.alias,
43+
alias: {
44+
"./NodeHttpClient": path.resolve(__dirname, "signalr/src/empty.ts"),
45+
...options.alias,
46+
}
4447
},
4548
output: {
4649
filename: `${browserBaseName}.js`,
@@ -73,7 +76,6 @@ module.exports = function (modulePath, browserBaseName, options) {
7376
}),
7477
// ES6 Promise uses this module in certain circumstances but we don't need it.
7578
new webpack.IgnorePlugin(/vertx/),
76-
new webpack.IgnorePlugin(/NodeHttpClient/),
7779
new webpack.IgnorePlugin(/eventsource/),
7880
],
7981
externals: options.externals,

0 commit comments

Comments
 (0)