4

我想根据查询参数提供静态文件。更具体地说,我想为搜索引擎优化提供预渲染快照。这些页面托管在 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

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

4

2 回答 2

4

我很高兴被证明是错误的,但我很确定在通过app.yaml.

于 2014-02-26T05:09:18.927 回答
0

我有同样的问题。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

于 2014-12-28T04:50:39.717 回答