How to cache response based on some flag in service worker - service-worker

I am new in progressive web application and don't know how to cache responses based on the content-type e.g. application/ javascript , image/jpg like this.
I don't want to cache request based on url's i want to cache them using some flag which is available in response headers
How can i check this. Please guide with some code snippet.
Help me out guys...thanks in advance

I found this, we will fetch the request and then get content type from headers and also get flags from response headers and based on these value we can decide whether we need to cache that request or not

Related

How to cache the data from post request

The API request is both post ,I wanna cache the data so as to reduce to load the same data per time.Now I have no idea, pls give me some advices about it .I know I can cache the data if the request is GET....
You can cache the response for any type of request:
Steps are below
First, You has to save response in share instance class.
Whenever you want to hit the API then first check that you have a pre-response that you have saved last in share instance class. If yes then show this response and update response on the success of API. No need to show loading.

Sending parameter with POST requests ,when content-type is multipart/form-data

I am new to JMeter. I am trying to create a test plan ,one of the requests is a POST request containing some parameter, the content type in the request header is Multipart/Form-data.
I am copying the headers/parameters from fiddler because the HTTP proxy recorder is not working.
Please see the image for the current settings I have.I am not able to get the required response using it.
As per HTTP Request Sampler Documentation
Use multipart/form-data for HTTP POST
Use a multipart/form-data or application/x-www-form-urlencoded post request
So all you need to do is:
Tick "Use multipart/form-data for POST" box
Remove all `Content-Disposition" lines
In regards to "proxy recorder not working", I have never experienced any problems with it so it might be misconfiguration or something like this. Some people find JMeter Chrome Extension easier to use.
Instead of copying the content-disposition etc, just send the parameters with name and you should be good. You are expected to send form data and it's value.
ideally it should look like, name should be just 'form' and it's value as 'buy-now'.
I would suggest you compare the requests that you are sending using developer tools and the request you are sending using JMeter, it will help you debug this quicker.
I hope it helps.

Authorization when using plupload to upload images with Breeze

I'm using plupload in my Breeze/Durandal application to upload photos.
As part of configuring plupload in a given viewmodel, I set its url configuration property to point to an action on a Web API controller that is decorated with BreezeController and Authorize.
Examining the network trace in the browser developer tools, I see that the user's authorization info doesn't make it into the request. This actually makes sense, because the ajax isn't happening within breeze.
Any thoughts on how to properly secure this call to upload the image? I'd like access to the authenticated user id, otherwise I would have just let it be anonymous.
Thank you!
UPDATE: There's a headers option which allows you to set the request headers. Missed this when I was going through the documentation initially.
Are you just trying to tunnel an AJAX request through Breeze? This can be done using the Breeze.AjaxPost.js adapter.
http://www.breezejs.com/documentation/breezeajaxpostjs
This would be used similar to a query except that you would attach a payload somehow -
var payload = JSON.stringify(payload);
// Query to post your payload
var query = breeze.EntityQuery
.from('api/postsomething')
.withParameters({
$method: 'POST',
$encoding: 'JSON',
$data: payload
});
return manager.executeQuery(query).then(saveSucceeded).fail(saveFailed);
}
Now, this example is stringifying some JSON to post to the server, how you would do this with an image is beyond me but this should hopefully get you started.
In fact, I forgot about this answer which should provide some extra resources if you need them - breeze 1.4.8 & angular ajax adapter: how to customize ajax settings?
There's a headers option which allows you to set the request headers. Missed this when I was going through the documentation initially.

How to know If the request came from Cache or not using AFNetworking

This is my first day to me with AFNetworking, I like it :)
I've only one question,
I've a request and I cached the response, and set the caching policy,
How I could know whether the response data is coming from the cache side or the data is loaded from the originating source ?
I need something like
NSLog(#"Cach Used : %#", [request didUseCachedResponse] ? #"YES" : #"NO");
Thanks for your help :)
There is no way to check in programatically if the response coming from cache or not, if the cache policy is set correctly for hhtp requests, your server response contains a valid Cache-Control header, responses will be automatically cached for subsequent requests. If the response comes from the cache it will be fraction earlier than from the server. Here is the link which can give you further info link and AFNetworking FAQ

url with question mark considered as new http request?

I am optimization my web page by implementing caching, so if I want the browser not to take data from cache, then I will append a dynamic number as query value.
eg: google.com?val=823746
But some time, if I want to bring data from cache for the below url, the browser is making a new http request to server, its not taking data from cache. Is that because of the question mark in URL ?
eg: http://google.com?
Please provide some reference document link.
Thanks in advance.
Regards,
Navin
Use appropriate HTTP headers.
Search of pragma: no-cache and Expires
Browsers may not cache URLs that contain a query string (part after ? ) unless the headers indicate the expiry time explicitly.
Cache policy is not same across all browsers. If you don't specify appropriate headers the results may be even more unpredictable.
Since query strings are used with dynamically generated pages, the browser may take that hint and fire a new request even if the query string is same.
For example, a desktop browser may err on side of caution and fire a new request. On the other hand a mobile browser with aggressive cache policy may pull the page from cache.

Resources