Generate netlify _headers file in root dir for multilingual site

I followed these instructions for a multilingual site with defaultContentLanguageInSubdir: true.
This results in the following build output (I use victor-hugo, thus the dist/ folder):

dist
├── de
│   ├── _headers
...
├── en
│   ├── _headers
...

Note that there is no top-level _headers file, but just one in each language directory.

My current workaround is to add a copy script as part of the build. I use victor-hugo, so the package.json then looks as follows:

"scripts": {
    "build": "npm run build:webpack && npm run build:hugo && npm run build:copy-headers",
   ...
    "build:copy-headers": "cp dist/en/_headers dist/_headers"

Is there any way to force the headers file to be generated top-level and not in each language subdirectory?

You should check Hugo Custom Output formats.

All you have to do is create a new mediatype and custom output formats for your _headers file and then add the newly added output format to your homepage:

# In your config file
# 1. Create Media Type
mediaTypes:
  text/netlify:
    delimiter: ''
    suffixes:
    - ''
# 2. Create Output Format
outputFormats:
  headers:
    baseName: _headers
    isPlainText: true
    mediatype: text/netlify
    notAlternative: true
# 3. Add to homepage
outputs:
  home:
  - HTML
  - headers

Last you want to create a template file for your headers, for the homepage it should live at layouts/index.headers
:point_up: from there you can hardcode your headers or dynamically generate them using Go Template…

Thank you @regis.

I followed those instructions (they are the same as in the post I linked to).

And for a multilingual site I end up with several _redirects files, each in the respective language directory.

If defaultContentLanguageInSubdir is set to false (which is the default) this is not a big problem, because one of the _redirects files ends up in the root publish directory.

But if defaultContentLanguageInSubdir is set to true, none of the _redirects files ends up in the root directory.

Netlify expects the file to be in the root publish directory.