【问题标题】:Define query param in app.yaml in Google Appengine在 Google Appengine 的 app.yaml 中定义查询参数
【发布时间】:2014-02-25 15:06:14
【问题描述】:

我想根据查询参数提供静态文件。更具体地说,我想为搜索引擎优化提供预渲染快照。这些页面托管在 Google Appengine 上,所以我使用 app.yaml 来定义这些 url。

handlers:
# Consider anything matching a dot static content.
- url: /(.*\..*)$
  static_files: dist/\1
  upload: dist/(.*\..*)$

# Serve snapshots for seo
- url: /?_escaped_fragment_=(.*)
  static_files: dist/snapshots/\1
  upload: dist/(.*)$

# Otherwise let Angular handle it.
- url: /(.*)
  static_files: dist/index.html
  upload: dist/index.html

但是,当我使用查询参数_escaped_fragment_ 获取 url 时,会触发最后一个 url 处理程序。是否可以在 url 中定义查询参数?如果是这样,我做错了什么?

【问题讨论】:

  • 您要获取的网址是什么?
  • 任何 url googlebot 都会获取。例如我定义了/support,所以googlebot 会获取/?_escaped_fragment_=support。这是一个 AngularJS 站点,所以/?_escaped_fragment_=support 应该获取一个预渲染的 html 文件以便对其进行索引。
  • 解决方法是创建一个处理程序,它要么返回 dist/index.html(当 escape_fragment 不在查询字符串中时),否则返回给定 的 snapshop html转义片段。我不知道以这种方式提供服务需要多少额外费用。

标签: python google-app-engine app.yaml


【解决方案1】:

我很高兴被证明是错误的,但我很确定在通过 app.yaml 调度时不会考虑查询参数。

【讨论】:

  • 我希望得到一个答案,但不幸的是我认为这个答案是正确的。
【解决方案2】:

我遇到了完全相同的问题。 App Engine 没有添加调度静态查询参数的能力有点蹩脚......无论如何。

import webapp2, urllib, logging, json, os

dp = os.path.dirname(os.path.realpath(__file__))
fp = os.path.join(dp, "resources", 'index.html')
w = open(fp, 'r').read()

class Fragment(webapp2.RequestHandler):
  def get(self, pathname):
    if '_escaped_fragment_' in self.request.arguments():
      CODE_GOES_HERE_FOR_BUILDING_YOUR_FRAGMENT_RESPONSE
    else:
      self.response.write(w)

application = webapp2.WSGIApplication(
    [('/(.*)', Fragment)],
  debug=True)

这段代码基本上猜测你是否在_escaped_fragment_查询参数上进行调度,并相应地修改输出。我不知道这比将index.html 留在static_files: 处理程序中的app.yaml 中的性能要低多少(如果有的话)。

【讨论】:

  • 当我仍然遇到问题时,我已经构建了类似的东西。如今,Google 索引 JavaScript 呈现的页面,使 _escaped_fragment_ 响应变得不那么重要。 (但对其他搜索引擎仍然有用。)
  • 好点。不幸的是,我仍然有一个动态站点,除非您使用 javascripting UI 元素,否则大部分内容都不会显示,因此 escaped_fragment 在我的特定情况下仍然有用。但是,是的,我同意,如果您的大部分内容出现在初始 javascript 渲染中,那么 google 可能会处理得很好。
猜你喜欢
  • 2011-05-12
  • 1970-01-01
  • 2016-08-11
  • 2015-10-08
  • 1970-01-01
  • 2011-02-07
  • 2011-11-10
  • 2015-01-26
  • 1970-01-01
相关资源
最近更新 更多