1

If I receive a request from a Spider, I kick off a Phantom JS process and render back dynamic HTML. (Using a OnExecuting filter and setting the ActionResult)

But the OutputCache filter is in place on this method as well and it is getting in the way!.

E.G:

step 1. Load page with normal user agent. (Output cache caches the URL) step 2. Load page with spider user agent. (the previous cached response is sent to the spider, and my Phantom JS filter never runs)

1 Answer 1

1

Use VaryByCustom to force a 'Cache Miss' when the request is from a Search Engine Crawler.

In your Controller/Action:

[OutputCache(VaryByCustom="Crawler")]
public ActionResult Index()
{
     // ...
     return View();
}

Then in your Global.asax:

public override string GetVaryByCustomString(HttpContext context, string arg)
{
    if (arg == "Crawler" && context.Request.Browser.Crawler)
           return Guid.NewGuid().ToString();

    return base.GetVaryByCustomString(context, arg);
}
Sign up to request clarification or add additional context in comments.

3 Comments

This only works if OutputCacheLocation.None or OutputCacheLocation.Server or NoStore = true atleast in MVC 5
@LostInComputer, reading the OP text the previous cached response is sent to the spider, it's obvious that he uses OutputCacheLocation.Server.
I may have misunderstood the problem. I was thinking that he wanted to disable server caching and remove client caching if requester is a crawler

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.