• Stars
    star
    224
  • Rank 172,068 (Top 4 %)
  • Language
    TypeScript
  • Created about 2 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

让 Taro H5 支持 Pre-rendering、SSR 和 ISR,极致的首屏速度🚀,利于 SEO🔍

Taro Next.js SSR 插件

让 Taro H5 支持 Pre-renderingSSRISR,极致的首屏速度 🚀,利于 SEO 🔍

作为该项目的 Owner,我目前的工作不再涉及 Taro 的开发,难以投入持续的精力来维护该项目,所以正在积极寻找本插件的贡献者。如果你有兴趣,请添加微信 SharpYourMind 与我联系。

安装

你需要先有一个 Taro 项目,如果你还不知该如何创建一个 Taro 项目,请先从这里开始:Taro 安装及使用

要安装和设置本插件,请运行:

npm init tarojs-plugin-platform-nextjs

或者,你可以手动安装在你的 Taro 项目中安装本插件Next.js

npm install next tarojs-plugin-platform-nextjs

并在 Taro 项目的 config/index.js 中添加插件。

const config = {
    plugins: [
        'tarojs-plugin-platform-nextjs'
    ]
}

使用

开发环境

npx taro build --type nextjs --watch

生产环境

项目必须先使用以下命令进行编译:

npx taro build --type nextjs

启动应用:

npx taro start -p 10086

产出目录下是一个完整的 Next.js 应用,你也可直接使用 next 命令启动它:

npx next start dist -p 10086

谁在使用

Beauty wiki logo

主要功能

Pre-rendering - 预渲染

插件默认 Pre-rendering 所有页面。这意味着提前为每个页面生成 HTML,而不是让浏览器端 JavaScript 完成所有工作。预渲染可以带来更好的性能和搜索引擎优化。

示例 - 房贷计算器

阅读 Next.js 文档了解更多:Pre-rendering

SSR - 服务端渲染

在页面中导出 getServerSideProps 函数来使用 SSR 功能,插件将对每个请求使用 getServerSideProps 返回的数据预先渲染该页面。

export async function getServerSideProps(context) {
    return {
        props: {} // 将作为页面组件的属性
    }
}

示例 - Data Fetch Demo

阅读 Next.js 文档了解更多:getServerSideProps

ISR - 增量静态生成

在页面导出 getStaticProps 函数并对其添加 revalidate 属性来使用 ISR 功能。

ISR 功能允许你单独对某个页面进行增量静态生成,无需重新生成整个网站。使用 ISR,你可以在数百万页面的规模上同时保留静态的好处。

export async function getStaticProps() {
  const res = await fetch('https://.../posts')
  const posts = await res.json()

  return {
    props: {
      posts,
    },
    // Next.js 将尝试重新生成页面
    // - 当接受到一个请求
    // - 最多每 10 秒一次
    revalidate: 10 // 单位为秒
  }
}

示例 - GitHub Reactions Demo

阅读 Next.js 文档了解更多:Incremental Static Regeneration

自定义 Next.js 配置

可以在项目的 config 目录下添加 next.config.js 文件来自定义 Next.js 应用的相关配置。

如自定义 H5 应用的基本路径(basePath):

module.exports = {
    basePath: '/swan'
}

阅读 Next.js 文档了解更多:next.config.js

注意事项

功能限制

由于插件基于 Next.js 框架,故受其影响,有以下限制:

  1. 仅支持 browser 路由模式。
  2. 组件级样式必须使用 CSS Module。

获取路由参数

Taro 有两种获取路由参数的方式,一种是调用方法 getCurrentInstance().router.params,另一种是使用 React Hook useRouter().params

推荐使用 useRouter 来获取路由参数,因为它内部直接使用 Next.js 提供的 useRouter React Hook 实现,具有很好的一致性。

你仍可以使用 getCurrentInstance 方法,大多数情况它都会运行的很好。但明白插件是如何处理该方法,会让你避免遇到一些问题时不知所措。

当你在一个类组件中调用 getCurrentInstance 时,在编译阶段插件会在该组件外部使用 Next.js 的 withRouter 方法进行包装,让类组件能够响应 Next.js 路由的变化。

+ import {withRouter} from 'next/router'

class MyComponent extends Component {
    $instance = getCurrentInstance()

    render() {
        console.log(this.$instance.router)
        return null
    }
}

- export default MyComponent
+ export default withRouter(MyComponent)

Taro 组件的 React 实现

Taro 官方的 H5 组件库是基于 Stencil 框架开发,为了更好的性能和兼容性,本项目使用完全基于 React 开发的 Taro 组件库 @taror/components

参与建设

十分欢迎大家参与对插件问题的修复和功能的改进,如果你有任何问题,都可以在本项目的 issue 中进行提问!

本地编译

使用以下命令在本地编译插件

yarn install

yarn lerna run build

架构设计

你可以通过阅读架构设计来了解本插件的工作原理。

贡献者


SyMind

zygoing

zengshide123

wpigdry

License

MIT

More Repositories

1

taro

开放式跨端跨框架解决方案,支持使用 React/Vue/Nerv 等框架来开发微信/京东/百度/支付宝/字节跳动/ QQ 小程序/H5/React Native 等应用。 https://taro.zone/
TypeScript
34,816
star
2

nerv

A blazing fast React alternative, compatible with IE8 and React 16.
JavaScript
5,421
star
3

taro-ui

一款基于 Taro 框架开发的多端 UI 组件库
TypeScript
4,452
star
4

awesome-taro

多端统一开发框架 Taro 优秀学习资源汇总
2,683
star
5

taro-zhihu-sample

a demo based on taro
JavaScript
342
star
6

taro-ui-demo

非 Taro UI 官网示例代码
JavaScript
284
star
7

taro-native-shell

Taro 原生 React Native 壳子
Java
193
star
8

taro-sample-weapp

与小程序原生融合的示例
JavaScript
159
star
9

taro-v2ex

使用 Taro 实现 V2EX 小程序
TypeScript
153
star
10

taro-v2ex-hooks

使用 Taro 和 React Hooks 构建 v2ex 小程序
TypeScript
139
star
11

taro-rfcs

为了重大特性更新和架构更改顺利推进的 RFC 流程机制
74
star
12

taro-project-templates

Templates for `taro init`
JavaScript
70
star
13

taro-ui-sample

基于 Taro 的多端 UI 库示范用例
JavaScript
68
star
14

taro-antd-mobile

Taro 兼容 Ant Design Mobile
SCSS
60
star
15

taro-todo

多端解决方案示例
JavaScript
56
star
16

nerv-webpack-boilerplate

A webpack boilerplate with Nervjs
JavaScript
52
star
17

taro-components-sample

taro components sample
JavaScript
52
star
18

TodoMVC

TodoMVC with Taro + Redux
JavaScript
43
star
19

taro3-vant-sample

Taro3 中使用 vant-weapp 的示例
JavaScript
42
star
20

taro-todomvc-hooks

用 Taro 和 React Hooks API 实现 TodoMVC
JavaScript
41
star
21

taro-vant

Taro 兼容 VantUI
Vue
40
star
22

taro-redux-sample

taro redux sample
JavaScript
37
star
23

taro-uilib-react

Taro UI 库 React 版本范例
JavaScript
34
star
24

at-ui-nerv

AT-UI for Nerv
TypeScript
31
star
25

taro-plugin-inject

Taro 小程序端平台中间层插件
TypeScript
31
star
26

taro-user-cases

使用 Taro 开发的案例
JavaScript
29
star
27

taro-plugin-mock

Taro 数据 Mock 插件
TypeScript
27
star
28

taro-plugin-platform-kwai

快手小程序平台插件
TypeScript
26
star
29

taro-plugin-platform-weapp-qy

企业微信插件
TypeScript
20
star
30

taro-components-test

Taro 组件化方案测试
JavaScript
20
star
31

taro-weui

Taro 兼容 WEUI
SCSS
18
star
32

taro-plugin-platform-lark

飞书(Lark)小程序平台插件
TypeScript
16
star
33

taro-bot

A bot app for Taro
TypeScript
16
star
34

taro-plugin-platform-alipay-dd

钉钉小程序平台插件
TypeScript
16
star
35

taro-docs

Taro 文档
MDX
15
star
36

taro-apis-sample

taro apis sample
JavaScript
11
star
37

taro-blended

在原生小程序项目中使用 Taro
JavaScript
10
star
38

taro-flexbox

Taro 跨端的flexbox布局案例
TypeScript
10
star
39

nerv-website

The landing page of Nerv
JavaScript
10
star
40

nerv-redux-todomvc

TodoMVC example using Nerv and Redux
TypeScript
9
star
41

parse-css-to-stylesheet

css转stylesheet插件,适用于鸿蒙、RN
Rust
8
star
42

taro-test-utils

多端单测工具
TypeScript
8
star
43

taro-component-website

Taro 基础组件库文档
CSS
6
star
44

taro-plugin-platform-xhs

小红书小程序平台插件
TypeScript
6
star
45

taro-ui-theme-preview

Taro UI 自定义主题生成器
JavaScript
5
star
46

taro-calendar

一款基于 Taro 框架开发的多端 日历组件
TypeScript
5
star
47

taro-doctor

taro doctor based on NAPI-RS
Rust
4
star
48

nerv-server

Server-side rendering for Nerv.js
TypeScript
4
star
49

postcss-pxtransform

JavaScript
3
star
50

taro-plugin-indie

Taro 单独分包运行插件
TypeScript
3
star
51

taro-website

The landing page of Taro
JavaScript
3
star
52

taro-benchmark

Benchmark of Taro, weapp and jdapp.
JavaScript
3
star
53

taro-todos-pinia

Taro3 + Vue3 + pinia
Vue
2
star
54

taro-plugin-platform-alipay-iot

支付宝 IOT 端小程序平台插件
TypeScript
2
star
55

taro-plugin-shared-runtime

微信小程序多分包共享Taro运行时插件
TypeScript
2
star
56

nerv-test-utils

Mock react-test-utils used for Nerv.js
TypeScript
2
star
57

taro-issue-helper

https://nervjs.github.io/taro-issue-helper/
Vue
1
star
58

taro-ui-bot

A bot app for taro-ui
TypeScript
1
star
59

taro-plugin-migrate

快速升级迁移插件
TypeScript
1
star