简体   繁体   English

如何让 Next.js 做 SSR(服务器端渲染)?

[英]How to make Next.js do SSR (Server Side Rendering)?

To experiment with Next.js, I created an empty Next.js project, and added some code to fetch data for weather:为了试验 Next.js,我创建了一个空的 Next.js 项目,并添加了一些代码来获取天气数据:

export default function Home() {

  const [data, setData] = useState(null);

  useEffect(() => {
    fetch("https://api.weather.gov/gridpoints/MTR/84,105/forecast")
      .then((res) => res.json())
      .then((d) => setData(d));
  }, []);

  const details = data && data.properties.periods[0];

  return (
    <div className={styles.container}>
      <Head>
        <title>Create Next App</title>
        <link rel="icon" href="/favicon.ico" />
      </Head>

      <main className={styles.main}>
        Hello world
        {details && (
          <div>
            <h2>San Francisco Forecast</h2>
            <img src={details.icon} />
            <p>
              Temperature: {details.temperature}°{details.temperatureUnit} /{" "}
              {Math.round(((details.temperature - 32) * 50) / 9) / 10}°C
          </p>
            <p>{details.shortForecast}</p>
            <p>{details.detailedForecast}</p>
            <p>{new Date(details.startTime).toLocaleString()}</p>
          </div>
        )}
      </main>
    </div>
  )
}

Demo: https://codesandbox.io/s/staging-snow-6tvqc?file=/pages/index.js演示: https://codesandbox.io/s/staging-snow-6tvqc?file=/pages/index.js
and the standalone page: https://6tvqc.sse.codesandbox.io/和独立页面: https://6tvqc.sse.codesandbox.io/

How can we make the following command我们如何制作以下命令

curl https://6tvqc.sse.codesandbox.io/

be able to show the content as if on the webpage?能够像在网页上一样显示内容吗? (so should be able to have Temperature and the numbers after it). (所以应该能够有Temperature和它后面的数字)。

Do data fetching in these functions.这些函数中获取数据。

Next.js provides 3 functions for data fetching. Next.js 提供 3 种数据获取功能。 You have to export them from your page.您必须从您的页面导出它们。

  1. GetStaticProps - will only execute when you build. GetStaticProps - 只会在你构建时执行。
  2. GetServerSideProps - will execute on every request. GetServerSideProps - 将在每个请求上执行。
  3. GetStaticPaths - to get dynamic paths for GetStaticProps, when you export this you have to export GetStaticProps also. GetStaticPaths - 要获取 GetStaticProps 的动态路径,当您导出它时,您还必须导出 GetStaticProps。

You have to return data in a format and it will be injected in props of your react component.您必须以某种格式返回数据,它将被注入到您的 react 组件的 props 中。

You can also provide fallback on props to dynamically render pages, like you don't have to make every page static (using GetStaticProps/Paths).您还可以在道具上提供后备以动态呈现页面,就像您不必制作每个页面 static(使用 GetStaticProps/Paths)一样。 Make only some of them and Next.js will make remaining on request and will cache them for next request.只制作其中的一些,Next.js 将在请求时剩余,并将缓存它们以供下一个请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 next.js 与传统 SSR 进行服务器端渲染 - Server side rendering with next.js vs traditional SSR 我们如何处理 Next.js 服务器端渲染 SSR 的构建时数据获取与实时数据? - How do we deal with the build time data fetch vs real time data for Next.js Server Side Rendering SSR? 如何使用React可加载和获取组件数据(如Next.js)进行服务器端渲染? - How to do Server Side Rendering in React With React Loadable and Fetching Data for Components (Like Next.js)? Next.JS:如何在服务器端发出所有请求 - Next.JS: How to make ALL requests server-side Next.js 未在服务器端渲染中渲染 CSS - Next.js is not rendering CSS in Server Side Rendering Next.js static 服务器端渲染和 Gatsby.js - Next.js static server side rendering and Gatsby.js 如何在广泛使用 React hooks 的同时利用 Next.js 服务器端渲染? - How do I take advantage of Next.js server-side rendering while using React hooks extensively? 使用 Next.js 对服务器端渲染进行 React Query - React Query with server side rendering using Next.js 用于服务器端渲染的 Next.js 路由器中的匹配和参数对象 - Match and Param object in Next.js router for Server side rendering Next.js:如何将仅外部客户端的 React 组件动态导入到开发的服务器端渲染应用程序中? - Next.js: How to dynamically import external client-side only React components into Server-Side-Rendering apps developed?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM