npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

next-axios-network

v1.1.5

Published

一款在开发时监控 next.js + axios 项目服务端请求的 Network 面板

Downloads

3

Readme

next-axios-network

中文English

一款在开发时监控 next.js + axios 项目服务端请求的 Network 面板

'demo.png' 'demo.png'

什么是 next-axios-network

在使用 next.js 开发时,有时候会遇到服务端请求无法详细 debug 的烦恼,例如:

// `pages` directory
export const getStaticProps = async () => {
  const data = await axios.get("https://example.com/api/home");
  return {
    props: {
      data,
    },
  };
};
// `app` directory
export default async function Dashboard() {
  const data = await axios.get("https://example.com/api/home");
  return (
    <ul>
      {data.map((item) => (
        <li key={item.id}>{item.name}</li>
      ))}
    </ul>
  );
}

这样的请求是在 node 环境进行的,无法通过浏览器 Network 面板进行监控,有了 next-axios-network 即可解决这个痛点

安装

npm i next-axios-network -D

使用

  1. next.config.js 加入配置:
const NextAxiosNetworkPlugin = require("next-axios-network/plugin");

/** @type {import('next').NextConfig} */
const nextConfig = {
  webpack: (config) => {
    config.plugins.push(NextAxiosNetworkPlugin());
    return config;
  },
  // ...
};

module.exports = nextConfig;
  1. 在封装 axios 的地方或者项目首次执行的文件加入 axios 拦截器配置:
import axios from "axios";
import nextAxiosNetwork from "next-axios-network";
nextAxiosNetwork(axios);

如果使用了自定义 axios 实例,需要手动引入拦截器,例如:

import axios from "axios";
import nextAxiosNetwork, { middlewares } from "next-axios-network";
nextAxiosNetwork(axios);
const yourAxiosInstance = axios.create({
  /** ... */
});
yourAxiosInstance.instance.interceptors.request.use(
  middlewares.requestMiddleWare,
  middlewares.requestError
);
yourAxiosInstance.instance.interceptors.response.use(
  middlewares.responseMiddleWare,
  middlewares.responseError
);

完成上述配置,启动项目后访问:

http://localhost:2999

即可看到监控面板

API

在上述 NextAxiosNetworkPlugin 中可配置自定义参数,例如:

const NextAxiosNetworkPlugin = require("next-axios-network/plugin");

/** @type {import('next').NextConfig} */
const nextConfig = {
  webpack: (config) => {
    config.plugins.push(
      NextAxiosNetworkPlugin({
        maxCaches: 100, // 最大缓存请求日志数量,默认 50
      })
    );
    return config;
  },
  // ...
};

module.exports = nextConfig;

多项目共享面板

如果有多个项目想共享面板,可以不配置打包插件,只配置拦截器并使用 npm 启动面板,例如:

  1. 在每个项目封装 axios 的地方或者项目首次执行的文件加入 axios 拦截器配置:
import axios from "axios";
import nextAxiosNetwork from "next-axios-network";
nextAxiosNetwork(axios);
  1. 在任意一个项目的 package.json 加入启动命令
"scripts": {
  "network-start": "nan-start",
},
  1. 在终端执行 npm run network-start 即可,多个项目同时启动,面板启动命令只需要在任意一个项目配置启动即可

自定义参数可以通过启动命令设置环境变量,面板服务会读取 process.env.MAX_CACHES