如果您想在 next.js 网站上展示您的 dev.to 博客文章,那么您很幸运! dev.to 提供了一个易于使用的 api,可让您以编程方式获取博客文章。在本指南中,我将向您展示如何将 dev.to 的 api 集成到您的 next.js 应用程序中并动态显示您的博客内容。
让我们开始吧!
1. 设置 next.js 项目
首先,如果您还没有设置一个新的 next.js 项目,请运行:
npx create-next-app@latest my-dev-blogcd my-dev-blog
登录后复制
现在我们已经准备好了 next.js 应用程序,让我们继续获取我们的博客文章。
2. 从 dev.to api 获取博客文章
dev.to api 通过简单的 http 请求提供对您发布的文章的访问。您可以通过点击端点来按用户获取文章:
https://dev.to/api/articles?username=yourusername
登录后复制
为了获取 next.js 应用中的博客文章,我们将使用 swr 库。 swr 是一个流行的数据获取库,旨在让您在 react/next.js 应用程序中轻松获取、缓存和更新数据。
安装 swr:
npm install swr
登录后复制
现在,让我们创建一个实用函数来处理 api 请求:
// src/lib/fetcher.tsexport default async function fetcher(url: string) { const response = await fetch(url); if (!response.ok) { throw new error("failed to fetch data"); } return response.json();}
登录后复制
3. 创建博客页面
现在我们有了 fetcher 实用程序,让我们创建一个博客页面来显示您的 dev.to 帖子。
在pages/blog/index.tsx中,使用swr获取并显示博客文章:
import { container, row, col, card, button, badge } from 'react-bootstrap';import head from 'next/head';import useswr from 'swr';import fetcher from '../../lib/fetcher';import link from 'next/link';import { formatdistancetonow, parseiso } from 'date-fns';interface blogpost { id: number; title: string; description: string; slug: string; cover_image: string; tag_list: string[]; reading_time_minutes: number; published_timestamp: string; positive_reactions_count: number;}const blog = () => { const { data, error } = useswr('https://dev.to/api/articles?username=yourusername', fetcher); if (error) returnfailed to load posts; if (!data) returnloading...; return (blog | your name blog
{data.map((post: blogpost) => ( {post.title.length > 50 ? `${post.title.substring(0, 50)}...` : post.title} {post.description}{post.tag_list.map((tag: string) => ( {tag} ))}{post.reading_time_minutes} min read))} 登录后复制
{formatdistancetonow(parseiso(post.published_timestamp), { addsuffix: true })}
{post.positive_reactions_count} likes4. 添加动态博客页面
next.js 提供动态路由,允许您为每个博客文章生成单独的页面。让我们创建一个动态路由来显示每个帖子。
创建一个名为pages/blog/[slug].tsx的文件:
import { userouter } from 'next/router';import useswr from 'swr';import { container, row, col, card, button } from 'react-bootstrap';import head from 'next/head';import image from "next/image";import fetcher from '../../lib/fetcher';const blogpost = () => { const router = userouter(); const { slug } = router.query; const { data, error } = useswr(slug ? `https://dev.to/api/articles/yourusername/${slug}` : null, fetcher); if (error) returnfailed to load the post; if (!data) returnloading...; return ({data.title} | your name {data.title}
{data.readable_publish_date}
{data.cover_image && ( )} 登录后复制此页面使用 url 中的 slug 获取各个帖子,并使用angerouslysetinnerhtml 安全地使用 html 内容呈现它们。
5. 最后的润色
您现在可以通过运行以下命令启动 next.js 应用程序:
npm run dev登录后复制
访问 /blog 路线,您应该会看到显示您的 dev.to 博客文章。单击任何帖子都会将您带到单个博客帖子页面。
结论在本教程中,我们学习了如何在 next.js 应用程序中从 dev.to api 获取和显示博客文章。这是将您的 dev.to 内容集成到您的个人网站中的强大方法,同时利用静态网站生成和客户端渲染的优势。
您可以随意进一步自定义此设置、添加分页或改进样式以匹配您网站的设计!
如果您有任何问题或建议,请在评论中告诉我。
以上就是使用 DEVto API 在 Nextjs 中获取博客文章的详细内容,更多请关注【创想鸟】其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。
发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2669562.html