amplify-js: Manage 404 page in Next app deployment
Hi,
I am following these instructions to deploy a Next app with the CLI.
Everything works great.
However, I have a question regarding 404 page. I have set up a custom 404 page which is correctly exported by Next in the /out folder. This 404 is showing well in dev mode when I try to access a wrong URL.
After deploying my app with amplify publish, I try to access a wrong URL and I’m getting a 403 AccessDenied. It seems logical since the deployment is only static.
So my question is: is there any way to be redirected to /404 when accessing a wrong URL?
Thanks for your help.
Which Category is your question related to?
Hosting
Amplify CLI Version
4.37.1
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 2
- Comments: 18
If anyone is asking, I found a solution that works for me. Basically you need to remove the rule for redirecting to 404 completely in Rewrites and redirects. Next.js will handle the ‘not found’ itself.
You can add a custom redirect for folders and paths that can’t be found and map it to your
/404.htmlpage.In your AWS Amplify console - click on the app and then click Rewrites and redirects. Add the
/<*>source address to map to/404.htmltarget address (your next.js custom page), with the type of 404 (Rewrite).The rewrites and redirects for my site look like:
As you mentioned, it’s not really next.js specific it’s just a static site, but it should maybe be added in the docs, since you would have to add this redirect in 100% of the cases when deploying a Next.js static site.
Ok, I found by myself a solution, but it looks like NextJs have some bug… on Amplify Rewrites, I just left the first Rewrite as @fanismahmalat said.
Just if anyone have the same issue:
NextJs app features
pages/404.jsbut not an _error custom pagepages/_error(it wasn’t logical to create it because all the possible errors in Amplify SSR service would be 404; 500 errors aren’t possible).fallback: falseattribute.With that conditions, subpages 404 work well, but root 404 shows the default 404 error page
Solution
/pages/_error.jsand keeping/pages/404.jsWith that, 404 subpages (
fallback:false) use custom/pages/404.jscomponent, beside 404 root pages use custom/pages/_error.jscomponent.Hi @ACPK did you find the solution? I’m having the same issue if I remove the 404 redirect. It would be great if I get any help. Thank you
@fanismahmalat If I remove the 404 redirect tool, I get the AWS error below:
Thanks @bobbyhadz problem solved! I added exactly the same redirect rule as you and it works.
I misunderstood those redirect rules and thought I would have to explicitly add a rule for each URL. But it actually works with dynamic routing as you told me.
@MrHertal Yes, it is hosted on Amplify, which uses S3 and Cloudfront under the hood, pushes to my blog-repo initialize the build and deploy to Amplify.
Hello @MrHertal ,
On my site bobbyhadz.com I use Next.js to
exportto static html files. Myfallbackproperty in the return value of thegetStaticPathsfunction in Next.js is set to false.I do have a dynamic route -
/blog/[slug].js- an-existing-blog-path which does404when I go to non-existing-blog-path because all the static files are generated at build time sincefallbackis set to false.As far as I’m aware Amplify hosting only supports fully static Next.js apps at the moment, and any of the features that require a server are not supported - like
fallbackset to true.