Skip to main content

CORS, S3 and Chrome

There was an issue that I could not figure out what was going on until last minute of the release. I decided to write about it as the solution was very simple,it is a hint given to me by a genius, my fellow colleague.

Let me describe the issue first.

I wanted to generate a PDF in the browser and used Pdf make, which is an awesome javascript library, works well with data tables. There was a requirement to add the logo/image to the page header. The logo is already loaded on the page as and the image was stored in S3. When a user prints the PDF, it will download the image via ajax call(then cache it). So it was a simple implementation what I had to implement the ajax call and convert to image data URL and pass it to the pdfmake( Currently, pdf make only accepts base64 image dataurls as it is not asynchronous PDF generation ). Every time I try to print, Chrome throws "No 'Access-Control-Allow-Origin' header is present on the requested resource".

What could be the reason?.

It turns out that Chrome will cache the response for the performance if the same resource is requested multiple times. When the page loads it does not send Origin HTTP header and requests the image from S3 and assign to HTML image source. Once the user prints PDF then it will use the cached response. But the second request is an ajax call and it expects Allow Origin: * in the response header and it fails 😡. And Google won't fix it.

In the Amazon side, S3 sends the Allow Origin response only if it receives the Origin header in the request. Obviously, the first Image rendering call won't send the Origin header. There is no way to add the response headers in S3 side unless you use cloud front as a proxy. Amazon won't change their implementation in S3 as well.

After doing some research and trying a number of alternatives, my friend gave me a hint to fool the browser. For ajax call, we changed the URL to https://s3.fake.image.url.com?ver=foolme. Chrome suddenly decided not to cache it and it will request from the server again.  

So everyone was happy !!!.

Comments

Popular posts from this blog

Good Bye WSO2

Today is the last day of my beloved company WSO2. I joined here just passing out from the university and it is about one and half years that I have been working here. It is very much enjoyable and pleasure to work here. The culture in WSO2 is much different from all the other software companies in Sri Lanka. FREEDOM is the most important thing. We can do our work without any pressure because our senior management does not push us and they are very much helpful. The most important thing is that our CEO Dr.Sanjiva Weerawarana is very much friendly person. I have not seen such a management person with such kind of attitudes. He is giving every thing to his employee's what he is capable of. I am proud of him as Sri Lankan since he is keen on developing our country by motivating people to educate. And I should note about my boss Mr.Samisa Abeysinghe who has a huge experience in this industry. Since I had a chance to work under him I could gain much knowledge about software process. He

THE LAST LECTURE

Ultimately I was a able to read a non-fictional book till the end page. This is another great achievement in my life :) since I was used to read only the academic book which I was asked to read at university or I had read to gain some knowledge in programming. But my fiance is fond of reading.She urges me to read every day some times she gave some fictions to read but some how I didn't have continuous habit of reading :(.When I came to India last time I was certainly fascinated to the book of The Last Lecture by Randy Pausch .I just read about half of the book when I was in India last time unfortunately it took another two months to come back again read the other half.Ultimately I was able to finish to the book. It seems to me that this book has changed me a lot. Actually when I was reading the book , the feeling of doing higher studies came to my mind. And also it encourage to read a lot.And this time I have bought three books already,plan to buy more and definitely finish all