首页
学习
活动
专区
圈层
工具
发布
    • 综合排序
    • 最热优先
    • 最新优先
    时间不限
  • 来自专栏嘿嘿的专栏

    next.js 源码解析 - getServerSideProps

    老规矩,昨天写了关于 getServerSideProps 的内容,今天趁热写一下 getServerSideProps 相应的源码,看看 next.js getServerSideProps 是怎么实现的 loadComponents,将路由文件中的 getServerSideProps 通过从 require 后的页面中取出。 上面的代码可以看出 SSR 的时候是直接调用 getServerSideProps 传入 context 内容,context 的内容也一目了然。 动态加载处理 看完了 SSR 场景下,next.js 如何处理 getServerSideProps,我们再看下页面为动态加载时的处理。 总结 getServerSideProps 相关的源码还是有点绕的,其中应该还少了一些其它场景的相关代码,不过只看主场景应该就是这些了。

    1.3K30编辑于 2023-05-09
  • 来自专栏嘿嘿的专栏

    一起来学 next.js - getServerSideProps

    getServerSideProps 是 next.js 中的一项特色功能,可以让我们在给页面设置一些初始的 props 参数。 props 来获取 getServerSideProps 注入的 props 了。 import { GetServerSideProps } from 'next'; export const getServerSideProps: GetServerSideProps = async /api/test.ts'; export const getServerSideProps: GetServerSideProps = async context => { return { 总结 通过 next.js 的 getServerSideProps,我们在开发中可以很好的协调前后端数据,一些页面初始化数据、页面鉴权可以直接在 getServerSideProps 中进行处理,这样可以大大简化页面逻辑

    2.1K51编辑于 2023-05-09
  • 来自专栏嘿嘿的专栏

    一起来学 next.js - getStaticProps、getStaticPaths 篇

    之前讲过 next.js 中的 getServerSideProps,今天来讲一讲另一个很类似的 API:getStaticProps,以及和 getStaticProps 紧密相关的 getStaticPaths 和 getServerSideProps 需要注意 getStaticProps 和 getServerSideProps 无法混用,在 next.js 的定位中,getStaticProps 主要用于 with getServerSideProps. To use SSG, please remove getServerSideProps。 如果遇到页面中既有动态数据又有静态数据,那还是老老实实使用 getServerSideProps 吧。

    2K30编辑于 2023-05-09
  • 来自专栏cc log

    Next.js 简明教程

    `getServerSideProps`(SSR)每次访问时请求数据 页面中export一个async的getServerSideProps方法,next就会在每次请求时候在服务端调用这个方法。 方法只会在服务端运行,每次请求都运行一边getServerSideProps方法 如果页面通过浏览器端Link组件导航而来,Next会向服务端发一个请求,然后在服务端运行getServerSideProps 如果没有特殊问题,建议使用getServerSideProps替代getInitialProps方法。 App: NextPage<PostProps> = props => { return

    } export const getServerSideProps: GetServerSideProps /pages/_app.tsx来自定义应用App,可以配置全局的css,或者getServerSideProps方法来给每个页面添加数据。

    3.7K20编辑于 2022-09-21
  • 来自专栏腾讯新闻前端团队

    助力ssr,使用concent为nextjs应用加点料

    ^_^ 支持预渲染 next提供两种级别的预渲染接口,即getServerSideProps和getStaticProps,两种的区别是执行时机不同,getServerSideProps是每次请求页面都会执行 ,而getStaticProps是构建时执行,我们先处理getServerSideProps这种情况吧,看看如何集合concent做预渲染支持。 首先我们不考虑concent的存在,在next里做预渲染支持,只需要在你的页面组件里暴露一个getServerSideProps接口即可。 // 此函数在每次请求改页面时被调用 export async function getServerSideProps() { // 调用外部 API 获取博文列表 const res = await 形如{module:string, state: object}这样的结构,然后在_app.js文件里记录到store即可 // 此函数在每次请求时被调用 export async function getServerSideProps

    2.8K81发布于 2020-12-19
  • 来自专栏我的专栏1

    markdown链接解析错误处理

    常见触发场景​​ ​​异步数据加载不一致​​: 服务端渲染时使用了初始数据(如 getServerSideProps 返回的数据),但客户端 hydration 后,组件立即触发新的异步请求(如 useEffect 确保服务端与客户端数据同步​​ 如果冲突是由于服务端与客户端初始数据不一致导致的,需确保两者使用相同的初始数据源(如 getServerSideProps 或 getStaticProps 返回的数据直接传递给客户端 ): // 服务端渲染(Next.js 示例) export async function getServerSideProps() { const data = await fetchData() MyComponent /> </Suspense> ​​检查路由与参数一致性​​ 在动态路由场景中,确保服务端渲染的路由参数与客户端导航后的参数一致(如 Next.js 的 getStaticPaths 或 getServerSideProps

    29110编辑于 2025-08-11
  • 来自专栏前端专享

    React 必学SSR框架——next.js

    getServerSideProps(SSR)每次访问时请求数据 页面中export一个async的getServerSideProps方法,next就会在每次请求时候在服务端调用这个方法。 方法只会在服务端运行,每次请求都运行一遍getServerSideProps方法 如果页面通过浏览器端Link组件导航而来,Next会向服务端发一个请求,然后在服务端运行getServerSideProps 如果没有特殊问题,建议使用getServerSideProps替代getInitialProps方法。 App: NextPage<PostProps> = props => { return

    } export const getServerSideProps: GetServerSideProps /pages/_app.tsx来自定义应用App,可以配置全局的css,或者getServerSideProps方法来给每个页面添加数据。

    9K20发布于 2021-11-15
  • 来自专栏Web前端

    Next.js进阶:静态生成、服务器端渲染与SEO优化

    使用getServerSideProps获取服务器端数据与getStaticProps类似,getServerSideProps也在服务器端运行,但每次用户请求时都会触发。适合需要实时数据的场景。 jsxexport const getServerSideProps = async (context) => { const currentUserId = context.req.user?.

    2.1K10编辑于 2024-04-16
  • 来自专栏前端达人

    动手练一练,使用 React 和 Next.js 做一个简单的博客网站(下)

    如果要使用服务端渲染,我们可以使用 getServerSideProps() 函数,在页面请求时由服务端执行此函数逻辑,完成数据的渲染。 1、我们现在 /pages/about.js 页面里添加 getServerSideProps() 函数,这个方法里我们使用 node-fetch 模块,获取 API 数据后,通过 props 将返回的数据通过组件属性进行传递 (请看第2点的相关代码),示例代码如下: // fetch a random joke (generated on every reqest) export async function getServerSideProps random'); return { props: { data: await res.json() } } } 2、接下来我们来编写组件 JSX 的部分,接收 getServerSideProps 四、生成静态HTML页面 Next.js 允许你将现有的站点生成静态的 HTML 页面(除了需要服务端渲染的界面),如果你为页面定义了 getServerSideProps() 服务端渲染相关的函数,导出将会失败

    2K31发布于 2021-04-22
  • 来自专栏海怪的编程小屋

    十分钟上手 Next.js

    question/379598562/answer/1081908468 和 Static Generation 类似,Server-side Rendering 同样有一个对应的需要 export 出一个 getServerSideProps export async function getServerSideProps(context) { return { props: { // props for your component 在页面组件文件里,可以通过前面说到的 getStaticProps 和 getServerSideProps 获取 params: export async function getStaticProps

    2.2K20编辑于 2022-03-30
  • 来自专栏前端专享

    【实战】Next.js + 云函数开发一个面试刷题网站

    服务端渲染 为了能够让搜索引擎收录内容,我们选择服务端渲染,在 Next.js 中,可以再导出一个函数getServerSideProps ,这个函数名称是 Next.js 固定的,不可以写错哦。

    ) } // 每次页面刷新都会执行这个方法 export async function getServerSideProps() { // 从云函数请求数据 const res = await res.json() // 将返回的结果通过 props 传递给组件 return { props: { data } } } export default Page 注意:getServerSideProps 其实 vercel 部署的时候会把 getServerSideProps 中方法部署为 serverless 云函数。

    5.3K30编辑于 2022-09-21
  • 来自专栏Cellinlab's Blog

    React 应用架构实战 0x3:构建和配置页面

    return (

    {user.name}

    {user.bio}

    ); }; export const getServerSideProps await getUser(userId); return { props: { user, }, }; }; 正如我们在这里看到的,除了页面组件,还导出了 getServerSideProps from "@/testing/test-data"; type PublicOrganizationPageProps = InferGetServerSidePropsType<typeof getServerSideProps ReactElement) => <PublicLayout>{page}</PublicLayout>; export default PublicJobPage; export const getServerSideProps 一些 SSR 的缺点,主要包括: 需要更多的计算资源,这可能会影响服务器成本 较长的 getServerSideProps 执行时间可能会阻塞整个应用程序 因此,我们只希望在合适的情况下使用 SSR,比如需要对

    1.2K20编辑于 2023-05-17
  • 来自专栏前端达人

    静态网站生成器与服务器端渲染有啥区别

    getServerSideProps函数是一种技术,它指示Next.js在服务器上使用返回的props预渲染页面。这意味着数据获取和页面内容的生成在服务器上提前完成,并在用户请求时提供给用户。 以下是在Next.js项目中使用getServerSideProps函数的示例: export default function Home({ data }) { return ( <main> > car

    ))} </main> ); } export const getServerSideProps = await Supabase.from("cars").select(); return { props: { data: data, }, }; }; 在这段代码中,getServerSideProps

    76910编辑于 2023-11-30
  • 来自专栏Cellinlab's Blog

    React 应用架构实战 0x7:测试

    }); # 公开组织页面 // src/__tests__/public-organization-page.test.tsx import PublicOrganizationPage, { getServerSideProps to fetch data", async () => { const { props } = await getServerSideProps({ params: { organizationId toBeInTheDocument(); }); }); # 公开职位页面 // src/__tests__/public-job-page.test.tsx import PublicJobPage, { getServerSideProps const organization = testData.organizations[0]; describe("Public Job Page", () => { it("should use getServerSideProps to fetch data", async () => { const { props } = await getServerSideProps({ params: { organizationId

    2.1K80编辑于 2023-05-17
  • 来自专栏黯羽轻扬

    鱼和熊掌兼得:Next.js 混合渲染

    无缝加载新内容,甚至能够预测用户行为提前加载目标页的内容 即,首屏加载工作交给更快的 SSR 来做,交互过程中让 CSR 大展身手: When you request this page directly, getServerSideProps transitions through next/link or next/router, Next.js sends an API request to the server, which runs getServerSideProps It’ll return JSON that contains the result of running getServerSideProps, and the JSON will be used to will be handled automatically by Next.js, so you don’t need to do anything extra as long as you have getServerSideProps

    3.5K20发布于 2020-12-29
  • 来自专栏轻量级微服务

    Next.js - SSR / SSG / CSR / ISR / Dynamic Routing

    SSR (Server-side Rendering)在 Next.js 中使用 getServerSideProps 来实现服务端渲染,该动作在用户发起页面请求时执行,示例代码如下:function Page({ data }) { // Render data...}// This gets called on every requestexport async function getServerSideProps export default Page两种模式:用户直接请求:服务端请求数据 -> 服务端渲染页面 -> 用户用户通过 next/link 或 next/router 跳转:客户端请求数据 -> 服务端执行 getServerSideProps

    1.6K20编辑于 2022-12-01
  • 来自专栏Cellinlab's Blog

    React 应用架构实战 0x5:集成 API 到应用中

    from "@/layouts/public-layout"; type PublicOrganizationPageProps = InferGetServerSidePropsType<typeof getServerSideProps return <PublicLayout>{page}</PublicLayout>; }; export default PublicOrganizationPage; export const getServerSideProps PublicLayout } from "@/layouts/public-layout"; type PublicJobPageProps = InferGetServerSidePropsType<typeof getServerSideProps ReactElement) => <PublicLayout>{page}</PublicLayout>; export default PublicJobPage; export const getServerSideProps

    2K20编辑于 2023-05-17
  • 来自专栏前端技术归纳

    梳理NextJS13两种路由下的不同渲染方式:SSG,ISR,SSR,RSC

    res.json(); const { data } = resdata; return ( //... ); } pages 在pages路由下,如果我们要开启SSR,需要实现getServerSideProps export async function getServerSideProps(context: any) { const data = await getPokemon(null, context.params.name 比如一个传统的博客页面采用 SSR 的方式使用 getServerSideProps 的方式渲染,那么就需要等 3 个接口全部返回才可以看到页面。 export async function getServerSideProps() { const list = await getBlogList() const detail = await

    3.1K31编辑于 2023-09-01
  • 来自专栏前端从进阶到入院

    Next.js,到底为什么这样对我?

    getServerSideProps()中你可以访问 IncomingMessage 和 OutgoingMessage 对象,这样你可以在服务器端渲染页面前,在服务端运行一些代码。 export const getServerSideProps = async ( req: IncomingMessage, res: OutgoingMessage, ) => { req.headers.cookie 随意的限制 还记得在 Edge 环境下你无法在 getServerSideProps()中设置 cookie 吗?

    1K20编辑于 2023-10-14
  • 前端全栈进阶 Nextjs打造跨框架SaaS应用

    lint:运行next-lint来设置next.js的内置ESLint配置5、SSR 服务端渲染next 中服务端渲染需要用到 getServerSideProps 函数,而后端的数据获取都是在该函数内来获取 data.username}

    email:{data.email}

    )}export default Userexport async function getServerSideProps

    1K10编辑于 2024-05-06
  • 领券