question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Pages with getServerSideProps always "Miss from cloudfront"

See original GitHub issue

Describe the bug

Hi, pages with getServerSideProps always “Miss from cloudfront” instead pages with getInitialProps correctly give “Hit from cloudfront”.

this is my default cache behaviour:

cloudfront:
      defaults:
        ttl: ${env.DEFAULT_BH_TTL}
        allowedHttpMethods: ['HEAD', 'GET']
        viewerProtocolPolicy: 'redirect-to-https'
        forward:
          headers: 'none'
          cookies: 'all'
          queryString: true
        lambda@edge:
          viewer-request: ${env.REWRITE_LAMBDA_ARN}

In the response there is:

Cache-Control: private, no-cache, no-store, max-age=0, must-revalidate

Versions

Checklist

  • You have reviewed the README and FAQs, which answers several common questions.
  • Please first try using the most recent latest or alpha @sls-next/serverless-component release version, which may have already fixed your issue or implemented the feature you are trying to use. Note that the old serverless-next.js component and the serverless-next.js plugin are deprecated and no longer maintained.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
dphangcommented, Jan 6, 2021

Yes this is expected by default as it should be server-side rendered and not cached as content is dynamic. This component does not set any cache-control headers for you for server-side rendered pages. I believe the cache-control header for pages with getServerSideProps is set by Next.js to private, no-cache, no-store, max-age=0, must-revalidate. So it won’t be cached by CloudFront (unless you had set a min TTL of more than 0).

I did notice however that if you use getInitialProps, Next.js doesn’t seem to set a cache control header. In which case, CloudFront will cache it for whatever TTLs that you set. If you had it to cache for 60 seconds or whatever as the default TTL, then it will be a hit from CloudFront for 60 seconds.

The workaround would be to set cache-control headers yourself in getServerSideProps if you wish to cache it for longer.

0reactions
pecoramcommented, Jan 11, 2021

thank you very much I understand!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Troubleshoot the CloudFront distribution “X-Cache:Miss from ...
To diagnose and troubleshoot a "X-Cache:Miss from CloudFront" response, check the following: Which edge location is receiving the requests?
Read more >
CloudFront headers in Next.js missing when navigating to page
The map will load using the defaults for the userLocation , because the CloudFront headers are coming through as undefined in getServerSideProps ......
Read more >
Data Fetching: getServerSideProps - Next.js
First, immediately show the page without data. Parts of the page can be pre-rendered using Static Generation. You can show loading states for...
Read more >
A Complete Guide To Incremental Static Regeneration (ISR ...
Incremental Static Regeneration (ISR) is a new evolution of the Jamstack, allowing you to update static content instantly without needing a ...
Read more >
Serverless Next.js Component - Serverless Framework: Plugins
Statically optimised pages compiled by next are served from CloudFront edge locations ... [x] Opt-in to server-side rendering (SSR) via getServerSideProps .
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found