Spock icon indicating copy to clipboard operation
Spock copied to clipboard

Route with trailing slash

Open Hithroc opened this issue 8 years ago • 3 comments

Is there a way to have different actions for /foo/bar/ and /foo/bar?

In my situation specificaly, I want all /foo/bar reuest be redirected to /foo/bar/ so relative paths on frontend lead to /foo/bar/baz instead of /foo/baz

Hithroc avatar Feb 28 '17 14:02 Hithroc

Currently there is not, this is normalized. If you'd like you could implement the behavior behind some configuration and send a PR :-)

agrafix avatar Mar 01 '17 20:03 agrafix

Unfortunately I'm not too familiar with Spock's internal structure to do implement something like that.

Currently I have this ~~hack~~ workaround: I just take the raw path and see if there was a trailing slash.

directoryHook :: MonadIO m => ActionCtxT ctx m () -> ActionCtxT ctx m ()
directoryHook action = do
  rawPath <- T.decodeUtf8 . rawPathInfo <$> request
  if T.last rawPath /= '/'
  then redirect $ rawPath <> "/"
  else action

Hithroc avatar Mar 02 '17 12:03 Hithroc

Nice! I'll look into the real issue as I find time for it :-)

agrafix avatar Mar 02 '17 15:03 agrafix