Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

urlParamRenderOverride method for Yesod class #1257

Merged
merged 1 commit into from
Aug 10, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions yesod-core/Yesod/Core/Class/Yesod.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import Yesod.Core.Handler

import Yesod.Routes.Class

import Blaze.ByteString.Builder (Builder)
import Blaze.ByteString.Builder.Char.Utf8 (fromText)
import Blaze.ByteString.Builder (Builder, toByteString)
import Blaze.ByteString.Builder.ByteString (copyByteString)
import Blaze.ByteString.Builder.Char.Utf8 (fromText, fromChar)
import Control.Arrow ((***), second)
import Control.Exception (bracket)
#if __GLASGOW_HASKELL__ < 710
Expand All @@ -36,7 +37,7 @@ import Data.Text.Lazy.Builder (toLazyText)
import Data.Text.Lazy.Encoding (encodeUtf8)
import Data.Word (Word64)
import Language.Haskell.TH.Syntax (Loc (..))
import Network.HTTP.Types (encodePath)
import Network.HTTP.Types (encodePath, renderQueryText)
import qualified Network.Wai as W
import Data.Default (def)
import Network.Wai.Parse (lbsBackEnd,
Expand Down Expand Up @@ -107,6 +108,28 @@ class RenderRoute site => Yesod site where
urlRenderOverride :: site -> Route site -> Maybe Builder
urlRenderOverride _ _ = Nothing

-- | Override the rendering function for a particular URL and query string
-- parameters. One use case for this is to offload static hosting to a
-- different domain name to avoid sending cookies.
--
-- For backward compatibility default implementation is in terms of
-- 'urlRenderOverride', probably ineffective
--
-- Since 1.4.23
urlParamRenderOverride :: site
-> Route site
-> [(T.Text, T.Text)] -- ^ query string
-> Maybe Builder
urlParamRenderOverride y route params = addParams params <$> urlRenderOverride y route
where
addParams [] routeBldr = routeBldr
addParams nonEmptyParams routeBldr =
let routeBS = toByteString routeBldr
qsSeparator = fromChar $ if S8.elem '?' routeBS then '&' else '?'
valueToMaybe t = if t == "" then Nothing else Just t
queryText = map (id *** valueToMaybe) nonEmptyParams
in copyByteString routeBS `mappend` qsSeparator `mappend` renderQueryText False queryText

-- | Determine if a request is authorized or not.
--
-- Return 'Authorized' if the request is authorized,
Expand Down Expand Up @@ -290,6 +313,7 @@ class RenderRoute site => Yesod site where
yesodWithInternalState :: site -> Maybe (Route site) -> (InternalState -> IO a) -> IO a
yesodWithInternalState _ _ = bracket createInternalState closeInternalState
{-# INLINE yesodWithInternalState #-}
{-# DEPRECATED urlRenderOverride "Use urlParamRenderOverride instead" #-}

-- | Default implementation of 'makeLogger'. Sends to stdout and
-- automatically flushes on each write.
Expand Down
2 changes: 1 addition & 1 deletion yesod-core/Yesod/Core/Internal/Run.hs
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ yesodRender y ar url params =
fromMaybe
(joinPath y ar ps
$ params ++ params')
(urlRenderOverride y url)
(urlParamRenderOverride y url params)
where
(ps, params') = renderRoute url

Expand Down
2 changes: 1 addition & 1 deletion yesod-static/Yesod/EmbeddedStatic.hs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
-- contains the created 'EmbeddedStatic'.
--
-- It is recommended that you serve static resources from a separate domain to save time
-- on transmitting cookies. You can use 'urlRenderOverride' to do so, by redirecting
-- on transmitting cookies. You can use 'urlParamRenderOverride' to do so, by redirecting
-- routes to this subsite to a different domain (but the same path) and then pointing the
-- alternative domain to this server. In addition, you might consider using a reverse
-- proxy like varnish or squid to cache the static content, but the embedded content in
Expand Down
2 changes: 1 addition & 1 deletion yesod-static/Yesod/Static.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
--
-- In fact, in an ideal setup you'll serve your static files from
-- a separate domain name to save time on transmitting
-- cookies. In that case, you may wish to use 'urlRenderOverride'
-- cookies. In that case, you may wish to use 'urlParamRenderOverride'
-- to redirect requests to this subsite to a separate domain
-- name.
--
Expand Down