添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Angular 4 throwing error EventSource's response has a MIME type ("text/html") that is not "text/event-stream" after migrating to .NetCore 2.0

Ask Question

I have an existing SPA based on Asp .Net Core (I was using Yo generator-aspnetcore-spa to generate a template). It worked perfectly fine, but after migration to .NetCore 2.0 it started to throw the error:

EventSource's response has a MIME type ("text/html") that is not "text/event-stream". Aborting the connection.

As I understood this problem affects only auto-refresh after updating any file (hot module replacement as far as I know). All other stuff is working fine.

So, the question is how to fix the error above?

I found the solution, mainly the problem is in .NetCore routing system, it is taking over and trying to handle the request, returning text/html, so it's sending the actual webpack_hmr hot file. To fix it you need to edit Configure method in the Startup.cs file.

  • Before:

    // some code
    app.UseWebpackDevMiddleware(new WebpackDevMiddlewareOptions {
                HotModuleReplacement = true                    
    //some code
    
  • After:

    // some code
    app.UseWebpackDevMiddleware(new WebpackDevMiddlewareOptions {
                HotModuleReplacement = true,
                HotModuleReplacementEndpoint = "/dist/__webpack_hmr"                   
    // some code
    

    The solution is taken from this thread on GitHub

  • Check whether your Hosting environment is set to Production or to Development (Webpack HMR is disabled for Production)
  • Change the Hosting environment to Development.
    Add export ASPNETCORE_ENVIRONMENT=development to your ~/.bash_profile or ~/.zshrc file. Follow the link for more exhaustive explanation. Thus your output should be:

    This link explains how to change the env in more depth.

    The problem existed for the development environment. Everything was working before migration to .net core 2 – Serg.ID Oct 21, 2017 at 1:43 I tried your your answer and it didn't work. Then the link at my 2nd point fixed my error. The problem was my hosting env. – Eddy Ekofo Oct 21, 2017 at 9:32 From this point of view you are absolutely right, HMR works only in Dev env. However, this quetion was about “EventSource's response has a MIME type ("text/html")” error – Serg.ID Oct 21, 2017 at 20:56 You're right! this the same error I also got but my problem was different I guess, and I was able to resolve it by changing the Hosting env. – Eddy Ekofo Oct 21, 2017 at 21:41 @Serg.ID Still Unable to figure out this. After deployed, our env is not Dev then how to handle this issue? there it won't use any HMR. – k11k2 Dec 12, 2017 at 16:28

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.

  •