spa(single page application)のindex.htmlをh2oで配布する際どうすりゃいいのか。
vueとかのspaだと、vue-routerとかつかってルーティングするため、
http://xxx.com/aaa/bbb/ccc
でアクセスしたときも、index.html
を返却して、その中でjsを解釈してルーティングをする。
h2oでindex.htmlを配布するさい、どうすりゃいいのかわからなかった。
nginxだとtry_files
とかいうので対応できるみたい。
h2oはmrubyでフック処理をかけるので、それで簡単に対応できたけど、本番環境だと、他にいろいろ考慮しなければいけない感じがする。
runitで動かしてるのでファイル指定などは相対パスにしてる。
listen:
port: 8000
hosts:
"0.0.0.0":
paths:
/:
mruby.handler-file: './forspa.rb'
file.dir: /tmp/somewhere
./forspa.rbの内容。
path = "/tmp/somewhere"
cont = File.open("#{path}/index.html").read()
lambda do |env|
unless File.exist?("#{path}/" + env["PATH_INFO"])
return [200, {'content-type' => 'text/html'}, [cont]]
end
return [399, {}, []]
end