AWSAmplify上的NextJSISR页面返回404,而在本地主机上运行正常
创始人
2024-11-19 02:00:23
0

此问题常见于运行Next.js应用于AWS Amplify时。原因是在部署应用程序时,可能会出现缺少构建文件或未正确设置路由的情况,导致应用程序无法正确路由至ISR页面。

为了解决这个问题,可以使用下列步骤:

  1. 确保启用了ISR。在Next.js应用程序中,您可以使用以下配置启用:

export async function getStaticProps(context) { return { props: {}, // will be passed to the page component as props revalidate: 60, //time to re-generate static page } }

  1. 检查您的应用程序是否具有正确的构建文件。您可以通过在本地构建应用程序来生成构建文件。为了在本地构建,请使用以下命令:

npm run build

  1. 检查您的AWS Amplify构建配置。确保您已正确地设置构建配置,以指向正确的构建文件和正确的路由规则。可参考以下YAML文件为例:

version: 1 frontend: phases: preBuild: commands: - npm install build: commands: - npm run build artifacts: baseDirectory: .next files: - '/*' cache: paths: - node_modules//*

  1. 可以使用以下示例作为参考,正确设置ISR页面的路由规则。

在‘app.js’中:

import { useRouter } from 'next/router' import { useEffect } from 'react'

export default function App({ Component, pageProps }) { const router = useRouter()

useEffect(() => { const handleRouteChange = url => { // check for the current route if (url.includes('/products/') || url.includes('/about/')) { router.prefetch(url) } }

// Subscribe to router events
router.events.on('routeChangeStart', handleRouteChange)

// Unsubscribe from router events when component is unmounted
return () => {
  router.events.off('routeChangeStart', handleRouteChange)
}

}, [])

return }

在‘[slug].js’页面中:

export const getStaticPaths = async () => { const paths = [ { params: { slug: 'products/a' } }, { params: { slug: 'products/b' } }, { params: { slug: 'products/c' } }, ] return { paths, fallback: true } }

export const getStaticProps = async ({ params }) => { const slug = params.slug const title = Product ${slug.split('/')[1]} return { props: { title, }, revalidate: 60 * 60 * 24, // 24 Hours } }

export default function Product({ title }) { return

{title}

}

通过遵循以上步骤,您应该能够消除此问题并成功在AWS Amplify上部署NextJS应用程序。

相关内容

热门资讯

6分钟辅助!hhpoker是真... 6分钟辅助!hhpoker是真的假的,hhpoker真的有透视吗,演示教程(真是有挂)1、每一步都需...
第七分钟辅助!we poker... 第七分钟辅助!we poker插件,we poker免费辅助器,手筋教程(有挂方式)暗藏猫腻,小编详...
七分钟辅助!aa poker辅... 七分钟辅助!aa poker辅助包,pokemmo脚本辅助器下载,讲义教程(有挂秘籍)1、pokem...
第四分钟辅助!wepoker辅... 第四分钟辅助!wepoker辅助器安装包定制,aapoker透视脚本,大纲教程(有挂总结)所有人都在...
第四分钟辅助!wpk透视辅助靠... 第四分钟辅助!wpk透视辅助靠谱吗,wepoker透视苹果系统,诀窍教程(有挂猫腻);运wepoke...
七分钟辅助!aapoker透视... 七分钟辅助!aapoker透视脚本入口,wejoker开挂,学习教程(有挂教程)1、打开软件启动之后...
第四分钟辅助!淘宝买wepok... 第四分钟辅助!淘宝买wepoker透视有用吗,wpk俱乐部怎么作弊,积累教程(有挂规律)1、下载好淘...
6分钟辅助!we-poker软... 6分钟辅助!we-poker软件,购买的wpk辅助在哪里下载,积累教程(有挂技巧)购买的wpk辅助在...
第2分钟辅助!如何下载wepo... 第2分钟辅助!如何下载wepoker安装包,newpoker怎么安装脚本,教程书教程(今日头条)1、...
4分钟辅助!wepoker钻石... 4分钟辅助!wepoker钻石怎么看底牌,wepoker辅助器有哪些功能,法门教程(有挂详细)该软件...