Serverless-next.js: JSON data files not properly cached

Created on 2 Sep 2020  路  2Comments  路  Source: serverless-nextjs/serverless-next.js

Describe the bug
JSON data files for SSG pages (e.g. `_next/data/{build id}/file.json) are not being cached by Cloudfront, meaning that subsequent calls always go to the origin (and trigger the Lambdas@Edge).

To Reproduce

  1. Deploy a website with at least one SSG page.
  2. Visit the SSG page once. This should cache the JSON data.
  3. Refresh that page and observe the x-cache response header set to Miss from cloudfront

Expected behavior
On step 3 above, we should observe Hit from cloudfront

Additional context
During the upload of these files, no Cache Control is set, which I suspect is the underlying cause. There should be no concern about caching these for a long time, since the build ID is in the path and SSG data should not vary within a single build.

bug good first issue

Most helpful comment

Nice find. This would be a good case to cover for e2e tests I'm working on, we can inspect the x-cache header after hitting it a second time to ensure it says Hit from cloudfront

All 2 comments

Nice find. This would be a good case to cover for e2e tests I'm working on, we can inspect the x-cache header after hitting it a second time to ensure it says Hit from cloudfront

Looks like the cache-control header needs to be set, but also the CloudFront behavior for _next/data/* has a default, min and max TTL of 0, so cache-control header alone won't help.

We will need to fix the aws-cloudfront component to set these three values independently. In fact, it's also affecting the other CloudFront behaviors, e.g for static files, default/min/max TTL is always 86400, so even for static files they'll get evicted from the CloudFront edge cache after 1 day, even though we are setting Cache-Control for those in S3 for much longer (e.g 1 year for static JS files, 31 days for static pages).

Will work on a PR to fix it this week.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Vadorequest picture Vadorequest  路  5Comments

futuraprime picture futuraprime  路  6Comments

sidartha picture sidartha  路  6Comments

manojHDwork picture manojHDwork  路  4Comments

danielcondemarin picture danielcondemarin  路  4Comments