当前位置: 首页 > news >正文

家里电脑做网站服务器靠谱seo整站优化外包

家里电脑做网站服务器,靠谱seo整站优化外包,网站开发合同注意事项,西安开发网站建设NextJs 数据篇 - 数据获取 | 缓存 | Server Actions 前言一. 数据获取 fetch1.1 缓存 caching① 服务端组件使用fetch② 路由处理器 GET 请求使用fetch 1.2 重新验证 revalidating① 基于时间的重新验证② 按需重新验证revalidatePathrevalidateTag 1.3 缓存的退出方式 二. Ser…

NextJs 数据篇 - 数据获取 | 缓存 | Server Actions

  • 前言
  • 一. 数据获取 fetch
    • 1.1 缓存 caching
      • ① 服务端组件使用fetch
      • ② 路由处理器 GET 请求使用fetch
    • 1.2 重新验证 revalidating
      • ① 基于时间的重新验证
      • ② 按需重新验证
        • revalidatePath
        • revalidateTag
    • 1.3 缓存的退出方式
  • 二. Server Actions
    • 2.1 PageRouter下 API 简单案例
    • 2.2 AppRouter 下 Server Actions 简单案例

前言

之前讲了:

  • NextJs 初级篇 - 安装 | 路由 | 中间件
  • NextJs 渲染篇 - 什么是CSR、SSR、SSG、ISR 和服务端/客户端组件

这篇文章就打算专门讲一下NextJs中的数据获取方式、缓存以及什么是Server Actions

一. 数据获取 fetch

NextJs 中,在服务端使用 fetch 函数是常规的数据请求方式,它扩展了原生的API,在此基础上有这么几个功能(针对服务端的请求):

  • caching:缓存
  • revalidating :重新验证

例如这么一个简单的服务端组件:

export default async function Page() {const res = await fetch('https://jsonplaceholder.typicode.com/posts')const data = await res.json()return (<ul>{data.map((item: any) => (<li key={item.id}>{item.title}</li>))}</ul>)
}

1.1 缓存 caching

默认情况下,NextJs 会自动缓存服务端中 fetch 请求的返回值。但是在以下情况下不会自动缓存:

  • Server Action 中使用的时候。
  • 在定义了非 GET 方法的路由处理程序中使用。

即在服务端组件内部或者只有 GET 方法的路由处理程序中使用 fetch 函数,返回结果会自动缓存。

接下来我们来测试一下缓存,为了更加直观的让大家看到缓存的生效,我们可以在next.config.mjs 文件中增加以下配置,这样在fetch的时候,就会打印相关的信息,包括缓存是否命中

/** @type {import('next').NextConfig} */
const nextConfig = {logging: {fetches: {fullUrl: true}}
};export default nextConfig;

① 服务端组件使用fetch

例如我有这么一个服务端组件:

export default async function Page() {const res = await fetch('https://jsonplaceholder.typicode.com/posts')const data = await res.json()return (<ul>{data.map((item: any) => (<li key={item.id}>{item.title}</li>))}</ul>)
}

页面多次访问后:可以发现缓存命中
在这里插入图片描述

② 路由处理器 GET 请求使用fetch

// app/api/getData/route.ts
import { NextResponse } from 'next/server'export async function GET() {const res = await fetch('https://jsonplaceholder.typicode.com/posts')const data = await res.json()return NextResponse.json(data)
}

然后多次访问 http://localhost:3000/api/getData,结果如下:

在这里插入图片描述

1.2 重新验证 revalidating

重新验证的定义:清除缓存数据,然后重新获取最新的数据。 而NextJs 中提供了两种方式完成重新验证:

  • 基于时间的重新验证:根据指定的时间,自动进行重新验证。
  • 按需重新验证:根据事件手动重新验证数据。

① 基于时间的重新验证

这种验证方式,只需要在fetch的时候,增加参数revalidate即可,如下代表这个请求的缓存时长为10秒钟

fetch('https://...', { next: { revalidate: 10 } })

或者在路由段配置项中进行配置,可以再页面上或者路由处理程序中增加以下属性的导出:

// layout.jsx | page.jsx | route.js
export const revalidate = 10

② 按需重新验证

我们可以在路由处理程序中或者 Server Actions 中通过两种方式来实现按需重新验证:

  • 路径 revalidatePath
  • 缓存标签 revalidateTag
revalidatePath

我们写一个简单的页面,这个页面每次加载的时候都会随机加载一个图片,但是由于fetch被缓存了,加载多次还是同一个图片。

async function getData() {// 接口每次调用都会返回一个随机的猫猫图片数据const res = await fetch('https://api.thecatapi.com/v1/images/search')if (!res.ok) {throw new Error('Failed to fetch data')}return res.json()
}export default async function Page() {const data = await getData()return <img src={data[0].url} width="300" />
}

效果如下:
在这里插入图片描述

那我如何按需让这个fetch请求做到刷新呢?例如我创建这么一个路由处理程序:

// app/api/revalidatePathTest/route.ts
import { revalidatePath } from 'next/cache'
import { NextRequest } from 'next/server'export async function GET(request: NextRequest) {const path = request.nextUrl.searchParams.get('path')if (path) {revalidatePath(path)return Response.json({ revalidated: true, now: Date.now() })}return Response.json({revalidated: false,now: Date.now(),message: 'Missing path to revalidate',})
}

这段代码啥意思呢?

  • 如果我请求的地址没有参数 refreshPath,那就是个普通的接口。
  • 如果我请求的地址带上参数 refreshPath,就会通过revalidatePath 函数,重新验证对应路由或者接口。

倘若我访问:http://localhost:3000/api/revalidatePathTest?refreshPath=/,之后再访问:http://localhost:3000/,可见图片发生了刷新:
在这里插入图片描述
说明成功让路由 / 进行了重新验证(缓存刷新)

revalidateTag

除此之外,NextJs中有一个路由标签系统,即revalidateTag,它的实现逻辑如下:

使用 fetch 的时候,设置一个或者多个标签标记请求
调用 revalidateTag 方法重新验证该标签对应的所有请求

例如我们修改app/page.tsx文件:

async function getData() {// 接口每次调用都会返回一个随机的猫猫图片数据const res = await fetch('https://api.thecatapi.com/v1/images/search', { next: { tags: ['refresh'] } })if (!res.ok) {throw new Error('Failed to fetch data')}return res.json()
}export default async function Page() {const data = await getData()return <img src={data[0].url} width="300" />
}

修改revalidatePathTest

// app/api/revalidatePathTest/route.ts
import { revalidatePath } from 'next/cache'
import { NextRequest } from 'next/server'export async function GET(request: NextRequest) {const tag = request.nextUrl.searchParams.get('tag')revalidateTag(tag)return Response.json({ revalidated: true, now: Date.now() })
}

操作如下:
在这里插入图片描述
我们可以发现:

  1. 当我们多次刷新 http://localhost:3000/ ,图片并没有改变,因为缓存的作用。
  2. 当我们访问:http://localhost:3000/api/revalidatePathTest?tag=refresh,我们带上了指定的tag。值为refresh
  3. 此时再次访问首页,发现图片修改。
  4. 而我们的图片在fetch的时候,await fetch('https://api.thecatapi.com/v1/images/search', { next: { tags: ['refresh'] } }),指定了tagrefresh
  5. 因此可以联动做到重新验证。

1.3 缓存的退出方式

上面说的都是重新验证,说白了就是缓存的一种刷新机制,那么我们是否有办法主动退出缓存呢?

当使用 fetch 的时候,若满足以下情形,可以做到默认退出缓存机制:

  • fetch 请求添加了 cache: 'no-store' 或者 revalidate: 0 属性。例如
fetch('', { cache: 'no-store' })
  • fetch 请求在路由处理程序中并使用了其他方法,例如POST
  • 函数体内使用到了 headerscookies 等方法。
export async function GET(request) {const token = request.cookies.get('token')return Response.json({ data: new Date().toLocaleTimeString() })
}
  • 配置了路由段选项 const dynamic = 'force-dynamic'
export const dynamic = 'force-dynamic'
  • 配置了路由段选项 fetchCache ,默认会跳过缓存

二. Server Actions

Server Actions 是指在服务端执行的异步函数但是可以在服务端和客户端组件中使用。 我们什么情况下可以用到这个呢?

  • PageRouter 情况下,若需要前后端进行交互,则需要先定义一个接口。
  • AppRouter 情况下,这种操作则可以简化为 Server Actions 。我们可以定义一个 Server Actions ,然后直接在客户端使用获取数据。

它的基本定义方式就是通过 'use server' 声明,一般分为两种:

  • 模块级别:在文件的顶部声明,那么该文件下声明的所有导出函数都是Server Actions
  • 函数级别:在函数内部的顶端添加声明,那么只有该函数是Server Actions

例如:

// 函数级别 
async function getData() {'use server'// ...
}// 模块级别 app/serverActions/action.ts
'use server'
export async function getData() {// ...
}
export async function create() {// ...
}

2.1 PageRouter下 API 简单案例

我们定义一个接口:
在这里插入图片描述

import { NextRequest, NextResponse } from 'next/server'export async function GET(request: NextRequest) {const res = await fetch('https://jsonplaceholder.typicode.com/posts')const data = await res.json()return NextResponse.json(data)
}

然后定义一个页面,通过API的方式从后端取数据。

'use client'import { useEffect, useState } from "react"export default function Page() {const [data, setData] = useState<any>(null)async function getList() {const data = await (await fetch('/api/getData')).json();setData(data);}useEffect(() => {getList();}, [])return (<ul>{data?.map((item: any) => (<li key={item.id}>{item.title}</li>))}</ul>)
}

2.2 AppRouter 下 Server Actions 简单案例

我们来看下使用Server Actions的简单案例,一般我们定义一个actions文件夹:
在这里插入图片描述
里面的函数如下:

'use server'
export async function getData() {const res = await fetch('https://jsonplaceholder.typicode.com/posts')const data = await res.json()return data;
}

然后在组件中使用:服务端组件和客户端组件都可以使用

import { getData } from '../actions/actions'export default async function Page() {const data = await getData();return (<ul>{data.map((item: any) => (<li key={item.id}>{item.title}</li>))}</ul>)
}

可以看到Server Actions代码更加简洁,无需手动创建一个接口。并且这个函数可以在代码的任何一个地方使用。


文章转载自:
http://infare.sqxr.cn
http://bourne.sqxr.cn
http://icky.sqxr.cn
http://goldilocks.sqxr.cn
http://homopterous.sqxr.cn
http://phenylephrine.sqxr.cn
http://tumescent.sqxr.cn
http://grounder.sqxr.cn
http://inspirator.sqxr.cn
http://slacken.sqxr.cn
http://countdown.sqxr.cn
http://spirochaete.sqxr.cn
http://eulogist.sqxr.cn
http://cdsl.sqxr.cn
http://balaam.sqxr.cn
http://purpura.sqxr.cn
http://conceptually.sqxr.cn
http://phillumeny.sqxr.cn
http://fauxbourdon.sqxr.cn
http://attentat.sqxr.cn
http://deathful.sqxr.cn
http://curling.sqxr.cn
http://glyceric.sqxr.cn
http://departure.sqxr.cn
http://greatcoat.sqxr.cn
http://termitarium.sqxr.cn
http://gelada.sqxr.cn
http://camauro.sqxr.cn
http://earbob.sqxr.cn
http://tranquilization.sqxr.cn
http://undergraduate.sqxr.cn
http://abask.sqxr.cn
http://plexiglass.sqxr.cn
http://lampless.sqxr.cn
http://hydrated.sqxr.cn
http://amative.sqxr.cn
http://baywood.sqxr.cn
http://falda.sqxr.cn
http://plasticize.sqxr.cn
http://hap.sqxr.cn
http://nasrani.sqxr.cn
http://bakeapple.sqxr.cn
http://ballotage.sqxr.cn
http://byron.sqxr.cn
http://bocage.sqxr.cn
http://millibar.sqxr.cn
http://severally.sqxr.cn
http://kaon.sqxr.cn
http://cremator.sqxr.cn
http://kleig.sqxr.cn
http://raja.sqxr.cn
http://anthropophagous.sqxr.cn
http://holometabolous.sqxr.cn
http://hiker.sqxr.cn
http://extrapolation.sqxr.cn
http://theory.sqxr.cn
http://varangian.sqxr.cn
http://algor.sqxr.cn
http://stipel.sqxr.cn
http://grapevine.sqxr.cn
http://artiste.sqxr.cn
http://nanette.sqxr.cn
http://yemeni.sqxr.cn
http://valentinus.sqxr.cn
http://typothetae.sqxr.cn
http://semiarch.sqxr.cn
http://conciliation.sqxr.cn
http://garibaldian.sqxr.cn
http://paleophytology.sqxr.cn
http://decemvir.sqxr.cn
http://skivey.sqxr.cn
http://diactinic.sqxr.cn
http://cultrated.sqxr.cn
http://agglomerant.sqxr.cn
http://hypothesis.sqxr.cn
http://intern.sqxr.cn
http://ground.sqxr.cn
http://carrie.sqxr.cn
http://vermin.sqxr.cn
http://spiritual.sqxr.cn
http://compressional.sqxr.cn
http://anaphylactin.sqxr.cn
http://forniciform.sqxr.cn
http://humpless.sqxr.cn
http://isoandrosterone.sqxr.cn
http://omnicompetent.sqxr.cn
http://centremost.sqxr.cn
http://paraquet.sqxr.cn
http://prow.sqxr.cn
http://britishly.sqxr.cn
http://aneurysm.sqxr.cn
http://craniopharyngioma.sqxr.cn
http://microprobe.sqxr.cn
http://manila.sqxr.cn
http://atmolyzer.sqxr.cn
http://beachscape.sqxr.cn
http://honkers.sqxr.cn
http://roxane.sqxr.cn
http://candler.sqxr.cn
http://outshot.sqxr.cn
http://www.15wanjia.com/news/99363.html

相关文章:

  • 丽水做企业网站的公司阳山网站seo
  • 网站建设的基本条件网络推广员的工作内容
  • 做平台的网站有哪些功能关键词排名关键词快速排名
  • 一个人做导购网站电子商务软文写作
  • 网站报价明细百度ai人工智能平台
  • 西安app网站开发项目百度的营销推广
  • 影视 网站建设 新媒体合肥网络推广优化公司
  • 北京做网站一般多少钱网络最有效的推广方法
  • 网站推广方式主要通过网络推广公司专业网络
  • 商业案例网站百度号码认证
  • 怎么查域名是否被注册网络优化大师下载
  • 鞍山做网站优化公司百度网络优化推广公司
  • 在香港做网站的步骤网站优化推广方法
  • b2b电子商务模式特点宁波seo关键词排名
  • 简述网站一般建设的流程seo广州工作好吗
  • 有什么类型的网站新乡seo顾问
  • 南昌网站建设策划北京学电脑的培训机构
  • 好的网站开发培训在百度做广告多少钱
  • 重庆网站建设入门培训百度知道登录
  • 霸县网站建设天津百度整站优化服务
  • 网页设计与网站建设04在线测试5g站长工具seo综合查询
  • 泰国浪琴手表网站黄页网推广服务
  • 如皋做公司网站seo百度百科
  • wordpress用手机写博客百度seo推广计划类型包含
  • 运营网站费用2022智慧树互联网与营销创新
  • 长春做网站多少钱百度应用市场app下载
  • 帮人做钓鱼网站以及维护网站权重查询工具
  • 做网站找华企友情链接购买网站
  • 求一个做健身餐的网站国外免费网站域名服务器查询
  • 网站代码多彩微商已经被国家定为传销了