Breeze Metadata request URL with cache bust
Asked Answered
C

2

6

I'm currently using Breeze.js (version 1.4.1) along with the HotTowel template, and everything is working wonderfully.

However, I was wondering if there is a way to have breeze include in the request sent to get the Metadata a configurable suffix, such as ?v=1.0.0.1, in a similar fashion to what I'm currently doing with require.js to bust the local cache during version changes.

This would provide the benefit of having users cache the metadata locally and avoid unnecessary requests for the same unchanged metadata. Even also have server-side caching and avoid the metadata generation altogether when possible.

For the actual WebApi caching I'm currently using WebApi.OutputCache so that would fit in nicely with this.

Cari answered 13/12, 2013 at 19:19 Comment(0)
C
6

Well, I've found one way of doing it, without any specific support from breeze, just WebApi routing.

I just basically changed the controller route to the following:

GlobalConfiguration.Configuration.Routes.MapHttpRoute(
            name: "BreezeApi",
            routeTemplate: "breeze/{appVersion}/{controller}/{action}"
        );

And then when creating the EntityManager, I do it as follows:

var manager = new breeze.EntityManager('breeze/' + appVersion + '/data');

Where appVersion has the incremental numeric version value.

This lets you later configure caching for the Metadata action, such as:

[HttpGet]
[CacheOutput(ClientTimeSpan = CLIENT_DURATION, ServerTimeSpan = SERVER_DURATION)]
public string Metadata()
{
    return _contextProvider.Metadata();
}

I'll leave the question open in case someone has a cleaner solution, or one implemented by breeze internally.

Cari answered 13/12, 2013 at 20:14 Comment(1)
That's a fascinating technique for versioning, Pablo. Thanks for sharing it.Leper
L
3

You might look at the Breeze documentation topic "Load metadata from script" which describes capturing metadata in a JavaScript file and loading it in your index.html next to other script. You could apply standard cache-busting techniques to such metadata.js and even distribute from a CDN if that floats your boat.

Leper answered 13/12, 2013 at 21:44 Comment(2)
Yes, I looked into that, however I didn't want a physically generated file to maintain nor to have the application alter its own physical structure during start-up since I'm deploying automatically through WebDeploy. Fortunately I found a quick solution as shown above that is working quite nicely. BTW, breeze is awesome! :DCari
That makes sense. In fact, it makes so much sense that I've taken the liberty of updating the breeze documentation (that I mentioned above) to point to this question and your answer here. Thanks for that :)Leper

© 2022 - 2024 — McMap. All rights reserved.