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

mini-next

v3.0.1

Published

mini-next是一个近乎零成本升级react服务端渲染的框架,支持静态导出,cdn部署,前后端同构,自定义路由前缀,自定义webpack,服务端渲染缓存

Downloads

104

Readme

mini-next是一个基于react v16.0+,react-router-dom v4.0+,koa2.0搭建的一个服务端渲染框架。可近乎零成本使历史项目工程具备服务端渲染能力,并支持导出静态资源功能。

初衷

  • 统一团队React工程配置,无需单独额外配置,开箱即用
  • 统一管理React,webpack,babel版本
  • 更好的用户体验,SEO支持
  • 相比业内实现,快速支持已有项目进行服务端渲染

使用

脚手架安装

框架提供mini-next-cli脚手架快速初始化模板工程

 npm  install mini-next-cli -g

初始化新项目

mini-next-cli init name

安装

yarn add mini-next 
or
npm install -S mini-next

app.js

const Koa = require('koa');
const miniNext = require('mini-next');
const app = new Koa();
new miniNext(app); // mini-next服务端渲染基于koa封装,开启ssr时需传入koa实例对象
app.listen(8001);

package.json

"scripts": {
    "start":"node app.js", // 启动
    "build":"npx mini-next build" // 生产环境部署前预编译构建
    "output":"npx mini-next output" // 导出静态资源
  },

工程目录

框架默认配置属性rootDir默认为根目录下src,pages目录下必须为项目文件夹,项目名不能命名为index

└── src
    └── pages
        └── demo
            ├── index.js 
            └── demo.scss

开发预览

mini-next采用多入口配置,src/pages下按照文件夹项目进行项目代码隔离,无论客户端还是服务端渲染均使用项目文件夹名称进行路由匹配。eg:localhost:8001/index 访问项目index.

服务端渲染预取数据

服务端渲染预初始化数据,通过静态方法getInitialProps函数调用接口返回。

APP.getInitialProps = async (ctx,query,pathname) => {
        //...
        return { count: 1 }
    }

客户端通过组件的props获取服务端数据,getInitialPropsreturn返回的属性会挂载到组件的props上

let APP = props => {
    const [count, setCount] = useState(props.count);
    return (
        <div>
            <span>Coweunt: {count}</span>
        </div>
    );
};

props除了挂载我们getInitialProps的返回值外,还会挂载url中的的pathname和query,可通过全局对象window.__SSR_DATA__访问服务端预渲染返回的初始化数据。

高级配置

ssr.config.js

可在config/ssr.config.js下对我们的项目进行相关配置。

module.exports = {
    ssr: true, // 全局开启服务端渲染
    cache: true, // 全局使用服务端渲染缓存 第一次ssr,再次采用缓存,适用与存静态资源或者所有人访问的页面都是一样的工程
    staticPages: [], // 纯静态页面 执行一次服务端渲染,之后采用缓存html
    rootDir:'src', // 配置客户端根目录
    prefixCDN:'/', // 构建后静态资源CDN地址前缀
    prefixRouter: '', // 页面路由前缀
    ssrIngore: null or new RegExp() // 指定某一个或者多个page项目不采用服务端渲染
}

更多