caddyserver / replace-response

Caddy module that performs replacements in response bodies
Apache License 2.0
98 stars 27 forks source link

Support for Path-Matchers #19

Closed syxolk closed 1 year ago

syxolk commented 1 year ago

Let's say I've got an index.html and serveral static JS+CSS files that are served via Caddy's file_server directive. Is there a way to make sure that the replace directive is only run for my index.html file, e.g. with path matchers? I didn't see any example or documentation about that in this repository.

Just for the record: these are my files

My index.html looks like this:

<meta name="BASE_URL" content="__BASE_URL__" />

And my Caddyfile looks like this:

{
    order replace after encode
}

:80 {
    root * /srv
    file_server

    route {
        try_files {path} /index.html

        header /index.html {
            Cache-Control no-cache
        }

        replace __BASE_URL__ https://example.com
    }
}
syxolk commented 1 year ago

I've tried replace /index.html __BASE_URL__ https://example.com and it seems to work. At least, I don't get any warnings or errors. Is this actually a supported syntax?

mholt commented 1 year ago

I'm not sure I understand what you mean by a patch matcher?

syxolk commented 1 year ago

Sorry, I meant "path matcher" of course. This: https://caddyserver.com/docs/caddyfile/matchers#path-matchers

I fixed the title.

mholt commented 1 year ago

Ah, ok -- I wasn't sure, since PATCH is also an HTTP method, and could theoretically be related to matchers.

Is there a way to make sure that the replace directive is only run for my index.html file, e.g. with path matchers?

I would do:

replace /index.html __BASE_URL__ https://example.com

Or if the request is for / instead:

replace / __BASE_URL__ https://example.com