Issue Scaffolding Pre-rendered Pages in Elm Pages

I’m trying to modify the AddRoute.elm file in the elm-pages-start repo to scaffold a pre-rendered page. For reasons I don’t understand, the generated route seems to what you expect for a server-rendered route:

createFile : CliOptions -> { path : String, body : String }
createFile { moduleName, zip, county, genderS, ageS, tobaccoS, fields } =
    let 
        genderExpr = unsafeStringToGender genderS
        genderSR = case (String.left 1 (String.toLower genderS)) of
            "m" -> "M"
            _ -> "F"
        ageExpr = unsafeStringToAge ageS
        tobaccoExpr = unsafeStringToTobacco tobaccoS
        tobaccoSR = case (String.left 1 (String.toLower tobaccoS)) of
            "1"  -> "True"
            "t" -> "True"
            _ -> "False"
    in
    Scaffold.Route.preRender
        { moduleName = moduleName
        , pages = 
            Gen.BackendTask.succeed
                (Response.render
                    (Elm.record [])
                )
        , data =
            ( Alias (Type.record [])
            , \_ -> Gen.BackendTask.Http.getJson
                    (   "https://speakeasy.ngrok.dev/api/plans?zip=" 
                        ++ zip 
                        ++ "&age="
                        ++ ageS 
                        ++ "&county="
                        ++ county
                        ++ "&gender="
                        ++ genderSR
                        ++ "&tobacco="
                        ++ tobaccoSR
                        ++ "&discounts=False&" 
                        ++ "date=2024-02-01&" 
                        ++ "plan=G&plan=N&"
                        ++ "naic="
                        ++  naicListStr
                    )
                    Gen.Utils.planXDecoder
                |> Gen.BackendTask.allowFatal
            )
        , head = \app -> Elm.list []
        }
        |> Scaffold.Route.buildWithSharedState
        ...............

yields:

type alias ActionData =
    BackendTask.BackendTask FatalError.FatalError (List RouteParams)


data :
    RouteParams
    -> Server.Request.Request
    -> BackendTask.BackendTask FatalError.FatalError Data
data routeParams request =
    BackendTask.allowFatal
        (BackendTask.Http.getJson
            "https://speakeasy.ngrok.dev/api/plans?zip=75201&age=65&county=DALLAS&gender=M&tobacco=False&discounts=False&date=2024-02-01&plan=G&plan=N&naic=79413&naic=20699&naic=72052"
            Utils.planXDecoder
        )

If i understand this right, for pre-rendered routes, data should only take the first RouteParams arg, not the second Server.Request.Request arg. However, I can’t figure how to fix this in the scaffolding script. Is scaffolding not fully operational for pre-rendered routes?

1 Like

If i’m reading this right, the issue occurs in the case block on lines 652 through 697 of elm-pages/src/Scaffold/Route.elm at 10.0.2 · dillonkearns/elm-pages · GitHub – where the catchall _ block is being executed instead of the block above for definition.action of “Pages Nothing”.

1 Like

Hey @pyrex41, I fixed the issue with the generator script helper Scaffold.Route.preRender :+1: I also added a static route generator example script to the starter template generator in the package. That will be released shortly. I’ll add that to the git starter repo, too.

And I simplified Scaffold.Route.preRender so that it will automatically use RouteBuilder.single when there are no dynamic route segments, and otherwise will use RouteBuilder.preRender. A lot cleaner and more intuitive to use now I think :slightly_smiling_face:

2 Likes

Now released as part of 10.0.3!

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.