I attempted to create a straightforward app version check system by sending the current server version to the client in the HTTP header. If there's a newer version available, it should trigger a notification for the user to reload the application. Initially, I believed this would be a simple task, but my Angular interceptor is not capturing the new response header sent by ASP.NET Core. My setup involves using .NET 6 and Angular 13.
Startup.cs
app.Use(async (context, next) =>
{
context.Response.Headers.Add("X-AppVersion", Assembly.GetEntryAssembly()!.GetName().Version!.ToString());
await next();
});
app.UseSpa(spa =>
{
spa.Options.SourcePath = "ClientApp";
spa.Options.DefaultPageStaticFileOptions = new StaticFileOptions
{
OnPrepareResponse = context =>
{
var headers = context.Context.Response.GetTypedHeaders();
headers.CacheControl = new CacheControlHeaderValue
{
NoCache = true,
NoStore = true,
MustRevalidate = true,
MaxAge = TimeSpan.Zero
};
// Uncertain about this component
headers.Append("X-AppVersion", Assembly.GetEntryAssembly()!.GetName().Version!.ToString());
}
};
});
Angular interceptor
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const reqWithCredentials = req.clone({ withCredentials: true });
return next.handle(reqWithCredentials).pipe(
map(resp => {
if (resp instanceof HttpResponse) {
if (resp.headers.get('x-appversion')) {
console.log(resp.headers.get('x-appversion'));
} else {
console.log(null); // The result is consistently null
}
return resp;
}
})
)
}