Redirect plus adding cookies

I am trying to do redirect while adding cookies without success

Trying 2 things

  1. Response.redirect(url, 301);
    I couldn’t add cookies, even if I tried to add save the response add cookies and then return it doesn’t work

  2. Create new response
    The problem I am facing that create new response to create redirect is not working (it just return the response without change the url in the browser)

    return new Response(response.body, {
         status: 301,
         statusText: "Moved Permanently",
         headers: response.headers,
         location: redirectedUrl,
         url: redirectedUrl
    

});

The original url is subdomain and the redirectedUrl is response url without the subdomain,

This snippet is not working, if it will work I will be able to add cookies quite easly, can you help me understand why its not working? and what to do?

Currently to do redirect only this is working
Response.redirect(url, 301);

But using it not allowing adding cookies (I also tried to save this response add cookies and then return the response withotut success)

Have you already tried

return new Response(null, {
	status: 301,
	headers: {
		'Location': 'https://www.example.com',
		'Set-Cookie': 'mycookie=value'
	}
});

It do redirect the url (change the address bar in the browser) however I didn’t see the cookie,

I also tried

let response = new Response(null, {
status: 301,
headers: {
‘Location’: urlRedirected
}
});

let cookieName = “Redirect”;
let cookieValue = “true”

response.headers.append(‘Set-Cookie’, ${cookieName}=${cookieValue}; path=/)

return response;

it does the redirect but no cookie as well, any idea what to do?

Seems to work for me

https://cloudflareworkers.com/#5242604095345741384195400ae4bc0d:https://tutorial.cloudflareworkers.com/

Shows a redirection loop but the cookie gets set