Redirect when working locally on HTMLRewriter

Hey! Sorry in advance if it’s a common topic, but I couldn’t find the answer with search or in documentation.

I am trying to understand how to work on HTMLRewriter locally. I copied this example code, and it works perfectly when deployed:

export default {
  async fetch(request) {
    const OLD_URL = "my-old-domain.com";
    const NEW_URL = "my-new-domain.com";

    class AttributeRewriter {
      constructor(attributeName) {
        this.attributeName = attributeName;
      }
      element(element) {
        const attribute = element.getAttribute(this.attributeName);
        if (attribute) {
          element.setAttribute(
            this.attributeName,
            attribute.replace(OLD_URL, NEW_URL)
          );
        }
      }
    }

    const rewriter = new HTMLRewriter()
      .on("a", new AttributeRewriter("href"))
      .on("img", new AttributeRewriter("src"));

    const res = await fetch(request);
    const contentType = res.headers.get("Content-Type");

    // If the response is HTML, it can be transformed with
    // HTMLRewriter -- otherwise, it should pass through
    if (contentType.startsWith("text/html")) {
      return rewriter.transform(res);
    } else {
      return res;
    }
  },
};

However, when I try it locally with wrangler dev, I get redirected to my-new-domain.com as soon as I hit localhost:port. Here’s my wrangler.toml:

name = "update-links"
main = "src/index.js"
compatibility_date = "2023-12-06"
routes = [
	{ pattern = "my-new-domain.com/*", custom_domain = false }
]

[dev]
host = "my-new-domain.com"
routes = [
	{ pattern = "my-new-domain.com/*", custom_domain = true }
]

Is it something wrong with my config? Or do I need a completely different setup for working locally? I was thinking of spinning up another simple server with some sample HTML to test stuff, but maybe there is a better way?

Try removing host from your wrangler.toml.

Same. I still get redirected to my-new-domain.com. My new config is:

name = "update-links"
main = "src/index.js"
compatibility_date = "2023-12-06"
routes = [
	{ pattern = "my-new-domain.com/*", custom_domain = false }
]

I also removed routes from dev section since I noticed that wrangler ignores it anyway:

▲ [WARNING] Processing wrangler.toml configuration:

    - Unexpected fields found in dev field: "routes"

I had a similar issue. Inside my worker code, I use something like this to fetch my production site content without being redirected:

if (env.ENVIRONMENT === 'development') {
  const url = new URL(request.url);
  response = await fetch(`${env.MY_PRODUCTION_SITE_URL}${url.pathname}${url.search}`, 
  request);
} else {
  response = await fetch(request);
}

Plus I have to use dev --remote because the rewriting does not work otherwise for some reason.
Probably, you also need to remove host and routes for development environment – but I am not entirely sure about that.