Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Failed to execute 'replaceState' on 'History'

When google index my angular web application. It can't see the pages and in the console I have this error :

SecurityError: Failed to execute 'replaceState' on 'History': A history state object with URL 'https://MYDOMAIN/universe/' cannot be created in a document with origin 'https://webcache.googleusercontent.com' and URL 'https://webcache.googleusercontent.com/search?q=cache:YlVK5uwbqWcJ:https://MYDOMAIN>/universe+&cd=2&hl=fr&ct=clnk&gl=fr'.

But I don't understand the error. Is that mean I have to allow cross origin for webcache.googleusercontent ?

I'm currently using angular universal and nginx as a reverse proxy.

like image 239
Kevin Vincent Avatar asked Dec 02 '25 06:12

Kevin Vincent


1 Answers

If you want your history to work when accessing your side from google's cache, you need to use relative a URL when you call pushState. AFAIK, there is no cross origin setting that will change this.

The new URL must be of the same origin as the current URL; otherwise, pushState() will throw an exception

https://developer.mozilla.org/en-US/docs/Web/API/History_API

You could use the hashLocation strategy instead of default path location strategy (https://angular.io/guide/router#appendix-locationstrategy-and-browser-url-styles). This should fix the problem

@NgModule({
  imports: [
  BrowserModule,
  RouterModule.forRoot(routes, { useHash: true }) 
],
like image 61
David Avatar answered Dec 04 '25 21:12

David