我正在使用MongoDB Realm/App Services为现有的iOS应用程序制作Web前端.

标准的Realm Web SDK只允许身份验证和后端功能,所以我try 使用Web Assembly版本(realm@12.0.0-browser.2)的预览版,以便我可以使用设备同步并以我习惯的方式与Realm对象交互.

详情请浏览:Get Started with Realm Web & Atlas Device Sync (Preview)

我只是使用vite创建的基本应用程序脚手架,然后在App.vue中导入领域.我(还没有)在代码中的任何其他地方使用领域.

import Realm, { App } from "realm";

正如领域SDK文档中所建议的,对于Web Assembly版本,我必须在vite.config.js中启用顶级等待:

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [vue()],
  esbuild: {
    supported: {
      'top-level-await': true
    },
  },
  optimizeDeps: {
    esbuildOptions: {
      supported: {
        "top-level-await": true
      },
    },
  },
})

因此,当我构建和预览(vite build之后是vite preview)时,这是没有错误的.

但是,当我执行npm run dev(vite)时,服务器按预期启动,但浏览器控制台中有以下错误:

Console errors

[Error] wasm streaming compile failed: TypeError: Unexpected response MIME type. Expected 'application/wasm'
    (anonymous function) (realm.js:726)
[Error] falling back to ArrayBuffer instantiation
    (anonymous function) (realm.js:727)
[Error] failed to asynchronously prepare wasm: CompileError: WebAssembly.Module doesn't parse at byte 0: module doesn't start with '\0asm'
    (anonymous function) (realm.js:717)
[Error] Aborted(CompileError: WebAssembly.Module doesn't parse at byte 0: module doesn't start with '\0asm')
    abort (realm.js:661)
    (anonymous function) (realm.js:718)

如果我构建和预览,这不会发生,所以我希望它实际上不会成为一个问题,但我不明白这里发生了什么,我很好奇如何在开发过程中修复它.


编辑

感谢VonC个人的回答.我try 使用自定义中间件,但收到找不到该文件的错误.

.wasm个文件在node_modules/realm/dist中,但它似乎在node_modules/.vite中查找.

如果我使用const wasmPath = path.join(__dirname, req.url);(即不使用public路径组件),这就是错误.

环境:没有这样的文件或目录,请打开‘<my_local_path>/vite-project/node_modules/.vite/deps/realm-js-wasm.wasm’

最后,我使用中间件解决方案并将其直接重定向到领域文件夹,从而使其正常工作:

const wasmPath = path.join(__dirname, 'node_modules/realm/dist', path.basename(req.url));

推荐答案

这些错误表明WASM模块预期的MIME类型以及加载或解析WebAssembly模块的方式存在问题,如在this issue中:Vite开发服务器没有正确地为具有application/wasm MIME类型的.wasm文件提供服务.

您可以try 并配置VITE以正确的MIME类型正确地为WASM文件提供服务,修改您的vite.config.js文件以包括用于处理.wasm个文件的定制中间件,如mycelial/mycelial-js issue 25:

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import fs from 'fs'
import path from 'path'

// Custom middleware to serve wasm files with the correct MIME type
const wasmMiddleware = () => {
  return {
    name: 'wasm-middleware',
    configureServer(server) {
      server.middlewares.use((req, res, next) => {
        if (req.url.endsWith('.wasm')) {
          const wasmPath = path.join(__dirname, 'public', req.url);
          const wasmFile = fs.readFileSync(wasmPath);
          res.setHeader('Content-Type', 'application/wasm');
          res.end(wasmFile);
          return;
        }
        next();
      });
    },
  };
};

export default defineConfig({
  plugins: [vue(), wasmMiddleware()],
  esbuild: {
    supported: {
      'top-level-await': true
    },
  },
  optimizeDeps: {
    esbuildOptions: {
      supported: {
        "top-level-await": true
      },
    },
  },
})

That custom middleware checks for requests ending in .wasm and serves them with the correct Content-Type header. That assumes the .wasm files are located in a public directory at the root of your project.
And that would be tailored for development with Vite's dev server and assumes your production environment already correctly serves .wasm files with the appropriate MIME type, as the issue does not manifest when using vite build followed by vite preview.


最后,我通过使用中间件选项让它工作,但直接将其重定向到Realm文件夹:

const wasmPath = path.join(__dirname, 'node_modules/realm/dist', path.basename(req.url));

True:不是从通用或不正确的路径(如public文件夹或.vite缓存)提供.wasm个文件,而是直接指向这些文件在node_modules目录中的实际位置.在中间件功能中调整路径:

const wasmMiddleware = () => {
  return {
    name: 'wasm-middleware',
    configureServer(server) {
      server.middlewares.use((req, res, next) => {
        if (req.url.endsWith('.wasm')) {
          // Directly point to the .wasm file within the realm/dist directory
          const wasmPath = path.join(__dirname, 'node_modules/realm/dist', path.basename(req.url));
          try {
            const wasmFile = fs.readFileSync(wasmPath);
            res.setHeader('Content-Type', 'application/wasm');
            res.end(wasmFile);
          } catch (error) {
            console.error('Failed to load WASM file:', error);
            next();
          }
          return;
        }
        next();
      });
    },
  };
};

Javascript相关问答推荐

获取表格的左滚动位置

Phaser框架-将子对象附加到Actor

Redux工具包查询(RTKQ)端点无效并重新验证多次触发

Cypress -使用commands.js将数据测试id串在一起失败,但在将它们串在一起时不使用命令有效

类型脚本中只有字符串或数字键而不是符号键的对象

防止用户在selectizeInput中取消 Select 选项

使用下表中所示的值初始化一个二维数组

将本机导航路由react 到导航栏中未列出的屏幕?

MathJax可以导入本地HTML文档使用的JS文件吗?

WebGL 2.0无符号整数输入变量

对网格项目进行垂直排序不起作用

在使用REACT更改了CSS类之后,无法更改CSS样式

当用户点击保存按钮时,如何实现任务的更改?

TypeError:无法读取未定义的属性(正在读取';宽度';)

使用Document.Evaluate() Select 一个包含撇号的HTML元素

在高位图中显示每个y轴系列的多个值

Firefox的绝对定位没有达到预期效果

REACT-本机错误:错误类型错误:无法读取未定义的容器的属性

在HTML中使用meta标记来指定定制元数据以用于使用JavaScript进行检索是不是一个坏主意?

暂停后只有一次旋转JS