我正在尝试将缓存控制标头添加到wwwroot / dist中的.js和.css文件中。如果我将文件直接输出到wwwroot而不是从Webpack中输出到wwwroot / dist,则通过以下代码添加标题,而不会出现问题:
app.UseStaticFiles(new StaticFileOptions
{
OnPrepareResponse = ctx =>
{
const int durationInSeconds = 60 * 60 * 24 * 365;
ctx.Context.Response.Headers[HeaderNames.CacheControl] =
"public,max-age=" + durationInSeconds;
}
});
但是,此方法似乎忽略子文件夹。 因此,我在上面的代码下面直接添加了以下代码:
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(
Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "dist")),
RequestPath = "/dist",
OnPrepareResponse = ctx =>
{
const int durationInSeconds = 60 * 60 * 24 * 365;
ctx.Context.Response.Headers[HeaderNames.CacheControl] =
"public,max-age=" + durationInSeconds;
}
});
理想情况下,文件确实需要包含在“ dist”文件夹中,因为在源代码管理中会忽略该文件,并且输出文件包含哈希,因此我无法手动将它们添加到我的gitignore中。
此外,似乎从wwwroot的子文件夹提供的任何文件总是在请求标头中设置无缓存。
任何对此问题的建议将不胜感激。