Skip to content

[Bug] Bug Report:Android 低版本设备(Android 9)上 pageFrame.js 加载失败 #243

Description

@jovkchan

描述问题 (Describe the bug)

环境
Dimina SDK 版本: v1.3.1
Android 设备: Android 9 (API 28) 商米 L2S PDA 设备
问题表现: 白屏,pageFrame 无法加载
错误日志

Failed to load module script: Expected a JavaScript module script
but the server responded with a MIME type of "application/octet-stream".
Strict MIME type checking is enforced for module scripts per HTML spec.

Uncaught TypeError: DiminaRenderBridge.onMessage is not a function

根本原因:

[pageFrame.html](vscode-file://vscode-app/d:/Programs/Microsoft%20VS%20Code/8761a5560c/resources/app/out/vs/code/electron-browser/workbench/workbench.html) 以 ES Module 方式引用 JS:
<script type="module" crossorigin src="/assets/pageFrame.js"></script>

请求进入 WebViewAssetLoader → DiminaPathHandler,MIME 类型通过以下代码确定:

文件: android/dimina/src/main/kotlin/com/didi/dimina/ui/view/WebViewCacheManager.kt

val mimeType = MimeTypeMap.getSingleton()
    .getMimeTypeFromExtension(targetCanonical.extension)
    ?: "application/octet-stream"

问题: 部分 Android 设备(尤其是国产 PDA/定制 ROM)的 MimeTypeMap 中缺少 .js → text/javascript 的映射,getMimeTypeFromExtension("js") 返回 null,导致兜底为 application/octet-stream。ES Module 规范要求 JavaScript 文件的 MIME type 必须是 text/javascript 或 application/javascript,否则拒绝执行。

复现步骤 (To Reproduce)

pageFrame.html 加载成功
↓ 引用 <script type="module" src="/assets/pageFrame.js">
WebViewAssetLoader 拦截请求
↓ DiminaPathHandler 找到文件但 MIME type 错误
浏览器拒绝执行(application/octet-stream ≠ text/javascript)
↓ pageFrame.js 未执行 → DiminaRenderBridge.onMessage 未注册
Native 调用 evaluateJavascript("DiminaRenderBridge.onMessage(...)")
↓ TypeError: DiminaRenderBridge.onMessage is not a function

复现链接 (Reproduce link)

No response

预期行为 (Expected behavior)

建议修复在 DiminaPathHandler.handle() 中增加对 JavaScript 文件的显式判断:
// 修复前
val mimeType = MimeTypeMap.getSingleton()
.getMimeTypeFromExtension(targetCanonical.extension)
?: "application/octet-stream"

// 修复后
val mimeType = if (targetCanonical.extension.equals("js", ignoreCase = true)) {
"text/javascript" // ES Module 要求明确的 JavaScript MIME type
} else {
MimeTypeMap.getSingleton()
.getMimeTypeFromExtension(targetCanonical.extension)
?: "application/octet-stream"
}

请验证

截图 (Screenshots)

Image

平台 (Platform)

  • Android
  • iOS
  • Harmony
  • Web

操作系统 (OS)

Windows

浏览器/终端 (Browser/Terminal)

No response

其它相关环境信息 (Other info)

No response

补充信息 (Additional context)

No response

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions