Given a default Yesod scaffold, with file upload implemented by moving the uploaded file into the static
directory, how do I link to the file in the static directory? For normal static files, the staticFiles
splice will generate an identifier I can reference, but for user uploaded files obviously I cannot hardcode these identifiers. I can do this by manually using a #{}
splice, but I was hoping there might be something a little more typesafe, even the only checkable component is that I used the static/
prefix correctly.
staticFiles
generates a bunch of identifiers with type Route Static
. Unfortunately, Haddock doesn't display the information on associated type families, so you can't see the constructor in the docs, but the only constructor available is StaticRoute
:
https://github.com/yesodweb/yesod/blob/master/yesod-static/Yesod/Static.hs#L142
The two fields are the path info and the query string parameters. So to create a link to /static/foo/bar?baz=bin
, you could use:
StaticRoute ["foo", "bar"] [("baz", "bin")]
Usually creating a query string parameter isn't necessary, but staticFiles
will use it to insert a hash value for cache-busting purposes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With