Render manifest at build-time as well as in dev server.

This commit is contained in:
Dillon Kearns 2021-05-14 11:57:40 -07:00
parent 20038d75b1
commit 4c1f70c060
2 changed files with 24 additions and 51 deletions

View File

@ -40,6 +40,7 @@ function generateTemplateModuleConnector(phase) {
mainModule: `port module TemplateModulesBeta exposing (..)
import Api
import ApiRoute
import Browser.Navigation
import Route exposing (Route)
import Document
@ -397,7 +398,8 @@ main =
, fromJsPort = fromJsPort identity
, data = dataForRoute
, sharedData = Shared.template.data
, apiRoutes = Api.routes
, apiRoutes = \\htmlToString -> manifestHandler :: Api.routes htmlToString
}
dataForRoute : Maybe Route -> DataSource PageData
@ -454,30 +456,35 @@ getStaticRoutes =
|> DataSource.map (List.map Just)
manifestGenerator : List ( Maybe Route ) -> DataSource (Result anyError { path : List String, content : String })
manifestGenerator resolvedRoutes =
Site.config resolvedRoutes
|> .data
|> DataSource.map
(\\data ->
(Site.config resolvedRoutes |> .manifest) data
|> manifestToFile (Site.config resolvedRoutes |> .canonicalUrl)
)
manifestHandler : ApiRoute.Done ApiRoute.Response
manifestHandler =
ApiRoute.succeed
(getStaticRoutes
|> DataSource.andThen
(\\resolvedRoutes ->
Site.config resolvedRoutes
|> .data
|> DataSource.map
(\\data ->
(Site.config resolvedRoutes |> .manifest) data
|> manifestToFile (Site.config resolvedRoutes |> .canonicalUrl)
)
)
)
|> ApiRoute.literal "manifest.json"
|> ApiRoute.singleRoute
manifestToFile : String -> Manifest.Config -> Result anyError { path : List String, content : String }
manifestToFile : String -> Manifest.Config -> { body : String }
manifestToFile resolvedCanonicalUrl manifestConfig =
manifestConfig
|> Manifest.toJson resolvedCanonicalUrl
|> (\\manifestJsonValue ->
Ok
{ path = [ "manifest.json" ]
, content = Json.Encode.encode 0 manifestJsonValue
}
{ body = Json.Encode.encode 0 manifestJsonValue
}
)
port toJsPort : Json.Encode.Value -> Cmd msg
port fromJsPort : (Json.Decode.Value -> msg) -> Sub msg

View File

@ -1,11 +1,8 @@
module RenderRequest exposing (..)
import ApiRoute
import DataSource
import HtmlPrinter
import Json.Decode as Decode
import Json.Encode
import Pages.Manifest as Manifest
import Pages.PagePath as PagePath exposing (PagePath)
import Pages.ProgramConfig exposing (ProgramConfig)
import Regex
@ -115,9 +112,7 @@ requestPayloadDecoder config =
apiRoute : Maybe (ApiRoute.Done ApiRoute.Response)
apiRoute =
ApiRoute.firstMatch (String.dropLeft 1 path)
(manifestHandler config
:: config.apiRoutes HtmlPrinter.htmlToString
)
(config.apiRoutes HtmlPrinter.htmlToString)
in
case route of
Just justRoute ->
@ -139,35 +134,6 @@ requestPayloadDecoder config =
|> Decode.field "payload"
manifestHandler : ProgramConfig userMsg userModel (Maybe route) siteData pageData sharedData -> ApiRoute.Done ApiRoute.Response
manifestHandler config =
ApiRoute.succeed
(config.getStaticRoutes
|> DataSource.andThen
(\resolvedRoutes ->
config.site resolvedRoutes
|> .data
|> DataSource.map
(\data ->
(config.site resolvedRoutes |> .manifest) data
|> manifestToFile (config.site resolvedRoutes |> .canonicalUrl)
)
)
)
|> ApiRoute.literal "manifest.json"
|> ApiRoute.singleRoute
manifestToFile : String -> Manifest.Config -> { body : String }
manifestToFile resolvedCanonicalUrl manifestConfig =
manifestConfig
|> Manifest.toJson resolvedCanonicalUrl
|> (\manifestJsonValue ->
{ body = Json.Encode.encode 0 manifestJsonValue
}
)
pathToUrl : String -> Url
pathToUrl path =
{ protocol = Url.Https