dougmoscrop / serverless-http

Use your existing middleware framework (e.g. Express, Koa) in AWS Lambda 🎉
Other
1.74k stars 167 forks source link

No content-type header for requests checking for cache expiration (304) #55

Closed huksley closed 6 months ago

huksley commented 6 years ago

If client issues requests with if-modified-since': 'Tue, 01 Jan 1980 00:00:00 GMT' i.e. to check whether resource have had been updated, there is no content-type header generated, but binary handler still being called:

  binary: headers => {
    const ct = headers['content-type'];
    if (ct === undefined) {
      console.error("No content-type header: " + JSON.stringify(headers));
      return false;
    }
    return String(ct).match(/text\/.*/) || ct == "application/json" ? false : true;
  },

resulting in multiple errors "No content-type header" in logs/CloudWatch.

dougmoscrop commented 6 years ago

Youre suggesting that we should not ask about binary if there's no content type?

huksley commented 6 years ago

Currently, 304 requests processed wrong, responses have application/json Content-Type? Anyways I resorted into disabling etag in ExpressJS, resulting in no If-modified-Since sent by browser.

I think If-Modified-Since must be handled differently. Of course binary must be called only if ExpressJS actually give out body in handler.

// Disable 304 support, works wrong IMO
app.set('etag', false);
// Always send last-modified as current time
app.get('/*', function(req, res, next){ 
  res.setHeader('Last-Modified', (new Date()).toUTCString());
  next(); 
});

https://github.com/huksley/todo-react-ssr-serverless/

huksley commented 6 years ago

Just a thought... Maybe we can do the following:

This way, all static resources will be handled by S3 bucket

image

P.S. best way is to skip API GW entirely and serve them from S3 bucket/CDN directly

dougmoscrop commented 6 years ago

I mean yeah that sounds like something that could be a serverless plugin - it's def. out of scope for this library.

For the content-type stuff, it's always good to just set up an express app first and verify how it behaves, on its own.