Skip to content

Commit e697c56

Browse files
committed
simplify node configuration
1 parent 59c6aa8 commit e697c56

2 files changed

Lines changed: 23 additions & 23 deletions

File tree

TechStacks/Program.cs

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -99,26 +99,6 @@
9999
});
100100

101101
var app = builder.Build();
102-
103-
var nextServerBase = app.Environment.IsDevelopment()
104-
? new Uri("http://localhost:3000")
105-
: new Uri("http://127.0.0.1:3000");
106-
107-
var allowInvalidCertsForNext = false; // No HTTPS when proxying to Next internally
108-
109-
HttpMessageHandler nextHandler = allowInvalidCertsForNext
110-
? new HttpClientHandler
111-
{
112-
ServerCertificateCustomValidationCallback =
113-
HttpClientHandler.DangerousAcceptAnyServerCertificateValidator
114-
}
115-
: new HttpClientHandler();
116-
117-
var nextClient = new HttpClient(nextHandler)
118-
{
119-
BaseAddress = nextServerBase
120-
};
121-
122102
app.UseForwardedHeaders();
123103
app.UseWebSockets();
124104

@@ -140,6 +120,7 @@
140120
}
141121

142122
// After all .NET middleware has run, let Next.js handle 404s
123+
var nextClient = Proxy.CreateNodeClient();
143124
Proxy.MapNotFoundToNode(app, nextClient, ignorePaths:[
144125
"/api",
145126
"/auth",
@@ -174,14 +155,14 @@
174155
app.MapRazorPages();
175156
app.MapAdditionalIdentityEndpoints();
176157

177-
// Proxy development HMR WebSocket and fallback routes to the Next server
158+
// Proxy development HMR WebSocket and fallback routes to the Next.js server
178159
if (app.Environment.IsDevelopment())
179160
{
180161
app.Map("/_next/webpack-hmr", async context =>
181162
{
182163
if (context.WebSockets.IsWebSocketRequest)
183164
{
184-
await Proxy.WebSocketToNode(context, nextServerBase, allowInvalidCertsForNext);
165+
await Proxy.WebSocketToNode(context, nextClient.BaseAddress!, allowInvalidCerts:true);
185166
}
186167
else
187168
{
@@ -214,7 +195,7 @@
214195
}
215196
}
216197

217-
// Fallback: any unmatched route goes to Next.js
198+
// Fallback: Proxy any unmatched routes to Next.js
218199
app.MapFallback(context => Proxy.HttpToNode(context, nextClient));
219200

220201
app.Run();

TechStacks/Proxy.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,25 @@ public static class Proxy
5757

5858
private static ConcurrentDictionary<string, (string mimeType, byte[] data, string? encoding)> Cache { get; } = new();
5959

60+
public static HttpClient CreateNodeClient(string nodeBaseUrl="http://localhost:3000")
61+
{
62+
var allowInvalidCertsForNext = false; // No HTTPS when proxying to Next internally
63+
64+
HttpMessageHandler nextHandler = allowInvalidCertsForNext
65+
? new HttpClientHandler
66+
{
67+
ServerCertificateCustomValidationCallback =
68+
HttpClientHandler.DangerousAcceptAnyServerCertificateValidator
69+
}
70+
: new HttpClientHandler();
71+
72+
var nextClient = new HttpClient(nextHandler)
73+
{
74+
BaseAddress = new Uri(nodeBaseUrl)
75+
};
76+
return nextClient;
77+
}
78+
6079
public static bool TryStartNode(string workingDirectory, out Process process, string logPrefix="[node]")
6180
{
6281
process = new Process

0 commit comments

Comments
 (0)