Skip to content
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

Cyclic dependency in HTTP_INTERCEPTORS when using platform-server #23023

Closed
marcin-j opened this issue Mar 27, 2018 · 7 comments
Closed

Cyclic dependency in HTTP_INTERCEPTORS when using platform-server #23023

marcin-j opened this issue Mar 27, 2018 · 7 comments
Labels
Milestone

Comments

@marcin-j
Copy link

I'm submitting a...


[ ] Regression (a behavior that used to work and stopped working in a new release)
[ x] Bug report  
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question

Current behavior

When using platform-server and injecting service which uses HttpClient into HttpInterceptor the cyclic dependency error occurs.

Expected behavior

There shouldn't be any cyclic dependency error.

For platform-browser it was fixed after #18224

Minimal reproduction of the problem with instructions

Create service (DummyService) which uses HttpClient
Create HTTP_INTERCEPTORS interceptor and inject DummyService there

Environment


Angular version: 5.2.3
@ngbot ngbot bot added this to the needsTriage milestone Mar 27, 2018
@MarkPieszak
Copy link
Member

Try using the Injector to grab HttpClient, you shouldn't have any issues that way:

get http() {
    return this._injector.get(HttpClient);
  }

constructor(private injector: Injector) { }

// somewhere else
this.http.get();

Hope that helps!

@marcin-j
Copy link
Author

Hey, thats what I'm doing for now and it works. I just thought that if it works for platform-browser (no cyclic dependencies) it should work for platform-server as well. Thanks for answer :)

@jaumard
Copy link

jaumard commented May 28, 2018

Still same error with angular 6.0.3, I'll try the injector solution

@Willovent
Copy link

I have this error as well on 6.0.3. But my interceptor is not using httpClient.
Here is the code of the interceptor causing the issue on my project :

import { REQUEST } from '@nguniversal/express-engine/tokens';

@Injectable()
export class RefererInterceptor implements HttpInterceptor {
  constructor(@Optional() @Inject(REQUEST) private request) {}

  public intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    if (this.request) {
      const host = `${this.request.protocol}://${this.request.headers.host}`;
      req = req.clone({
        headers: req.headers.append('StSsrReferer', host)
      });
      return next.handle(req);
    }
  }
}

vikerman added a commit to vikerman/angular that referenced this issue May 31, 2018
Fixes angular#23023.

When a HTTP Interceptor injects HttpClient it causes a DI cycle. This fix is to use Injector to lazily inject HTTP_INTERCEPTORS while setting up the HttpHandler on the server so as to break the cycle.
@vikerman
Copy link
Contributor

@Willovent - what's the exact DI exception message. Want to make sure it's the same issue.

vikerman added a commit to vikerman/angular that referenced this issue May 31, 2018
Fixes angular#23023.

When a HTTP Interceptor injects HttpClient it causes a DI cycle. This fix is to use Injector to lazily inject HTTP_INTERCEPTORS while setting up the HttpHandler on the server so as to break the cycle.
@Willovent
Copy link

Willovent commented Jun 1, 2018

Turn out it was not the interceptor that I linked, but an another one using a service using an httpClient. Sorry for that :(

@vicb vicb closed this as completed in 60aa943 Jun 1, 2018
vicb pushed a commit that referenced this issue Jun 1, 2018
…tor (#24229)

Fixes #23023.

When a HTTP Interceptor injects HttpClient it causes a DI cycle. This fix is to use Injector to lazily inject HTTP_INTERCEPTORS while setting up the HttpHandler on the server so as to break the cycle.

PR Close #24229
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 13, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants
@vikerman @IgorMinar @MarkPieszak @jaumard @marcin-j @Willovent and others