登录
首页 >  文章 >  前端

使用 DEVto API 在 Nextjs 中获取博客文章

来源:dev.to

时间:2024-11-27 14:42:43 170浏览 收藏

golang学习网今天将给大家带来《使用 DEVto API 在 Nextjs 中获取博客文章》,感兴趣的朋友请继续看下去吧!以下内容将会涉及到等等知识点,如果你是正在学习文章或者已经是大佬级别了,都非常欢迎也希望大家都能给我建议评论哈~希望能帮助到大家!

使用 DEVto API 在 Nextjs 中获取博客文章

如果您想在 next.js 网站上展示您的 dev.to 博客文章,那么您很幸运! dev.to 提供了一个易于使用的 api,可让您以编程方式获取博客文章。在本指南中,我将向您展示如何将 dev.to 的 api 集成到您的 next.js 应用程序中并动态显示您的博客内容。

让我们开始吧!

1. 设置 next.js 项目

首先,如果您还没有设置一个新的 next.js 项目,请运行:

npx create-next-app@latest my-dev-blog
cd 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.ts
export 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) return 
failed to load posts
; if (!data) return
loading...
; 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} likes
))}
); }; export default blog;

4. 添加动态博客页面

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) return 
failed to load the post
; if (!data) return
loading...
; return ( <> {data.title} | your name

{data.title}

{data.readable_publish_date}

{data.cover_image && ( {data.title} )}
); }; export default blogpost;

此页面使用 url 中的 slug 获取各个帖子,并使用angerouslysetinnerhtml 安全地使用 html 内容呈现它们。

5. 最后的润色

您现在可以通过运行以下命令启动 next.js 应用程序:

npm run dev

访问 /blog 路线,您应该会看到显示您的 dev.to 博客文章。单击任何帖子都会将您带到单个博客帖子页面。

  1. 结论 在本教程中,我们学习了如何在 next.js 应用程序中从 dev.to api 获取和显示博客文章。这是将您的 dev.to 内容集成到您的个人网站中的强大方法,同时利用静态网站生成和客户端渲染的优势。

您可以随意进一步自定义此设置、添加分页或改进样式以匹配您网站的设计!

如果您有任何问题或建议,请在评论中告诉我。

文中关于的知识介绍,希望对你的学习有所帮助!若是受益匪浅,那就动动鼠标收藏这篇《使用 DEVto API 在 Nextjs 中获取博客文章》文章吧,也可关注golang学习网公众号了解相关技术文章。

声明:本文转载于:dev.to 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>