TOML different headers for different contexts

Thank you for the pointers @fool, I’m getting a better understanding of how to customise the config more to my needs.

I’ve moved my build commands out into rake commands which is simplifying the toml file itself and also getting rid of a lot of the code repetition I was getting with the various build contexts.

Now I have removed that context specific header and my branch context command looks as follows:

command = "rake netlify_build:branch"

My rake file is then similar to:

namespace :netlify_build do
  desc "Run build commands on Netlify"

  add_non_production_headers = "cp config/netlify/non_production_headers public/_headers"
  yarn_test = "yarn test"
  rspec_test = "RUBYOPT=-W0 parallel_rspec -n 2 spec"
  middleman_build = "NO_CONTRACTS=true RUBYOPT='-W0' middleman build"

  task :production do
    sh "#{yarn_test} && #{rspec_test} && #{middleman_build} --verbose"
  end

  task :branch do
    sh "#{add_non_production_headers} && #{yarn_test} && #{rspec_test} && #{middleman_build} --verbose"
  end

  task :cms, [:build_option] do |_task, args|
    sh "#{add_non_production_headers} && #{middleman_build} #{args[:build_option]}"
  end
end

I have all my headers that are shared with every context within the toml file. Then, before the build I am copying the additional headers I need per context into the _headers file.

1 Like