Monday, 15 May 2017

Skip CloudFront cache for a request with session data

I'm running a NodeJS app (via Passenger) on EC2. I have an AWS CloudFront distribution pointing at the app.

I'm using express-session for the app to record a shopping basket. I have CloudFront configured to whitelist the session cookie, but requests to the shopping basket page are cached and not routed to the origin. When I add an item to my basket, it seems to work (eg. it increments a counter in the session store), but when reloading the page, the request never touches the origin server and so I don't see the basket.

I'm not sure how I should approach this: I've set CloudFront to pass on my cookies, but the set-cookie header is only provided when I first add an item to my basket (eg. this is what CloudFront varies caching on).

I don't want to simply save a cookie on every session request as this will effectively kill my caching. What's the best way to handle this case? I need this shopping basket page to skip the cache if there's session data, basically. Do I simply check for req.session and return a no-cache header? Or is there a better way?



via Matt Andrews

No comments:

Post a Comment