question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

🐛 BUG: Pages `next()` headers mutable in local dev, but not in production

See original GitHub issue

What version of Wrangler are you using?

2.0.9

What operating system are you using?

Windows 10

Describe the Bug

The Pages Functions docs has this example:

const hello = async ({ next }) => {
  const response = await next();
  response.headers.set('X-Hello', 'Hello from functions Middleware!');
  return response;
};

This works perfectly fine in local dev with pages dev, but fails in production due to the next() response having immutable headers there. This leads to frequent frustration for folks since there’s no way to get function error logs to debug this inconsistency.

To make this work in prod, you have to do something like this:

const hello = async ({ next }) => {
  const rawResponse = await next();
  const response = new Response(rawResponse.body, rawResponse); // clone response to create mutable headers
  response.headers.set('X-Hello', 'Hello from functions Middleware!');
  return response;
};

Related: https://github.com/cloudflare/cloudflare-docs/pull/4773

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
Cherrycommented, Jun 22, 2022

The change in #1325 appears to work great, thanks very much! I ran the following:

export async function onRequestGet({next}){
	const response = await next();
	response.headers.set('x-test', 'hello')
	return response;
}

and was able to see the x-test header when using the new wrangler version both in prod and dev, vs an exception in prod when using the current release version, as expected. Woot! 🎉

1reaction
sidharthachatterjeecommented, Jun 22, 2022

@Cherry It’s now out in wrangler@2.0.15 🎉

Read more comments on GitHub >

github_iconTop Results From Across the Web

Fetch Response Headers Mutable in Miniflare, not on ... - GitHub
Hey all, been working on a framework for building cloudflare workers to serve static sites. In the development process I stumbled across a ......
Read more >
Dart: HTTP headers are not mutable - Stack Overflow
Basically, when you get an "HTTP headers not mutable" error, it means that you are trying to modify the headers after writing to...
Read more >
Advanced Features: Security Headers - Next.js
This header controls DNS prefetching, allowing browsers to proactively perform domain name resolution on external links, images, CSS, JavaScript, and more. This ...
Read more >
Safari Technology Preview Release Notes - Apple Developer
Note: Shared Tab Groups and syncing for Tab Groups, Website Settings, and Web Extensions are not enabled in this release. WebAssembly. Fixed error...
Read more >
Communicating with backend services using HTTP - Angular
Most front-end applications need to communicate with a server over the HTTP protocol, to download or upload data and access other back-end services....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found