SecurityError: Failed to execute 'replaceState' on 'History':

Adam Young picture Adam Young · Apr 3, 2018 · Viewed 13.2k times · Source

I am facing this issue across all browsers. This comes up whenever I try to view my site in Google cache

main.bundle.js:1 ERROR Error: Uncaught (in promise): SecurityError: Failed to execute 'replaceState' on 'History': A history state object with URL 'https://mydomainn.com/' cannot be created in a document with origin 'http://webcache.googleusercontent.com'. The same problem lies with express google. http://webcache.googleusercontent.com/search?q=cache%3Ahttps%3A%2F%2Fexpress.google.com%2Fstores&oq=cache%3Ahttps%3A%2F%2Fexpress.google.com%2Fstores&aqs=chrome..69i57j69i58.3999j0j4&sourceid=chrome&ie=UTF-8.

My base href is /

Answer

theeGwandaru picture theeGwandaru · Jun 5, 2018

In your routes configuration add

 RouterModule.forRoot(routes, {useHash: true});

In your index.html do this;

 <!-- <base href="/"> -->
 <script>document.write('<base href="' + document.location + '" />');</script>

And then build with --base-href ./ e.g

ng build --prod --base-href ./

source https://github.com/angular/angular/issues/13948