Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Link to static file in Yesod that doesn't have Haskell identifier

Tags:

haskell

yesod

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.

like image 762
Edward Z. Yang Avatar asked Oct 28 '12 03:10

Edward Z. Yang


1 Answers

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.

like image 189
Michael Snoyman Avatar answered Nov 05 '22 02:11

Michael Snoyman