Add jsAttributesHandler
This adds `jsAttributesHandler` to run arbitrary Handler code before building the attributes map for the script tag generated by `widgetFile`. This is useful if you need to add a randomised nonce value to that tag. Closes https://github.com/yesodweb/yesod/issues/1621
This commit is contained in:
parent
2c2531c499
commit
59988f46a1
@ -1,5 +1,10 @@
|
||||
# ChangeLog for yesod-core
|
||||
|
||||
## 1.6.16
|
||||
|
||||
* Add `jsAttributesHandler` to run arbitrary Handler code before building the
|
||||
attributes map for the script tag generated by `widgetFile` [#1622](https://github.com/yesodweb/yesod/pull/1622)
|
||||
|
||||
## 1.6.15
|
||||
|
||||
* Move `redirectToPost` JavaScript form submission from HTML element to
|
||||
|
||||
@ -249,6 +249,17 @@ class RenderRoute site => Yesod site where
|
||||
jsAttributes :: site -> [(Text, Text)]
|
||||
jsAttributes _ = []
|
||||
|
||||
-- | Same as @jsAttributes@ but allows you to run arbitrary Handler code
|
||||
--
|
||||
-- This is useful if you need to add a randomised nonce value to the script
|
||||
-- tag generated by @widgetFile@. If you use both this function and
|
||||
-- @jsAttributes@, the two attribute maps will be merged, favouring the
|
||||
-- keys from @jsAttributes@ in case of a collision.
|
||||
--
|
||||
-- @since 1.6.16
|
||||
jsAttributesHandler :: HandlerFor site [(Text, Text)]
|
||||
jsAttributesHandler = pure []
|
||||
|
||||
-- | Create a session backend. Returning 'Nothing' disables
|
||||
-- sessions. If you'd like to change the way that the session
|
||||
-- cookies are created, take a look at
|
||||
@ -520,7 +531,9 @@ defaultCsrfMiddleware = defaultCsrfSetCookieMiddleware . defaultCsrfCheckMiddlew
|
||||
widgetToPageContent :: Yesod site
|
||||
=> WidgetFor site ()
|
||||
-> HandlerFor site (PageContent (Route site))
|
||||
widgetToPageContent w = HandlerFor $ \hd -> do
|
||||
widgetToPageContent w = do
|
||||
jsAttrsFromHandler <- jsAttributesHandler
|
||||
HandlerFor $ \hd -> do
|
||||
master <- unHandlerFor getYesod hd
|
||||
ref <- newIORef mempty
|
||||
unWidgetFor w WidgetData
|
||||
@ -531,6 +544,10 @@ widgetToPageContent w = HandlerFor $ \hd -> do
|
||||
let title = maybe mempty unTitle mTitle
|
||||
scripts = runUniqueList scripts'
|
||||
stylesheets = runUniqueList stylesheets'
|
||||
jsAttrs =
|
||||
let jsAttrsStatic = Map.fromList (jsAttributes master)
|
||||
jsAttrsDynamic = Map.fromList jsAttrsFromHandler
|
||||
in Map.toList $ Map.union jsAttrsStatic jsAttrsDynamic
|
||||
|
||||
flip unHandlerFor hd $ do
|
||||
render <- getUrlRenderParams
|
||||
@ -564,7 +581,7 @@ widgetToPageContent w = HandlerFor $ \hd -> do
|
||||
^{mkScriptTag s}
|
||||
$maybe j <- jscript
|
||||
$maybe s <- jsLoc
|
||||
<script src="#{s}" *{jsAttributes master}>
|
||||
<script src="#{s}" *{jsAttrs}>
|
||||
$nothing
|
||||
<script>^{jelper j}
|
||||
|]
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
name: yesod-core
|
||||
version: 1.6.15
|
||||
version: 1.6.16
|
||||
license: MIT
|
||||
license-file: LICENSE
|
||||
author: Michael Snoyman <michael@snoyman.com>
|
||||
|
||||
Loading…
Reference in New Issue
Block a user