Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Error: Can't set headers after they are sent. #491

Closed
hongbo-miao opened this issue Oct 19, 2016 · 8 comments
Closed

Error: Can't set headers after they are sent. #491

hongbo-miao opened this issue Oct 19, 2016 · 8 comments

Comments

@hongbo-miao
Copy link

hongbo-miao commented Oct 19, 2016

I put the error log here, since it is a little bit long
http://pastie.org/10945816

The reproduce codes app is here
https://github.com/Hongbo-Miao/zonejs-bug

It uses universal-starter, just added socket.io, see in second commit.

To see the error, npm start and open page http://localhost:3000/.

The app works with zone.js 0.6.21, and it shows the error starts from 0.6.22 and also shows in latest 0.6.26

@JiaLiPassion
Copy link
Collaborator

I made a pull request to fix this issue, for now you can change your package.json to make zone.js reference to https://github.com/JiaLiPassion/zone.js.git to see the result.

JiaLiPassion added a commit to JiaLiPassion/zone.js that referenced this issue Nov 9, 2016
mhevery pushed a commit that referenced this issue Nov 16, 2016
…atch. (#500)

* fix long-stack-trace zone will not render correctly when reject a promise

* fix issue #484 and #491, patch EventEmitter.once and removeAllListeners

* add event emitter once test case

* prependlistener should add listener at the beginning of the listener array.
add test cases for prepend listener and once.

* use newest fetch to test
mhevery pushed a commit that referenced this issue Dec 5, 2016
…tch process.nextTick, fix removeAllListeners bug (#516)

* fix long-stack-trace zone will not render correctly when reject a promise

* fix issue #484 and #491, patch EventEmitter.once and removeAllListeners

* add event emitter once test case

* prependlistener should add listener at the beginning of the listener array.
add test cases for prepend listener and once.

* use newest fetch to test

* patch process.nextTick

* restore ZoneAwareError captureStackTrace function

* move captureStackTrace test into node

* use hasOwnProperty for check captureStackTrace exist or not

* change var to const

* add process.spec.ts into node_tests.ts target

* add done in process.spec.ts

* change var to let

* add nexttick order case

* add prepareStackTrace callback to ZoneAwareError

* fix when EventEmitter removeAllListeners has no event type parameter, should remove all listeners

* change some var to let/const, remove unnecessary cancelTask call

* modify testcases

* remove typo

* use zone.scheduleMicrotask to patch process.nextTick

* forget use let/const again

* add comment to removeAllListeners patch, and remove useCapturing parameter cause it is not needed

* update fetch to 2.0.1
JiaLiPassion added a commit to JiaLiPassion/zone.js that referenced this issue Dec 6, 2016
@NgxDev
Copy link

NgxDev commented Mar 5, 2018

So, after more than a year, this is still around in 0.8.20.

My scenario, I have an SSR Response Service:

import { Injectable, Inject } from '@angular/core';

import { Response } from 'express';
import { RESPONSE } from '@nguniversal/express-engine';

@Injectable()
export class SsrResponseService {
  constructor(@Inject(RESPONSE) private response: Response) {}

  redirect(url: string, code?: number) {
    if (!!code) {
      return this.response.redirect(code, url);
    } else {
      return this.response.redirect(url);
    }
  }
}

Then, in a component, based on some logic, I might need to redirect the user:

this.responseService.redirect('/some-other-url', 301);

The redirect works, but the server logs:

Unhandled Promise rejection: Can't set headers after they are sent. ; 
Zone: <root> ; Task: Promise.then ; Value: Error: Can't set headers after they are sent.

Full stack trace here

Any ETA on this fix?

@mike-suggitt
Copy link

Bump - same here

@JiaLiPassion
Copy link
Collaborator

@mike-suggitt , @MrCroft , I don't think the Can't set headers after they are sent is zone.js error, it is just express http response error.

@NgxDev
Copy link

NgxDev commented Jul 5, 2018

@JiaLiPassion I believe so. But my issue (or question, rather) is: how could we set appropriate headers/response codes?
Keeping in mind that those are conditioned by what information we only have in the Angular code, at runtime, and nowhere else.
Example: I want to access /post/100/some-slug (by directly entering the URL in the address bar - or a search engine might crawl that, from a bad link) but that post doesn't exist. The fact that it doesn't exist is only known after making the API call. So, how do I set a 404 response code at this point? Because, when the app is rendered server-side, we need to send not found header.

@JiaLiPassion
Copy link
Collaborator

@MrCroft , sorry I am not familiar with SSR, you can ask at stackoverflow.

@NgxDev
Copy link

NgxDev commented Jul 5, 2018

@JiaLiPassion actually, I remember that the response headers were in fact being set (at least in my case). And the app worked fine. But I was just seeing that error in the log and it mentioned zone.js. Weird... Haven't been doing SSR recently.

@JiaLiPassion
Copy link
Collaborator

@MrCroft, yeah, almost all the errors in angular will mention zone.js, so really very little error is really zone.js error.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants