Skip to content

Commit 80f740a

Browse files
Revert "Webpack HMR EventSource requests are now proxied (rather than redirected) to the local HMR server" because of 'ECANCELED'/'EPIPE broken pipe' issues. Awaiting feedback from Kestrel team.
1 parent f071590 commit 80f740a

File tree

3 files changed

+21
-22
lines changed

3 files changed

+21
-22
lines changed

src/Microsoft.AspNetCore.SpaServices/Webpack/ConditionalProxyMiddleware.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ public ConditionalProxyMiddleware(
3030
_pathPrefix = pathPrefix;
3131
_options = options;
3232
_httpClient = new HttpClient(new HttpClientHandler());
33-
_httpClient.Timeout = _options.RequestTimeout;
3433
}
3534

3635
public async Task Invoke(HttpContext context)
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
1-
using System;
2-
31
namespace Microsoft.AspNetCore.SpaServices.Webpack
42
{
53
internal class ConditionalProxyMiddlewareOptions
64
{
7-
public ConditionalProxyMiddlewareOptions(string scheme, string host, string port, TimeSpan requestTimeout)
5+
public ConditionalProxyMiddlewareOptions(string scheme, string host, string port)
86
{
97
Scheme = scheme;
108
Host = host;
119
Port = port;
12-
RequestTimeout = requestTimeout;
1310
}
1411

1512
public string Scheme { get; }
1613
public string Host { get; }
1714
public string Port { get; }
18-
public TimeSpan RequestTimeout { get; }
1915
}
2016
}

src/Microsoft.AspNetCore.SpaServices/Webpack/WebpackDevMiddleware.cs

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@
77
using Microsoft.AspNetCore.Hosting;
88
using Microsoft.Extensions.PlatformAbstractions;
99
using Newtonsoft.Json;
10-
using System.Threading;
1110

1211
// Putting in this namespace so it's always available whenever MapRoute is
1312

1413
namespace Microsoft.AspNetCore.Builder
1514
{
1615
public static class WebpackDevMiddleware
1716
{
17+
private const string WebpackDevMiddlewareScheme = "http";
18+
private const string WebpackHotMiddlewareEndpoint = "/__webpack_hmr";
1819
private const string DefaultConfigFile = "webpack.config.js";
1920

2021
public static void UseWebpackDevMiddleware(
@@ -61,27 +62,30 @@ public static void UseWebpackDevMiddleware(
6162
JsonConvert.SerializeObject(devServerOptions)).Result;
6263

6364
// Proxy the corresponding requests through ASP.NET and into the Node listener
64-
// Anything under /<publicpath> (e.g., /dist) is proxied as a normal HTTP request with a typical timeout (100s is the default from HttpClient),
65-
// plus /__webpack_hmr is proxied with infinite timeout, because it's an EventSource (long-lived request).
66-
appBuilder.UseProxyToLocalWebpackDevMiddleware(devServerInfo.PublicPath, devServerInfo.Port, TimeSpan.FromSeconds(100));
67-
appBuilder.UseProxyToLocalWebpackDevMiddleware("/__webpack_hmr", devServerInfo.Port, Timeout.InfiniteTimeSpan);
68-
}
69-
70-
private static void UseProxyToLocalWebpackDevMiddleware(this IApplicationBuilder appBuilder, string publicPath, int proxyToPort, TimeSpan requestTimeout)
71-
{
7265
// Note that this is hardcoded to make requests to "localhost" regardless of the hostname of the
7366
// server as far as the client is concerned. This is because ConditionalProxyMiddlewareOptions is
7467
// the one making the internal HTTP requests, and it's going to be to some port on this machine
7568
// because aspnet-webpack hosts the dev server there. We can't use the hostname that the client
7669
// sees, because that could be anything (e.g., some upstream load balancer) and we might not be
7770
// able to make outbound requests to it from here.
78-
// Also note that the webpack HMR service always uses HTTP, even if your app server uses HTTPS,
79-
// because the HMR service has no need for HTTPS (the client doesn't see it directly - all traffic
80-
// to it is proxied), and the HMR service couldn't use HTTPS anyway (in general it wouldn't have
81-
// the necessary certificate).
82-
var proxyOptions = new ConditionalProxyMiddlewareOptions(
83-
"http", "localhost", proxyToPort.ToString(), requestTimeout);
84-
appBuilder.UseMiddleware<ConditionalProxyMiddleware>(publicPath, proxyOptions);
71+
var proxyOptions = new ConditionalProxyMiddlewareOptions(WebpackDevMiddlewareScheme,
72+
"localhost", devServerInfo.Port.ToString());
73+
appBuilder.UseMiddleware<ConditionalProxyMiddleware>(devServerInfo.PublicPath, proxyOptions);
74+
75+
// While it would be nice to proxy the /__webpack_hmr requests too, these return an EventStream,
76+
// and the Microsoft.AspNetCore.Proxy code doesn't handle that entirely - it throws an exception after
77+
// a while. So, just serve a 302 for those. But note that we must use the hostname that the client
78+
// sees, not "localhost", so that it works even when you're not running on localhost (e.g., Docker).
79+
appBuilder.Map(WebpackHotMiddlewareEndpoint, builder =>
80+
{
81+
builder.Use(next => ctx =>
82+
{
83+
var hostname = ctx.Request.Host.Host;
84+
ctx.Response.Redirect(
85+
$"{WebpackDevMiddlewareScheme}://{hostname}:{devServerInfo.Port.ToString()}{WebpackHotMiddlewareEndpoint}");
86+
return Task.FromResult(0);
87+
});
88+
});
8589
}
8690

8791
#pragma warning disable CS0649

0 commit comments

Comments
 (0)