CORS error until glitch site wakes up


#1

No ‘Access-Control-Allow-Origin’ header is present, says service worker console for the first request, and then after a few seconds second request goes fine.

I assume it happens due to glitch site sleeping at the time of the first request.

Is there any way to handle this other than to actually make two time separated requests?


#2

Also, is it not surprising the timeout is not applied and instead it cancels connection straight away?


#3

This is happening because of the loading page we added that is displayed until your project starts. You can avoid that by adding a Content-Type header to your request - then we won’t try to display the welcome page.


#4

Thank you, Tim. So I went to add that header and noticed I already had it, like this:

$.ajaxSetup({async:true, cache:false, timeout:9999,
dataType:‘text’, contentType:‘text/plain’, processData:false});

I then removed both dataType and contentType from the setup and it worked in that it didn’t cancel connection, but timed out after 10 seconds. Second request went fine as before.

I’m now waiting for glitch site to fall asleep and will try again, and maybe it will work, but I thought I should report what wappened so far in any case… second attempt timed out as well.

p23.glitch.me (sends request):

    $.ajaxSetup({async:true, cache:false, timeout:9999, processData:false});
    var appPath= 'https://pok.glitch.me';
    $.ajax(
    {
      url:appPath +'/lod', type:'GET',
      error:function(e, f)
      {
        adminInfo.innerText+= 'FAIL@client:'+ f +'\n';
      },
      success:function(r, s, x)
      {
        var d= r.replace(/\n|\r/g, '');
      }
    });

pok.glitch.me (serves response):

var ex= require('express'), ap= ex();
var wl= ['https://pokerica.github.io', 'https://p23.glitch.me'];

function sh(q,a)
{
  var o= q.headers.origin;
  if(wl.indexOf(o) < 0) { q.connection.destroy(); return; }  
  a.header({'Access-Control-Allow-Origin':o});
}
ap.get('/lod', function(q, a)
{
  sh(q,a);
  a.sendFile(__dirname + '/db.txt');
});

#5

Update…

After all it seems “processData:false” was the true troublemaker here, took that out too and now it works as desired/expected.

I could perhaps put back “dataType:‘text’, contentType:‘text/plain’”, but why would when it already works as it is.

Thanks again.