Setting Custom Headers For Static Deployments

Describe your feature request
I would like to have a feature which lets you set headers per route or deployment on static deployments
What problem(s) would this feature solve?
You can have text compression and use things like SharedArrayBuffer to make your apps faster.
Explain what you were trying to do when you came across the problem leading to this feature request
I tried to compress a big js file, I tried to use ffmpeg’s multithreading which needs SharedArrayBuffer.

Edit: Turns out this is already a feature, but it is barely working, you:

  • can only set headers for all routes
  • can’t set headers like content-encoding
  • can’t set headers for extension deployments
  • headers don’t always get passed
6 Likes

Actually, assets in static deployments should be compressed (gzip/br) and cached by default (they aren’t currently). But having a header control will still be a very good feature.

2 Likes

You can already configure response headers in static deployments in your .replit file:

run = "…"

# …

[[deployment.responseHeaders]]
path = "/"
name = "access-control-allow-origin"
value = "https://example.com"

Note: last time I tested this was a while ago so I’m not 100% sure if it still works, but it should.
Oh and yeah this is undocumented.

5 Likes

Oh, thanks, I made this because Replit support told me I couldn’t.

2 Likes

How do I set multiple headers?
Do I need [[deployment.responseHeaders]] more than once?

2 Likes

Aw, it doesn’t work >:

2 Likes

Wouldn’t that just override headers you put previously? IDK how TOML handles it

1 Like

I don’t think I tried with [deployment.responseHeaders], and it gave me duplicate error, but with [[deployment.responseHeaders]] it doesn’t.

1 Like

In TOML, [[key]] defines an array, whereas [key] defines an object:

[[my_array]]
key = "value"

[[my_array]]
foo = "bar"
{
  "my_array": [
    {
      "key": "value"
    },
    {
      "foo": "bar"
    }
  ]
}

[my_object]
key = "value"

[my_object]
foo = "bar"
{
  "my_object": {
    "foo": "bar"
  }
}
4 Likes

I’m mainly interested about extensions as well, so far this doesn’t work on extension deployments, static deployments, autoscale deployments and I haven’t tested reserved vm. @LuisAFK

2 Likes

Ok I just tested it in a Repl and for some reason the only setting I can get to work for a header’s path is *, although that applies it to all routes that exist in your static deployment[1]. Setting it to any other value doesn’t seem to work.

This was the relevant .replit config I used while testing:

[deployment]
publicDir = "/"
deploymentTarget = "static"

[[deployment.responseHeaders]]
path = "*"
name = "x-my-static-header"
value = "replit-test"

[[deployment.responseHeaders]]
path = "test"
name = "x-my-static-header-again"
value = "replit-test-again"

I made a static deployment with the following routes:

If you visit each of the above routes and check DevTools you can see that in all of them the x-my-static-header is always set. If you visit the /test route, the x-my-static-header-again header is not set. I have tried setting the path to both test and /test but neither work.


  1. Although if you visit any route that doesn’t exist, for example /404, /abcd or /*, sometimes the x-my-static-header is set and sometimes it’s not, which is interesting. ↩︎

3 Likes

Well, that’s awesome I only needed *, I’ll try it out.
Edit: good news, it works for SharedArrayBuffer on static deployment, but for extension deployment I can’t seem to be able to set headers.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.