kwhitley / itty-router

A little router.
MIT License
1.69k stars 77 forks source link

Throw unexpected error #249

Open wqcstrong opened 1 month ago

wqcstrong commented 1 month ago

I've been using the Router from itty-router in a Cloudflare Worker, and I've encountered some unexpected behavior during its usage. The simplified logic of the code is to return the content of https://example.com upon receiving a user request.

// Without using itty-router
export default {
  async fetch(request) {
    return await fetch('https://example.com');
  }
}

When testing in the local development environment using curl, I received the normal HTML content from example.com:

curl http://localhost:8787/

After introducing itty-router:

import { Router, cors, error, json, text, withParams } from 'itty-router';

const { preflight, corsify } = cors({
    origin: /.*/,
    credentials: true,
});

const router = Router({
    before: [preflight, withParams],
    catch: error,
    finally: [corsify, json],
});

router.all('/test', () => {
  return await fetch('https://example.com');
})

export default {
  ...router
}

Continuing the test with curl http://localhost:8787/test, I received the following response:

{"status":500,"error":"Can't modify immutable headers."}%

Could someone please explain what might be causing this issue?