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

pre-fetch-api-webpack-plugin

v1.0.3

Published

H5接口预请求插件

Downloads

2

Readme

应用场景:

可以应用在首屏接口上 和 js 并行发送请求 减少网络耗时 提升页面秒开率

实现原理

插件会在 html 的 head 里面插入一段 code 从 query 上面获取参数 提前进行请求接口,插件会在 window 上挂载 usePreFetchApiService 方法,业务方可以使用此方法来获取结果数据。

功能列表

  1. 可以配置一个页面 并行请求多个接口
  2. 可以配置多个页面 请求一个接口
  3. 可以配置多个页面请求多个接口
  4. 多个页面数据通过 hash||path 标识 防止数据互串
  5. 数据获取 支持静态参数,query 参数,cookie 参数

参数说明

配置一个数组,根据 hashList || pathList 来匹配第一个命中的选项, 若两个字段同时存在则 pathList 优先级更高,每项有如下字段。

pathList: 路径维度

用于拦截是否发送预请求 只有命中才会发送 设置["all"] 则不区分页面 通过location.pathname比较

hashList:hash 维度

用于拦截是否发送预请求 只有命中才会发送 设置["all"] 则不区分页面 通过location.hash比较

urls:

需要请求的接口数组 可以是一个字符串也可以是一个对象 如果是对象必须包含 url 可以覆盖外层的配置 会对 method,params,staticParams,cookieParams 做聚合处理,覆盖外层参数。

method:

请求的方法 默认为post 支持post||get

params:

请求参数从query上解析

cookieParams:

请求参数从cookie中解析

staticParams:

静态写死的参数

同名参数覆盖优先级:

params > cookieParams > staticParams

示例代码:

1: npm i pre-fetch-api-webpack-plugin

2: vue.config.js

const PreFetchApiPlugin = require('pre-fetch-api-webpack-plugin');
chainWebpack: config => {
       config
           .plugin('PreFetchApiPlugin')
           .use(PreFetchApiPlugin, [[{
                method: 'post',
                params: { name: 'username' },
                staticParams: {
                    age: '18',
                },
                cookieParams:{
                    _address:"somewhere"
                }
                hashList: ['all'],
                urls: [
                    { url: `/aaa` },
                    {
                        url: `/bbb`,
                        method: 'get',
                        params: { add: 'add' },
                        staticParams: {
                            age: 33,
                            name: '333333',
                        },
                    },
                ],
            }]]);
   }

3:业务使用

const usePreFetchApiService = window.usePreFetchApiService?.();

export const getPageData = async () => {
    const { preFetchService } = usePreFetchApiService?.() || {};
    if (preFetchService) {
        try {
            const res = await preFetchService; 
            //可能设置多个接口 所以返回的是一个数组
            return res[0];
        } catch (error) {
            // 接口请求报错 这里可以再次发送请求
        }
    } else {
        // 因为异常原因 预请求运行报错导致 方法缺失 
    }
};