Skip to content

UseStaticFiles conflicting with UseWebpackDevMiddleware #10076

@andyfurniss4

Description

@andyfurniss4

Describe the bug

I have set up Webpack for my project to compile TypeScript and Sass and do my bundling for me. I am using UseWebpackDevMiddleware from Microsoft.AspNetCore.SpaServices to enable Hot Module Replacement when developing locally. I am also using Webpack for G-Zip pre-compression. To serve my compressed files I am providing a method to the OnPrepareResponse property of StaticFileOptions and passing this into the UseStaticFiles middleware where I will intercept calls to files and serve the G-zipped versions manually.

I am encountering problems using these two pieces of middleware together. It seems that using them together means that certain files are not intercepteed by the Static Files middleware (i.e. my big JavaScript bundle that I desperately want to replace with the compressed version). If I add a breakpoint in to my custom OnPrepareResponse action, it will not be hit for my Webpack-compiled files. It is only hit for a couple of images and my favicon. This prevents me from doing what I need to do with regards to serving compressed files.

When I remove UseWebpackDevMiddleware from my Configure method, all works as expected and my JavaScript files are intercepted.

To Reproduce

Using .NET Core 2.1

File structure:

image

TypeScript files from js folder and Sass files from css folder are compiled into dist/main.js and dist/bundles.css.

Relevant code from Startup.cs

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
	if (env.IsDevelopment())
	{
		//app.UseBrowserLink();
		app.UseDeveloperExceptionPage();
		app.UseWebpackDevMiddleware(new WebpackDevMiddlewareOptions
		{
		   HotModuleReplacement = true
		});
	}
	else
	{
		app.UseHsts();
		app.UseHttpsRedirection();
		app.UseExceptionHandler("/Error");
	}

	app.UseSession();
	app.UseAuthentication();
	app.UseStaticFiles(StaticFileOptions);

	app.UseMvcWithDefaultRoute();
}

private StaticFileOptions StaticFileOptions
{
	get
	{
		return new StaticFileOptions
		{
			OnPrepareResponse = OnPrepareResponse
		};
	}
}

private void OnPrepareResponse(StaticFileResponseContext context)
{
	var mimeTypeProvider = new FileExtensionContentTypeProvider();
	var headers = context.Context.Response.Headers;
	var contentType = headers["Content-Type"];

	if (contentType != "application/x-gzip" && !context.File.Name.EndsWith(".gz"))
	{
		return;
	}

	var fileNameToTry = context.File.Name.Substring(0, context.File.Name.Length - 3);

	if (mimeTypeProvider.TryGetContentType(fileNameToTry, out var mimeType))
	{
		headers.Add("Content-Encoding", "gzip");
		headers["Content-Type"] = mimeType;
	}
}

Expected behavior

HMR to work using UseWebpackDevMiddleware as well as static files in dist folder to be intercepted to allow me to do what I need to do regarding compression and headers etc.

Screenshots

N/A

Additional context

N/A

Metadata

Metadata

Labels

area-mvcIncludes: MVC, Actions and Controllers, Localization, CORS, most templatesinvestigate

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions