app.yaml : wildcard in URL with static_dir?

2019-09-03 05:19发布

My attempt at matching a regex as directory name in app.yaml doesn't work :

- url: /v1_.*
  static_dir: static/v1
  expiration: "364d"

Although this official spec says regex syntax is supported. Is there a way to make this work ?

I.e. /v1_2014-01-29/img/logo.png should match the static file /static/v1/img/logo.png.

Trivia

I use Google App Engine to serve a Go webapp.

I'd like to maximize browser cache longevity, minimize number of requests and still serve the fresh versions of my css/js/png, and I believe revving filenames is a best practice to achieve this. Also as adding a variable querystring (/v1/img/logo.png?2014-01-29) might cause proxy and cache problems, I prefer to show a variable directory name (/v1_2014-01-29/img/logo.png), pointing to the same underlying server dir.

2条回答
仙女界的扛把子
2楼-- · 2019-09-03 05:32

After olivierdm's answer I changed my yaml into :

- url: /v1_.*_
  static_dir: static/v1
  expiration: "364d"

and my html templates to produce /v1_2014-01-29_/img/logo.png.

Basically, the extra arbitrary character underscore _ forces .* to match 2014-01-29, not the empty string.

Now every time I want the visitors to reload the static files, I just change the date in the tempating (I don't touch the app.yaml anymore). Also, any accidental request to an "outdated" URL will still succeed and serve the fresh resource.

查看更多
何必那么认真
3楼-- · 2019-09-03 05:52

Seems to me that whatever part of the URL that is beyond the match of the url definition (which matches from the start) is appended to the static_dir.

So the following handler should match /v1_2014-01-29/img/logo.png if the file path is static/v1/img/logo.png (tried with Python):

- url: /v1_(\d+-?)+
  static_dir: static/v1
查看更多
登录 后发表回答