10

Not sure if this can work. Created new angular-cli project, run ng serve and opened browser. It worked.

Run ng build --prod, created dist folder with index.html and number of js bundles.

I then opened index.html in browser. Hangs waiting for javascript (so I just see "loading ...").

Should this work? And if not, could someone explain why?

2
  • 1
    Did you just open the file or did you start an http server? Commented Sep 19, 2016 at 13:07
  • Just opened file. But I have run it under nginx (index file plus js bundles) and get exactly the same result. Which is just sits there showing loading.... Commented Sep 20, 2016 at 7:32

3 Answers 3

7

You should not just open this file. Web server is what you need for production usage.

If you want to serve build version of application using built in server, you can use ng serve --prod command.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks Arek. Could you explain why just running the html locally with the javascript bundled locally does not work because I have certainly run html with javascript before when developing something so wondered what is different here. I do notice that if I browser dev tools, the scripts are undefined. Again, do you know why?
The difference is that SystemJS (module loader in angular-cli) makes XHR requests to load any js file (even if it is one bundle). This is problematic due to security reasons.
@Arek Żelechowski Hi, I need to open my index-dev.html instead of index.html, do you know how to config angular-cli to do that?
I do not know if this is possible. You should ask separate question.
4

After build Angular with ng build, from the current directory we use http-server ./dist. http-server is module run server nodeJS.

Reference: https://www.npmjs.com/package/http-server

ng build <br>
http-server ./dist

Demo

Comments

3

http/web server is a software that implements http protocol and its primary goal is provide static html page.

when you perform ng serve -o it brings server up with sole purpose is when application hits localhost (ex. http://localhost:4200/) provide files main.js, polyfills.js, runtime.js and other files such as favicon.ico or stylesheet, after that browser is smart enough read this files and render application

now when you run ng build --prod=true it creates dist folder with all required dependencies enter image description here

and if you open index.html it will not show you content like regular HTML page(static web app) because generated index.html has src considering all dependencies served by server

<body>
  <app-root></app-root>
  <script type="text/javascript" src="runtime.ec2944dd8b20ec099bf3.js"></script>
  <script type="text/javascript" src="polyfills.e6fbd82b34ccb0c8609f.js"></script>
  <script type="text/javascript" src="main.de97b389e57e8b1c5843.js"></script>
</body>

Below information is just for understanding purpose its strictly not recommended to do this changes in production

If you want to bring up app just clicking index.html like static web page comes up you need to update these path

ex. create basic project with

ng new my-app

ng build --prod=true

dist Location : D:\angular\dist\my-app so index.html will be in D:\angular\dist\my-app\index.html. Update index.html as below

<body>
<app-root></app-root>
<script type="text/javascript" src="angular/dist/my-app/runtime.ec2944dd8b20ec099bf3.js"></script>
<script type="text/javascript" src="angular/dist/my-app/polyfills.e6fbd82b34ccb0c8609f.js"></script>
<script type="text/javascript" src="angular/dist/my-app/main.de97b389e57e8b1c5843.js"></script>

And after that reload you web page.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.