如何将微信小程序从WebView迁移到Skyline

什么是 Skyline

微信小程序新的渲染引擎,使用更加高效的渲染管线,提供更好的性能和全新的交互动画体验。

具体可以查阅官网介绍

将开发者工具切换成 Sykline 模式

  1. 调试基础库切到 2.30.4 或以上版本

  2. 确保右上角 > 详情 > 本地设置里的 开启 Skyline 渲染调试、启用独立域进行调试 选项被勾选上

  3. 确保右上角 > 详情 > 本地设置里的 将 JS 编译成 ES5 选项被勾选上

使用 skylint 工具迁移

复制代码
npx skylint

使用过程中可能会出现文件未找到错误,例如

原因就是使用绝对路径 <import src="/components/chooserList/index.wxml" />导入模块,而 skylint 无法找到该文件,需要修改为相对路径 <import src="../../components/chooserList/index.wxml" />导入模块

有几种提示不是很准确,可以评估下:

  1. @position-fixed 不支持 position: fixed:如果你根据不同 renderer 兼容,则会导致该提示一直存在

  2. @no-pseudo-element 不支持伪元素: 目前对已经支持的 ::before 和 ::after 也会进行提示

手动迁移

在 app.json 配置

json 复制代码
{
"lazyCodeLoading": "requiredComponents", // 开启按需注入
    "rendererOptions": {
        "skyline": {
            "defaultDisplayBlock": true // skyline 下节点默认为 flex 布局,可以在此切换为默认 block 布局
        }
    }
}

在 page.json 配置

json 复制代码
{
    "renderer": "skyline", // 声明为 skyline 渲染,对于已有的项目,建议渐进式迁移,对于新项目,直接全局打开,在 app.json 里进行配置
    "componentFramework": "glass-easel", // 声明使用新版 glass-easel 组件框架
    "disableScroll": true, // skyline 不支持页面全局滚动,为了使之与WebView保持兼容,在此禁止滚动
    "navigationStyle": "custom" // skyline 不支持原生导航栏,为了使之与WebView保持兼容,并且自行实现自定义导航栏
}

skyline 不支持页面全局滚动,如果需要页面滚动,在需要滚动的区域使用 scroll-view 实现

html 复制代码
<scroll-view type="list" scroll-y style="flex: 1; width: 100%; overflow: hidden;"></scroll-view>
css 复制代码
page {
    display: flex;
    flex-direction: column;
    height: 100vh;
}

skyline 渲染模式下 flex-direction 默认值是 column,为了使之与WebView保持兼容,需要在 flex 布局里将 flex-direction 默认值改为 row

在真机上调试 skyline 渲染模式

小程序菜单 > 开发调试 > Switch Render,会出现三个选项,说明如下:

Auto :跟随 AB 实验,即对齐小程序正式用户的表现

WebView :强制切为 WebView 渲染

Skyline :若当前页面已迁移到 Skyline,则强制切为 Skyline 渲染

常见问题

position: fixed 不支持

需要将

html 复制代码
<view class="background"></view>
css 复制代码
.background {

    position: fixed;

}

修改为

html 复制代码
<root-portal>

    <view class="background"></view>

</root-portal>
css 复制代码
.background {

    position: absolute;

}

如果无法做到适配,则可以根据不同 renderer 兼容

html 复制代码
<view class="position {{renderer}}"></view>
css 复制代码
.position {
    position: fixed;
}

.position.skyline {
    position: absolute;
}
js 复制代码
Page({
    data: {
        renderer: 'webview'
    },

    onLoad() {
        this.setData({
            renderer: this.renderer,
        })
    },
})

不支持 Canvas 旧版接口

Skyline 渲染模式下,旧版 Canvas 绘制图案无效(使用 wx.createCanvasContext 创建的上下文)

在真机中图片的 referer 丢失

测试结果如下:

使用 WebView 渲染 Image,请求的 header 是:

json 复制代码
{
    host: 'xxx',
    connection: 'keep-alive',
    accept: 'image/webp,image/avif,video/*;q=0.8,image/png,image/svg+xml,image/*;q=0.8,*/*;q=0.5',
    'user-agent': 'xxx',
    'accept-language': 'zh-CN,zh-Hans;q=0.9',
    referer: 'https://servicewechat.com/',
    'accept-encoding': 'gzip, deflate'
}

使用 Skyline 渲染 Image,请求的 header 是:

json 复制代码
{

    'user-agent': 'Dart/2.16 (dart:io)',
    'accept-encoding': 'gzip',
    host: 'xxx'
}

官方 Demo

可以参考官方Demo学习使用 Skyline 的增强特性,比如 Worklet 动画、手势系统等,但在首次下载编译时,会遇到【交互动画】页面为空的问题,主要原因是该页面是由 TypeScript 写的,编译成 JavaScript 需要开启工具内置的 TypeScript编译插件,需要在project.config.json project.config.json 配置:

json 复制代码
setting.useCompilerPlugins: ["typescript"]

参考

相关推荐
StevenLdh20 小时前
第四期 · 云端同步与冲突仲裁:离线可写、多端一致的工程解法
java·微信小程序
m0_4628038821 小时前
会议签到如何设置手写签名?从创建签到到导出签名 PDF 的完整流程
微信小程序
AI工具人PM产品经理2 天前
零基础微信小程序入门:一个真实项目的里程碑路线图
微信小程序·云开发·学习路线·零基础入门·ai 编程
拖孩3 天前
这个小程序是 AI 帮我写的,可它里面一个 AI 功能都没有
前端·后端·微信小程序
克里斯蒂亚诺更新3 天前
小程序自定义导航栏怎么写:从配置到实战
微信小程序
liyinchi19883 天前
微信小程序支付遇到“由于小程序违规,支付功能暂时无法使用” 解决办法
java·微信小程序·go
silianpan3 天前
Office 文档预览 UTS 插件
android·微信小程序·harmonyos
微笑的曙光3 天前
第三期 · 账号体系与双 Token 鉴权:让用户「无感登录,有感安全」
微信小程序
耀耀切克闹灬4 天前
Skyline 渲染问题记录以及总结
微信小程序
小程序开发X4 天前
2026 深圳 APP 开发甄选:技术架构搭建、全流程落地、综合选型解读
微信小程序·小程序·app开发