cloudflare / cloudflare/workers-sdk

🐛 BUG: Redirects do not work properly in wrangler 3

Open
#5,860 10 comments 1 reaction 0 assignees View on GitHub
Dominant language
TypeScript
Stars
4.5k
Forks
1.5k
Avg merge
3d 8h
Merged PRs (30d)
187

Description

### Which Cloudflare product(s) does this pertain to?

Wrangler core

### What version(s) of the tool(s) are you using?

3.56.0 [Wrangler]

### What version of Node are you using?

v20.11.0

### What operating system and version are you using?

macOS sonoma 14.4.1 (23E224)

### Describe the Bug

### Observed behavior

When calling `return Response.redirect(url.href, 301)` from within Worker code, I'm getting errors, both when using the `unstable_dev` API as well as when using `wrangler dev` with a custom local host specified.

### Expected behavior

My redirect code fully worked with v2 of wrangler, but after upgrading to v3 I'm getting errors in both my tests and my local dev environment.

### Steps to reproduce

1. Visit https://stackblitz.com/edit/stackblitz-starters-vjsh9a?file=index.js
2. Run `node index.js` (if it doesn't happen automatically)
3. View the error logged to the terminal.

![Screenshot 2024-05-16 at 8 56 06 PM](https://github.com/cloudflare/workers-sdk/assets/326742/c7c46a1e-8dc1-4b15-b8be-57549f719440)

Here is the contents of the worker file in the repro:

```js
// worker.js
export default {
async fetch(request) {
const url = new URL(request.url);

if (url.pathname === '/needs-redirect') {
url.pathname = '/redirected';
return Response.redirect(url.href, 301);
}

return new Response('Hello World!');
},
};
```

In addition to the error shown above when using `unstable_dev`, I'm also seeing errors for the same worker file when running `wrangler dev` locally with a local host specified:

```sh
wrangler dev --host=localhost:3000 ./worker.js
```
Then, if I use `curl` to test the redirect, I get an invalid `Location` header returned with the response. See the following screenshot:

![Screenshot 2024-05-16 at 8 52 56 PM](https://github.com/cloudflare/workers-sdk/assets/326742/125df105-97ae-4e47-b5f5-af8db0ce9270)

Notice how path was property updated by the redirect logic in the worker, but the URL in the `Location` header is invalid, as it now includes port 3000 appended to the existing port (which is also repeated for some reason). This also did not happen when using wrangler v2.

### Please provide a link to a minimal reproduction

https://stackblitz.com/edit/stackblitz-starters-vjsh9a?file=index.js

### Please provide any relevant error logs

Error when using `unstable_dev`:

```sh
✘ [ERROR] failed to start worker registry TypeError: fetch failed

at fetch
(/home/projects/stackblitz-starters-vjsh9a/node_modules/wrangler/wrangler-dist/cli.js:17033:19)
at async getRegisteredWorkers
(/home/projects/stackblitz-starters-vjsh9a/node_modules/wrangler/wrangler-dist/cli.js:160730:22)
at async getBoundRegisteredWorkers
(/home/projects/stackblitz-starters-vjsh9a/node_modules/wrangler/wrangler-dist/cli.js:160750:29)
at async startDevServer
(/home/projects/stackblitz-starters-vjsh9a/node_modules/wrangler/wrangler-dist/cli.js:204454:40)
at async getDevServer
(/home/projects/stackblitz-starters-vjsh9a/node_modules/wrangler/wrangler-dist/cli.js:205190:12)
at async startApiDev
(/home/projects/stackblitz-starters-vjsh9a/node_modules/wrangler/wrangler-dist/cli.js:205251:21)
at async Module.unstable_dev
(/home/projects/stackblitz-starters-vjsh9a/node_modules/wrangler/wrangler-dist/cli.js:205672:23)
at async _0x500592.run
(https://stackblitzstartersvjsh9a-m5vp.w-credentialless-staticblitz.com/blitz.1d4c3cdd.js:40:785487)
at async _0x5084b5._evaluate
(https://stackblitzstartersvjsh9a-m5vp.w-credentialless-staticblitz.com/blitz.1d4c3cdd.js:40:792870)
at async ModuleJob.run (node:internal/modules/esm/module_job:155:2441) {
cause: AssertionError [ERR_ASSERTION]: The expression evaluated to a falsy value:

assert27(Number.isFinite(client[kMaxHeadersSize]) && client[kMaxHeadersSize] > 0)

at new Parser
(file:///home/projects/stackblitz-starters-vjsh9a/node_modules/wrangler/wrangler-dist/cli.js:6858:9)
at connect
(file:///home/projects/stackblitz-starters-vjsh9a/node_modules/wrangler/wrangler-dist/cli.js:7376:29)
{
generatedMessage: true,
code: 'ERR_ASSERTION',
actual: false,
expected: true,
operator: '=='
}
}
```

Invalid response when running wrangler with host `localhost:3000` specified:

```sh
HTTP/1.1 301 Moved Permanently
Content-Length: 0
Location: http://localhost:8787:8787:3000/redirected
```

Contributor guide

Open the contributing guide

Research direction

Start with the worker.js reproduction and run it through both unstable_dev and `wrangler dev --host=localhost:3000 ./worker.js`. Trace how the local dev server constructs the redirect Location header and compare it with the reported `http://localhost:8787:8787:3000/redirected` result. Done means Response.redirect works without an unstable_dev error and returns one valid Location header in local development.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, node.js, typescript
Domain
cli
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
58/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.