aws-amplify / amplify-hosting

AWS Amplify Hosting provides a Git-based workflow for deploying and hosting fullstack serverless web applications.
https://aws.amazon.com/amplify/hosting/
Apache License 2.0
459 stars 116 forks source link

Statically generated dynamic pages in Nextjs are not served from cache #3530

Open ishaqibrahimbot opened 1 year ago

ishaqibrahimbot commented 1 year ago

Before opening, please confirm:

App Id

ds9myrwx73uix

AWS Region

ap-southeast-2

Amplify Hosting feature

Deployments, Frontend builds, SSR

Describe the bug

I have a Nextjs v12.2 app deployed on Amplify with the WEB_COMPUTE platform. As part of this, I have some dynamic routes for which I am statically generating some paths using getStaticPaths.

In the Frontend build logs, these paths show up as successfully generated. However, when I try to access those paths, I get the fallback mode and it takes a while for the actual page data to come through. So basically these paths weren't really cached.

Expected behavior

Paths that are statically generated at build time should be served from the Nextjs cache and I should get the rendered HTML on the first request instead of getting the fallback mode.

Reproduction steps

Not sure how to describe reproduction, but here's how I get the bug:

Build Settings

No response

Log output

``` # Put your logs below this line ```

Additional information

I've checked the headers on a lot of pages including non-dynamic ones such as the homepage of my app, and it looks like none of these are hitting the nextjs cache either. I'm getting a constant MISS for the x-nextjs-cache header. The non-dynamic ones are instead coming from Cloudfront's cache.

Jay2113 commented 1 year ago

Hi @ishaqibrahimbot 👋 , thanks for reaching out to us!

Just a quick note here, I am working on reproducing this behavior on my end and will keep you posted on the findings.

Thanks!

ishaqibrahimbot commented 1 year ago

Hey @Jay2113. did you find anything? 😄

Edit: Have some more information for you. I created a new Amplify project in another AWS account. Here's what I found that was consistent across both of my Amplify projects:

Examples of this:

  1. https://develop.d35wgr4vxwu7j7.amplifyapp.com/blog/koorong-digital-transformation
  2. https://develop.d35wgr4vxwu7j7.amplifyapp.com/category/bibles
  3. https://develop.d35wgr4vxwu7j7.amplifyapp.com/product/amplified-battlefield-of-the-mind-bible-joyce-meyer_9781455595303

Example:

  1. https://develop.d35wgr4vxwu7j7.amplifyapp.com/bible-guide

Maybe this helps you debug the issue better? 🙂

ishaqibrahimbot commented 1 year ago

Hey @Jay2113! Is there any update about this? My project is really close to launch so I would really like to solve this issue. Thanks 🙂

miki725 commented 1 year ago

I ran a preview build with a set of paths in getStaticPaths and fallback: false for the pages/[...path].tsx route. The build logs report that all those paths were generated successfully. However, I get a 404 whenever I visit any of those pages.

I am seeing same behavior where logs show pages being prebuilt but they show as 404

next version:

➜ cat package.json | grep '"next"'
    "next": "13.4.10",
alex-anikeev commented 12 months ago

Any progress on this or known workarounds?

ishaqibrahimbot commented 12 months ago

Any progress on this or known workarounds?

@alex-anikeev We figured out that the issue was with ISR. If your use case can bear it, I would suggest not using ISR for your dynamic routes. We started getting correct cache hits from the nextjs server when we did that.