Breeze / breeze-client

Breeze for JavaScript clients
MIT License
38 stars 16 forks source link

How can I overwrite the metadata url? #84

Open jgtestw opened 5 months ago

jgtestw commented 5 months ago

Hi,

To cache the metadata in the browser, I want to be able to add a version number to the metadata url. What is the best way to overwrite the default metadata url? It defaults to the service name property. I am aware of the technique of generating metadata at compile time/start up time and writing it to a file. I'd rather not do that.

russelg commented 3 months ago

I had to do similar, this is what I ended up with. I'm using Angular, so this may not apply for you. All AjaxAdapter have requestInterceptor, so you can do this for non-angular as well. Here I'm adding a "?v=versionnumber" to the url, but you can also change the URL itself through the same method.

import { HttpRequest } from "@angular/common/http";
import { AjaxRequestInterceptor } from "breeze-client";
import { AjaxHttpClientAdapter } from "breeze-client/adapter-ajax-httpclient";
import { AjaxRequest } from "breeze-client/src/interface-registry";

const metadataCacheBustRequestInterceptor: AjaxRequestInterceptor = Object.assign(
    // AjaxRequest interface does not include the "request" param, it's added by AjaxHttpClientAdapter
    (req: AjaxRequest & { request: HttpRequest<null> }) => {
        // add a cache-busting query param for metadata
        if (req.request.url.endsWith("/Metadata")) {
            const params = req.request.params.set("v", "version string goes here!!!!!!!!!!");
            req.request = req.request.clone({ params });
        }
    },
    // will only run on the first request (i.e. Metadata)
    { oneTime: true },
);

// other breeze initialisation etc... goes here

// httpclient is an instance of angular HttpClient
const ajaxAdapter = AjaxHttpClientAdapter.register(httpClient);
ajaxAdapter.requestInterceptor = metadataCacheBustRequestInterceptor;
iz-iznogood commented 3 months ago

One approach I used when I implemented a POS system that needed to work fully offline was to cache the metadata (along with data) in indexed db and when I needed to load the metadata, i used the MetadataStore.importMetadata. However I was not on .Net so I am not sure if you can generate the metadata from .Net and send them to the server using another call (for caching) I also had to use a version field like you, the above approach worked