NextJS开发服务器响应时间

发布于 2025-02-11 00:09:27 字数 1890 浏览 1 评论 0原文

我的主要问题是; Local -Host与Live/Live/Production的响应时间有差异吗?

我在NextJs中有一个IM构建,带有GraphCMS,我正在使用GraphQL/GraphQl-Request来获取数据。当我第一次启动Localhost和页面加载时,我单击页面导航中的一个链接以转到页面上的,实际上需要2秒才能获取数据并更改页面。我正在查看Chrome DevTools中的“网络”选项卡,.json文件状态是(待定),然后切换到200一旦内容为下载。这是来自DevTools的屏幕快照:

”在此处输入图像描述

当我悬停在瀑布上时,它说等待服务器响应为1.86,内容下载为0.46ms。因此,等待服务器响应是因为IM在本地主机上的原因,还是与GraphCMS服务器有关的是从中获取数据?

另外,您可能会注意到JSON文件大小仅为5.2kb,因此它不是大的提取。

为了给您一些关于代码的上下文,我的查询&客户端存储在/lib文件夹中:

// /lib/client.js
import { GraphQLClient } from 'graphql-request'

export const graphcmsClient = () =>
  new GraphQLClient(process.env.NEXT_PUBLIC_GRAPHCMS_URL, {
    headers: {
      authorization: `Bearer ${process.env.GRAPHCMS_TOKEN}`,
    },
  })

// example of a query in ./lib/queries.js
import { gql } from 'graphql-request'
    
const blogPageQuery = gql`
  fragment BlogPostFields on BlogPost {
    id
    category
    content
    coverImage {
      id
      height
      url
      width
    }
    excerpt
    published
    slug
    title
   }
  `

这是一个示例,即IM使用getstaticProps并获取查询数据:

export async function getStaticProps({ params, preview = false }) {
  const client = graphcmsClient(preview)
  const collectionCards = await getAllCollections()

  const { page, navigation } = await client.request(pageQuery, {
    slug: params.slug
  })

  if (!page) {
    return {
      notFound: true
    }
  }

  const parsedPageData = await parsePageData(page)

  return {
    props: {
      page: parsedPageData,
      navigation,
      collectionCards,
      preview
    },
    revalidate: 60
  }
}

My main question is; is there a difference in response time for fetching in localhost vs live/production?

I have a project im building in NextJS, with GraphCMS and I'm using GraphQL/graphql-request to fetch the data. When I first start up localhost and the pages loads, I click a link in the page navigation to go to about page and it literally takes 2 seconds for the data to fetch and the page to change. I'm watching the network tab in Chrome DevTools and the .json file status is (pending) and then switches to 200 once the content is downloaded. Here is a screenshot from DevTools:

enter image description here

When I hover over the waterfall, It says the Waiting for server response is 1.86s and content download is 0.46ms. So is the waiting for server response, something because im on a localhost, or is this something to do with the GraphCMS server were its fetching the data from?

Also you may note that the json file size is only 5.2kB, so its not a large fetch.

To give you a little context on the code, my queries & client are stored in the /lib folder:

// /lib/client.js
import { GraphQLClient } from 'graphql-request'

export const graphcmsClient = () =>
  new GraphQLClient(process.env.NEXT_PUBLIC_GRAPHCMS_URL, {
    headers: {
      authorization: `Bearer ${process.env.GRAPHCMS_TOKEN}`,
    },
  })

// example of a query in ./lib/queries.js
import { gql } from 'graphql-request'
    
const blogPageQuery = gql`
  fragment BlogPostFields on BlogPost {
    id
    category
    content
    coverImage {
      id
      height
      url
      width
    }
    excerpt
    published
    slug
    title
   }
  `

and here is an example where im using getStaticProps and fetching the query data:

export async function getStaticProps({ params, preview = false }) {
  const client = graphcmsClient(preview)
  const collectionCards = await getAllCollections()

  const { page, navigation } = await client.request(pageQuery, {
    slug: params.slug
  })

  if (!page) {
    return {
      notFound: true
    }
  }

  const parsedPageData = await parsePageData(page)

  return {
    props: {
      page: parsedPageData,
      navigation,
      collectionCards,
      preview
    },
    revalidate: 60
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文