-
Notifications
You must be signed in to change notification settings - Fork 803
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
🐛 BUG: Redirects to not work properly in wrangler 3 #5860
Comments
Hello Philip Walton! You encountered this issue because you need to:
Let's go through the solution step by step. First, modify your code as follows: worker.js should look like this (your code is ok): export default {
async fetch(request) {
const url = new URL(request.url);
console.log('incoming request') // to see in console
if (url.pathname === '/needs-redirect') {
url.pathname = '/redirected';
return Response.redirect(url.href, 301);
}
return new Response('Hello!');
},
}; Next, update your index.js: import { unstable_dev } from 'wrangler';
const worker = await unstable_dev('./worker.js', {
experimental: { disableExperimentalWarning: true },
});
const response = await worker.fetch('/needs-redirect', { redirect: 'manual' }) // prevent automatic following by option redirect: 'manual'
if (response.status === 301)
console.log('Redirected to:', response.headers.get('Location'));
else
console.log('Response status:', response.status); Testing the SetupOpen two terminals in your project directory. In the first terminal, run your worker locally on localhost with port 3000: $ wrangler dev --host=localhost --port=3000 ./worker.js In the second terminal, run your index.js with the following command: node index.js outputYou should see the following outputs: In the first terminal:
In the second terminal:
Cheers, |
Hi @rameardo, I appreciate the suggestion for how to work around this error via setting Also, you didn't comment on the second issue I reported, starting with the text:
Do you have any idea as to what's causing the redirect URL to contain multiple ports: |
Hello Philip Walton! You are right; usually, Cloudflare Workers follow redirects. However, in local testing, I believe there are two types of execution: the first one is remote as local, and the second one is fully local. For example, this is fully local: $ wrangler dev And this is remote: wrangler dev -r I think with remote, it should automatically follow redirects because the
|
Your example is actually different from mine, and I should clarify that in my real application code I'm setting both "host" and "port" to different port values. Here's what I have in my host = "localhost:3001" # Origin server
port = 3000 # Port the worker should listen on Notice how I have a different port set under "host" then I do under "port". The reason I'm doing this is because I have an application server running on port "3001", and I have my Cloudflare worker running on port "3000", which acts as a proxy for my application server. According to the wrangler documentation, the "port" configuration should not be the port of the application server but rather the port of the worker, which is how I have it configured. I assume running a local application server during development time is very common, so if there's a better way to configure my worker to support that use case, please let me know. |
By the way, in my case I actually need to test the 'follow redirects' behavior because I have a redirect chain scenario that I want to make sure redirects to completion. Also, to emphasize something I said above, this behavior worked in wrangler version 2, so this is a regression in version 3. |
Wrangler 2 defaulted to remote mode dev, while Wrangler 3 defaults to local mode dev. I think the #5221 tracks the issue with redirect URLs containing too many ports. Could you confirm whether |
The
I understand, but to clarify, wrangler 2 (when using the |
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 theunstable_dev
API as well as when usingwrangler 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
node index.js
(if it doesn't happen automatically)Here is the contents of the worker file in the repro:
In addition to the error shown above when using
unstable_dev
, I'm also seeing errors for the same worker file when runningwrangler dev
locally with a local host specified:Then, if I use
curl
to test the redirect, I get an invalidLocation
header returned with the response. See the following screenshot: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
:Invalid response when running wrangler with host
localhost:3000
specified:The text was updated successfully, but these errors were encountered: