简体   繁体   English

Next.js 中的客户端和服务器端是什么?

[英]What is client side and server side in Next.js?

Can anyone please explain me the concept of client side and server side in Next.js as they have mentioned in their documentation.正如他们在文档中提到的那样,任何人都可以向我解释 Next.js 中客户端和服务器端的概念。 What I know is that Next.js works on react which is client side and run in the browser and server side means the api (backend).我所知道的是 Next.js 在客户端的反应上工作并在浏览器和服务器端运行意味着 api (后端)。 Any help would be appreciated.任何帮助,将不胜感激。 Thanks谢谢

From Next.js documentation:来自 Next.js 文档:

This function gets called at build time on server-side.这个 function 在构建时在服务器端被调用。 It won't be called on client-side, so you can even do direct database queries.它不会在客户端调用,因此您甚至可以直接进行数据库查询。 See the "Technical details" section.请参阅“技术细节”部分。

 export async function getStaticProps() {
    
      const postsDirectory = path.join(process.cwd(), 'posts')
      const filenames = await fs.readdir(postsDirectory)
    
 }

I started writing NextJS app few months before, i'll explain as far as i know check whether it would be helpful.几个月前我开始编写 NextJS 应用程序,据我所知,我会解释它是否有帮助。

Your understanding on client and server(API) is correct but in case of NextJS there is another client side and server side as NextJS is used for Server-Side Rendering(SSR).您对客户端和服务器(API)的理解是正确的,但在 NextJS 的情况下,还有另一个客户端和服务器端,因为 NextJS 用于服务器端渲染(SSR)。

In simple a same page Ex: pages/home.js when loaded with browser hard re-load https://example.com/home is loaded as server side.在简单的同一页面中,例如:pages/home.js 在使用浏览器硬重新加载时加载https://example.com/home作为服务器端加载。 Pages written under /pages/ folder will be rendered server side on navigation. /pages/ 文件夹下的页面将在导航时呈现在服务器端。 So the DOM elements of the page will be available in page source(view page source option in browser) which will be used by crawlers too.因此页面的 DOM 元素将在页面源(在浏览器中查看页面源选项)中可用,爬虫也将使用这些元素。

You can find the difference by checking whether type of window,== 'undefined'.您可以通过检查 window,== 'undefined' 的类型是否找到差异。 as window represents browser which is client and view page source of browser represents server side rendering.因为 window 代表浏览器是客户端,浏览器的视图页面源代表服务器端渲染。

In Pages also you can check在页面中,您还可以检查

  1. Create a Next.js project创建 Next.js 工程

  2. Have two pages index.js and home.js有两个页面 index.js 和 home.js

  3. In home.js write Home.getInitalProps method which is similar to useEffect or componentDidMount in react component.在 home.js 中编写 Home.getInitalProps 方法,类似于 react 组件中的 useEffect 或 componentDidMount。 Here pages cannot contain componentDidMount or useEffect instead all API calls before render has to be done in getInitialProps or other related methods.这里的页面不能包含 componentDidMount 或 useEffect 而是所有 API 在渲染之前的调用都必须在 getInitialProps 或其他相关方法中完成。

     Home.getInitialProps = async (context) => { const { req, query, res, asPath, pathname } = context if (.req) { if (typeof window.== 'undefined') { //its server side request happened on } } else { // its client side call that calls getInitialProps when routing happened Router.push('/home') from index page or inside components rendered from pages/index.js } }

Let me know if you need some more details, we can explore and figure it out.如果您需要更多详细信息,请告诉我,我们可以探索并弄清楚。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM