SquishIt
SquishIt copied to clipboard
Calling .RenderNamed() doesn't render BaseOutputHref into src when using .AsNamed() and .WithOutputBaseHref()
I'm trying to define a bundle somewhere in my app using .WithOutputBaseHref("http://my.cdn.com")
and saving it for later reference using .AsNamed("my-name", "/local/path.js")
.
Now, when I call Bundle.JavaScript().RenderNamed("my-name")
I get a script tag without the http://my.cdn.com
prepended to the value of its src
attribute.
I've investigated this problem quite thoroughly by now and the problem lies in the cache key: during the initial call of .AsNamed()
after .WithOutputBaseHref()
the rendered content is put into the cache with the key "http://my.cdn.com" + "js" + "my-name"
whereas in .RenderNamed("my-name")
the code checks only for a cache entry with key "js" + "my-name"
.
One possible solution would be not to use the BaseOutputHref
as part of the cache key so that the caller of .RenderNamed()
will never need to know about it. This unfortunately breaks the CanBundleJsVaryingOutputBaseHrefRendersIndependantUrl
test case but I was honestly wondering if the behavior specified there is really needed?!
Cheers, Oliver
Yeah I'm not sure we need that either - seems to me like if you're using a CDN, you're pretty much always going to be using it (at least for that particular bundle). That change came in a pull request IIRC - I will see if I can contact the user it came from, but I'm inclined to think that your proposed solution fits the more common use case. Maybe there is another way we can cover the need for varying the base href.
While investigating we should see if there are any similar issues with the cached bundles.
Shouldn't be a problem to get this fixed in the next release, thanks for pointing it out. I will actually need this to work in the nearish future ;)
Hello Alex,
thanks for following up! Would be great to sort it out the way you propose. Also, if I stumble upon any other issue I'll get back to you.
Cheers, Oliver
Gruß, Oliver Friedrich www.teamaton.com Blog: shades-of-orange.com teamaton UG (haftungsbeschränkt) Richard-Sorge-Straße 78, 10249 Berlin Registergericht: Amtsgericht Charlottenburg, HRB 130292 B Geschäftsführer: Andréj Telle
2012/5/10 Alex Ullrich < [email protected]
Yeah I'm not sure we need that either - seems to me like if you're using a CDN, you're pretty much always going to be using it (at least for that particular bundle). That change came in a pull request IIRC - I will see if I can contact the user it came from, but I'm inclined to think that your proposed solution fits the more common use case. Maybe there is another way we can cover the need for varying the base href.
While investigating we should see if there are any similar issues with the cached bundles.
Shouldn't be a problem to get this fixed in the next release, thanks for pointing it out. I will actually need this to work in the nearish future ;)
Reply to this email directly or view it on GitHub: https://github.com/jetheredge/SquishIt/issues/183#issuecomment-5636448
Hi Alex, and Oliver,
The code was modified for the following reason:
Typically CDNs will charge extra for the use of SSL (e.g. Akamai/Rackspace cloud files or Level3). In our case we wanted to use a CDN for all assets that were served using straight HTTP, but not over SSL (where we would just serve directly from the web server). So I wrote some code to detect SSL/HTTP and then dynamically insert the CDN URL with BaseOutputHref, dependant upon SSL/non-SSL connection status.
I've taken a quick look at the code, and I think it should be possible to encompass both use cases.
I will take a look over the next couple of days to see what can be done.
Best, Adam
Ok I read over the requirement from that pull request again, and it does seem to be a pretty valid use case (a CDN that doesn't support SSL, so he wants to use local files for SSL traffic). I was able to fix your problem, but it requires appending the BaseOutputHref (if set) to the beginning of the key used to retrieve the cached bundle.
public string RenderNamed(string name)
{
bundleState = GetCachedGroupBundle(name);
//this sucks
var fullName = (BaseOutputHref ?? "") + CachePrefix + name;
var content = bundleCache.GetContent(fullName);
if(content == null)
{
AsNamed(name, bundleState.Path);
return bundleCache.GetContent(CachePrefix + name);
}
return content;
}
I will leave a TODO in there to hopefully get this done in a cleaner fashion in the future but for now it should satisfy your needs.
It's worth noting that on the bundle you call .RenderNamed("name") from, .BaseOutputHref MUST be set for this to work. I know this may be sub-optimal but it'll have to do until we can come up with a better way to reconcile these two requirements.
I did add a way to set up a default BaseOutputHref to the configuration work that @laazyj has added (I'm working on some stuff for seamless integration with amazon s3, and wanted to have some code I can easily add via WebActivator) so this could make your life a bit easier.
You can use it in Global.asax.cs like so:
Bundle.ConfigureDefaults().UseDefaultOutputBaseHref("http://my.cdn.com");
There is a bug in what is currently on NuGet for the configuration work but it is fixed in the trunk. There should be a new package on NuGet soon though, maybe even a real 0.8.7
Hey @adamduke, thanks for getting back - sorry about the cross-posting. I put a relatively hacky fix in for now but hope to come up with a better way for these requirements to play nice together in the not too distant future. Let me know if you come up with anything :)
@adamduke: I'm wondering why you need the same file name for the bundle - since when using the CDN you'll serve the content from a different URL anyways. Why not simply use two different destination file names instead of one? Would this pose a problem?
Cheers, Oliver
http://tauday.com/
2012/5/11 Alex Ullrich <
[email protected]
>
> Hey @adamduke, thanks for getting back - sorry about the cross-posting. I
> put a relatively hacky fix in for now but hope to come up with a better way
> for these requirements to play nice together in the not too distant future.
> Let me know if you come up with anything :)
>
> ---
> Reply to this email directly or view it on GitHub:
> https://github.com/jetheredge/SquishIt/issues/183#issuecomment-5650128
>
>
I've been trying to build out better CDN support (s3/cloudfront in particular) and this issue keeps coming up. Because of the limits on cloudfront invalidation, I really need to be able to render bundles on app_start. I think it is fair to expect in this case that bundles rendered into the cache will keep their configuration options.
This won't happen till the next version but its coming - I am going to try to think of a good way to provide the functionality @adamduke needs via an extensibility point of some kind but I'm not really seeing it at the moment. I don't think 2 different destination files would be the end of the world.
Thoughts?