How to set up index.html in cloudfront as a root objest if it is not in a root folder?

0

Hello. I am getting all the time an error:

Code: NoSuchKey Message: The specified key does not exist. Key: index.html

We are using Next Js to build our app and publish on cloudfront. But the problem is that ./next folder (main folder for the build artefact) has no any index.html in its root. This file index.html is in ./next/server/pages/ folder after build is done and in ./standalone/.next/server/pages/index.html So, how to setup index.html with the path as a root object in cloudfront to be able to load it after typing of our root domain (without any extra /path/and/so/on/index.html? I tried to do it in cloudfront cache behavior or s3 properties but it does not work. Any ideas? Thanks.

已提問 8 個月前檢視次數 253 次
2 個答案
0

Hi,

Do you really need to expose all these folders instead of only /pages/? If not, I would suggest to either do not upload them to S3 during the deployment or/and use Origin path set.

CloudFront Origin path

profile picture
專家
已回答 8 個月前
  • Hello Well, i have started from /pages deploy, but there is an issue, that this folder does not include all the neccessary files for this app. For example /img and other, and as a result deployment site looks ugly. That is why i have tried to deploy ./next folder instead, but it has no strict structure (like should be in artefact folder). Also i tried to run "npx next export" but it generates only pages folder without any other needed. So i suppose there is an issue with export. But the strange thing is that ./next folder works perfectly in AWS amplify but doesn`t work properly on cloudfront.

0

Hi,

You can use cloudfront function to re-write the URL and fullfill your requirement. Please take a look at the following links for more detailed information.

https://github.com/aws-samples/amazon-cloudfront-functions/tree/main/url-rewrite-single-page-apps

https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/example-function-add-index.html

Hope this helps.

AWS
已回答 8 個月前

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南