HTML5历史pushState链接返回
今天早些时候看到另一篇关于 HTML5 History API 的文章并阅读 Mozilla 上的教程后,我已经能够实现使用此 API 的基本工作演示,以允许 URL“重写”,而无需重新加载页面且无需使用哈希。
我的问题是: 假设您有一位用户来到一个页面,单击其中一个使用 History API 编写新 URL 的链接。然后用户将该页面添加为书签。现在我假设它现在将为重写的 URL 添加书签。那么,当用户几天后回来并尝试转到添加书签的页面时,它不会返回 404 吗?那么如何才能找到一种方法来解决这个问题呢?
这是假设我重写的 URL 指向一个不存在的位置。
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.
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. Then the user bookmarks the page. Now I'm assuming it will now bookmark the rewritten 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? 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我知道这已经很旧了,但你完全滥用了 Pushstate。假设您有一个查看艺术家的页面,我们将其命名为 arts.php。如果他们点击其中一个,他们就可以看到艺术家的简介。所以你有“另一个”页面,artists.php?artist=journey
现在,假设你决定在模式窗口中异步加载生物,所以你添加一个 ajax 触发器来禁用布局并只获取内容 (artists.php ?艺术家=旅程&ajax=true)。您可以“推送”artist.php?artist=journey 到历史记录,并使其在加载该页面时加载您的艺术家页面,并在模态窗口中预加载旅程。
这样您就可以使用它为页面的不同状态添加书签 - 不仅仅是在单击某个位置时拥有漂亮的 URL。
I know this is old but you are completely misusing pushstate. Say you have a page to see artists, lets call it artists.php. And if they click on one, they can see an artists bio. So you have "another" page, artists.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). 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.
That way you can use it to make bookmarking different states of your page possible - not just have pretty URLs when they click somewhere.
我今天刚刚遇到这个问题,并在我的 blog:使用 HTML5 PushState 时,如果用户复制或添加深层链接并再次访问它,那么这将是直接服务器命中,将出现 404。
即使是 js PushState 库也不会在这里为您提供帮助,因为这是在页面和任何 JS 加载之前请求的直接服务器调用。
最简单的解决方案是向 Nginx 或 Apache 服务器添加重写规则,以在内部重写对同一 index.html 页面的所有调用。浏览器认为它正在提供一个独特的页面,而实际上它是同一页面:
Apache(如果您使用的是在您的虚拟主机中):
Nginx
当然,您仍然需要添加一些 JS 逻辑来显示正确的内容。就我而言,我使用的是 Backbone 路由,它可以轻松处理这个问题。
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.
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.
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. 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):
Nginx
You would still, of course, add some JS logic to display the correct content. In my case I'm using Backbone routes which handles this easily.
这应该由服务器端脚本完成。服务器应该解析 URL 并制作页面。
This should be done by server side scripts. The server should parse URL and make the page.