2
votes

I built a website using Angular. I'm a fairly basic Angular user, but my application is in a state that I am currently happy with. Throughout development I always used ng serve to test and debug my application.

The dist compiles successfully on ng build and with ng build --prod>. Running ng serve --prod works just fine too. However, when I try to open the application through the index.html page in the dist folder (using the -- bh . flag when building, I get one of two very similar errors)

Error: Uncaught (in promise): SecurityError: Failed to execute 'replaceState' on 'History': A history state object with URL 'file:///C:/Users/quent/website/PersonalWebApp/dist/' cannot be created in a document with origin 'null' and URL 'file:///C:/Users/quent/website/PersonalWebApp/dist/index.html'. Error: Failed to execute 'replaceState' on 'History': A history state object with URL 'file:///C:/Users/quent/website/PersonalWebApp/dist/' cannot be created in a document with origin 'null' and URL 'file:///C:/Users/quent/website/PersonalWebApp/dist/index.html'.

There is also one error where the page cannot correctly get an image

Failed to load resource: net::ERR_FILE_NOT_FOUND

So, this leads me to believe that this is an Angular issue I am having and not a deployment issue. For reference, when I try to deploy to Azure, I get a Failed to load resource: the server error in the console and am presented with presented with Cannot GET / on the page.

I included my angular-cli.json file for reference. If this is not enough information to help me start debugging this issue please let me know. Thanks much!

My .angular-cli.json

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "project": {
    "name": "personal-web-app"
  },
  "apps": [
    {
      "root": "src",
      "outDir": "dist",
      "assets": [
        "assets",
        "favicon.ico",
        "web.config"
      ],
      "index": "index.html",
      "main": "main.ts",
      "polyfills": "polyfills.ts",
      "test": "test.ts",
      "tsconfig": "tsconfig.app.json",
      "testTsconfig": "tsconfig.spec.json",
      "prefix": "app",
      "styles": [
        "styles.scss"
      ],
      "scripts": [
        "../node_modules/jquery/dist/jquery.js",
        "../node_modules/hammerjs/hammer.min.js",
        "../node_modules/tether/dist/js/tether.js",
        "../node_modules/bootstrap/dist/js/bootstrap.js"
      ],
      "environmentSource": "environments/environment.ts",
      "environments": {
        "dev": "environments/environment.ts",
        "prod": "environments/environment.prod.ts"
      }
    }
  ],
  "e2e": {
    "protractor": {
      "config": "./protractor.conf.js"
    }
  },
  "lint": [
    {
      "project": "src/tsconfig.app.json",
      "exclude": "**/node_modules/**"
    },
    {
      "project": "src/tsconfig.spec.json",
      "exclude": "**/node_modules/**"
    },
    {
      "project": "e2e/tsconfig.e2e.json",
      "exclude": "**/node_modules/**"
    }
  ],
  "test": {
    "karma": {
      "config": "./karma.conf.js"
    }
  },
  "defaults": {
    "styleExt": "scss",
    "component": {}
  }
} 
1
How are you serving the index.html locally? What server are you using? In Azure, how are you serving it? Did you enable HTML5 mode for the routing to work? - elclanrs
Locally I'm running through the NG Live Development server. In Azure, I'm simply it through Node.js. Can you explain HTML5 mode? I have <!doctype html> on my page but I don't think that's what you're referring to. - qgoehrig

1 Answers

0
votes

Not an angular person, but you might need to serve the static files made by ng build --prod via a web server, rather than just opening them in a browser. The latter is usually not what you want, because there's a bunch of restrictions when the protocol is file (the origin is null which has a lot of implications via the same-origin policy, there are restrictions with cross-site XHR/fetch requests etc).

A cheap way to serve some static files via a web server is via python3 -m http.server 8080 which will serve files on http://localhost:8080.