{"id":1312,"date":"2022-08-01T01:23:33","date_gmt":"2022-07-31T17:23:33","guid":{"rendered":"https:\/\/fromideatocreation.com\/?p=1312"},"modified":"2023-07-02T16:19:25","modified_gmt":"2023-07-02T08:19:25","slug":"%e3%80%90next-js-%e7%b3%bb%e5%88%97%e3%80%91%e5%9f%ba%e6%9c%ac%e5%8a%9f%e8%83%bd-%e9%a0%81%e9%9d%a2","status":"publish","type":"post","link":"https:\/\/fromideatocreation.com\/?p=1312","title":{"rendered":"\u3010NEXT.JS \u7cfb\u5217\u3011\u57fa\u672c\u529f\u80fd\u2014\u9801\u9762"},"content":{"rendered":"\n<p>\u5728 Next.js \u4e2d\uff0c\u9801\u9762\u662f\u5f9e pages \u76ee\u9304\u4e2d\u7684 <code>.js<\/code>\u3001<code>.jsx<\/code>\u3001<code>.ts<\/code> \u6216 <code>.tsx<\/code> \u6587\u4ef6\u532f\u51fa\u7684 React \u5143\u4ef6\u3002 \u6bcf\u500b\u9801\u9762\u90fd\u6839\u64da\u5176\u6587\u4ef6\u540d\u8207\u4e00\u500b\u8def\u7531\u76f8\u95dc\u806f\u3002<\/p>\n\n\n\n<p>\u4f8b\u5982\uff1a\u5982\u679c\u60a8\u5efa\u7acb\u532f\u51fa React \u5143\u4ef6\u7684 <code>pages\/about.js<\/code>\uff0c\u5982\u4e0b\u6240\u793a\uff0c\u5b83\u5c07\u53ef\u4ee5\u5728 <code>\/about<\/code> \u4e2d\u8a2a\u554f\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"jsx\" class=\"language-jsx\">function About() {\n  return &lt;div&gt;About&lt;\/div&gt;\n}\n\nexport default About<\/code><\/pre>\n\n\n\n<div style=\"height:24px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">\u64c1\u6709\u52d5\u614b\u8def\u7531\u7684\u9801\u9762<\/h2>\n\n\n\n<p>Next.js \u652f\u63f4\u5177\u6709\u52d5\u614b\u8def\u7531\u7684\u9801\u9762\u3002 \u4f8b\u5982\uff0c\u5982\u679c\u60a8\u5efa\u7acb\u4e86\u4e00\u500b\u540d\u70ba <code>pages\/posts\/[id].js<\/code> \u7684\u6587\u4ef6\uff0c\u90a3\u9ebc\u5b83\u5c07\u53ef\u4ee5\u5728 <code>posts\/1<\/code>\u3001<code>posts\/2<\/code> \u7b49\u8655\u8a2a\u554f\u3002<\/p>\n\n\n\n<div style=\"height:24px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">\u9810\u5148\u6e32\u67d3<\/h2>\n\n\n\n<p>\u9810\u8a2d\u60c5\u6cc1\u4e0b\uff0cNext.js \u9810\u5148\u6e32\u67d3\u6bcf\u500b\u9801\u9762\u3002 \u9019\u610f\u5473\u8457 Next.js \u6703\u63d0\u524d\u70ba\u6bcf\u500b\u9801\u9762\u751f\u6210 HTML\uff0c\u800c\u4e0d\u662f\u5168\u90e8\u7531\u5ba2\u6236\u7aef JavaScript \u5b8c\u6210\u3002 \u9810\u5148\u6e32\u67d3\u53ef\u4ee5\u5e36\u4f86\u66f4\u597d\u7684\u6027\u80fd\u548c SEO\u3002<\/p>\n\n\n\n<p>\u6bcf\u500b\u751f\u6210\u7684 HTML \u90fd\u8207\u8a72\u9801\u9762\u6240\u9700\u7684\u6700\u5c11 JavaScript \u7a0b\u5f0f\u78bc\u76f8\u95dc\u806f\u3002 \u7576\u700f\u89bd\u5668\u52a0\u8f09\u9801\u9762\u6642\uff0c\u5176 JavaScript \u7a0b\u5f0f\u78bc\u5c07\u904b\u884c\u4e26\u4f7f\u9801\u9762\u5b8c\u5168\u53ef\u4e92\u52d5\u3002 \uff08\u9019\u500b\u904e\u7a0b\u7a31\u70ba\u6c34\u5408\u4f5c\u7528 hydration\u3002\uff09<\/p>\n\n\n\n<div style=\"height:24px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">\u5169\u7a2e\u9810\u5148\u6e32\u67d3\u7684\u5f62\u5f0f<\/h2>\n\n\n\n<p>Next.js \u6709\u5169\u7a2e\u9810\u5148\u6e32\u67d3\u5f62\u5f0f\uff1a\u975c\u614b\u751f\u6210\u548c\u670d\u52d9\u5668\u7aef\u6e32\u67d3\u3002\u4e0d\u540c\u4e4b\u8655\u5728\u65bc\u5b83\u4f55\u6642\u70ba\u9801\u9762\u751f\u6210 HTML\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u975c\u614b\u751f\u6210 Static Generation\uff08\u63a8\u85a6\uff09\uff1aHTML \u5728 runtime \u5efa\u69cb\u6642\u751f\u6210\uff0c\u4e26\u5c07\u5728\u6bcf\u500b\u8acb\u6c42\u4e2d\u91cd\u8907\u4f7f\u7528\u3002<\/li>\n\n\n\n<li>\u670d\u52d9\u5668\u7aef\u6e32\u67d3 Server-side Rendering\uff1a\u5728\u6bcf\u500b\u8acb\u6c42\u4e0a\u751f\u6210 HTML\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u91cd\u8981\u7684\u662f\uff0cNext.js \u5141\u8a31\u60a8\u9078\u64c7\u8981\u70ba\u6bcf\u500b\u9801\u9762\u4f7f\u7528\u7684\u9810\u5148\u6e32\u67d3\u5f62\u5f0f\u3002\u60a8\u53ef\u4ee5\u901a\u904e\u5c0d\u5927\u591a\u6578\u9801\u9762\u4f7f\u7528\u975c\u614b\u751f\u6210\u4e26\u70ba\u5176\u4ed6\u9801\u9762\u4f7f\u7528\u670d\u52d9\u5668\u7aef\u6e32\u67d3\u4f86\u5efa\u7acb\u201c\u6df7\u5408\u201dNext.js \u61c9\u7528\u7a0b\u5f0f\u3002<\/p>\n\n\n\n<p>\u51fa\u65bc\u6027\u80fd\u539f\u56e0\uff0c\u6211\u5011\u5efa\u8b70\u4f7f\u7528\u975c\u614b\u751f\u6210\u800c\u4e0d\u662f\u670d\u52d9\u5668\u7aef\u6e32\u67d3\u3002\u975c\u614b\u751f\u6210\u7684\u9801\u9762\u53ef\u4ee5\u88ab CDN \u5feb\u53d6\uff0c\u7121\u9700\u984d\u5916\u914d\u7f6e\u4f86\u63d0\u9ad8\u6027\u80fd\u3002\u4f46\u662f\uff0c\u5728\u67d0\u4e9b\u60c5\u6cc1\u4e0b\uff0c\u670d\u52d9\u5668\u7aef\u6e32\u67d3\u53ef\u80fd\u662f\u552f\u4e00\u7684\u9078\u64c7\u3002<\/p>\n\n\n\n<p>\u60a8\u9084\u53ef\u4ee5\u5c07\u5ba2\u6236\u7aef\u6e32\u67d3\u8207\u975c\u614b\u751f\u6210\u6216\u670d\u52d9\u5668\u7aef\u6e32\u67d3\u4e00\u8d77\u4f7f\u7528\u3002\u9019\u610f\u5473\u8457\u9801\u9762\u7684\u67d0\u4e9b\u90e8\u5206\u53ef\u4ee5\u5b8c\u5168\u7531\u5ba2\u6236\u7aef JavaScript \u5448\u73fe\u3002\u8981\u4e86\u89e3\u66f4\u591a\u8cc7\u8a0a\uff0c\u8acb\u67e5\u770b<a href=\"https:\/\/nextjs.org\/docs\/basic-features\/data-fetching\/client-side\" target=\"_blank\" rel=\"noreferrer noopener\" title=\"\u6578\u64da\u7372\u53d6\">\u6578\u64da\u7372\u53d6<\/a>\u6587\u4ef6\u3002<\/p>\n\n\n\n<div style=\"height:24px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">\u975c\u614b\u751f\u6210 <span style=\"color: var(--nv-text-color); font-family: Lato, var(--nv-fallback-ff); letter-spacing: 0px; background-color: var(--nv-site-bg);\">\uff08\u63a8\u85a6\uff09<\/span> <\/h2>\n\n\n\n<p>\u5982\u679c\u9801\u9762\u4f7f\u7528\u975c\u614b\u751f\u6210\uff0c\u5247\u9801\u9762 HTML \u662f\u5728\u5efa\u69cb\u6642\u751f\u6210\u7684\u3002 \u9019\u610f\u5473\u8457\u5728\u751f\u7522\u4e2d\uff0c\u9801\u9762 HTML \u6703\u5728\u60a8\u904b\u884c next build \u6642\u751f\u6210\u3002 \u7136\u5f8c\uff0c\u6b64 HTML \u5c07\u5728\u6bcf\u500b\u8acb\u6c42\u4e0a\u91cd\u8907\u4f7f\u7528\u3002 \u5b83\u53ef\u4ee5\u7531 CDN \u5feb\u53d6\u3002<\/p>\n\n\n\n<p>\u5728 Next.js \u4e2d\uff0c\u60a8\u53ef\u4ee5\u975c\u614b\u751f\u6210\u5305\u542b\u6216\u4e0d\u5305\u542b\u6578\u64da\u7684\u9801\u9762\u3002 \u8b93\u6211\u5011\u4f86\u770b\u770b\u6bcf\u500b\u6848\u4f8b\u3002<\/p>\n\n\n\n<div style=\"height:24px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">\u6c92\u6709\u8cc7\u6599\u7684\u975c\u614b\u751f\u6210<\/h3>\n\n\n\n<p>\u9810\u8a2d\u60c5\u6cc1\u4e0b\uff0cNext.js \u4f7f\u7528\u975c\u614b\u751f\u6210\u9810\u5148\u6e32\u67d3\u9801\u9762\u800c\u4e0d\u7372\u53d6\u6578\u64da\u3002 \u9019\u662f\u4e00\u500b\u4f8b\u5b50\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"jsx\" class=\"language-jsx\">function About() {\n  return &lt;div&gt;About&lt;\/div&gt;\n}\n\nexport default About<\/code><\/pre>\n\n\n\n<p>\u8acb\u6ce8\u610f\uff0c\u6b64\u9801\u9762\u4e0d\u9700\u8981\u7372\u53d6\u4efb\u4f55\u8981\u9810\u5148\u6e32\u67d3\u7684\u5916\u90e8\u6578\u64da\u3002 \u5728\u9019\u7a2e\u60c5\u6cc1\u4e0b\uff0cNext.js \u5728\u5efa\u69cb\u671f\u9593\u70ba\u6bcf\u500b\u9801\u9762\u751f\u6210\u4e00\u500b HTML \u6587\u4ef6\u3002<\/p>\n\n\n\n<div style=\"height:24px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">\u6709\u8cc7\u6599\u7684\u975c\u614b\u751f\u6210<\/h3>\n\n\n\n<p>\u67d0\u4e9b\u9801\u9762\u9700\u8981\u7372\u53d6\u5916\u90e8\u6578\u64da\u4ee5\u9032\u884c\u9810\u5148\u6e32\u67d3\u3002 \u6709\u5169\u7a2e\u60c5\u6cc1\uff0c\u53ef\u80fd\u9069\u7528\u4e00\u7a2e\u6216\u5169\u7a2e\u60c5\u6cc1\u3002 \u5728\u6bcf\u7a2e\u60c5\u6cc1\u4e0b\uff0c\u60a8\u90fd\u53ef\u4ee5\u4f7f\u7528 Next.js \u63d0\u4f9b\u7684\u9019\u4e9b\u529f\u80fd\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u60a8\u7684\u9801\u9762<strong>\u5167\u5bb9<\/strong>\u53d6\u6c7a\u65bc\u5916\u90e8\u6578\u64da\uff1a\u4f7f\u7528 <code>getStaticProps<\/code>\u3002<\/li>\n\n\n\n<li>\u60a8\u7684\u9801\u9762<strong>\u8def\u5f91<\/strong>\u53d6\u6c7a\u65bc\u5916\u90e8\u6578\u64da\uff1a\u4f7f\u7528 <code>getStaticPaths<\/code>\uff08\u901a\u5e38\u9664\u4e86 <code>getStaticProps<\/code> \u4e4b\u5916\uff09\u3002<\/li>\n<\/ol>\n\n\n\n<div style=\"height:24px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">\u60c5\u5883 1\uff1a\u4f60\u7684\u9801\u9762\u5167\u5bb9\u4f9d\u8cf4\u65bc\u5916\u90e8\u6578\u64da<\/h3>\n\n\n\n<p>\u7bc4\u4f8b\uff1a\u60a8\u7684\u90e8\u843d\u683c\u9801\u9762\u53ef\u80fd\u9700\u8981\u5f9e CMS\uff08\u5167\u5bb9\u7ba1\u7406\u7cfb\u7d71\uff09\u7372\u53d6\u90e8\u843d\u683c\u6587\u7ae0\u5217\u8868\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"jsx\" class=\"language-jsx\">\/\/ TODO: Need to fetch `posts` (by calling some API endpoint)\n\/\/       before this page can be pre-rendered.\nfunction Blog({ posts }) {\n  return (\n    &lt;ul&gt;\n      {posts.map((post) =&gt; (\n        &lt;li&gt;{post.title}&lt;\/li&gt;\n      ))}\n    &lt;\/ul&gt;\n  )\n}\n\nexport default Blog<\/code><\/pre>\n\n\n\n<p>\u70ba\u4e86\u5728\u9810\u5148\u6e32\u67d3\u4e2d\u7372\u53d6\u9019\u4e9b\u6578\u64da\uff0cNext.js \u5141\u8a31\u60a8\u5f9e\u540c\u4e00\u500b\u6587\u4ef6\u4e2d\u532f\u51fa\u4e00\u500b\u540d\u70ba <code>getStaticProps<\/code> \u7684\u7570\u6b65\u51fd\u6578\u3002 \u6b64\u51fd\u6578\u5728\u5efa\u69cb\u6642\u88ab\u8abf\u7528\uff0c\u4e26\u5141\u8a31\u60a8\u5728\u9810\u5148\u6e32\u67d3\u6642\u5c07\u7372\u53d6\u7684\u6578\u64da\u50b3\u905e\u7d66\u9801\u9762\u7684 <code>props<\/code>\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"jsx\" class=\"language-jsx\">function Blog({ posts }) {\n  \/\/ Render posts...\n}\n\n\/\/ This function gets called at build time\nexport async function getStaticProps() {\n  \/\/ Call an external API endpoint to get posts\n  const res = await fetch('https:\/\/...\/posts')\n  const posts = await res.json()\n\n  \/\/ By returning { props: { posts } }, the Blog component\n  \/\/ will receive `posts` as a prop at build time\n  return {\n    props: {\n      posts,\n    },\n  }\n}\n\nexport default Blog<\/code><\/pre>\n\n\n\n<p>\u8981\u4e86\u89e3\u6709\u95dc <code>getStaticProps<\/code> \u5de5\u4f5c\u539f\u7406\u7684\u66f4\u591a\u5167\u5bb9\uff0c\u8acb\u67e5\u770b<a href=\"https:\/\/nextjs.org\/docs\/basic-features\/data-fetching\/get-static-props\" target=\"_blank\" rel=\"noreferrer noopener\" title=\"\u6578\u64da\u7372\u53d6\">\u6578\u64da\u7372\u53d6<\/a>\u6587\u4ef6\u3002<\/p>\n\n\n\n<div style=\"height:24px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">\u60c5\u5883 2\uff1a\u60a8\u7684\u9801\u9762\u8def\u5f91\u4f9d\u8cf4\u65bc\u5916\u90e8\u6578\u64da<\/h3>\n\n\n\n<p>Next.js \u5141\u8a31\u60a8\u5efa\u7acb\u5177\u6709\u52d5\u614b\u8def\u7531\u7684\u9801\u9762\u3002 \u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u5efa\u7acb\u4e00\u500b\u540d\u70ba <code>pages\/posts\/[id].js<\/code> \u7684\u6587\u4ef6\u4f86\u986f\u793a\u57fa\u65bc <code>id<\/code> \u7684\u55ae\u500b\u90e8\u843d\u683c\u6587\u7ae0\u3002 \u9019\u5c07\u5141\u8a31\u60a8\u5728\u8a2a\u554f <code>posts\/1<\/code> \u6642\u986f\u793a <code>id: 1<\/code> \u7684\u90e8\u843d\u683c\u6587\u7ae0\u3002<\/p>\n\n\n\n<p>\u4f46\u662f\uff0c\u60a8\u5e0c\u671b\u5728\u5efa\u69cb\u6642\u9810\u5148\u6e32\u67d3\u54ea\u500b <code>id<\/code> \u53ef\u80fd\u53d6\u6c7a\u65bc\u5916\u90e8\u6578\u64da\u3002<\/p>\n\n\n\n<p>\u4f8b\u5982\uff1a\u5047\u8a2d\u60a8\u53ea\u5411\u8cc7\u6599\u5eab\u6dfb\u52a0\u4e86\u4e00\u7bc7\u6587\u7ae0\uff08<code>id\uff1a1<\/code>\uff09\u3002 \u5728\u9019\u7a2e\u60c5\u6cc1\u4e0b\uff0c\u60a8\u53ea\u60f3\u5728\u5efa\u69cb\u6642\u9810\u5148\u6e32\u67d3 <code>posts\/1<\/code>\u3002<\/p>\n\n\n\n<p>\u7a0d\u5f8c\uff0c\u60a8\u53ef\u80fd\u6703\u6dfb\u52a0\u7b2c\u4e8c\u500b\u5e36\u6709 <code>id: 2<\/code> \u7684\u6587\u7ae0\u3002\u7136\u5f8c\u60a8\u9084\u60f3\u9810\u5148\u6e32\u67d3\u5e16\u5b50 <code>posts\/2<\/code>\u3002<\/p>\n\n\n\n<p>\u56e0\u6b64\uff0c\u9810\u5148\u6e32\u67d3\u7684\u9801\u9762\u8def\u5f91\u53d6\u6c7a\u65bc\u5916\u90e8\u6578\u64da\u3002 \u70ba\u4e86\u8655\u7406\u9019\u500b\u554f\u984c\uff0cNext.js \u5141\u8a31\u60a8\u5f9e\u52d5\u614b\u9801\u9762\uff08\u5728\u672c\u4f8b\u4e2d\u70ba <code>pages\/posts\/[id].js<\/code>\uff09\u5c0e\u51fa\u4e00\u500b\u540d\u70ba getStaticPaths \u7684\u7570\u6b65\u51fd\u6578\u3002 \u6b64\u51fd\u6578\u5728\u5efa\u69cb\u6642\u88ab\u8abf\u7528\uff0c\u4e26\u5141\u8a31\u60a8\u6307\u5b9a\u8981\u9810\u5148\u6e32\u67d3\u7684\u8def\u5f91\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"jsx\" class=\"language-jsx\">\/\/ This function gets called at build time\nexport async function getStaticPaths() {\n  \/\/ Call an external API endpoint to get posts\n  const res = await fetch('https:\/\/...\/posts')\n  const posts = await res.json()\n\n  \/\/ Get the paths we want to pre-render based on posts\n  const paths = posts.map((post) =&gt; ({\n    params: { id: post.id },\n  }))\n\n  \/\/ We'll pre-render only these paths at build time.\n  \/\/ { fallback: false } means other routes should 404.\n  return { paths, fallback: false }\n}<\/code><\/pre>\n\n\n\n<p>\u540c\u6a23\u5728 <code>pages\/posts\/[id].js<\/code> \u4e2d\uff0c\u60a8\u9700\u8981\u532f\u51fa <code>getStaticProps<\/code> \u4ee5\u4fbf\u60a8\u53ef\u4ee5\u4f7f\u7528\u6b64 <code>id<\/code> \u7372\u53d6\u6709\u95dc\u6587\u7ae0\u7684\u6578\u64da\u4e26\u4f7f\u7528\u5b83\u4f86\u9810\u5148\u6e32\u67d3\u9801\u9762\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"jsx\" class=\"language-jsx\">function Post({ post }) {\n  \/\/ Render post...\n}\n\nexport async function getStaticPaths() {\n  \/\/ ...\n}\n\n\/\/ This also gets called at build time\nexport async function getStaticProps({ params }) {\n  \/\/ params contains the post `id`.\n  \/\/ If the route is like \/posts\/1, then params.id is 1\n  const res = await fetch(`https:\/\/...\/posts\/${params.id}`)\n  const post = await res.json()\n\n  \/\/ Pass post data to the page via props\n  return { props: { post } }\n}\n\nexport default Post<\/code><\/pre>\n\n\n\n<p>\u8981\u4e86\u89e3\u6709\u95dc <code>getStaticPaths<\/code> \u5982\u4f55\u5de5\u4f5c\u7684\u66f4\u591a\u5167\u5bb9\uff0c\u8acb\u67e5\u770b<a href=\"https:\/\/nextjs.org\/docs\/basic-features\/data-fetching\/get-static-paths\" target=\"_blank\" rel=\"noreferrer noopener\" title=\"\u6578\u64da\u7372\u53d6\">\u6578\u64da\u7372\u53d6<\/a>\u6587\u4ef6\u3002<\/p>\n\n\n\n<div style=\"height:24px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">\u6211\u4ec0\u9ebc\u6642\u5019\u61c9\u8a72\u4f7f\u7528\u975c\u614b\u751f\u6210\uff1f<\/h2>\n\n\n\n<p>\u6211\u5011\u5efa\u8b70\u76e1\u53ef\u80fd\u4f7f\u7528\u975c\u614b\u751f\u6210\uff08\u6709\u6578\u64da\u548c\u7121\u6578\u64da\uff09\uff0c\u56e0\u70ba\u60a8\u7684\u9801\u9762\u53ef\u4ee5\u5efa\u69cb\u4e00\u6b21\u4e26\u7531 CDN \u63d0\u4f9b\u670d\u52d9\uff0c\u9019\u6bd4\u8b93\u670d\u52d9\u5668\u5728\u6bcf\u6b21\u8acb\u6c42\u6642\u5448\u73fe\u9801\u9762\u8981\u5feb\u5f97\u591a\u3002<\/p>\n\n\n\n<p>\u60a8\u53ef\u4ee5\u5c07\u975c\u614b\u751f\u6210\u7528\u65bc\u591a\u7a2e\u985e\u578b\u7684\u9801\u9762\uff0c\u5305\u62ec\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u71df\u92b7\u9801\u9762<\/li>\n\n\n\n<li>\u90e8\u843d\u683c\u6587\u7ae0\u548c\u4f5c\u54c1\u96c6<\/li>\n\n\n\n<li>\u96fb\u5b50\u5546\u52d9\u7522\u54c1\u5217\u8868<\/li>\n\n\n\n<li>\u5e6b\u52a9\u548c\u6587\u4ef6<\/li>\n<\/ul>\n\n\n\n<p>\u60a8\u61c9\u8a72\u554f\u81ea\u5df1\uff1a\u201c\u6211\u53ef\u4ee5\u5728\u7528\u6236\u8acb\u6c42\u4e4b\u524d\u9810\u5148\u6e32\u67d3\u6b64\u9801\u9762\u55ce\uff1f\u201d\u5982\u679c\u7b54\u6848\u662f\u80af\u5b9a\u7684\uff0c\u90a3\u9ebc\u60a8\u61c9\u8a72\u9078\u64c7\u975c\u614b\u751f\u6210\u3002<\/p>\n\n\n\n<p>\u53e6\u4e00\u65b9\u9762\uff0c\u5982\u679c\u60a8\u4e0d\u80fd\u5728\u7528\u6236\u8acb\u6c42\u4e4b\u524d\u9810\u5148\u6e32\u67d3\u9801\u9762\uff0c\u5247\u975c\u614b\u751f\u6210\u4e0d\u662f\u4e00\u500b\u597d\u4e3b\u610f\u3002\u4e5f\u8a31\u60a8\u7684\u9801\u9762\u6703\u986f\u793a\u983b\u7e41\u66f4\u65b0\u7684\u6578\u64da\uff0c\u4e26\u4e14\u9801\u9762\u5167\u5bb9\u6703\u96a8\u6bcf\u500b\u8acb\u6c42\u800c\u66f4\u6539\u3002<\/p>\n\n\n\n<p>\u5728\u9019\u7a2e\u60c5\u6cc1\u4e0b\uff0c\u60a8\u53ef\u4ee5\u57f7\u884c\u4ee5\u4e0b\u64cd\u4f5c\u4e4b\u4e00\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5728\u5ba2\u6236\u7aef\u6e32\u67d3\u4e2d\u4f7f\u7528\u975c\u614b\u751f\u6210\uff1a\u60a8\u53ef\u4ee5\u8df3\u904e\u9810\u5148\u6e32\u67d3\u9801\u9762\u7684\u67d0\u4e9b\u90e8\u5206\uff0c\u7136\u5f8c\u4f7f\u7528\u5ba2\u6236\u7aef JavaScript \u4f86\u586b\u5145\u5b83\u5011\u3002\u8981\u4e86\u89e3\u6709\u95dc\u6b64\u65b9\u6cd5\u7684\u66f4\u591a\u8cc7\u8a0a\uff0c\u8acb\u67e5\u770b<a href=\"https:\/\/nextjs.org\/docs\/basic-features\/data-fetching\/client-side\" target=\"_blank\" rel=\"noreferrer noopener\" title=\"\u6578\u64da\u7372\u53d6\">\u6578\u64da\u7372\u53d6<\/a>\u6587\u4ef6\u3002<\/li>\n\n\n\n<li>\u4f7f\u7528\u670d\u52d9\u5668\u7aef\u6e32\u67d3\uff1aNext.js \u5728\u6bcf\u500b\u8acb\u6c42\u4e0a\u9810\u5148\u6e32\u67d3\u4e00\u500b\u9801\u9762\u3002\u5b83\u6703\u66f4\u6162\uff0c\u56e0\u70ba\u9801\u9762\u7121\u6cd5\u88ab CDN \u5feb\u53d6\uff0c\u4f46\u9810\u5148\u6e32\u67d3\u7684\u9801\u9762\u5c07\u59cb\u7d42\u662f\u6700\u65b0\u7684\u3002\u6211\u5011\u5c07\u5728\u4e0b\u9762\u8a0e\u8ad6\u9019\u7a2e\u65b9\u6cd5\u3002<\/li>\n<\/ul>\n\n\n\n<div style=\"height:24px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">\u670d\u52d9\u5668\u7aef\u6e32\u67d3<\/h2>\n\n\n\n<p>\u4e5f\u7a31\u70ba\u201cSSR\u201d\u6216\u201c\u52d5\u614b\u6e32\u67d3\u201d\u3002<\/p>\n\n\n\n<p>\u5982\u679c\u9801\u9762\u4f7f\u7528\u670d\u52d9\u5668\u7aef\u5448\u73fe\uff0c\u5247\u5728\u6bcf\u6b21\u8acb\u6c42\u6642\u90fd\u6703\u751f\u6210\u9801\u9762 HTML\u3002<\/p>\n\n\n\n<p>\u8981\u5c0d\u9801\u9762\u4f7f\u7528\u670d\u52d9\u5668\u7aef\u6e32\u67d3\uff0c\u60a8\u9700\u8981\u532f\u51fa\u4e00\u500b\u540d\u70ba <code>getServerSideProps<\/code> \u7684\u7570\u6b65\u51fd\u6578\u3002 \u670d\u52d9\u5668\u5c07\u5728\u6bcf\u6b21\u8acb\u6c42\u6642\u8abf\u7528\u6b64\u51fd\u6578\u3002<\/p>\n\n\n\n<p>\u4f8b\u5982\uff0c\u5047\u8a2d\u60a8\u7684\u9801\u9762\u9700\u8981\u9810\u5148\u6e32\u67d3\u983b\u7e41\u66f4\u65b0\u7684\u6578\u64da\uff08\u5f9e\u5916\u90e8 API \u7372\u53d6\uff09\u3002 \u60a8\u53ef\u4ee5\u7de8\u5beb <code>getServerSideProps<\/code> \u4f86\u7372\u53d6\u6b64\u6578\u64da\u4e26\u5c07\u5176\u50b3\u905e\u7d66 <code>Page<\/code>\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"jsx\" class=\"language-jsx\">function Page({ data }) {\n  \/\/ Render data...\n}\n\n\/\/ This gets called on every request\nexport async function getServerSideProps() {\n  \/\/ Fetch data from external API\n  const res = await fetch(`https:\/\/...\/data`)\n  const data = await res.json()\n\n  \/\/ Pass data to the page via props\n  return { props: { data } }\n}\n\nexport default Page<\/code><\/pre>\n\n\n\n<p>\u5982\u60a8\u6240\u898b\uff0c<code>getServerSideProps<\/code> \u985e\u4f3c\u65bc <code>getStaticProps<\/code>\uff0c\u4f46\u4e0d\u540c\u4e4b\u8655\u5728\u65bc <code>getServerSideProps<\/code> \u5728\u6bcf\u500b\u8acb\u6c42\u4e0a\u904b\u884c\uff0c\u800c\u4e0d\u662f\u5728\u5efa\u69cb\u6642\u904b\u884c\u3002<\/p>\n\n\n\n<p>\u8981\u4e86\u89e3\u6709\u95dc <code>getServerSideProps<\/code> \u5de5\u4f5c\u539f\u7406\u7684\u66f4\u591a\u5167\u5bb9\uff0c\u8acb\u67e5\u770b<a href=\"https:\/\/nextjs.org\/docs\/basic-features\/data-fetching\/get-server-side-props\" target=\"_blank\" rel=\"noreferrer noopener\" title=\"\u6578\u64da\u7372\u53d6\">\u6578\u64da\u7372\u53d6<\/a>\u6587\u4ef6\u3002<\/p>\n\n\n\n<div style=\"height:24px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">\u7e3d\u7d50<\/h2>\n\n\n\n<p>\u6211\u5011\u5df2\u7d93\u8a0e\u8ad6\u4e86 Next.js \u7684\u5169\u7a2e\u9810\u5148\u6e32\u67d3\u5f62\u5f0f\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u975c\u614b\u751f\u6210\uff08\u63a8\u85a6\uff09\uff1aHTML \u5728\u5efa\u69cb\u6642\u751f\u6210\uff0c\u4e26\u5c07\u5728\u6bcf\u500b\u8acb\u6c42\u4e2d\u91cd\u8907\u4f7f\u7528\u3002 \u8981\u4f7f\u9801\u9762\u4f7f\u7528\u975c\u614b\u751f\u6210\uff0c\u8acb\u532f\u51fa\u9801\u9762\u5143\u4ef6\uff0c\u6216\u532f\u51fa <code>getStaticProps<\/code>\uff08\u5982\u679c\u9700\u8981\uff0c\u9084\u53ef\u4ee5\u532f\u51fa <code>getStaticPaths<\/code>\uff09\u3002 \u9019\u5c0d\u65bc\u53ef\u4ee5\u5728\u7528\u6236\u8acb\u6c42\u4e4b\u524d\u9810\u5148\u6e32\u67d3\u7684\u9801\u9762\u975e\u5e38\u6709\u7528\u3002 \u60a8\u9084\u53ef\u4ee5\u5c07\u5b83\u8207\u5ba2\u6236\u7aef\u6e32\u67d3\u4e00\u8d77\u4f7f\u7528\u4ee5\u5f15\u5165\u984d\u5916\u7684\u6578\u64da\u3002<\/li>\n\n\n\n<li>\u670d\u52d9\u5668\u7aef\u6e32\u67d3\uff1a\u5728\u6bcf\u500b\u8acb\u6c42\u4e0a\u751f\u6210 HTML\u3002 \u8981\u4f7f\u9801\u9762\u4f7f\u7528\u670d\u52d9\u5668\u7aef\u6e32\u67d3\uff0c\u8acb\u532f\u51fa <code>getServerSideProps<\/code>\u3002 \u56e0\u70ba\u670d\u52d9\u5668\u7aef\u6e32\u67d3\u5c0e\u81f4\u6027\u80fd\u6bd4\u975c\u614b\u751f\u6210\u6162\uff0c\u6240\u4ee5\u53ea\u6709\u5728\u7d55\u5c0d\u5fc5\u8981\u6642\u624d\u4f7f\u7528\u5b83\u3002<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Next.js \u4e2d\uff0c\u9801\u9762\u662f\u5f9e pages \u76ee\u9304\u4e2d\u7684 .js\u3001.jsx\u3001.ts \u6216 .tsx \u6587\u4ef6\u532f\u51fa\u7684 React \u5143\u4ef6\u3002 \u6bcf\u500b\u9801\u9762\u90fd\u6839\u64da\u5176\u6587\u4ef6\u540d\u8207\u4e00\u500b\u8def\u7531\u76f8\u95dc\u806f\u3002<\/p>\n","protected":false},"author":1,"featured_media":1165,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"qubely_global_settings":"","qubely_interactions":"","inline_featured_image":false,"_ti_tpc_template_sync":false,"_ti_tpc_template_id":"","footnotes":""},"categories":[164,106],"tags":[163],"class_list":["post-1312","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-next-js","category-106","tag-next-js"],"acf":[],"aioseo_notices":[],"qubely_featured_image_url":{"full":["https:\/\/fromideatocreation.com\/wp-content\/uploads\/2023\/05\/app-download.png",720,454,false],"landscape":["https:\/\/fromideatocreation.com\/wp-content\/uploads\/2023\/05\/app-download.png",720,454,false],"portraits":["https:\/\/fromideatocreation.com\/wp-content\/uploads\/2023\/05\/app-download-540x320.png",540,320,true],"thumbnail":["https:\/\/fromideatocreation.com\/wp-content\/uploads\/2023\/05\/app-download-150x150.png",150,150,true],"medium":["https:\/\/fromideatocreation.com\/wp-content\/uploads\/2023\/05\/app-download-300x189.png",300,189,true],"medium_large":["https:\/\/fromideatocreation.com\/wp-content\/uploads\/2023\/05\/app-download.png",640,404,false],"large":["https:\/\/fromideatocreation.com\/wp-content\/uploads\/2023\/05\/app-download.png",640,404,false],"1536x1536":["https:\/\/fromideatocreation.com\/wp-content\/uploads\/2023\/05\/app-download.png",720,454,false],"2048x2048":["https:\/\/fromideatocreation.com\/wp-content\/uploads\/2023\/05\/app-download.png",720,454,false],"qubely_landscape":["https:\/\/fromideatocreation.com\/wp-content\/uploads\/2023\/05\/app-download.png",720,454,false],"qubely_portrait":["https:\/\/fromideatocreation.com\/wp-content\/uploads\/2023\/05\/app-download-540x320.png",540,320,true],"qubely_thumbnail":["https:\/\/fromideatocreation.com\/wp-content\/uploads\/2023\/05\/app-download-140x100.png",140,100,true],"nazare_single_thumb":["https:\/\/fromideatocreation.com\/wp-content\/uploads\/2023\/05\/app-download.png",720,454,false],"nazare_post_card_thumb":["https:\/\/fromideatocreation.com\/wp-content\/uploads\/2023\/05\/app-download-480x454.png",480,454,true],"nazare_widget_thumb":["https:\/\/fromideatocreation.com\/wp-content\/uploads\/2023\/05\/app-download-100x100.png",100,100,true],"nazare_widget_first_thumb":["https:\/\/fromideatocreation.com\/wp-content\/uploads\/2023\/05\/app-download-350x206.png",350,206,true],"nazare_el_banner_left":["https:\/\/fromideatocreation.com\/wp-content\/uploads\/2023\/05\/app-download.png",720,454,false],"nazare_el_banner_right":["https:\/\/fromideatocreation.com\/wp-content\/uploads\/2023\/05\/app-download-560x454.png",560,454,true],"nazare_el_post_400x376":["https:\/\/fromideatocreation.com\/wp-content\/uploads\/2023\/05\/app-download-400x376.png",400,376,true],"nazare_el_post_520x460":["https:\/\/fromideatocreation.com\/wp-content\/uploads\/2023\/05\/app-download-520x454.png",520,454,true],"nazare_el_post_500x335":["https:\/\/fromideatocreation.com\/wp-content\/uploads\/2023\/05\/app-download-500x335.png",500,335,true],"nazare_el_post_500x300":["https:\/\/fromideatocreation.com\/wp-content\/uploads\/2023\/05\/app-download-500x300.png",500,300,true],"nazare_el_post_655x640":["https:\/\/fromideatocreation.com\/wp-content\/uploads\/2023\/05\/app-download-655x454.png",655,454,true],"nazare_el_post_500x290":["https:\/\/fromideatocreation.com\/wp-content\/uploads\/2023\/05\/app-download-500x290.png",500,290,true],"nazare_el_post_416x278":["https:\/\/fromideatocreation.com\/wp-content\/uploads\/2023\/05\/app-download-416x278.png",416,278,true],"nazare_el_post_480x425":["https:\/\/fromideatocreation.com\/wp-content\/uploads\/2023\/05\/app-download-480x425.png",480,425,true],"nazare_el_post_635x640":["https:\/\/fromideatocreation.com\/wp-content\/uploads\/2023\/05\/app-download-635x454.png",635,454,true],"nazare_el_post_305x177":["https:\/\/fromideatocreation.com\/wp-content\/uploads\/2023\/05\/app-download-305x177.png",305,177,true],"nazare_el_post_400x400":["https:\/\/fromideatocreation.com\/wp-content\/uploads\/2023\/05\/app-download-400x400.png",400,400,true],"nazare_el_post_400x360":["https:\/\/fromideatocreation.com\/wp-content\/uploads\/2023\/05\/app-download-400x360.png",400,360,true],"nazare_el_post_416x416":["https:\/\/fromideatocreation.com\/wp-content\/uploads\/2023\/05\/app-download-416x416.png",416,416,true],"nazare_el_post_485x430":["https:\/\/fromideatocreation.com\/wp-content\/uploads\/2023\/05\/app-download-485x430.png",485,430,true],"nazare_el_post_700x467":["https:\/\/fromideatocreation.com\/wp-content\/uploads\/2023\/05\/app-download-700x454.png",700,454,true],"nazare_el_post_1000x581":["https:\/\/fromideatocreation.com\/wp-content\/uploads\/2023\/05\/app-download.png",720,454,false],"nazare_post_1320x605":["https:\/\/fromideatocreation.com\/wp-content\/uploads\/2023\/05\/app-download.png",720,454,false],"nazare_post_705x565":["https:\/\/fromideatocreation.com\/wp-content\/uploads\/2023\/05\/app-download-705x454.png",705,454,true],"nazare_post_400x265":["https:\/\/fromideatocreation.com\/wp-content\/uploads\/2023\/05\/app-download-400x265.png",400,265,true],"nazare_el_post_1248x630":["https:\/\/fromideatocreation.com\/wp-content\/uploads\/2023\/05\/app-download.png",720,454,false],"woocommerce_thumbnail":["https:\/\/fromideatocreation.com\/wp-content\/uploads\/2023\/05\/app-download-300x300.png",300,300,true],"woocommerce_single":["https:\/\/fromideatocreation.com\/wp-content\/uploads\/2023\/05\/app-download-600x378.png",600,378,true],"woocommerce_gallery_thumbnail":["https:\/\/fromideatocreation.com\/wp-content\/uploads\/2023\/05\/app-download-100x100.png",100,100,true],"shop_catalog":["https:\/\/fromideatocreation.com\/wp-content\/uploads\/2023\/05\/app-download-300x300.png",300,300,true],"shop_single":["https:\/\/fromideatocreation.com\/wp-content\/uploads\/2023\/05\/app-download-600x378.png",600,378,true],"shop_thumbnail":["https:\/\/fromideatocreation.com\/wp-content\/uploads\/2023\/05\/app-download-100x100.png",100,100,true]},"qubely_author":{"display_name":"Wen","author_link":"https:\/\/fromideatocreation.com\/?author=1"},"qubely_comment":0,"qubely_category":"<a href=\"https:\/\/fromideatocreation.com\/?cat=164\" rel=\"category\">Next.js<\/a> <a href=\"https:\/\/fromideatocreation.com\/?cat=106\" rel=\"category\">\u6559\u5b78<\/a>","qubely_excerpt":"\u5728 Next.js \u4e2d\uff0c\u9801\u9762\u662f\u5f9e pages \u76ee\u9304\u4e2d\u7684 .js\u3001.jsx\u3001.ts \u6216 .tsx \u6587\u4ef6\u532f\u51fa\u7684 R...","_links":{"self":[{"href":"https:\/\/fromideatocreation.com\/index.php?rest_route=\/wp\/v2\/posts\/1312","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fromideatocreation.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fromideatocreation.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fromideatocreation.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fromideatocreation.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1312"}],"version-history":[{"count":1,"href":"https:\/\/fromideatocreation.com\/index.php?rest_route=\/wp\/v2\/posts\/1312\/revisions"}],"predecessor-version":[{"id":1609,"href":"https:\/\/fromideatocreation.com\/index.php?rest_route=\/wp\/v2\/posts\/1312\/revisions\/1609"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/fromideatocreation.com\/index.php?rest_route=\/wp\/v2\/media\/1165"}],"wp:attachment":[{"href":"https:\/\/fromideatocreation.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1312"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fromideatocreation.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1312"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fromideatocreation.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1312"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}