简体   繁体   English

使用错误(无)MIME 类型的 Vite 服务着色器文件

[英]Vite serving shader file with wrong (none) MIME type

I'm developing a BabylonJS application.我正在开发一个 BabylonJS 应用程序。 BabylonJS PostProcess class appends .fragment.fx to a given file name and requests that from the server. BabylonJS PostProcess class 将.fragment.fx附加到给定的文件名并从服务器请求它。 When my local Vite (version 4.0.4) dev server serves this file the content-type header is empty.当我的本地 Vite(版本 4.0.4)开发服务器提供此文件时, content-type header 为空。 This causes Firefox to intepret it as type xml and fail.这导致 Firefox 将其解释为类型xml并失败。 Chrome fails through a different, but I think related, mechanism. Chrome 通过不同但我认为相关的机制失败。

来自 .fragment.fx 文件的错误解释为 MIME 类型 XML

How do you configure Vite to serve the *.fragment.fx static files as text/plain ?如何配置 Vite 以text/plain格式提供*.fragment.fx static 文件? I assume I need to disable the default middleware and write some custom code instead, like this: https://vitejs.dev/config/server-options.html#server-middlewaremode but I wanted to first check there wasn't something else going on / a simpler way to configure / fix this.我假设我需要禁用默认中间件并改为编写一些自定义代码,如下所示: https://vitejs.dev/config/server-options.html#server-middlewaremode但我想先检查没有别的东西继续/配置/修复此问题的更简单方法。

The vite dev server is started using vite --host --port 3000 --force and the config in vite.config.js is: vite dev server 使用vite --host --port 3000 --force启动, vite.config.js中的配置为:

import { defineConfig } from 'vite';

export default defineConfig(({ command, mode }) => {
    // if (command === 'serve') {
    //     return {
    //       // dev specific config
    //     }
    // } else {
    //     // command === 'build'
    //     return {
    //         // build specific config
    //     }
    // }


    return {
        resolve: {
            alias: {
                "babylonjs": mode === "development" ? "babylonjs/babylon.max" : "babylonjs",
            }
        },
        base: "",
        // assetsInclude: ['**/*.fx'],
    };
});

* edit 1 * * 编辑 1 *

I have seen there's a parameter ?raw that can be added to the URL however I don't control how BabylonJS forms the URL so I can't see how to make this work in this situation.我已经看到有一个 参数?raw可以添加到 URL 但是我不控制 BabylonJS forms URL 的方式,所以我看不到如何在这种情况下进行这项工作。

I followed these instructions and set up a dev server using express.我按照这些说明使用 express 设置了一个开发服务器。 I added this block of code above the call to app.use(vite.middlewares) :我在调用app.use(vite.middlewares)的上方添加了这段代码:

  app.use("**/*.*.fx", async (req, res, next) => {
    const url = req.originalUrl

    const file_path = path.resolve(__dirname, "." + url)

    const file = fs.readFileSync(file_path, "utf-8")

    res.status(200).set({ "Content-Type": "text/plain" }).end(file)
  })

I now start the dev server using the following script line in the package.json of "dev": "node server",我现在使用"dev": "node server",

I could not find a way to solve this by configuring the default vite dev server.我找不到通过配置默认的 vite 开发服务器来解决这个问题的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM