是否曾有人提议将 URL 片段包含到 HTTP 请求中?
在当前的 HTTP 规范中,URL 片段(URL 中包含并跟随 #
的部分)不会以任何方式发送到服务器。然而,随着 AJAX(使用片段来维护某种形式的状态)的普及,在很多情况下,服务器在请求时了解 URL 片段会很有用。
例如,如果您访问 http://facebook.com,然后点击信息流中的用户名,则该 URL 将变成 http://faceboook.com/#!/username - 让 FB 无需更新您的页面重新加载所有引导 JS 和 HTML。但是,如果您使用浏览器重新加载此内容,服务器将无法看到 URL 的“#/!username”部分,因此无法为您预呈现内容。一旦客户端 JavaScript 加载并解析了片段,这就会迫使您的浏览器发出额外的请求。
我想知道是否有任何努力或建议来创建一个标准机制来实现这一目标。
例如,可能有一个标准的 HTTP 标头,该标头将与 URL 片段的值一起发送 - 任何关心此类事情的服务器都可以访问它。
这对于整个 Web 应用程序社区来说似乎是一件非常有用的事情,所以我很惊讶没有听到任何建议。也许我错过了。
In the current HTTP spec, the URL fragment (the part of the URL including and following the #
) is not sent to the server in any way. However with the increased spread of AJAX, which uses the fragment to maintain some form of state, there are a lot of situations where it would be useful for the server to have knowledge of the URL fragment at request time.
For example, if you go to http://facebook.com, then click a user name in your stream, the URL will become http://faceboook.com/#!/username - to allow FB to update your page without reloading all of its bootstrap JS and HTML. However, if you were to reload this with your browser, the server would have no way of seeing the "#/!username" part of the URL, and therefore could not pre-render the content for you. This forces your browser to make an extra request once the client Javascript has loaded and parsed the fragment.
I am wondering if there have been any efforts or proposals towards creating a standard mechanism to achieve this.
For example, there could be a standard HTTP header, which would be sent with the value of the URL fragment - any server which cared about such things could then have access to it.
It seems like this would be a very useful thing for the web-application community as a whole, so I am surprised to not have heard anything proposed. Perhaps I missed it though.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
恕我直言,片段标识符确实不是存储状态的好地方,它是为其他目的而设计的。
话虽这么说,http://www.jenitennison.com/blog/node/154对整个主题进行了很好的讨论。
Imho, the fragment identifier really is not a good place to store the state, it has been designed for something else.
That being said, http://www.jenitennison.com/blog/node/154 has a good discussion of the whole subject.
我发现Google 提出的这项提案用于制作 Ajax页面可抓取,但它解决了一组更受限制的用例。具体来说,它创建了一种用 URL 参数替换 URL 片段的方法,以便从服务器获取与客户端访问具有该片段的等效 URL 时生成的相同的 HTML 输出。然而,这样的 URL 对于实际运行 Ajax 应用程序来说是无用的,因为它们每次都需要重新加载页面。
I found this proposal by Google to make Ajax pages crawlable, but it addresses a more constrained set of use cases. Specifically, it creates a way to replace the URL fragment with a URL parameter to obtain the same HTML output from the server as would be generated by a client visiting the equivalent URL with the fragment. However, such URLs are useless for actually running the Ajax apps, since they would necessitate a page reload every time.
Webkit 错误 24175 - URL 重定向丢失片段 指的是 处理重定向 URL 中的片段标识符,这可能是您感兴趣的。
Webkit Bug 24175 - URL Redirect Loses Fragment refers to Handling of fragment identifiers in redirected URLs which may be of interest.