{"id":11199,"date":"2023-05-23T14:57:14","date_gmt":"2023-05-23T06:57:14","guid":{"rendered":"http:\/\/123.57.164.21\/?p=11199"},"modified":"2023-05-23T15:03:48","modified_gmt":"2023-05-23T07:03:48","slug":"next-js%e7%ae%80%e4%bb%8b","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=11199","title":{"rendered":"Next.js\u7b80\u4ecb"},"content":{"rendered":"\n<p><strong>Next.js\u662f\u4ec0\u4e48<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2023\/05\/\u56fe\u7247-127-1024x598.png\" alt=\"\" class=\"wp-image-11200\" width=\"439\" height=\"256\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/05\/\u56fe\u7247-127-1024x598.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2023\/05\/\u56fe\u7247-127-300x175.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2023\/05\/\u56fe\u7247-127-768x449.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2023\/05\/\u56fe\u7247-127-830x485.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2023\/05\/\u56fe\u7247-127-230x134.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/05\/\u56fe\u7247-127-350x204.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/05\/\u56fe\u7247-127-480x280.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2023\/05\/\u56fe\u7247-127.png 1274w\" sizes=\"(max-width: 439px) 100vw, 439px\" \/><\/figure><\/div>\n\n\n\n<p>Next.js \u662f\u7528\u4e8e React \u5e94\u7528\u7a0b\u5e8f\u7684 Web \u5f00\u53d1\u6846\u67b6\u3002\u5b83\u7684\u4e3b\u8981\u4f18\u70b9\u662f\u5185\u7f6e\u652f\u6301\u670d\u52a1\u5668\u7aef\u6e32\u67d3\u548c\u81ea\u52a8\u4ee3\u7801\u62c6\u5206\uff0c\u65e0\u9700\u4efb\u4f55\u914d\u7f6e\u3002<\/p>\n\n\n\n<p>\u4f20\u7edf\u7684\u7eaf React\uff1a\u4e00\u4e2a\u5178\u578b\u7684 React \u5e94\u7528\u7a0b\u5e8f\u5728\u5ba2\u6237\u7aef\u5448\u73b0\u3002\u8fd9\u79cd\u65b9\u6cd5\u6709\u4e24\u4e2a\u4e3b\u8981\u7f3a\u70b9\uff1a<\/p>\n\n\n\n<ul><li>\u6d4f\u89c8\u5668\u5fc5\u987b\u5148\u5448\u73b0\u7ad9\u70b9\uff0c\u7136\u540e\u624d\u80fd\u5411\u7528\u6237\u663e\u793a\u3002\u8fd9\u5305\u62ec\u5c06 React \u4ee3\u7801\u8f6c\u6362\u4e3a HTML\uff0c\u8fd9\u9700\u8981\u4e00\u4e9b\u65f6\u95f4\u624d\u80fd\u8ba9\u7528\u6237\u770b\u5230\u4efb\u4f55\u4e1c\u897f\u3002<\/li><li>\u641c\u7d22\u5f15\u64ce\u722c\u866b\u5fc5\u987b\u5728\u9875\u9762\u88ab\u7d22\u5f15\u4e4b\u524d\u6267\u884c Javascript \u4ee3\u7801\u3002\u8fd9\u53ef\u80fd\u4f1a\u5bfc\u81f4\u90e8\u5206\u7d22\u5f15\uff0c\u4ece\u800c\u5bfc\u81f4\u66f4\u5dee\u7684 SEO \u6392\u540d\u3002<\/li><\/ul>\n\n\n\n<p>Next.js \u6846\u67b6\u63d0\u4f9b\u670d\u52a1\u5668\u7aef\u6e32\u67d3\uff0c\u65e0\u9700\u4efb\u4f55\u914d\u7f6e\u3002\u670d\u52a1\u5668\u6e32\u67d3\u7684 HTML \u9875\u9762\u5c06\u88ab\u53d1\u9001\u5230\u5ba2\u6237\u7aef\uff0c\u8ba9\u4ed6\u4eec\u7acb\u5373\u770b\u5230\u9875\u9762\u7684\u4e3b\u8981\u5185\u5bb9\u3002\u56e0\u6b64\uff0c\u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\u7b49\u53ef\u4ee5\u9644\u52a0\u5230\u201c\u5e72\u201dHTML\uff0c\u4f7f\u5176\u5177\u6709\u4ea4\u4e92\u6027\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u79f0\u4e3a\u6c34\u5408\u4f5c\u7528\u3002<\/p>\n\n\n\n<p><strong>\u5173\u4e8e\u6e32\u67d3\u8fc7\u7a0b\uff0cNext.js \u6709\u4ee5\u4e0b\u9009\u9879\uff1a<\/strong><\/p>\n\n\n\n<ul><li>\u9884\u6e32\u67d3\uff1a\u5c06 React \u4ee3\u7801\u8f6c\u6362\u4e3a HTML \u53d1\u751f\u5728\u670d\u52a1\u5668\u7aef\u3002\u8fd9\u662f\u9ed8\u8ba4\u9009\u9879\u3002<br>&#8211; \u9759\u6001\u7ad9\u70b9\u751f\u6210\uff1a\u5185\u5bb9\u5728\u6784\u5efa\u65f6\u751f\u6210\u4e00\u6b21\uff0c\u5e76\u5728\u6bcf\u6b21\u8bf7\u6c42\u65f6\u91cd\u590d\u4f7f\u7528\u3002 HTML \u5b58\u50a8\u5728 CDN \u4e2d\uff0cCDN \u57fa\u672c\u4e0a\u662f\u4e00\u7ec4\u5206\u5e03\u5f0f\u670d\u52a1\u5668\uff0c\u53ef\u5feb\u901f\u5411\u7528\u6237\u63d0\u4f9b\u5185\u5bb9\u3002<br>&#8211; \u670d\u52a1\u5668\u7aef\u6e32\u67d3\uff1aHTML \u662f\u5728\u6bcf\u4e2a\u5ba2\u6237\u7aef\u8bf7\u6c42\u7684\u670d\u52a1\u5668\u4e0a\u751f\u6210\u7684\u3002\u8fd9\u5bf9\u4e8e\u4ea4\u4e92\u5f0f\u5185\u5bb9\u5f88\u6709\u7528\u3002<br>&#8211; \u589e\u91cf\u9759\u6001\u518d\u751f\uff1a\u5728\u540e\u53f0\u91cd\u65b0\u751f\u6210\u5355\u4e2a\u9875\u9762\u3002\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u770b\u4f5c\u662f\u9759\u6001\u7ad9\u70b9\u751f\u6210\u548c\u670d\u52a1\u5668\u7aef\u6e32\u67d3\u4e4b\u95f4\u7684\u4e2d\u95f4\u5730\u5e26\u3002<\/li><li>\u5ba2\u6237\u7aef\u6e32\u67d3\uff1a\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u5ba2\u6237\u7aef\u6e32\u67d3\u53ef\u80fd\u4ecd\u7136\u662f\u9996\u9009\u3002<\/li><\/ul>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2023\/05\/\u56fe\u7247-128-1024x526.png\" alt=\"\" class=\"wp-image-11201\" width=\"463\" height=\"238\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/05\/\u56fe\u7247-128-1024x526.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2023\/05\/\u56fe\u7247-128-300x154.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2023\/05\/\u56fe\u7247-128-768x395.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2023\/05\/\u56fe\u7247-128-830x427.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2023\/05\/\u56fe\u7247-128-230x118.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/05\/\u56fe\u7247-128-350x180.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/05\/\u56fe\u7247-128-480x247.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2023\/05\/\u56fe\u7247-128.png 1288w\" sizes=\"(max-width: 463px) 100vw, 463px\" \/><\/figure><\/div>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Next.js\u7684\u4e00\u4e9b\u7279\u6027\uff1a<\/strong>\n\n    \u652f\u6301\u4e24\u79cd\u9884\u6e32\u67d3\u65b9\u5f0f, \u9759\u6001\u751f\u6210SSG\u548c\u670d\u52a1\u5668\u7aef\u6e32\u67d3SSR.\n    \u57fa\u4e8e\u9875\u9762\u7684\u8def\u7531\u7cfb\u7edf, \u8def\u7531\u96f6\u914d\u7f6e\n    \u81ea\u52a8\u4ee3\u7801\u62c6\u5206. \u4f18\u5316\u9875\u9762\u52a0\u8f7d\u901f\u5ea6.\n    \u652f\u6301\u9759\u6001\u5bfc\u51fa, \u53ef\u5c06\u5e94\u7528\u5bfc\u51fa\u4e3a\u9759\u6001\u7f51\u7ad9.\n    \u5185\u7f6e CSS-in-JS \u5e93 styled-jsx\n    \u65b9\u6848\u6210\u719f, \u53ef\u7528\u4e8e\u751f\u4ea7\u73af\u5883, \u4e16\u754c\u8bb8\u591a\u516c\u53f8\u90fd\u5728\u4f7f\u7528\n    \u5e94\u7528\u90e8\u7f72\u7b80\u5355, \u62e5\u6709\u4e13\u5c5e\u90e8\u7f72\u73af\u5883 Vercel, \u4e5f\u53ef\u4ee5\u90e8\u7f72\u5728\u5176\u4ed6\u73af\u5883\n\n<\/pre>\n\n\n\n<p>\u867d\u7136 Next.js \u603b\u88ab\u4eba\u79f0\u4e3a SSR \u6846\u67b6\uff0c\u5176\u5b9e Next.js \u8fd8\u63d0\u4f9b\u4e86\u5f88\u591a\u529f\u80fd\uff0c\u6bd4\u5982\u5b98\u7f51\u5217\u51fa\u6765\u7684\u8fd9\u4e9b\uff1a<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2023\/05\/\u56fe\u7247-129-1024x608.png\" alt=\"\" class=\"wp-image-11202\" width=\"519\" height=\"307\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/05\/\u56fe\u7247-129-1024x608.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2023\/05\/\u56fe\u7247-129-300x178.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2023\/05\/\u56fe\u7247-129-768x456.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2023\/05\/\u56fe\u7247-129-830x493.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2023\/05\/\u56fe\u7247-129-230x137.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/05\/\u56fe\u7247-129-350x208.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/05\/\u56fe\u7247-129-480x285.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2023\/05\/\u56fe\u7247-129.png 1256w\" sizes=\"(max-width: 519px) 100vw, 519px\" \/><\/figure><\/div>\n\n\n\n<p>\u6240\u4ee5\u8bf4\uff0cNext.js \u66f4\u50cf\u662f\u4e00\u4e2a\u6846\u67b6\uff0c\u5305\u542b\u4e86\u8def\u7531\u3001\u4f18\u5316\u3001SSR \u7b49\u4e00\u7cfb\u5217\u529f\u80fd\u3002<\/p>\n\n\n\n<p><strong>\u8d77\u6b65<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u4ee5\u524d\u4e00\u76f4\u5728\u7528 <strong>create-react-app<\/strong> \u6765\u521b\u5efa React \u9879\u76ee\uff0c\u5948\u4f55 CRA \u5b9e\u5728\u592a\u96be\u7528\u4e86\uff0c\u4eca\u5929\u82b1\u4e86\u70b9\u65f6\u95f4\u626b\u4e86\u4e00\u4e0b Next.js \u7684\u5b98\u7f51\uff0c\u53d1\u73b0\u7528\u8d77\u6765\u8fd8\u633a\u7b80\u5355\u7684\u3002<\/p>\n\n\n\n<p>\u548c create-react-app \u4e00\u6837\uff0cNext.js \u4e00\u6837\u6709\u4e2a <strong>create-next-app<\/strong> \u7684\u811a\u624b\u67b6\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">create-next-app demo<\/pre>\n\n\n\n<p>\u4f7f\u7528\u4e0a\u9762\u547d\u4ee4\u540e\u5c31\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a Next.js \u6846\u67b6\u7684 React \u9879\u76ee\u3002<\/p>\n\n\n\n<p><strong>\u8def\u7531<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>Next.js \u4e5f\u63d0\u4f9b\u4e86\u8def\u7531\u7cfb\u7edf\uff0c\u91c7\u7528\u540d\u5b57\u7ea6\u5b9a\u8def\u7531<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">    pages\/index.js \u5bf9\u5e94 \/\n    pages\/xxx\/first.js \u5bf9\u5e94 \/xxx\/first<\/pre>\n\n\n\n<p>\u4f7f\u7528 <code>Link<\/code> \u7ec4\u4ef6\u6765\u505a\u8def\u7531\u8df3\u8f6c<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">function FirstPost() {\n  return (\n    &lt;>\n      &lt;h1>First Post&lt;\/h1>\n      &lt;h2>\n        &lt;Link href=\"\/\">\n          &lt;a>Back to home&lt;\/a>\n        &lt;\/Link>\n      &lt;\/h2>\n    &lt;\/>\n  )\n}\n<\/pre>\n\n\n\n<p>\u6709\u7684\u65f6\u5019\u9700\u8981\u8df3\u8f6c\u5230\u5916\u94fe\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>&lt;a&gt;<\/code> \u6807\u7b7e\u5c31\u53ef\u4ee5\u3002<\/p>\n\n\n\n<p><strong>\u9759\u6001\u8d44\u6e90<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u9759\u6001\u8d44\u6e90\u7528\u7684\u6700\u591a\u7684\u5c31\u662f <strong>\u56fe\u7247<\/strong> \u4e86\uff0cNext.js \u63d0\u4f9b\u4e86 <code>Image<\/code> \u7ec4\u4ef6\u6765\u66ff\u4ee3 <code>img<\/code>\u3002<\/p>\n\n\n\n<p><code>Image<\/code> \u7ec4\u4ef6\u7684\u597d\u5904\u5c31\u662f\u53ef\u4ee5\u63d0\u9ad8\u7f51\u9875\u52a0\u8f7d\u56fe\u7247\u7684\u6027\u80fd\uff0c\u53ef\u4ee5\u81ea\u52a8\u6309\u9700\u52a0\u8f7d\uff0c\u5f53\u56fe\u7247\u8fdb\u5165\u89c6\u56fe\u65f6\u518d\u52a0\u8f7d\u56fe\u7247\u3002<\/p>\n\n\n\n<p>\u9664\u4e86\u76f8\u5bf9\u8def\u5f84\u5f15\u5165\uff0c\u8fd8\u53ef\u4ee5\u5c06\u56fe\u7247\u653e\u5728 <code>public\/images\/<\/code> \u4e0b\uff0c\u7136\u540e\u7528 \u201c\u7edd\u5bf9\u8def\u5f84\u201d \u5f15\u5165\u3002<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2023\/05\/\u56fe\u7247-130.png\" alt=\"\" class=\"wp-image-11205\" width=\"177\" height=\"112\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/05\/\u56fe\u7247-130.png 260w, https:\/\/92it.top\/wp-content\/uploads\/2023\/05\/\u56fe\u7247-130-230x145.png 230w\" sizes=\"(max-width: 177px) 100vw, 177px\" \/><\/figure><\/div>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">export default function Home() {\n  return (\n    &lt;>\n      &lt;h1>My Homepage&lt;\/h1>\n      &lt;Image\n        src=\"\/profile.jpg\"\n        alt=\"Picture of the author\"\n        width={500}\n        height={500}\n      \/>\n      &lt;p>Welcome to my homepage!&lt;\/p>\n    &lt;\/>\n  )\n}\n<\/pre>\n\n\n\n<p><strong>MetaData<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u7f51\u9875\u7684 Meta Data \u4e3b\u8981\u662f\u6307 <code>&lt;head&gt;<\/code> \u5143\u7d20\u91cc\u7684\u5185\u5bb9\uff0cNext.js \u76f4\u63a5\u63d0\u4f9b\u4e86\u4e00\u4e2a <code>&lt;Head&gt;<\/code> \u7ec4\u4ef6\u6765\u5305\u88f9\u8fd9\u4e9b Meta Data\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;Head>\n   &lt;title>First Post&lt;\/title>\n&lt;\/Head>\n<\/pre>\n\n\n\n<p>\u597d\u5904\u5c31\u662f\u53ef\u4ee5\u5728\u4e0d\u540c\u7684\u9875\u9762\u7ec4\u4ef6\u91cc\u5199\u4e0d\u540c\u7684 Meta Data\u3002<\/p>\n\n\n\n<p><strong>CSS<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u6837\u5f0f\u8fd9\u4e00\u5757\u548c <code>create-react-app<\/code> \u5dee\u4e0d\u591a\uff0c\u4f7f\u7528 CSS module\uff0c\u547d\u540d\u4e3a <code>xxx.module.css<\/code> \u5c31\u53ef\u4ee5\u4e86\uff0c\u5426\u5219\u522b\u7684 CSS \u6587\u4ef6\u90fd\u9700\u8981 <code>import 'xxx.css'<\/code> \u6765\u5f15\u5165 CSS \u6837\u5f0f\u3002<\/p>\n\n\n\n<p><strong>\u9700\u8981\u6ce8\u610f\u7684\u662f\u5168\u5c40\u6837\u5f0f\u5f15\u5165\u53ea\u80fd\u5728 <code>pages\/_app.js<\/code> \u7684\u6839\u6587\u4ef6\u91cc\u5f15\u5165\u3002<\/strong><\/p>\n\n\n\n<p>\u4e0a\u8ff0\u64cd\u4f5c Sass \u540c\u7406\u3002<\/p>\n\n\n\n<p><strong>\u9884\u6e32\u67d3<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u7ec8\u4e8e\u6765\u5230 Next.js \u6700\u5f15\u4ee5\u4e3a\u8c6a\u7684 <strong>\u9884\u6e32\u67d3<\/strong> \u4e86\u3002Next.js \u63d0\u4f9b\u4e86\u4e09\u79cd\u6e32\u67d3\u65b9\u5f0f\uff1a<\/p>\n\n\n\n<ul><li>Client-side Rendering (CSR)<\/li><li>Static Generation (SSG)<\/li><li>Server-side Rendering (SSR)<\/li><\/ul>\n\n\n\n<p><strong>Client-side Rendering<\/strong><\/p>\n\n\n\n<p>\u5ba2\u6237\u7aef\u6e32\u67d3\u5176\u5b9e\u5c31\u662f\u6211\u4eec\u7ecf\u5e38\u770b\u5230\u7684\u524d\u540e\u7aef\u5206\u79bb\u7684\u573a\u666f\u4e86\uff1a\u53ea\u63d0\u4f9b\u4e00\u4e2a html\uff0c\u62ff\u5230 <code>&lt;script&gt;<\/code> \u7684 JS \u518d\u53bb\u6e32\u67d3\u9875\u9762\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import useSWR from 'swr'\n\nfunction Profile() {\n  const { data, error } = useSWR('\/api\/user', fetch)\n\n  if (error) return &lt;div>failed to load&lt;\/div>\n  if (!data) return &lt;div>loading...&lt;\/div>\n  return &lt;div>hello {data.name}!&lt;\/div>\n}\n<\/pre>\n\n\n\n<p>\u7531\u4e8e\u9700\u8981\u7b49\u52a0\u8f7d\u5230 JS \u518d\u6e32\u67d3\u9875\u9762\uff0c\u6240\u4ee5\u8fd9\u79cd\u6e32\u67d3\u65b9\u5f0f\u7684\u6709\u4ee5\u4e0b\u7f3a\u70b9\uff1a <em>SEO \u4e0d\u53cb\u597d<\/em> \u767d\u5c4f\u65f6\u95f4\u8f83\u957f<\/p>\n\n\n\n<p>\u806a\u660e\u7684\u524d\u7aef\u7a0b\u5e8f\u5458\u5c31\u60f3\uff1a\u5f53\u8bbf\u95ee URL \u7684\u65f6\u5019\uff0c\u6211\u76f4\u63a5\u628a\u6570\u636e\u90fd\u653e\u5230 HTML \u4e0a\uff0c\u90a3\u722c\u866b\u5c31\u53ef\u4ee5\u76f4\u63a5\u62ff\u5230\u9875\u9762\u7684\u4fe1\u606f\uff0c\u89e3\u51b3 SEO \u7684\u95ee\u9898\u4e86\uff0c\u540c\u6837\u7684\u4e0d\u9700\u8981\u7b49 JS \u52a0\u8f7d\u5b8c\u518d\u53d1 Ajax \u83b7\u53d6\u6570\u636e\u4e86\uff0c\u57fa\u7840\u6570\u636e\u4f18\u5148\u5c55\u793a\uff0c\u4e5f\u5c31\u80fd\u89e3\u51b3\u767d\u5c4f\u65f6\u95f4\u8fc7\u957f\u7684\u95ee\u9898\u4e86\u3002<\/p>\n\n\n\n<p>\u6240\u4ee5\uff0c\u9884\u6e32\u67d3\u8bf4\u7684\u5c31\u662f SSG \u548c SSR\u3002<\/p>\n\n\n\n<p><strong>Static Generation<\/strong><\/p>\n\n\n\n<p>Static Generation \u4f1a\u5728 <strong>build time \u7684 production<\/strong> \u65f6\u5019\u76f4\u63a5\u5c06\u6570\u636e\u5199\u5728 HTML \u4e0a\uff0c\u6240\u4ee5\u4e00\u822c\u6765\u8bf4\u8fd9\u4e9b\u6570\u636e\u90fd\u662f\u4ee5\u9759\u6001\u3001\u56fa\u5b9a\u4e3a\u4e3b\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">export async function getStaticProps() {\n  const allPostsData = getSortedPostsData()\n  return {\n    props: {\n      allPostsData\n    }\n  }\n}\n<\/pre>\n\n\n\n<p>\u5c06 <code>getStaticProps<\/code> \u8fd9\u4e2a\u51fd\u6570 <code>export<\/code> \u51fa\u6765\uff0c\u91cc\u9762\u5219\u4e3a build time \u65f6\u83b7\u53d6\u6570\u636e\u7684\u903b\u8f91\u3002<\/p>\n\n\n\n<p><strong>Server-side Rendering<\/strong><\/p>\n\n\n\n<p>\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u5f88\u5c11\u4f7f\u7528\u9759\u6001\u7684\u6570\u636e\uff0c\u4e00\u822c\u4ee5\u52a8\u6001\u6570\u636e\u4e3a\u4e3b\uff0c\u4e0d\u53ef\u80fd\u6bcf\u6b21\u6570\u636e\u66f4\u65b0\u4e86\u53c8\u8981\u6253\u5305\u4e00\u904d\u3002\u6240\u4ee5\u5c31\u6709\u4e86 Server-side Rendring\u3002<\/p>\n\n\n\n<p>Server-side Rendering \u5219\u5728\u6bcf\u6b21 <strong>\u8bf7\u6c42\u8fd9\u4e2a URL<\/strong> \u7684\u65f6\u5019\uff0c\u90fd\u4f1a\u6267\u884c\u4e00\u6b21\u6570\u636e\u83b7\u53d6\u5e76\u751f\u6210 HTML \u8fd4\u56de\u7ed9\u524d\u7aef\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"> \u770b\u5230\u8fd9\u91cc\u4f60\u53ef\u80fd\u4f1a\u60f3 Next.js \u548c\u4ee5\u524d\u7684 PHP\u3001JSP \u6709\u4ec0\u4e48\u533a\u522b\u4e48\uff1f\u90fd\u662f\u5410 HTML \u7684\u5440\u3002 Next.js \u8fd9\u91cc\u7684 SSR \u5176\u5b9e\u662f\u540c\u6784\u6e32\u67d3\uff0c\u5373\u4e00\u5957\u4ee3\u7801\u4e24\u7aef\u6267\u884c\uff0c\u5177\u4f53\u533a\u522b\u8bf7\u770b\u4e0b\u9762\u56de\u7b54<\/pre>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2023\/05\/\u56fe\u7247-135-1024x887.png\" alt=\"\" class=\"wp-image-11211\" width=\"470\" height=\"406\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/05\/\u56fe\u7247-135-1024x887.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2023\/05\/\u56fe\u7247-135-300x260.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2023\/05\/\u56fe\u7247-135-830x719.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2023\/05\/\u56fe\u7247-135-230x199.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/05\/\u56fe\u7247-135-350x303.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/05\/\u56fe\u7247-135-480x416.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2023\/05\/\u56fe\u7247-135.png 1334w\" sizes=\"(max-width: 470px) 100vw, 470px\" \/><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2023\/05\/\u56fe\u7247-134-966x1024.png\" alt=\"\" class=\"wp-image-11210\" width=\"447\" height=\"474\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/05\/\u56fe\u7247-134-966x1024.png 966w, https:\/\/92it.top\/wp-content\/uploads\/2023\/05\/\u56fe\u7247-134-283x300.png 283w, https:\/\/92it.top\/wp-content\/uploads\/2023\/05\/\u56fe\u7247-134-768x814.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2023\/05\/\u56fe\u7247-134-830x880.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2023\/05\/\u56fe\u7247-134-230x244.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/05\/\u56fe\u7247-134-350x371.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/05\/\u56fe\u7247-134-480x509.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2023\/05\/\u56fe\u7247-134.png 1324w\" sizes=\"(max-width: 447px) 100vw, 447px\" \/><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2023\/05\/\u56fe\u7247-133-1024x774.png\" alt=\"\" class=\"wp-image-11209\" width=\"446\" height=\"337\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/05\/\u56fe\u7247-133-1024x774.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2023\/05\/\u56fe\u7247-133-300x227.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2023\/05\/\u56fe\u7247-133-768x580.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2023\/05\/\u56fe\u7247-133-830x627.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2023\/05\/\u56fe\u7247-133-230x174.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/05\/\u56fe\u7247-133-350x264.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/05\/\u56fe\u7247-133-480x363.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2023\/05\/\u56fe\u7247-133.png 1326w\" sizes=\"(max-width: 446px) 100vw, 446px\" \/><\/figure><\/div>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">export async function getServerSideProps(context) {\n  return {\n    props: {\n      \/\/ props for your component\n    }\n  }\n}\n<\/pre>\n\n\n\n<p><strong>\u52a8\u6001\u8def\u7531<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u6240\u8c13\u52a8\u6001\u8def\u7531\u5c31\u662f\u53ef\u4ee5\u751f\u6210 <code>posts\/:id<\/code> \u8fd9\u6837\u7684\u8def\u7531\uff0c<code>:id<\/code> \u53ef\u4ee5\u4e3a post \u7684 id\u3002\u6587\u4ef6\u547d\u540d\u53ea\u9700\u8981\u5199\u6210 <code>[id].js<\/code> \u5c31\u53ef\u4ee5\u4e86\u3002<\/p>\n\n\n\n<p>\u5728\u9875\u9762\u7ec4\u4ef6\u6587\u4ef6\u91cc\uff0c\u53ef\u4ee5\u901a\u8fc7\u524d\u9762\u8bf4\u5230\u7684 <code>getStaticProps<\/code> \u548c <code>getServerSideProps<\/code> \u83b7\u53d6 params\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">export async function getStaticProps({ params }) {\n  const postData = getPostData(params.id)\n  return {\n    props: {\n      postData\n    }\n  }\n}\n<\/pre>\n\n\n\n<p>\u5176\u4e2d\uff0c<code>pages\/posts\/[...id].js<\/code> \u4f1a\u5339\u914d <code>\/posts\/a\/b<\/code> \u8def\u7531\u548c <code>\/posts\/a<\/code>\uff0c<code>...<\/code> \u4e3a\u5168\u5339\u914d\u3002<\/p>\n\n\n\n<p><strong>API<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u9664\u4e86\u6b63\u5e38\u5199\u9875\u9762\u5916\uff0c\u6709\u65f6\u5019\u6211\u4eec\u8fd8\u9700\u8981\u63d0\u4f9b API \u63a5\u53e3\uff0c\u53ef\u4ee5\u5728 <code>pages\/api<\/code> \u4e0b\u6dfb\u52a0\u6587\u4ef6\uff0c\u6587\u4ef6\u540d\u5219\u4e3a API \u540d\u3002<\/p>\n\n\n\n<p>\u6ce8\u610f\uff1a<strong>\u4e0d\u80fd\u5728 <code>getStaticProps<\/code> \u548c <code>getStaticPaths<\/code> \u91cc\u6dfb\u52a0 fetch \u6570\u636e\uff0c\u56e0\u4e3a\u4ed6\u4eec\u53ea\u5728 server side \u8fd0\u884c\uff0c\u4e0d\u4f1a\u5728 client side \u8fd0\u884c\uff0c\u5e94\u8be5\u4f7f\u7528 helper function \u6765\u83b7\u53d6\u6570\u636e\u3002<\/strong><\/p>\n\n\n\n<p>API \u4ee3\u7801\u5c06\u4e0d\u4f1a\u5728 client side \u7684 bundle \u91cc\u3002<\/p>\n\n\n\n<p><strong>\u90e8\u7f72<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u90e8\u7f72\u8fd9\u4e00\u5757 Next.js \u63a8\u8350\u4f7f\u7528 <a href=\"https:\/\/link.zhihu.com\/?target=https%3A\/\/vercel.com\/dashboard\" target=\"_blank\" rel=\"noreferrer noopener\">Vercel<\/a> \u6765\u90e8\u7f72\u3002<\/p>\n\n\n\n<p>\u56e0\u4e3a Vercel \u672c\u8eab\u5c31\u662f\u4e3a Next.js \u670d\u52a1\u7684\uff0c\u6240\u4ee5\u53ea\u9700\u8981\u8fde\u4e0a Github Repo \u5c31\u53ef\u4ee5\u4e00\u952e\u90e8\u7f72\u4e86\u3002<\/p>\n\n\n\n<p><strong>\u603b\u7ed3<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u7a0d\u5fae\u603b\u7ed3\u4e00\u4e0b\uff0cNext.js \u63d0\u4f9b\u7684\u6709\u5982\u4e0b\u529f\u80fd\uff1a<\/p>\n\n\n\n<ul><li><code>Link<\/code> \u7ec4\u4ef6\uff0c\u65b9\u4fbf\u8def\u7531<\/li><li><code>Image<\/code> \u7ec4\u4ef6\uff0c\u4f18\u5316\u56fe\u7247\u52a0\u8f7d<\/li><li>\u6587\u4ef6\u8def\u5f84\u751f\u6210\u8def\u7531\u673a\u5236\uff0c\u52a8\u6001\u8def\u7531\u4f7f\u7528 <code>[id].js<\/code> \u8fd9\u6837\u7684\u547d\u4ee4<\/li><li>SSR\u3001SSG \u7684\u540c\u6784\u5f00\u53d1\u6a21\u5f0f\uff08\u5176\u5b9e\u5c31\u662f export \u4e00\u4e2a\u5bf9\u5e94\u540d\u5b57\u7684\u51fd\u6570\uff0c\u5728\u91cc\u9762\u63d0\u524d\u83b7\u53d6\u6570\u636e\u5c31\u597d\u4e86\uff09<\/li><li>\u6837\u5f0f\u65b9\u9762\u548c <code>create-react-app<\/code> \u5dee\u4e0d\u591a<\/li><\/ul>\n\n\n\n<ul><li>React\u662f\u6784\u5efa\u7528\u6237\u754c\u9762\u7684\u5e93\u3002<\/li><li>Next.js\u4f7f\u7528React\u3002\u5b83\u662f\u670d\u52a1\u5668\u6e32\u67d3\u7684React\u5e94\u7528\u7a0b\u5e8f\u7684\u6846\u67b6\u3002<\/li><li>Nextjs\u5141\u8bb8\u60a8\u5728\u670d\u52a1\u5668\u4e0a\u5448\u73b0React\u5e94\u7528\u7a0b\u5e8f\uff08SSR\uff09\u3002\u5982\u679c\u4f60\u68c0\u67e5\u4e00\u4e2a\u53cd\u5e94\u5e94\u7528\u7a0b\u5e8f\u7684\u6e90\u4ee3\u7801\uff0c\u5b83\u662f\u975e\u5e38\u7a7a\u7684\uff0c\u8fd9\u4e0d\u9002\u5408\u641c\u7d22\u5f15\u64ce\u4f18\u5316\u4e8b\u5b9c\uff0c\u4ee5\u53ca\u793e\u4ea4\u5a92\u4f53\u5171\u4eab\u3002\u968f\u7740Nextjs\uff0c\u4e00\u4e2a\u53cd\u5e94\u7684\u5e94\u7528\u7a0b\u5e8f\u53d8\u5f97\u666e\u904d\uff0c\u6240\u4ee5\u5b83\u5728\u670d\u52a1\u5668\u548c\u5ba2\u6237\u7aef\u90fd\u5448\u73b0\u3002<\/li><\/ul>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Next.js\u662f\u4ec0\u4e48 Next.js \u662f\u7528\u4e8e React \u5e94\u7528\u7a0b\u5e8f\u7684 Web \u5f00\u53d1\u6846\u67b6\u3002\u5b83\u7684\u4e3b\u8981\u4f18\u70b9\u662f\u5185\u7f6e\u652f\u6301 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[33],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/11199"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=11199"}],"version-history":[{"count":5,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/11199\/revisions"}],"predecessor-version":[{"id":11213,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/11199\/revisions\/11213"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=11199"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=11199"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=11199"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}