Dam*_*ski 6 javascript build next.js prismic.io
我试图理解为什么 Next.js 将我的一些页面构建为 SSG 而其中一些页面构建为静态,当它们都使用 getStaticProps 时。
让我们以我的 404 页面为例,该页面使用 getStaticProps 从带有 graphql 的棱镜中获取数据。它被呈现为静态网站,而我认为它应该呈现为 SSG(因为它使用 getStaticProps)。
我在我的 500 页中做完全相同的事情,但是使用不同的 graphql 查询并且它被呈现(在我看来是正确的)作为 SSG。
这是为什么?
404页面:
const NotFound = ({ data: { page } }) => {
return (
<div className={'not-found'}>
<p className={'not-found__description'}>{RichText.asText(page.description)}</p>
</div>
);
};
export const getStaticProps = async (context) => {
const currentLanguage = getCurrentLocale(context);
const response = await apolloClient.query({
query: gql`
query {
}
`
};
return {
props: {
data: {
page: response
}
}
}
});
export default NotFound;
Run Code Online (Sandbox Code Playgroud)
500页:
const InternalServerError = ({ data: { page } }) => {
return (
<div className={'internal-server-error'}>
<p className={'internal-server-error__description'}>{RichText.asText(page.description)}</p>
</div>
);
};
export const getStaticProps = async (context) => {
const currentLanguage = getCurrentLocale(context);
const response = await apolloClient.query({
query: gql`
query {
}
`
});
return {
props: {
data: {
page: response
}
}
}
};
Run Code Online (Sandbox Code Playgroud)
Next.js 中的 404.tsx 或 404.js 页面的独特之处在于,它不依赖服务器并且始终是静态的——在构建时仅依赖静态 html(无 json) ——即使GetStaticProps在文件中使用时也是如此。
404 页面只是一个包罗万象的funnel路由,当用户导航到以您的站点作为基本 URL 不存在的路径时,该页面将被重定向到。因此,它在初始构建时不依赖于服务器。这是路径不存在的后备,仅此而已。另一方面,500 页面处理应用程序中的内部错误,因此它确实依赖于.html和.json文件类型来查明错误的性质。
有趣的是,如果您在本地检查目录的内容.next,您会注意到所有使用 的页面和文件GetStaticProps都是静态生成的。使用with返回 ===或 的页面。是和的理想混合体,具有后台功能扫描生产中传入的更改/更新(您指定的数字是可能更新之间的时间量(以秒为单位))。因此,带有+的页面会在目录中生成三种文件类型—— 、和。也就是说,页面使用或仅在目录中生成文件。最后,纯粹的页面,不使用上述任何方法,只生成文件。.json.htmlGetStaticPropsrevalidateIncremental Static RegenerationISRISRSSGSSRGetStaticPropsISR.next.html.json.jsGetServerSidePropsGetInitialProps.js.nextStatic.html
404 页面及其静态本质背后的想法是通过加快自定义oops! that path doesn\'t exist页面的渲染(或更准确地说是预渲染)来增强用户体验,以便用户可以尽快返回到实际应用程序。
例如,我的页面中有以下内容,但它在构建时404.tsx仍然呈现为 Static 。html
import { Container } from \'@/components/UI\';\nimport { initializeApollo, addApolloState } from \'@/lib/apollo\';\nimport { NotFound } from \'@/components/NotFound\';\nimport { AppLayout } from \'@/components/Layout\';\nimport {\n GetStaticPropsContext,\n GetStaticPropsResult,\n InferGetStaticPropsType\n} from \'next\';\nimport {\n NotFoundQuery,\n NotFoundDocument,\n NotFoundQueryVariables,\n DynamicNavQuery,\n DynamicNavDocument,\n DynamicNavQueryVariables,\n WordpressMenuNodeIdTypeEnum,\n WordpressMediaItemSizeEnum,\n WordpressPageIdType\n} from \'@/graphql/generated/graphql\';\n\nexport function SOS({\n notFound,\n Header,\n Footer\n}: InferGetStaticPropsType<typeof getStaticProps>) {\n return (\n <>\n <AppLayout title={\'\xe2\x9c\x82 404 \xe2\x9c\x82\'} Header={Header} Footer={Footer}>\n <Container clean className=\'fit\'>\n <NotFound notFound={notFound} />\n </Container>\n </AppLayout>\n </>\n );\n}\n\nexport async function getStaticProps(\n ctx: GetStaticPropsContext\n): Promise<\n GetStaticPropsResult<{\n notFound: NotFoundQuery[\'NotFound\'];\n Header: DynamicNavQuery[\'Header\'];\n Footer: DynamicNavQuery[\'Footer\'];\n }>\n> {\n const params = ctx.params!;\n console.log(params ?? \'\');\n const apolloClient = initializeApollo();\n await apolloClient.query<\n DynamicNavQuery,\n DynamicNavQueryVariables\n >({\n query: DynamicNavDocument,\n variables: {\n idHead: \'Header\',\n idTypeHead: WordpressMenuNodeIdTypeEnum.NAME,\n idTypeFoot: WordpressMenuNodeIdTypeEnum.NAME,\n idFoot: \'Footer\'\n }\n });\n\n await apolloClient.query<NotFoundQuery, NotFoundQueryVariables>(\n {\n query: NotFoundDocument,\n variables: {\n id: \'/404-not-found/\' as \'/404/\',\n idType: WordpressPageIdType.URI,\n size: WordpressMediaItemSizeEnum.LARGE\n }\n }\n );\n return addApolloState(apolloClient, {\n props: {},\n revalidate: 60\n });\n}\nexport default SOS;\nRun Code Online (Sandbox Code Playgroud)\n有趣的是,因为我确实在页面中使用了GetStaticPropsand revalidatefor ,所以目录的内容反映了这一点,因为所有三种文件类型都存在于( )。如果您在自定义或文件中使用,则整个应用程序将禁用自动静态优化(静态页面的预渲染)。如果您好奇的话,请尝试一下,它应该会导致 404 页面在您的构建日志中旁边出现一个 lambda。但是,由于您已经拥有了,因此应该覆盖由根页面导致的应用程序范围的静态去优化ISR404.tsx.next404.js, .json, .htmlgetInitialProps_app.tsx_app.jsGetStaticPropsappGetInitialProps
例如,我GetInitialProps在_app.tsx创建自定义404.tsx页面之前使用过。我决定提取构建日志并拍摄了随附的屏幕截图。
Warning: You have opted-out of Automatic Static Optimization due to `getInitialProps` in `pages/_app`. This does not opt-out pages with `getStaticProps`.\nRun Code Online (Sandbox Code Playgroud)\n\n