0

I have a script that needs some external information to work with. It fetches this using Ajax requests. So far so good.

However, the script needs some of it's data right from the start. So I have been pondering a few options to supply it with that initial data at page load time:

  1. Simplest: Just have it perform an Ajax request for the data right away. Downside of this is extra latency and more requests than strictly needed.
  2. Ugly: Add a small script fragment at HTML render time that provides the initial data
  3. Bad caching properties: Create the whole JS file dynamically and add the data right then.
  4. Impossible: Something with headers... but unfortunately it seems we can't access them (see e.g. this question). Doing the extra Ajax request is not useful here as in that case we might just as well use option #1.
  5. Something with cookies...
  6. Not tried yet: Create a dynamic 'initial-data.js' script whose sole purpose it is to load the initial data. This would at least only send the data when needed, but it would require all users of my script to include 2 script files instead of one.... Also it will cause an extra request...

I am trying out the 4th option of using cookies to transport the initial data but so far not having any success. What I am trying to do:

  • When the browser requests the .js file, have the server add a Set-Cookie header with the initial data in it in the response.
  • In the JS file, read out the cookie.

It doesn't work. It seems I need to set the cookie on the response for the .html instead of the .js for the browser to make it available to the script... That's too bad as it would involve adding the Set-Cookie header to each page, even though it's only needed by that particular piece of JS.

I was actually very happy with the solution I thought I found because it let me send the initial data along with the request for the script only to those pages that actually use the script... Too bad!

Is there any way to do what I'm trying to do using cookies, headers or some similar mechanism?

Do you guys have any tips for this situation?

Background: I am trying to write a semi-offline application. Semi-offline in that it should continue to work (apart from some functions that just need connectivity) when offline, but is expected to have periods with connectivity regularly. So I'm using local storage and synching with the server when possible.

To be able to have the client generate new items when offline, I am including an ID generator that gets handed out ID blocks by the server, consuming them as it generates ID's. The data I was trying to send to the script in a cookie is the initial list of ID blocks and some settings and looks like this:

/suid/suid.json:3:3:dxb,dyb,dzb
       ^        ^ ^      ^   
      url     min max   blocks

Where:

url = path to JSON for subsequent Ajax requests
min = minimum amount of ID blocks to keep in local storage
max = maximum amount of ID blocks to keep in local storage
blocks = comma separated list of ID blocks

The ID blocks are encoded as sort-of Base32 strings. I'm using a custom formatting schema because I want 53-bit ID's to be as short as possible in text format while still being easily human readable and write-able and URL-safe.

Community
  • 1
  • 1
Stijn de Witt
  • 40,192
  • 13
  • 79
  • 80
  • are you running in apache? You can dinamically set Cookies to some urls with apache, and use them after that in javascript. – Alejandro Teixeira Muñoz May 25 '15 at 13:56
  • 1
    cookies are normally a bad solution since it is adding additional data to every request. – epascarello May 25 '15 at 13:57
  • Is that so bad to have yet another request? I would use option 1. – Rudy May 25 '15 at 13:58
  • Maybe you could show us what are the datas you actually need and how they are used by you js..? – n00dl3 May 25 '15 at 14:00
  • You could serve one dynamic js file containing those data and your static file. – Hacketo May 25 '15 at 14:04
  • Bootstrap your data in your HTML and when the app has been hydrated with your data, remove it. Not ugly and the only sound way IMO. – Henrik Andersson May 25 '15 at 14:24
  • I'm running in WildFly (a Java EE container). I think (was hoping) I could remove the cookie client-side after picking up the data to prevent the cookie from being there on every request. @Rudy No it's not that bad to make an extra request, but if there is an elegant solution to prevent it I would like to be able to use it. Hacketo Yes true, I thought of that but forgot to mention in the options. I will edit my question to include it. Junius Rendel I was afraid you'd ask that :) I will edit in some background info. – Stijn de Witt May 25 '15 at 14:28
  • @limelights Are you talking about option 2? Could you explain a bit more? – Stijn de Witt May 25 '15 at 14:46

0 Answers0