{"id":53379,"date":"2024-12-03T17:24:55","date_gmt":"2024-12-03T09:24:55","guid":{"rendered":"https:\/\/fwq.ai\/blog\/53379\/"},"modified":"2024-12-03T17:24:55","modified_gmt":"2024-12-03T09:24:55","slug":"nextjs%ef%bc%9a%e5%85%b7%e6%9c%89-api-%e9%9b%86%e6%88%90%e7%9a%84%e5%8a%a8%e6%80%81%e8%b7%af%e7%94%b1","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/53379\/","title":{"rendered":"Nextjs\uff1a\u5177\u6709 API \u96c6\u6210\u7684\u52a8\u6001\u8def\u7531"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Nextjs\uff1a\u5177\u6709 API \u96c6\u6210\u7684\u52a8\u6001\u8def\u7531<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span>    <\/p>\n<p><span style=\"color: #222222;, sans-serif;background-color: #FFFFFF\">\u5404\u4f4d\u5c0f\u4f19\u4f34\u4eec\uff0c\u5927\u5bb6\u597d\u5440\uff01\u770b\u770b\u4eca\u5929\u6211\u53c8\u7ed9\u5404\u4f4d\u5e26\u6765\u4e86\u4ec0\u4e48\u6587\u7ae0\uff1f\u672c\u6587\u6807\u9898<span style=\"color: #222222;, sans-serif;background-color: #FFFFFF\">\u662f<\/span><span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aNextjs\uff1a\u5177\u6709 API \u96c6\u6210\u7684\u52a8\u6001\u8def\u7531\u300b<\/span>\uff0c\u5f88\u660e\u663e\u662f\u5173\u4e8e<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u7684\u6587\u7ae0\u54c8\u54c8\u54c8\uff0c\u5176\u4e2d\u5185\u5bb9\u4e3b\u8981\u4f1a\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\uff0c\u5982\u679c\u80fd\u5e2e\u5230\u4f60\uff0c\u89c9\u5f97\u5f88\u4e0d\u9519\u7684\u8bdd\uff0c\u6b22\u8fce\u5404\u4f4d\u591a\u591a\u70b9\u8bc4\u548c\u5206\u4eab\uff01<\/span><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241129\/17328863306749bf3a6bb13.jpg\" class=\"aligncenter\" title=\"Nextjs\uff1a\u5177\u6709 API \u96c6\u6210\u7684\u52a8\u6001\u8def\u7531\u63d2\u56fe\" alt=\"Nextjs\uff1a\u5177\u6709 API \u96c6\u6210\u7684\u52a8\u6001\u8def\u7531\u63d2\u56fe\" \/><\/p>\n<p><strong>\u60f3\u6cd5<\/strong><\/p>\n<p>next.js \u63d0\u4f9b\u4e86\u4e00\u4e2a\u57fa\u4e8e\u6587\u4ef6\u7684\u8def\u7531\u7cfb\u7edf\uff0c\u652f\u6301\u52a8\u6001\u8def\u7531\uff08\u4f8b\u5982 \/product\/[id]\uff09\u3002\u60a8\u53ef\u4ee5\u5c06\u5176\u4e0e\u52a8\u6001\u6570\u636e\u83b7\u53d6\u7ed3\u5408\u8d77\u6765\uff0c\u521b\u5efa\u7075\u6d3b\u4e14\u53ef\u6269\u5c55\u7684\u5e94\u7528\u7a0b\u5e8f\u3002\u8fd9\u5bf9\u4e8e\u7535\u5b50\u5546\u52a1\u4ea7\u54c1\u9875\u9762\u3001\u7528\u6237\u4e2a\u4eba\u8d44\u6599\u6216\u4efb\u4f55\u5177\u6709\u552f\u4e00\u6807\u8bc6\u7b26\u7684\u5185\u5bb9\u7b49\u60c5\u51b5\u7279\u522b\u6709\u7528\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a\u52a8\u6001\u4ea7\u54c1\u9875\u9762<\/strong><\/p>\n<p><strong>1\u3002\u8bbe\u7f6e\u52a8\u6001\u8def\u7ebf<\/strong><\/p>\n<p>\u5728 \/pages\/product\/ \u7b49\u6587\u4ef6\u5939\u4e2d\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a [id].tsx \u7684\u6587\u4ef6\uff1a<\/p>\n<p>\u9875\u9762\/\u4ea7\u54c1\/[id].tsx<\/p>\n<p><strong>2\u3002\u83b7\u53d6\u52a8\u6001\u8def\u7531\u7684\u6570\u636e<\/strong><\/p>\n<pre>\/\/ pages\/product\/[id].tsx\n\nimport { getstaticpaths, getstaticprops } from 'next';\n\ntype productprops = {\n  product: {\n    id: string;\n    name: string;\n    description: string;\n    price: number;\n  };\n};\n\nexport default function productpage({ product }: productprops) {\n  return (\n    &lt;div&gt;\n      &lt;h1&gt;{product.name}&lt;\/h1&gt;\n      &lt;p&gt;{product.description}&lt;\/p&gt;\n      &lt;p&gt;price: ${product.price}&lt;\/p&gt;\n    &lt;\/div&gt;\n  );\n}\n\n\/\/ generate dynamic paths for the product pages\nexport const getstaticpaths: getstaticpaths = async () =&gt; {\n  const res = await fetch('https:\/\/api.example.com\/products');\n  const products = await res.json();\n\n  \/\/ map over the products to define paths\n  const paths = products.map((product: { id: string }) =&gt; ({\n    params: { id: product.id },\n  }));\n\n  return {\n    paths, \/\/ pre-render these paths at build time\n    fallback: 'blocking', \/\/ dynamically render other pages on request\n  };\n};\n\n\/\/ fetch product data for each page\nexport const getstaticprops: getstaticprops = async ({ params }) =&gt; {\n  const res = await fetch(`https:\/\/api.example.com\/products\/${params?.id}`);\n  const product = await res.json();\n\n  \/\/ pass the product data as props\n  return {\n    props: { product },\n    revalidate: 10, \/\/ revalidate every 10 seconds\n  };\n};\n<\/pre>\n<p><strong>3\u3002\u5904\u7406\u4e0d\u5b58\u5728\u7684\u9875\u9762<\/strong><\/p>\n<p>\u8981\u5904\u7406 id \u4e0d\u5b58\u5728\u7684\u60c5\u51b5\uff0c\u8bf7\u5728 getstaticprops \u4e2d\u8fd4\u56de notfound \u5c5e\u6027\uff1a<\/p>\n<pre>export const getStaticProps: GetStaticProps = async ({ params }) =&gt; {\n  const res = await fetch(`https:\/\/api.example.com\/products\/${params?.id}`);\n\n  if (res.status === 404) {\n    return { notFound: true };\n  }\n\n  const product = await res.json();\n\n  return {\n    props: { product },\n    revalidate: 10,\n  };\n};\n<\/pre>\n<p><strong>\u6b64\u65b9\u6cd5\u7684\u4e3b\u8981\u7279\u70b9\uff1a<\/strong><\/p>\n<ol>\n<li>\n<p><strong>seo \u53cb\u597d<\/strong>\uff1a\u9875\u9762\u4f7f\u7528\u5b8c\u6574\u7684 html \u8fdb\u884c\u9884\u6e32\u67d3\uff0c\u975e\u5e38\u9002\u5408\u641c\u7d22\u5f15\u64ce\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u53ef\u6269\u5c55<\/strong>\uff1a\u60a8\u53ef\u4ee5\u4f7f\u7528\u56de\u9000\u6e32\u67d3\uff08fallback\uff1a&#8217;blocking&#8217;\uff09\u4e3a\u65b0\u6570\u636e\u52a8\u6001\u751f\u6210\u9875\u9762\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u5b9e\u65f6\u66f4\u65b0<\/strong>\uff1a\u4e0e\u91cd\u65b0\u9a8c\u8bc1\u76f8\u7ed3\u5408\uff0c\u786e\u4fdd\u6570\u636e\u4fdd\u6301\u6700\u65b0\uff0c\u65e0\u9700\u624b\u52a8\u90e8\u7f72\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u9519\u8bef\u5904\u7406<\/strong>\uff1a\u4f7f\u7528 notfound \u4f18\u96c5\u5730\u5904\u7406 404 \u6216\u5176\u4ed6\u9519\u8bef\u3002<\/p>\n<\/li>\n<\/ol>\n<p>\u6b64\u65b9\u6cd5\u5141\u8bb8\u60a8\u6784\u5efa\u9ad8\u5ea6\u52a8\u6001\u4e14\u54cd\u5e94\u8fc5\u901f\u4e14\u6613\u4e8e\u6269\u5c55\u7684 web \u5e94\u7528\u7a0b\u5e8f\uff01<\/p>\n<p>\u4eca\u5929\u5173\u4e8e\u300aNextjs\uff1a\u5177\u6709 API \u96c6\u6210\u7684\u52a8\u6001\u8def\u7531\u300b\u7684\u5185\u5bb9\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\uff0c\u5982\u679c\u6709\u4ec0\u4e48\u7591\u95ee\u6216\u8005\u5efa\u8bae\uff0c\u53ef\u4ee5\u5728\u7c73\u4e91\u516c\u4f17\u53f7\u4e0b\u591a\u591a\u56de\u590d\u4ea4\u6d41\uff1b\u6587\u4e2d\u82e5\u6709\u4e0d\u6b63\u4e4b\u5904\uff0c\u4e5f\u5e0c\u671b\u56de\u590d\u7559\u8a00\u4ee5\u544a\u77e5\uff01<\/p>\n<p>      \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1adev.to \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb\u5220\u9664    <\/p>\n<dl>\n<dt><\/dt>\n<dd>\n   \u5982\u4f55\u5c06\u6570\u7ec4\u4e2d\u5bf9\u8c61\u7684\u952e\u540d\u66ff\u6362\u4e3a\u53e6\u4e00\u4e2a\u6570\u7ec4\u63d0\u4f9b\u7684\u952e\u540d\uff1f\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>Nextjs\uff1a\u5177\u6709 API \u96c6\u6210\u7684\u52a8\u6001\u8def\u7531 \u6536\u85cf \u5404\u4f4d\u5c0f\u4f19\u4f34\u4eec\uff0c\u5927\u5bb6\u597d\u5440\uff01\u770b\u770b\u4eca\u5929\u6211\u53c8\u7ed9\u5404\u4f4d\u5e26\u6765\u4e86\u4ec0\u4e48\u6587\u7ae0\uff1f\u672c\u6587\u6807\u9898\u662f\u300aNextjs\uff1a\u5177\u6709 API \u96c6\u6210\u7684\u52a8\u6001\u8def\u7531\u300b\uff0c\u5f88\u660e\u663e\u662f\u5173\u4e8e\u6587\u7ae0\u7684\u6587\u7ae0\u54c8\u54c8\u54c8\uff0c\u5176\u4e2d\u5185\u5bb9\u4e3b\u8981\u4f1a\u6d89\u53ca\u5230\u7b49\u7b49\uff0c\u5982\u679c\u80fd\u5e2e\u5230\u4f60\uff0c\u89c9\u5f97\u5f88\u4e0d\u9519\u7684\u8bdd\uff0c\u6b22\u8fce\u5404\u4f4d\u591a\u591a\u70b9\u8bc4\u548c\u5206\u4eab\uff01 \u60f3\u6cd5 next.js \u63d0\u4f9b\u4e86\u4e00\u4e2a\u57fa\u4e8e\u6587\u4ef6\u7684\u8def\u7531\u7cfb\u7edf\uff0c\u652f\u6301\u52a8\u6001\u8def\u7531\uff08\u4f8b\u5982 \/product\/[id]\uff09\u3002\u60a8\u53ef\u4ee5\u5c06\u5176\u4e0e\u52a8\u6001\u6570\u636e\u83b7\u53d6\u7ed3\u5408\u8d77\u6765\uff0c\u521b\u5efa\u7075\u6d3b\u4e14\u53ef\u6269\u5c55\u7684\u5e94\u7528\u7a0b\u5e8f\u3002\u8fd9\u5bf9\u4e8e\u7535\u5b50\u5546\u52a1\u4ea7\u54c1\u9875\u9762\u3001\u7528\u6237\u4e2a\u4eba\u8d44\u6599\u6216\u4efb\u4f55\u5177\u6709\u552f\u4e00\u6807\u8bc6\u7b26\u7684\u5185\u5bb9\u7b49\u60c5\u51b5\u7279\u522b\u6709\u7528\u3002 \u793a\u4f8b\uff1a\u52a8\u6001\u4ea7\u54c1\u9875\u9762 1\u3002\u8bbe\u7f6e\u52a8\u6001\u8def\u7ebf \u5728 \/pages\/product\/ \u7b49\u6587\u4ef6\u5939\u4e2d\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a [id].tsx \u7684\u6587\u4ef6\uff1a \u9875\u9762\/\u4ea7\u54c1\/[id].tsx 2\u3002\u83b7\u53d6\u52a8\u6001\u8def\u7531\u7684\u6570\u636e \/\/ pages\/product\/[id].tsx import { getstaticpaths, getstaticprops } from &#8216;next&#8217;; type productprops = { product: { id: string; name: string; description: string; price: number; }; }; export default function productpage({ product }: productprops) { return ( [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-53379","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/53379","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=53379"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/53379\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=53379"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=53379"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=53379"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}