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.

Response header set via `reply.header()` in a custom handler never gets sent

See original GitHub issue

šŸ› Bug Report

A response header that’s set via reply.header() or reply.headers() in a custom handler will never be sent.

This is because Fastify buffers headers internally instead of setting them on the http.ServerResponse instance, and only sends them after reply.send() is called. But since Next.js calls .end() directly on the http.ServerResponse instance, Fastify never has an opportunity to set response headers, and the buffered headers never get sent.

One way to address this might be for fastify-nextjs to iterate over Fastify’s response headers and call reply.res.setHeader(...) to set each header on the http.ServerResponse before passing control to the custom handler or Next.js. Unfortunately, Fastify doesn’t appear to provide any public API to get all the internally buffered headers; the only option is to get an individual header by name via reply.getHeader(), which is insufficient. I’m curious if anyone has ideas.

To Reproduce

Here’s a failing test that reproduces this:

test('should serve Fastify response headers set by a custom handler', t => {
  t.plan(3)

  const fastify = Fastify()

  fastify
    .register(require('./index'))
    .after(() => {
      fastify.next('/hello', (app, req, reply) => {
        reply.header('test-header', 'hello');
        app.render(req.req, reply.res, '/hello', req.query, {})
      })
    })

  fastify.inject({
    url: '/hello',
    method: 'GET'
  }, (err, res) => {
    t.error(err)
    t.equal(res.statusCode, 200)
    t.equal(res.headers['test-header'], 'hello')
  })

  fastify.close()
});

Expected behavior

The response served by Next.js should include headers set by Fastify, and the test above should pass.

Your Environment

  • node version: 12.15.0
  • fastify version: 2.12.1
  • os: Mac, Linux

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
mcollinacommented, Jul 7, 2020

There are quite a few reasons to not do what express does, for completeness, I’m listing a few here:

  1. IncomingMessage and ServerResponse are extremely tied to the HTTP/1 implementation of Node.js core. Which make supporting http/2 and in the future http/3 impossible for Express.
  2. Avoid monkeypatching - in order to work, Express monkeypatches the internals of those instances, fundamentally changing how they work. This makes it brittle and complex to maintain over time. Fastify relies only on public methods.
  3. Encapsulation and performance. A large amount of the performance gap between Express and Fastify can be attributed to the use of our won custom and Request and Reply instances.
1reaction
mcollinacommented, Mar 26, 2020

That’d be nice, yes!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unable to access custom response header in frontend ...
I am attempting to access it using axios as my HTTP client. I can see the header in the chrome dev-tools console, but...
Read more >
Response Header - an overview | ScienceDirect Topics
Response Headers are included with the data being sent back to the client to instruct the browser to do something or for informational...
Read more >
Unable to set header in server.ext() #2307 - hapijs/hapi - GitHub
I am trying to set a header in a server.ext() but it won't return. Here's a short example: server.ext('onPostHandler', function(request,Ā ...
Read more >
How to Set a Header on a Response with Spring 5 - Baeldung
In this section, we'll learn how to set headers on single endpoint responses using ServerHttpResponse, ResponseEntity or ServerResponse (forĀ ...
Read more >
mail - Manual - PHP
When sending mail, the mail must contain a From header. This can be set with the additional_headers parameter, or a default can be...
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