简体   繁体   English

HTML5历史记录pushState链接返回

[英]HTML5 history pushState link back

After seeing another post earlier today on the HTML5 History API and reading the tutorial at Mozilla , I've been able to implement a basic working demo for using this API to allow URL "rewriting" without reloading the page and without using a hash. 在今天早些时候看到另一篇关于HTML5 History API的文章并阅读了Mozilla的教程之后,我已经能够实现一个基本的工作演示,该演示使用该API允许URL“重写”而无需重新加载页面且不使用哈希。

My question is: Let's say you have a user who comes to a page, clicks on one of these links that uses the History API to write the new URL. 我的问题是:假设您有一个访问页面的用户,单击使用History API编写新URL的这些链接之一。 Then the user bookmarks the page. 然后,用户将页面添加为书签。 Now I'm assuming it will now bookmark the rewritten URL. 现在,我假设它将现在为重写的URL添加书签。 So when the user comes back in a couple of days or something and tries to go to the bookmarked page, won't it return a 404? 因此,当用户在几天之内回来并尝试转到已添加书签的页面时,它不会返回404吗? And so how can you implement a way for it to resolve somehow? 那么,如何实现以某种方式解决它的方法呢?

This is assuming that the URL I rewrite points to a non-existent location. 这是假设我重写的URL指向一个不存在的位置。

I just encountered this issue today and wrote an entry about it on my blog : When using HTML5 pushstate if a user copies or bookmarks a deep link and visits it again, then that will be a direct server hit which will 404. 我今天刚遇到这个问题,并在我的博客上写了一个有关此问题的条目:当使用HTML5 pushstate时,如果用户复制或添加了深链接并再次访问它,则将直接命中404。

Even a js pushstate library won't help you here as this is a direct server call being requested before the page and any JS even loads. 即使是js pushstate库也无法在这里为您提供帮助,因为这是在页面和所有JS加载之前就请求的直接服务器调用。

The easiest solution is to add a rewrite rule to your Nginx or Apache server to internally rewrite all calls to the same index.html page. 最简单的解决方案是向Nginx或Apache服务器添加重写规则,以在内部将所有调用重写到同一index.html页面。 The browser thinks it's being served a unique page when it's actually the same page: 当浏览器实际上是同一页面时,它会认为该页面已被提供给唯一页面:

Apache (in your vhost if you're using one): Apache(如果使用的是vhost,则在您的vhost中):

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

Nginx Nginx的

 rewrite ^(.+)$ /index.html last; 

You would still, of course, add some JS logic to display the correct content. 当然,您仍然可以添加一些JS逻辑来显示正确的内容。 In my case I'm using Backbone routes which handles this easily. 就我而言,我正在使用容易处理的骨干网路由。

I know this is old but you are completely misusing pushstate. 我知道这很旧,但是您完全滥用了pushstate。 Say you have a page to see artists, lets call it artists.php. 假设您有一个页面可以看到艺术家,可以将其命名为artist.php。 And if they click on one, they can see an artists bio. 如果他们单击一个,就可以看到艺术家的简历。 So you have "another" page, artists.php?artist=journey 因此,您有了“另一个”页面,artist.php?artist = journey

Now, say you decide you will load the bio asynchronously in a modal window, so you add an ajax trigger to disable the layout and just get the content (artists.php?artist=journey&ajax=true). 现在,假设您决定要在模式窗口中异步加载生物,因此您添加了一个ajax触发器来禁用布局并仅获取内容(artists.php?artist = journey&ajax = true)。 You could "push" artists.php?artist=journey to the history, and make it so that if that page is loaded, it loads your artists page with journey preloaded in a modal window. 您可以“将” artist.php?artist = journey“推送”到历史记录,并进行设置,以使如果该页面被加载,它会在模式窗口中加载您的艺术家页面,并预载旅程。

That way you can use it to make bookmarking different states of your page possible - not just have pretty URLs when they click somewhere. 这样,您就可以使用它来为页面的不同状态添加书签-不仅仅是单击它们的URL精美。

This should be done by server side scripts. 这应该由服务器端脚本来完成。 The server should parse URL and make the page. 服务器应解析URL并创建页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM