Netlify.toml headers don't work, but _headers do

Hello!

First off, thank you for the Netlify starter plan, it’s a super helpful product! :slight_smile:

I’ve seen older issues about this, but I thought I’d post a newer one. I’m fairly certain that setting headers in netlify.toml did not do anything. Putting this in

[[headers]]
  for = "/static/*"
  [headers.values]
  x-test-header = 'foobar'

did not set the header for paths under /static/. I also tried setting a cache-control header and that did not work either. On the Netlify deploy page, it said:

No header rules processed

This deploy did not include any header rules. Learn more about headers.

However, I know that the netlify.toml file at the root of the repository is working because I see log messages like:

Different build dir detected, going to use the one specified in the Netlify configuration file: 'site' versus '' in the Netlify UI

I then switched to a _headers file in the publish directory and that worked as expected. Putting this in

/static/*
  x-test-header: foobar

correctly set the header on static/ paths.

Given that the _headers file is working, I can work with that. However, I just thought I’d let you know that there is definitely a bug somewhere.

I’m not comfortable sharing my Netlify site publicly, but in case it helps, here are some current X-NF-Request-ID’s that you could use to find the project and its history?

  • 250ea57f-49a2-4fb9-854f-cae992084e29-5258920
  • ad87a1ea-bb08-4491-a19a-6da7730bbc08-1671098

Note that these are current request IDs and I’ve already switched to the _headers file, so the headers work as expected for these request IDs.

Hey there,
Thanks for this report! I’m wondering if this had to do with the issue you posted about over here and the issue I linked there —> Base directory build setting ignored in netlify.toml - #20 by jen When I check our database for the netlify.toml in some of your deploys that used one, I am not finding it. We’ll keep investigating but wanted to share the quick update.

My colleague @luke explained what happened here:

Ah, this is the new monorepo behavior. If a base directory is defined, the build starts over in that new directory. If there is no netlify.toml in the new directory, there won’t be one in the deployed files. This also means that any redirects or headers defined in the original netlify.toml will not be used when publishing. To summarize, the existence of a base setting in netlify.toml restarts the build there and causes the original netlify.toml to not be present in the deployed files.

As mentioned in the issue in that other post, we are working on getting this all documented :memo: but hope this is helpful for others who run into this issue in the meantime.

Thanks again, @jen! That makes sense, I’ll try it this way when I next update the site.

1 Like