GithubPages:如何为任何文件强制执行自定义内容类型类型?

lz9*_*z96 6 content-type github-pages

我们目前使用 Github 页面作为简单的 API 模拟服务器。我们将所有模拟文件放在 和 下docs/,只是GET username.github.io/project/api/someAPI为了检索数据。但是,默认情况下,Github Pages 返回的 Content-Type 不是application/octet-stream没有application/json扩展名的文件,因此我们的前端代码在解析结果时会抛出错误。

我想知道有什么方法可以更改 Github Page 上没有扩展名的文件返回的 Content-Type 吗?

Jes*_*ick 2

https://docs.github.com/en/pages/getting-started-with-github-pages/about-github-pages#mime-types-on-github-pages表明这是不可能的:

您无法在每个文件或每个存储库的基础上指定自定义 MIME 类型

通过实验,您可以创建subdir/index.json这将导致https://owner.github.io/repo/subdir/成功 (200) 提供类型为 的内容application/json。但https://owner.github.io/repo/subdir将提供 301 重定向,API 客户端可能无法容忍。

从https://jekyllrb.com/docs/configuration/options/获取提示我尝试创建_config.yml

include: .htaccess
Run Code Online (Sandbox Code Playgroud)

.htaccess

ForceType application/json
Run Code Online (Sandbox Code Playgroud)

没有成功。我还发现https://github.com/jekyll/jekyll/issues/5454表明这是不可能的。