Git Product home page Git Product logo

Comments (15)

pancho111203 avatar pancho111203 commented on May 30, 2024 1

Happy to help!

I looked at how serverless-next.js does this, and it seems to be the solution I mentioned above:

Basically they just copy all the html files from the nextjs output folder into a separate folder and then upload them to S3 to the root path.

from cdk-nextjs.

pancho111203 avatar pancho111203 commented on May 30, 2024

defaultTtl: Duration.seconds(0),

cachePolicy: cloudfront.CachePolicy.CACHING_DISABLED,

Is there any particular reason you disabled the cache on the lambda functions?

from cdk-nextjs.

khuezy avatar khuezy commented on May 30, 2024

The line you linked has the cachePolicy for the lambda (eg api), which we don't want to cache.

https://github.com/jetbridge/cdk-nextjs/blob/main/src/Nextjs.ts#L493, maybe something is wrong here?
Would changing it to... help?

'/': hasIndexHtml ? staticBehavior : lambdaBehavior ,

from cdk-nextjs.

pancho111203 avatar pancho111203 commented on May 30, 2024

Ah I see, if the same lambda is used for the API it cannot be cached.

I've done some more testing, and got the following results:

Using cdk-nextjs:

  • the html page download spends around 500ms waiting for a server response, and is not cached on CF. This time is much higher when the lambda function is cold (first attempt took 1.5s just waiting for a response)
  • the rest of the resources are cached, and take 30-60ms waiting for a server response.

Using vercel:

  • all the resources (including the html page) take 30-60ms waiting for a server response, so I assume they are all cached at the CDN level

A possible solution could be to store the static html pages in S3 as it's done with the resources. Then they will use the S3 Origin and benefit from the caching policies. But I'm not sure how that would work, as the path for the pages is different from the resource paths ('/' instead of '/_next/server/pages/index.html').

from cdk-nextjs.

pancho111203 avatar pancho111203 commented on May 30, 2024

The line you linked has the cachePolicy for the lambda (eg api), which we don't want to cache.

https://github.com/jetbridge/cdk-nextjs/blob/main/src/Nextjs.ts#L493, maybe something is wrong here? Would changing it to... help?

'/': hasIndexHtml ? staticBehavior : lambdaBehavior ,

I think this would help only on the /index.html page, but not on the others generated by nextjs.

from cdk-nextjs.

khuezy avatar khuezy commented on May 30, 2024

Thanks for doing the bench testing. I'm sure there's plenty to optimize. I'm been mostly testing local stuff.

from cdk-nextjs.

khuezy avatar khuezy commented on May 30, 2024

@revmischa, what do you think?

from cdk-nextjs.

revmischa avatar revmischa commented on May 30, 2024

I believe if you want to cache lambda responses it should be possible. You can specify what caching policy you want as part of the distribution config and enable it. I think we can enable GET/HEAD caching but with a default 0 TTL.

The idea is that you should be able to cache API responses, but you need to explicitly set the caching headers in your API response if you want that. I think s-maxage might be the cache-control option that tells CloudFront to cache it for everyone?

For static pages which are handled by NextJS I don't know what the best option is. I am not sure if sls-next.js's approach works with the standalone version of the output, it doesn't seem to output static HTML files. At least not in my setup.

from cdk-nextjs.

khuezy avatar khuezy commented on May 30, 2024

@pancho111203 The PR above adds caching to static pages. I'll be thoroughly testing this out before I mark it ready.

from cdk-nextjs.

pancho111203 avatar pancho111203 commented on May 30, 2024

@khuezy That's great to hear! I will re-do the bench testing and post it here as soon as it's merged.

from cdk-nextjs.

khuezy avatar khuezy commented on May 30, 2024

@pancho111203 are you using the new appDir or the old pages directory? If using the pages, are you exporting getStaticProps? You should be getting cached pages if you are exporting getStaticProps

from cdk-nextjs.

pancho111203 avatar pancho111203 commented on May 30, 2024

@khuezy In the tests I did a while ago I was using the pages dir without any static props, just a very basic page taken from a boilerplate, without any props.

from cdk-nextjs.

pancho111203 avatar pancho111203 commented on May 30, 2024

So you're saying that adding static props to the page enables caching?

from cdk-nextjs.

khuezy avatar khuezy commented on May 30, 2024

Yes, give that a shot. It should add cache-control w/ some max-age. You can return { props: {...}, revalidate: 60} to add ISR.
I'm not 100% sure this standalone works like Vercel though...
Vercel's non static pages loads in under 50ms, while ours takes 500-700 (maybe b/c they use edge lambdas and we don't)

from cdk-nextjs.

bestickley avatar bestickley commented on May 30, 2024

I think this issue has been fixed. Please re-open if you think otherwise. Thank you!

from cdk-nextjs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.