Search code examples
content-typegithub-pages

GithubPages: how to enforce custom Content-Type type for any file?


We are currently using Github pages as a simple API mock server. We put all mock files under docs/ and simply GET username.github.io/project/api/someAPI to retrieve data. However, by default, the Content-Type returned by Github Pages is application/octet-stream instead of application/json for files with no extensions, therefore our frontend code throws errors when parsing the result.

I want to know is there any way to change the returned Content-Type for files with no extension on Github Page?


Solution

  • https://docs.github.com/en/pages/getting-started-with-github-pages/about-github-pages#mime-types-on-github-pages suggests this is impossible:

    you can't specify custom MIME types on a per-file or per-repository basis

    From experimentation, you can create subdir/index.json which will cause https://owner.github.io/repo/subdir/ to successfully (200) serve content with type application/json. But https://owner.github.io/repo/subdir will serve a 301 redirect, which an API client may not tolerate.

    Taking a hint from https://jekyllrb.com/docs/configuration/options/ I tried creating _config.yml

    include: .htaccess
    

    and .htaccess

    ForceType application/json
    

    without success. I also found https://github.com/jekyll/jekyll/issues/5454 suggesting this is not possible.