1

On build --prod of my app I get this error when loading the index.html from a root dir or when hosted by a server :

ERROR Error: Uncaught (in promise): SecurityError: Failed to execute 'replaceState' on 'History': A history state object with URL 'file:///G:/#/' cannot be created in a document with origin 'null' and URL 'file:///G:/index.html'.
Error: Failed to execute 'replaceState' on 'History': A history state object with URL 'file:///G:/#/' cannot be created in a document with origin 'null' and URL 'file:///G:/index.html'.
    at e.replaceState (vendor.7c715405490dfd9b75c0.bundle.js:1)
    at e.replaceState (vendor.7c715405490dfd9b75c0.bundle.js:1)
    at t.replaceState (vendor.7c715405490dfd9b75c0.bundle.js:1)
    at t.resetUrlToCurrentUrlTree (vendor.7c715405490dfd9b75c0.bundle.js:1)
    at t.resetStateAndUrl (vendor.7c715405490dfd9b75c0.bundle.js:1)
    at vendor.7c715405490dfd9b75c0.bundle.js:1
    at t.invoke (polyfills.ae3fb5d11cc61f1acad9.bundle.js:1)
    at Object.onInvoke (vendor.7c715405490dfd9b75c0.bundle.js:1)
    at t.invoke (polyfills.ae3fb5d11cc61f1acad9.bundle.js:1)
    at e.run (polyfills.ae3fb5d11cc61f1acad9.bundle.js:1)
    at e.replaceState (vendor.7c715405490dfd9b75c0.bundle.js:1)
    at e.replaceState (vendor.7c715405490dfd9b75c0.bundle.js:1)
    at t.replaceState (vendor.7c715405490dfd9b75c0.bundle.js:1)
    at t.resetUrlToCurrentUrlTree (vendor.7c715405490dfd9b75c0.bundle.js:1)
    at t.resetStateAndUrl (vendor.7c715405490dfd9b75c0.bundle.js:1)
    at vendor.7c715405490dfd9b75c0.bundle.js:1
    at t.invoke (polyfills.ae3fb5d11cc61f1acad9.bundle.js:1)
    at Object.onInvoke (vendor.7c715405490dfd9b75c0.bundle.js:1)
    at t.invoke (polyfills.ae3fb5d11cc61f1acad9.bundle.js:1)
    at e.run (polyfills.ae3fb5d11cc61f1acad9.bundle.js:1)
    at P (polyfills.ae3fb5d11cc61f1acad9.bundle.js:1)
    at polyfills.ae3fb5d11cc61f1acad9.bundle.js:1
    at t.invokeTask (polyfills.ae3fb5d11cc61f1acad9.bundle.js:1)
    at Object.onInvokeTask (vendor.7c715405490dfd9b75c0.bundle.js:1)
    at t.invokeTask (polyfills.ae3fb5d11cc61f1acad9.bundle.js:1)
    at e.runTask (polyfills.ae3fb5d11cc61f1acad9.bundle.js:1)
    at v (polyfills.ae3fb5d11cc61f1acad9.bundle.js:1)
    at <anonymous>
kt @ vendor.7c715405490dfd9b75c0.bundle.js:1
t.handleError @ vendor.7c715405490dfd9b75c0.bundle.js:1
next @ vendor.7c715405490dfd9b75c0.bundle.js:1
e.object.i @ vendor.7c715405490dfd9b75c0.bundle.js:1
e.__tryOrUnsub @ vendor.7c715405490dfd9b75c0.bundle.js:1
e.next @ vendor.7c715405490dfd9b75c0.bundle.js:1
e._next @ vendor.7c715405490dfd9b75c0.bundle.js:1
e.next @ vendor.7c715405490dfd9b75c0.bundle.js:1
e.next @ vendor.7c715405490dfd9b75c0.bundle.js:1
e.emit @ vendor.7c715405490dfd9b75c0.bundle.js:1
(anonymous) @ vendor.7c715405490dfd9b75c0.bundle.js:1
t.invoke @ polyfills.ae3fb5d11cc61f1acad9.bundle.js:1
e.run @ polyfills.ae3fb5d11cc61f1acad9.bundle.js:1
t.runOutsideAngular @ vendor.7c715405490dfd9b75c0.bundle.js:1
onHandleError @ vendor.7c715405490dfd9b75c0.bundle.js:1
t.handleError @ polyfills.ae3fb5d11cc61f1acad9.bundle.js:1
e.runGuarded @ polyfills.ae3fb5d11cc61f1acad9.bundle.js:1
t @ polyfills.ae3fb5d11cc61f1acad9.bundle.js:1
n.microtaskDrainDone @ polyfills.ae3fb5d11cc61f1acad9.bundle.js:1
v @ polyfills.ae3fb5d11cc61f1acad9.bundle.js:1
Promise.then (async)
d @ polyfills.ae3fb5d11cc61f1acad9.bundle.js:1
t.scheduleTask @ polyfills.ae3fb5d11cc61f1acad9.bundle.js:1
e.scheduleTask @ polyfills.ae3fb5d11cc61f1acad9.bundle.js:1
e.scheduleMicroTask @ polyfills.ae3fb5d11cc61f1acad9.bundle.js:1
O @ polyfills.ae3fb5d11cc61f1acad9.bundle.js:1
t.then @ polyfills.ae3fb5d11cc61f1acad9.bundle.js:1
t.bootstrapModule @ vendor.7c715405490dfd9b75c0.bundle.js:1
x35b @ main.991a69005f6003446625.bundle.js:1
t @ inline.31e1fb380eb7cf3d75b1.bundle.js:1
0 @ main.991a69005f6003446625.bundle.js:1
t @ inline.31e1fb380eb7cf3d75b1.bundle.js:1
window.webpackJsonp @ inline.31e1fb380eb7cf3d75b1.bundle.js:1
(anonymous) @ main.991a69005f6003446625.bundle.js:1

I don't understand this because even when I open index.html and manually edit it here : <script>document.write('<base href="./" />') and here <base href="./"> the error persists.

please give me a guide to avoid this sort of error.

tatsu
  • 2,316
  • 7
  • 43
  • 87

1 Answers1

3

Angular projects can't be simply placed on a hard drive or virtual root drive and have their index.html double clicked on. this will show the page but it will recognize it has not been "served" by some sort of server.

Don't do this.

Instead bundle your app with tomcat or some other server software and that will be responsible for running it. in this manner there will be an origin point.

tatsu
  • 2,316
  • 7
  • 43
  • 87
  • But why we need to serve,Its just javascript no? – Nambi N Rajan Sep 07 '18 at 16:30
  • I'm no expert. I think it has to do with the state of the machine? is the machine configured to expose to the web whatever's in that folder or not is a server process of some sort running? I'm quite sure you'd have to have at least an appache up for js-based webpages to show up. again I'm no expert. but I suppose "serving" is just javascript's lightweight emulation of all this. – tatsu Sep 09 '18 at 14:39