Polymer app-route:拦截刷新页面

Polymer app-route: intercept refresh page

我正在努力摆脱目前的情况:我们有一个聚合物 SPA (A) 部署在某个服务器上并绑定到某个 url: http://example.com/A, we do need to show legacy pages of the old application for the time being, in order to do so, a new application (B) has been developed and bound to a different url (http://example.com/B).

B 所做的是将旧应用程序的页面框起来并暴露一个按钮,该按钮的目的是通过访问 document.referrer 值并重新加载页面返回到以前的位置,问题是作为 A SPA,服务器上不存在 URL。

更详细:

  1. 用户登录应用程序A(http://example.com/A/overview)
  2. 用户使用 SPA 并最终到达由路由 (http://example/A/stuff/we/sell/jackets)
  3. 管理的某个 url
  4. 在页面 http://example.com/A/stuff/we/sell/jackets 中有一个 link 到应用程序 B,构建了显示夹克的旧应用程序页面
  5. 用户单击 link 并转到 http://example.com/B/legacy/jackets, at this moment the document.referrer equals http://example.com/A/stuff/we/sell/jackets
  6. 用户单击按钮关闭视图,应用程序 B 将 window 的位置设置为 document.referrer 试图从 he/she 来自的地方登陆用户。
  7. 作为 SPA 的应用程序 A url http://example.com/A/stuff/we/sell/jackets 在服务器上不存在并且用户得到(正确) 一个 404.

所以问题是:有没有办法拦截app路由中window.location变量的变化,避免页面全刷新,让用户登陆页面link 在哪里?

如果我理解正确,那么这似乎是您的服务器配置有问题。

如果您有 SPA,您希望所有 URL 都转到您的 app-shell(通常是您的 index.html)。

示例:

http-root
├─ some-folder
│  └─ index.html
└─ index.html

如果你有例如 apache 运行 你可以去:

  • http://example.com/index.html
  • http://example.com/some-folder/index.html

如果你去 http://example.com/stuff/we/sell 你会得到一个错误,因为 stuff 不是一个有效的目录。

在您的 SPA 中,您可能会有一些路由系统,可以让您四处移动并更改 Url 而无需重新加载。因此,如果您使用 SPA 然后进行真正的重新加载,您应该仍会回到之前的位置。

这将如何工作取决于您的服务器。例如,对于 apache,您可以通过 .htaccess.

提供重写
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html[L]
</IfModule>

无论您的 url 是什么,使用它都将始终以 http-root index.html 结束。然后你的SPA就可以接管了;阅读 url 并显示适当的数据。

恕我直言,在服务器上解决这个问题是解决您问题的合适方法。

我同意@daKmoR 实际上你永远不应该从服务器收到 404 - 你可以从聚合物应用程序中得到它。

获取文件,如果不存在则尝试索引。 它在每个服务器中看起来都不同。您使用的是什么服务器?我可以帮助 IIS 和 nginx。