augnustin

augnustin

Making robots.txt environment specific

Hello,

I would like to have robots.txt empty in production, but in my staging environment that it contains:

User-agent: *
Disallow: /

Is there a way to simply achieve this?

Thanks

Marked As Solved

kokolegorille

kokolegorille

robots.txt is a static file pushed to web root by copy-webpack-plugin.

I think your solution is to check env in webpack, and change copy-webpack-plugin config to depend on it.

Also Liked

Phillipp

Phillipp

I would simply define a route for it and dynamically render it by environment.

sheerlox

sheerlox

Reviving this thread with a solution on how to achieve this behavior with a Plug, since I haven’t been able to find an answer here or on the Fly.io forums:

lib/my_app_web/plugs/robots.ex

defmodule MyAppWeb.Plugs.Robots do
  @behaviour Plug

  import Plug.Conn

  @impl true
  def init(opts), do: opts

  def call(%Plug.Conn{request_path: "/robots.txt"} = conn, _opts) do
    deploy_env = Application.get_env(:unill, :deploy_env)
    file = Application.app_dir(:unill, "priv/static/robots-#{deploy_env}.txt")


    content = File.read!(file)

    conn
    |> send_resp(200, content)
    |> halt()
  end

  @impl true
  def call(conn, _opts), do: conn
end

lib/my_app_web/endpoint.ex

defmodule MyAppWeb.Endpoint do
  use Phoenix.Endpoint, otp_app: :my_app

  plug MyAppWeb.Plugs.Robots

config/runtime.exs

deploy_env =
  case System.get_env("PHX_HOST") do
    "myapp.com" -> :production
    "staging.myapp.com" -> :staging
    _ -> :dev
  end

config :my_app, :deploy_env, deploy_env

Then, create the relevant robots-staging.txt and robots-production.txt files in priv/static/.

Remember to remove the robots.txt from the static_paths in lib/my_app_web.ex, although this plug should load and return a response before it if you’ve added it before the Plug.Static configuration.

cnck1387

cnck1387

Another option could be to have your configuration management tool write your robots.txt file. That’s what I do in Ansible. Then I just write a different robots.txt depending on which server / environment I’m deploying to and the code for it is alongside my other environment specific settings.

cnck1387

cnck1387

Certain configuration management tools allow you to have different settings for different environments. You could have your tool of choice write a robots.txt file to your staging server one way but have it different on your production server.

In other words your robots.txt file is managed by the tool you use to deploy your site instead of being part of your code repo. There’s little value in having it as part of your code repo IMO, especially since you’ll very likely want it to be different between environments.

Also in the future, if you want a dynamic robots.txt file you can either generate it on the spot with a custom Phoenix route, or have a cron job on your server generate a robots.txt file on whatever schedule you want so you can serve it with nginx or a CDN.

In all cases it doesn’t involve having a robots.txt file directly in your code repo.

Where Next?

Popular in Questions Top

srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
pgiesin
This should be a simple problem but I just can’t seem to figure it out. I have a standalone Elixir app that won’t find the database. Dep...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

Other popular topics Top

srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
AstonJ
by Lance Halvorsen Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
460 27162 124
New

We're in Beta

About us Mission Statement