[英]Google can't crawl generated CSS and JS paths originating from Custom outputcache
我已经为我的网站设置了自定义Outputcache。 一切都按预期工作,我可以看到带有二进制文件的缓存文件夹。 当我访问该网站时,我得到了缓存的页面,并按应有的方式呈现。
问题是,当我尝试使用Google网站管理员工具渲染页面时,Google无法访问在BundleConfig〜 ~/bundles/styles/maincss/
生成的生成的CSS路径,JavaScript路径也是如此。 当我访问这两个路径时,我会看到缩小的JS和CSS文件,并且浏览器确实正确呈现了页面。
这带来了一个问题,因为现在当我使用移动测试工具测试页面时,我发现页面不适合移动设备使用。 出于某种原因,Google无法访问这些路径,尽管当我在网站站长工具中运行网络抓取时,它确实对用户有利,但对Google-bot却没有好处。
当我不使用自定义输出缓存,而仅使用默认Outputcache时,此方法有效。
任何想法如何解决这个问题。
自定义outputcache代码:
Web.config:
</connectionStrings>
<appSettings>
<add key="CacheLocation" value="~/Cache"/>
</appSettings>
<system.web>
<caching>
<outputCache defaultProvider="FileCache">
<providers>
<add name="FileCache" type="ProjectX.FileCacheProvider"/>
</providers>
</outputCache>
</caching>
缓存类:
using System;
using System.Configuration;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Web;
using System.Web.Caching;
namespace ProjectX
{
[Serializable]
public class CacheItem
{
public object Item { get; set; }
public DateTime Expiry { get; set; }
}
public class FileCacheProvider : OutputCacheProvider
{
private string CacheLocation
{
get
{
string strCacheLocation = ConfigurationManager.AppSettings["CacheLocation"];
strCacheLocation = HttpContext.Current.Server.MapPath(strCacheLocation);
return strCacheLocation + @"\";
}
}
private string GetFullPathForKey(string key)
{
string temp = key.Replace('/', '$');
return CacheLocation + temp;
}
public override object Add(string key, object entry, DateTime utcExpiry)
{
object obj = this.Get(key);
if (obj != null)
{
return obj;
}
else
{
this.Set(key, entry, utcExpiry);
return entry;
}
}
public override void Remove(string key)
{
string filePath = GetFullPathForKey(key);
if (File.Exists(filePath))
{
File.Delete(filePath);
}
}
public override object Get(string key)
{
string filePath = GetFullPathForKey(key);
if (!File.Exists(filePath))
{
return null;
}
CacheItem item = null;
FileStream fileStream = File.OpenRead(filePath);
BinaryFormatter formatter = new BinaryFormatter();
item = (CacheItem)formatter.Deserialize(fileStream);
fileStream.Close();
if (item == null || item.Expiry <= DateTime.UtcNow)
{
Remove(key);
return null;
}
return item.Item;
}
public override void Set(string key, object entry, DateTime utcExpiry)
{
string filePath = GetFullPathForKey(key);
CacheItem item = new CacheItem { Expiry = utcExpiry, Item = entry };
FileStream fileStream = File.OpenWrite(filePath);
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(fileStream, item);
fileStream.Close();
}
}
}
如果您希望沿着这条路继续前进(我假设您已使用https://weblogs.asp.net/gunnarpeipman/asp-net-4-0-writing-custom-output-cache-providers作为起点?),您将需要更改GetFullPathForKey
以便它生成有效的文件名。 如何从路径和文件名中删除非法字符? 可能会帮助您做到这一点。 您还需要更改代码,以防止两个线程同时尝试写入同一文件时代码不会掉落。 另外,您确实应该引入MemoryCache
的用法,以避免在每次对类进行Get
调用时都访问文件系统。 这将是很多工作。
我建议考虑将这些NuGet软件包作为替代方案。 他们开箱即用地完成了这些工作,并且经过了充分的测试:
或只是在Web服务器前重做一个反向代理,以帮助进行缓存-例如http://mikehadlow.blogspot.com.au/2013/05/the-benefits-of-reverse-proxy.html 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.