【问题标题】:Failed to execute 'replaceState' on 'History'未能在“历史”上执行“replaceState”
【发布时间】:2018-05-12 19:48:48
【问题描述】:

当 google 索引我的 Angular Web 应用程序时。它看不到页面,并且在控制台中出现此错误:

SecurityError: 无法在“History”上执行“replaceState”:无法在源为“https://webcache.googleusercontent.com”和 URL 为“https://webcache.googleusercontent.com/search?q=cache:YlVK5uwbqWcJ:https://MYDOMAIN”的文档中创建 URL 为“https://MYDOMAIN/universe/”的历史状态对象>/universe+ &cd=2&hl=fr&ct=clnk&gl=fr'。

但我不明白这个错误。这是否意味着我必须允许 webcache.googleusercontent 的跨源?

我目前正在使用 Angular Universal 和 nginx 作为反向代理。

【问题讨论】:

    标签: angular nginx angular-universal


    【解决方案1】:

    如果您希望您的历史记录在从 google 的缓存访问您的一方时起作用,您需要在调用 pushState 时使用相对 URL。 AFAIK,没有跨原点设置可以改变这一点。

    新网址必须与当前网址同源;否则 pushState() 会抛出异常

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

    您可以使用 hashLocation 策略而不是默认路径位置策略 (https://angular.io/guide/router#appendix-locationstrategy-and-browser-url-styles)。这应该解决问题

    @NgModule({
      imports: [
      BrowserModule,
      RouterModule.forRoot(routes, { useHash: true }) 
    ],
    

    【讨论】:

    • 感谢您的回答,但实际上我只使用相对 url。我所有的网址都设置在角度路由器中。
    • 好的,我以为是你自己的代码。但是,您是否需要担心 google 的缓存?对我来说,请求的 url 是在尝试访问您网站的 google 缓存版本时,而不是在 google 爬虫访问您的网站时
    • 其实问题不在于 google 缓存。我的问题是,当谷歌抓取我的网站时,它会获取 html,因为我正在使用 angular Universal 进行服务器端渲染,但是当我查看谷歌搜索控制台时,谷歌如何查看我的网站,即使 html 存在,页面也是完全空白的.然后,我点击谷歌缓存查看我的页面,并在第一次渲染时显示页面,然后在处理 js 文件时,页面变为银行并通过此错误。顺便说一句,我不想​​使用 hashlocationstrategy。
    • 好吧,也许你应该再问一个问题。您报告的错误的原因具体是由于使用了 google 缓存。
    猜你喜欢
    • 2018-09-12
    • 1970-01-01
    • 2015-12-05
    • 2020-11-16
    • 2016-02-04
    • 1970-01-01
    • 2018-02-05
    • 1970-01-01
    • 2019-03-29
    相关资源
    最近更新 更多