elm-pages 3.0.0-beta.33 → 3.0.0-beta.35

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. package/README.md +1 -1
  2. package/codegen/elm-pages-codegen.cjs +165 -134
  3. package/generator/dead-code-review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/d.dat +0 -0
  4. package/generator/dead-code-review/elm-stuff/tests-0.19.1/js/node_runner.js +1 -1
  5. package/generator/dead-code-review/elm-stuff/tests-0.19.1/js/node_supervisor.js +1 -1
  6. package/generator/dead-code-review/elm.json +1 -1
  7. package/generator/review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/d.dat +0 -0
  8. package/generator/review/elm-stuff/tests-0.19.1/js/node_runner.js +1 -1
  9. package/generator/review/elm-stuff/tests-0.19.1/js/node_supervisor.js +1 -1
  10. package/generator/review/elm.json +1 -1
  11. package/generator/src/RouteBuilder.elm +2 -2
  12. package/generator/src/cli.js +5 -9
  13. package/generator/src/compatibility-key.js +2 -2
  14. package/generator/static-code/elm-pages.js +10 -0
  15. package/package.json +3 -3
  16. package/src/BackendTask/File.elm +43 -13
  17. package/src/BackendTask/Internal/Request.elm +48 -3
  18. package/src/FatalError.elm +4 -3
  19. package/src/Internal/Field.elm +19 -0
  20. package/src/Internal/Input.elm +81 -0
  21. package/src/Pages/Form.elm +229 -0
  22. package/src/Pages/Internal/Msg.elm +70 -61
  23. package/src/Pages/Internal/Platform/Cli.elm +423 -425
  24. package/src/Pages/Internal/Platform/CompatibilityKey.elm +1 -1
  25. package/src/Pages/Internal/Platform/GeneratorApplication.elm +14 -8
  26. package/src/Pages/Internal/Platform/StaticResponses.elm +1 -7
  27. package/src/Pages/Internal/Platform.elm +119 -100
  28. package/src/Pages/ProgramConfig.elm +12 -5
  29. package/src/Pages/StaticHttpRequest.elm +0 -1
  30. package/src/Pages/Transition.elm +9 -1
  31. package/src/PagesMsg.elm +0 -10
  32. package/src/Scaffold/Form.elm +9 -9
  33. package/src/Server/Request.elm +57 -61
  34. package/src/Form/Field.elm +0 -729
  35. package/src/Form/FieldStatus.elm +0 -36
  36. package/src/Form/FieldView.elm +0 -497
  37. package/src/Form/FormData.elm +0 -22
  38. package/src/Form/Validation.elm +0 -391
  39. package/src/Form/Value.elm +0 -118
  40. package/src/Form.elm +0 -1558
  41. package/src/FormDecoder.elm +0 -102
  42. package/src/Pages/FormState.elm +0 -257
  43. package/src/Pages/Internal/Form.elm +0 -37
@@ -388,448 +388,446 @@ initLegacy site ((RenderRequest.SinglePage includeHtml singleRequest _) as rende
388
388
 
389
389
  staticResponsesNew : BackendTask FatalError Effect
390
390
  staticResponsesNew =
391
- StaticResponses.renderApiRequest
392
- (case singleRequest of
393
- RenderRequest.Page serverRequestPayload ->
394
- let
395
- isAction : Maybe ActionRequest
396
- isAction =
397
- renderRequest
398
- |> RenderRequest.maybeRequestPayload
399
- |> Maybe.andThen (Decode.decodeValue isActionDecoder >> Result.withDefault Nothing)
400
-
401
- currentUrl : Url
402
- currentUrl =
403
- { protocol = Url.Https
404
- , host = site.canonicalUrl
405
- , port_ = Nothing
406
- , path = serverRequestPayload.path |> Path.toRelative
407
- , query = Nothing
408
- , fragment = Nothing
409
- }
410
- in
411
- --case isAction of
412
- -- Just actionRequest ->
413
- (if isDevServer then
414
- config.handleRoute serverRequestPayload.frontmatter
391
+ case singleRequest of
392
+ RenderRequest.Page serverRequestPayload ->
393
+ let
394
+ isAction : Maybe ActionRequest
395
+ isAction =
396
+ renderRequest
397
+ |> RenderRequest.maybeRequestPayload
398
+ |> Maybe.andThen (Decode.decodeValue isActionDecoder >> Result.withDefault Nothing)
399
+
400
+ currentUrl : Url
401
+ currentUrl =
402
+ { protocol = Url.Https
403
+ , host = site.canonicalUrl
404
+ , port_ = Nothing
405
+ , path = serverRequestPayload.path |> Path.toRelative
406
+ , query = Nothing
407
+ , fragment = Nothing
408
+ }
409
+ in
410
+ --case isAction of
411
+ -- Just actionRequest ->
412
+ (if isDevServer then
413
+ config.handleRoute serverRequestPayload.frontmatter
414
+
415
+ else
416
+ BackendTask.succeed Nothing
417
+ )
418
+ |> BackendTask.andThen
419
+ (\pageFound ->
420
+ case pageFound of
421
+ Nothing ->
422
+ --sendSinglePageProgress site model.allRawResponses config model payload
423
+ (case isAction of
424
+ Just _ ->
425
+ config.action (RenderRequest.maybeRequestPayload renderRequest |> Maybe.withDefault Json.Encode.null) serverRequestPayload.frontmatter |> BackendTask.map Just
415
426
 
416
- else
417
- BackendTask.succeed Nothing
418
- )
419
- |> BackendTask.andThen
420
- (\pageFound ->
421
- case pageFound of
422
- Nothing ->
423
- --sendSinglePageProgress site model.allRawResponses config model payload
424
- (case isAction of
425
- Just _ ->
426
- config.action (RenderRequest.maybeRequestPayload renderRequest |> Maybe.withDefault Json.Encode.null) serverRequestPayload.frontmatter |> BackendTask.map Just
427
-
428
- Nothing ->
429
- BackendTask.succeed Nothing
430
- )
431
- |> BackendTask.andThen
432
- (\something ->
433
- let
434
- actionHeaders2 : Maybe { statusCode : Int, headers : List ( String, String ) }
435
- actionHeaders2 =
436
- case something of
437
- Just (PageServerResponse.RenderPage responseThing _) ->
438
- Just responseThing
439
-
440
- Just (PageServerResponse.ServerResponse responseThing) ->
441
- Just
442
- { headers = responseThing.headers
443
- , statusCode = responseThing.statusCode
444
- }
445
-
446
- _ ->
447
- Nothing
448
-
449
- maybeRedirectResponse : Maybe Effect
450
- maybeRedirectResponse =
451
- actionHeaders2
452
- |> Maybe.andThen
453
- (\responseMetadata ->
454
- toRedirectResponse config
455
- serverRequestPayload
456
- includeHtml
457
- responseMetadata
458
- responseMetadata
459
- )
460
- in
461
- case maybeRedirectResponse of
462
- Just redirectResponse ->
463
- redirectResponse
464
- |> BackendTask.succeed
465
-
466
- Nothing ->
467
- BackendTask.map3
468
- (\pageData sharedData tags ->
469
- let
470
- renderedResult : Effect
471
- renderedResult =
472
- case pageData of
473
- PageServerResponse.RenderPage responseInfo pageData_ ->
474
- let
475
- currentPage : { path : Path, route : route }
476
- currentPage =
477
- { path = serverRequestPayload.path, route = urlToRoute config currentUrl }
478
-
479
- maybeActionData : Maybe actionData
480
- maybeActionData =
481
- case something of
482
- Just (PageServerResponse.RenderPage _ actionThing) ->
483
- Just actionThing
484
-
485
- _ ->
486
- Nothing
487
-
488
- pageModel : userModel
489
- pageModel =
490
- config.init
491
- Pages.Flags.PreRenderFlags
492
- sharedData
493
- pageData_
494
- maybeActionData
495
- (Just
496
- { path =
497
- { path = currentPage.path
498
- , query = Nothing
499
- , fragment = Nothing
500
- }
501
- , metadata = currentPage.route
502
- , pageUrl = Nothing
427
+ Nothing ->
428
+ BackendTask.succeed Nothing
429
+ )
430
+ |> BackendTask.andThen
431
+ (\something ->
432
+ let
433
+ actionHeaders2 : Maybe { statusCode : Int, headers : List ( String, String ) }
434
+ actionHeaders2 =
435
+ case something of
436
+ Just (PageServerResponse.RenderPage responseThing _) ->
437
+ Just responseThing
438
+
439
+ Just (PageServerResponse.ServerResponse responseThing) ->
440
+ Just
441
+ { headers = responseThing.headers
442
+ , statusCode = responseThing.statusCode
443
+ }
444
+
445
+ _ ->
446
+ Nothing
447
+
448
+ maybeRedirectResponse : Maybe Effect
449
+ maybeRedirectResponse =
450
+ actionHeaders2
451
+ |> Maybe.andThen
452
+ (\responseMetadata ->
453
+ toRedirectResponse config
454
+ serverRequestPayload
455
+ includeHtml
456
+ responseMetadata
457
+ responseMetadata
458
+ )
459
+ in
460
+ case maybeRedirectResponse of
461
+ Just redirectResponse ->
462
+ redirectResponse
463
+ |> BackendTask.succeed
464
+
465
+ Nothing ->
466
+ BackendTask.map3
467
+ (\pageData sharedData tags ->
468
+ let
469
+ renderedResult : Effect
470
+ renderedResult =
471
+ case pageData of
472
+ PageServerResponse.RenderPage responseInfo pageData_ ->
473
+ let
474
+ currentPage : { path : Path, route : route }
475
+ currentPage =
476
+ { path = serverRequestPayload.path, route = urlToRoute config currentUrl }
477
+
478
+ maybeActionData : Maybe actionData
479
+ maybeActionData =
480
+ case something of
481
+ Just (PageServerResponse.RenderPage _ actionThing) ->
482
+ Just actionThing
483
+
484
+ _ ->
485
+ Nothing
486
+
487
+ pageModel : userModel
488
+ pageModel =
489
+ config.init
490
+ Pages.Flags.PreRenderFlags
491
+ sharedData
492
+ pageData_
493
+ maybeActionData
494
+ (Just
495
+ { path =
496
+ { path = currentPage.path
497
+ , query = Nothing
498
+ , fragment = Nothing
503
499
  }
500
+ , metadata = currentPage.route
501
+ , pageUrl = Nothing
502
+ }
503
+ )
504
+ |> Tuple.first
505
+
506
+ viewValue : { title : String, body : List (Html (PagesMsg userMsg)) }
507
+ viewValue =
508
+ (config.view Dict.empty Dict.empty Nothing currentPage Nothing sharedData pageData_ maybeActionData |> .view) pageModel
509
+
510
+ responseMetadata : { statusCode : Int, headers : List ( String, String ) }
511
+ responseMetadata =
512
+ actionHeaders2 |> Maybe.withDefault responseInfo
513
+ in
514
+ (case isAction of
515
+ Just actionRequestKind ->
516
+ let
517
+ actionDataResult : Maybe (PageServerResponse actionData errorPage)
518
+ actionDataResult =
519
+ something
520
+ in
521
+ case actionDataResult of
522
+ Just (PageServerResponse.RenderPage ignored2 actionData_) ->
523
+ case actionRequestKind of
524
+ ActionResponseRequest ->
525
+ ( ignored2.headers
526
+ , ResponseSketch.HotUpdate pageData_ sharedData (Just actionData_)
527
+ |> config.encodeResponse
528
+ |> Bytes.Encode.encode
529
+ )
530
+
531
+ ActionOnlyRequest ->
532
+ ---- TODO need to encode action data when only that is requested (not ResponseSketch?)
533
+ ( ignored2.headers
534
+ , actionData_
535
+ |> config.encodeAction
536
+ |> Bytes.Encode.encode
537
+ )
538
+
539
+ _ ->
540
+ ( responseMetadata.headers
541
+ , Bytes.Encode.encode (Bytes.Encode.unsignedInt8 0)
504
542
  )
505
- |> Tuple.first
506
-
507
- viewValue : { title : String, body : List (Html (PagesMsg userMsg)) }
508
- viewValue =
509
- (config.view Dict.empty Dict.empty Nothing currentPage Nothing sharedData pageData_ maybeActionData |> .view) pageModel
510
-
511
- responseMetadata : { statusCode : Int, headers : List ( String, String ) }
512
- responseMetadata =
513
- actionHeaders2 |> Maybe.withDefault responseInfo
514
- in
515
- (case isAction of
516
- Just actionRequestKind ->
543
+
544
+ Nothing ->
545
+ ( responseMetadata.headers
546
+ , ResponseSketch.HotUpdate pageData_ sharedData Nothing
547
+ |> config.encodeResponse
548
+ |> Bytes.Encode.encode
549
+ )
550
+ )
551
+ |> (\( actionHeaders, byteEncodedPageData ) ->
517
552
  let
518
- actionDataResult : Maybe (PageServerResponse actionData errorPage)
519
- actionDataResult =
520
- something
553
+ rendered : { view : userModel -> { title : String, body : List (Html (PagesMsg userMsg)) }, head : List Tag }
554
+ rendered =
555
+ config.view Dict.empty Dict.empty Nothing currentPage Nothing sharedData pageData_ maybeActionData
521
556
  in
522
- case actionDataResult of
523
- Just (PageServerResponse.RenderPage ignored2 actionData_) ->
524
- case actionRequestKind of
525
- ActionResponseRequest ->
526
- ( ignored2.headers
527
- , ResponseSketch.HotUpdate pageData_ sharedData (Just actionData_)
528
- |> config.encodeResponse
529
- |> Bytes.Encode.encode
530
- )
531
-
532
- ActionOnlyRequest ->
533
- ---- TODO need to encode action data when only that is requested (not ResponseSketch?)
534
- ( ignored2.headers
535
- , actionData_
536
- |> config.encodeAction
537
- |> Bytes.Encode.encode
538
- )
539
-
540
- _ ->
541
- ( responseMetadata.headers
542
- , Bytes.Encode.encode (Bytes.Encode.unsignedInt8 0)
557
+ PageServerResponse.toRedirect responseMetadata
558
+ |> Maybe.map
559
+ (\{ location } ->
560
+ location
561
+ |> ResponseSketch.Redirect
562
+ |> config.encodeResponse
563
+ |> Bytes.Encode.encode
543
564
  )
544
-
545
- Nothing ->
546
- ( responseMetadata.headers
547
- , ResponseSketch.HotUpdate pageData_ sharedData Nothing
548
- |> config.encodeResponse
549
- |> Bytes.Encode.encode
550
- )
551
- )
552
- |> (\( actionHeaders, byteEncodedPageData ) ->
553
- let
554
- rendered : { view : userModel -> { title : String, body : List (Html (PagesMsg userMsg)) }, head : List Tag }
555
- rendered =
556
- config.view Dict.empty Dict.empty Nothing currentPage Nothing sharedData pageData_ maybeActionData
557
- in
558
- PageServerResponse.toRedirect responseMetadata
559
- |> Maybe.map
560
- (\{ location } ->
561
- location
562
- |> ResponseSketch.Redirect
563
- |> config.encodeResponse
564
- |> Bytes.Encode.encode
565
- )
566
- -- TODO handle other cases besides redirects?
567
- |> Maybe.withDefault byteEncodedPageData
568
- |> (\encodedData ->
569
- { route = currentPage.path |> Path.toRelative
570
- , contentJson = Dict.empty
571
- , html = viewValue.body |> bodyToString
572
- , errors = []
573
- , head = rendered.head ++ tags
574
- , title = viewValue.title
575
- , staticHttpCache = Dict.empty
576
- , is404 = False
577
- , statusCode =
578
- case includeHtml of
579
- RenderRequest.OnlyJson ->
580
- 200
581
-
582
- RenderRequest.HtmlAndJson ->
583
- responseMetadata.statusCode
584
- , headers =
585
- -- TODO should `responseInfo.headers` be used? Is there a problem in the case where there is both an action and data response in one? Do we need to make sure it is performed as two separate HTTP requests to ensure that the cookies are set correctly in that case?
586
- actionHeaders
587
- }
588
- |> ToJsPayload.PageProgress
589
- |> Effect.SendSinglePageNew encodedData
590
- )
591
- )
592
-
593
- PageServerResponse.ServerResponse serverResponse ->
594
- --PageServerResponse.ServerResponse serverResponse
595
- -- TODO handle error?
596
- let
597
- responseMetadata : PageServerResponse.Response
598
- responseMetadata =
599
- case something of
600
- Just (PageServerResponse.ServerResponse responseThing) ->
601
- responseThing
602
-
603
- _ ->
604
- serverResponse
605
- in
606
- toRedirectResponse config serverRequestPayload includeHtml serverResponse responseMetadata
607
- |> Maybe.withDefault
608
- ({ body = serverResponse |> PageServerResponse.toJson
609
- , staticHttpCache = Dict.empty
610
- , statusCode = serverResponse.statusCode
611
- }
612
- |> ToJsPayload.SendApiResponse
613
- |> Effect.SendSinglePage
614
- )
615
-
616
- PageServerResponse.ErrorPage error record ->
617
- let
618
- currentPage : { path : Path, route : route }
619
- currentPage =
620
- { path = serverRequestPayload.path, route = urlToRoute config currentUrl }
621
-
622
- pageModel : userModel
623
- pageModel =
624
- config.init
625
- Pages.Flags.PreRenderFlags
626
- sharedData
627
- pageData2
628
- Nothing
629
- (Just
630
- { path =
631
- { path = currentPage.path
632
- , query = Nothing
633
- , fragment = Nothing
565
+ -- TODO handle other cases besides redirects?
566
+ |> Maybe.withDefault byteEncodedPageData
567
+ |> (\encodedData ->
568
+ { route = currentPage.path |> Path.toRelative
569
+ , contentJson = Dict.empty
570
+ , html = viewValue.body |> bodyToString
571
+ , errors = []
572
+ , head = rendered.head ++ tags
573
+ , title = viewValue.title
574
+ , staticHttpCache = Dict.empty
575
+ , is404 = False
576
+ , statusCode =
577
+ case includeHtml of
578
+ RenderRequest.OnlyJson ->
579
+ 200
580
+
581
+ RenderRequest.HtmlAndJson ->
582
+ responseMetadata.statusCode
583
+ , headers =
584
+ -- TODO should `responseInfo.headers` be used? Is there a problem in the case where there is both an action and data response in one? Do we need to make sure it is performed as two separate HTTP requests to ensure that the cookies are set correctly in that case?
585
+ actionHeaders
634
586
  }
635
- , metadata = currentPage.route
636
- , pageUrl = Nothing
587
+ |> ToJsPayload.PageProgress
588
+ |> Effect.SendSinglePageNew encodedData
589
+ )
590
+ )
591
+
592
+ PageServerResponse.ServerResponse serverResponse ->
593
+ --PageServerResponse.ServerResponse serverResponse
594
+ -- TODO handle error?
595
+ let
596
+ responseMetadata : PageServerResponse.Response
597
+ responseMetadata =
598
+ case something of
599
+ Just (PageServerResponse.ServerResponse responseThing) ->
600
+ responseThing
601
+
602
+ _ ->
603
+ serverResponse
604
+ in
605
+ toRedirectResponse config serverRequestPayload includeHtml serverResponse responseMetadata
606
+ |> Maybe.withDefault
607
+ ({ body = serverResponse |> PageServerResponse.toJson
608
+ , staticHttpCache = Dict.empty
609
+ , statusCode = serverResponse.statusCode
610
+ }
611
+ |> ToJsPayload.SendApiResponse
612
+ |> Effect.SendSinglePage
613
+ )
614
+
615
+ PageServerResponse.ErrorPage error record ->
616
+ let
617
+ currentPage : { path : Path, route : route }
618
+ currentPage =
619
+ { path = serverRequestPayload.path, route = urlToRoute config currentUrl }
620
+
621
+ pageModel : userModel
622
+ pageModel =
623
+ config.init
624
+ Pages.Flags.PreRenderFlags
625
+ sharedData
626
+ pageData2
627
+ Nothing
628
+ (Just
629
+ { path =
630
+ { path = currentPage.path
631
+ , query = Nothing
632
+ , fragment = Nothing
637
633
  }
638
- )
639
- |> Tuple.first
640
-
641
- pageData2 : pageData
642
- pageData2 =
643
- config.errorPageToData error
644
-
645
- viewValue : { title : String, body : List (Html (PagesMsg userMsg)) }
646
- viewValue =
647
- (config.view Dict.empty Dict.empty Nothing currentPage Nothing sharedData pageData2 Nothing |> .view) pageModel
648
- in
649
- (ResponseSketch.HotUpdate pageData2 sharedData Nothing
650
- |> config.encodeResponse
651
- |> Bytes.Encode.encode
652
- )
653
- |> (\encodedData ->
654
- { route = currentPage.path |> Path.toRelative
655
- , contentJson = Dict.empty
656
- , html = viewValue.body |> bodyToString
657
- , errors = []
658
- , head = tags
659
- , title = viewValue.title
660
- , staticHttpCache = Dict.empty
661
- , is404 = False
662
- , statusCode =
663
- case includeHtml of
664
- RenderRequest.OnlyJson ->
665
- 200
666
-
667
- RenderRequest.HtmlAndJson ->
668
- config.errorStatusCode error
669
- , headers = record.headers
634
+ , metadata = currentPage.route
635
+ , pageUrl = Nothing
670
636
  }
671
- |> ToJsPayload.PageProgress
672
- |> Effect.SendSinglePageNew encodedData
673
- )
674
- in
675
- renderedResult
676
- )
677
- (config.data (RenderRequest.maybeRequestPayload renderRequest |> Maybe.withDefault Json.Encode.null) serverRequestPayload.frontmatter)
678
- config.sharedData
679
- globalHeadTags
680
- )
681
- |> BackendTask.onError
682
- (\((Pages.Internal.FatalError.FatalError fatalError) as error) ->
683
- let
684
- isPreRendered : Bool
685
- isPreRendered =
686
- let
687
- keys : Int
688
- keys =
689
- RenderRequest.maybeRequestPayload renderRequest |> Maybe.map (Decode.decodeValue (Decode.keyValuePairs Decode.value)) |> Maybe.withDefault (Ok []) |> Result.withDefault [] |> List.map Tuple.first |> List.length
690
- in
691
- -- TODO this is a bit hacky, would be nice to clean up the way of checking whether this is server-rendered or pre-rendered
692
- keys <= 1
693
- in
694
- if isDevServer || isPreRendered then
695
- -- we want to stop the build for pre-rendered routes, and give a dev server error popup in the dev server
696
- BackendTask.fail error
697
-
698
- else
699
- --only render the production ErrorPage in production server-rendered Routes
700
- config.sharedData
701
- |> BackendTask.andThen
702
- (\justSharedData ->
703
- let
704
- errorPage : errorPage
705
- errorPage =
706
- config.internalError fatalError.body
707
-
708
- dataThing : pageData
709
- dataThing =
710
- errorPage
711
- |> config.errorPageToData
712
-
713
- statusCode : Int
714
- statusCode =
715
- config.errorStatusCode errorPage
716
-
717
- byteEncodedPageData : Bytes
718
- byteEncodedPageData =
719
- ResponseSketch.HotUpdate
720
- dataThing
721
- justSharedData
722
- -- TODO remove shared action data
723
- Nothing
724
- |> config.encodeResponse
725
- |> Bytes.Encode.encode
726
-
727
- pageModel : userModel
728
- pageModel =
729
- config.init
730
- Pages.Flags.PreRenderFlags
731
- justSharedData
732
- dataThing
733
- Nothing
734
- (Just
735
- { path =
736
- { path = currentPage.path
737
- , query = Nothing
738
- , fragment = Nothing
739
- }
740
- , metadata = currentPage.route
741
- , pageUrl = Nothing
742
- }
637
+ )
638
+ |> Tuple.first
639
+
640
+ pageData2 : pageData
641
+ pageData2 =
642
+ config.errorPageToData error
643
+
644
+ viewValue : { title : String, body : List (Html (PagesMsg userMsg)) }
645
+ viewValue =
646
+ (config.view Dict.empty Dict.empty Nothing currentPage Nothing sharedData pageData2 Nothing |> .view) pageModel
647
+ in
648
+ (ResponseSketch.HotUpdate pageData2 sharedData Nothing
649
+ |> config.encodeResponse
650
+ |> Bytes.Encode.encode
743
651
  )
744
- |> Tuple.first
745
-
746
- currentPage : { path : Path, route : route }
747
- currentPage =
748
- { path = serverRequestPayload.path, route = urlToRoute config currentUrl }
749
-
750
- viewValue : { title : String, body : List (Html (PagesMsg userMsg)) }
751
- viewValue =
752
- (config.view Dict.empty Dict.empty Nothing currentPage Nothing justSharedData dataThing Nothing |> .view)
753
- pageModel
754
- in
755
- { route = Path.toAbsolute currentPage.path
756
- , contentJson = Dict.empty
757
- , html = viewValue.body |> bodyToString
758
- , errors = []
759
- , head = [] -- TODO render head tags --config.view Dict.empty Dict.empty Nothing pathAndRoute Nothing justSharedData pageData Nothing |> .head
760
- , title = viewValue.title
761
- , staticHttpCache = Dict.empty
762
- , is404 = False
763
- , statusCode = statusCode
764
- , headers = []
765
- }
766
- |> ToJsPayload.PageProgress
767
- |> Effect.SendSinglePageNew byteEncodedPageData
768
- |> BackendTask.succeed
769
- )
770
- )
771
-
772
- Just notFoundReason ->
773
- render404Page config
774
- Nothing
775
- -- TODO do I need sharedDataResult?
776
- --(Result.toMaybe sharedDataResult)
777
- isDevServer
778
- serverRequestPayload.path
779
- notFoundReason
780
- |> BackendTask.succeed
781
- )
782
-
783
- RenderRequest.Api ( path, ApiRoute apiHandler ) ->
784
- BackendTask.map2
785
- (\response _ ->
786
- case response of
787
- Just okResponse ->
788
- { body = okResponse
789
- , staticHttpCache = Dict.empty -- TODO do I need to serialize the full cache here, or can I handle that from the JS side?
790
- , statusCode = 200
791
- }
792
- |> ToJsPayload.SendApiResponse
793
- |> Effect.SendSinglePage
652
+ |> (\encodedData ->
653
+ { route = currentPage.path |> Path.toRelative
654
+ , contentJson = Dict.empty
655
+ , html = viewValue.body |> bodyToString
656
+ , errors = []
657
+ , head = tags
658
+ , title = viewValue.title
659
+ , staticHttpCache = Dict.empty
660
+ , is404 = False
661
+ , statusCode =
662
+ case includeHtml of
663
+ RenderRequest.OnlyJson ->
664
+ 200
665
+
666
+ RenderRequest.HtmlAndJson ->
667
+ config.errorStatusCode error
668
+ , headers = record.headers
669
+ }
670
+ |> ToJsPayload.PageProgress
671
+ |> Effect.SendSinglePageNew encodedData
672
+ )
673
+ in
674
+ renderedResult
675
+ )
676
+ (config.data (RenderRequest.maybeRequestPayload renderRequest |> Maybe.withDefault Json.Encode.null) serverRequestPayload.frontmatter)
677
+ config.sharedData
678
+ globalHeadTags
679
+ )
680
+ |> BackendTask.onError
681
+ (\((Pages.Internal.FatalError.FatalError fatalError) as error) ->
682
+ let
683
+ isPreRendered : Bool
684
+ isPreRendered =
685
+ let
686
+ keys : Int
687
+ keys =
688
+ RenderRequest.maybeRequestPayload renderRequest |> Maybe.map (Decode.decodeValue (Decode.keyValuePairs Decode.value)) |> Maybe.withDefault (Ok []) |> Result.withDefault [] |> List.map Tuple.first |> List.length
689
+ in
690
+ -- TODO this is a bit hacky, would be nice to clean up the way of checking whether this is server-rendered or pre-rendered
691
+ keys <= 1
692
+ in
693
+ if isDevServer || isPreRendered then
694
+ -- we want to stop the build for pre-rendered routes, and give a dev server error popup in the dev server
695
+ BackendTask.fail error
696
+
697
+ else
698
+ --only render the production ErrorPage in production server-rendered Routes
699
+ config.sharedData
700
+ |> BackendTask.andThen
701
+ (\justSharedData ->
702
+ let
703
+ errorPage : errorPage
704
+ errorPage =
705
+ config.internalError fatalError.body
706
+
707
+ dataThing : pageData
708
+ dataThing =
709
+ errorPage
710
+ |> config.errorPageToData
711
+
712
+ statusCode : Int
713
+ statusCode =
714
+ config.errorStatusCode errorPage
715
+
716
+ byteEncodedPageData : Bytes
717
+ byteEncodedPageData =
718
+ ResponseSketch.HotUpdate
719
+ dataThing
720
+ justSharedData
721
+ -- TODO remove shared action data
722
+ Nothing
723
+ |> config.encodeResponse
724
+ |> Bytes.Encode.encode
725
+
726
+ pageModel : userModel
727
+ pageModel =
728
+ config.init
729
+ Pages.Flags.PreRenderFlags
730
+ justSharedData
731
+ dataThing
732
+ Nothing
733
+ (Just
734
+ { path =
735
+ { path = currentPage.path
736
+ , query = Nothing
737
+ , fragment = Nothing
738
+ }
739
+ , metadata = currentPage.route
740
+ , pageUrl = Nothing
741
+ }
742
+ )
743
+ |> Tuple.first
744
+
745
+ currentPage : { path : Path, route : route }
746
+ currentPage =
747
+ { path = serverRequestPayload.path, route = urlToRoute config currentUrl }
748
+
749
+ viewValue : { title : String, body : List (Html (PagesMsg userMsg)) }
750
+ viewValue =
751
+ (config.view Dict.empty Dict.empty Nothing currentPage Nothing justSharedData dataThing Nothing |> .view)
752
+ pageModel
753
+ in
754
+ { route = Path.toAbsolute currentPage.path
755
+ , contentJson = Dict.empty
756
+ , html = viewValue.body |> bodyToString
757
+ , errors = []
758
+ , head = [] -- TODO render head tags --config.view Dict.empty Dict.empty Nothing pathAndRoute Nothing justSharedData pageData Nothing |> .head
759
+ , title = viewValue.title
760
+ , staticHttpCache = Dict.empty
761
+ , is404 = False
762
+ , statusCode = statusCode
763
+ , headers = []
764
+ }
765
+ |> ToJsPayload.PageProgress
766
+ |> Effect.SendSinglePageNew byteEncodedPageData
767
+ |> BackendTask.succeed
768
+ )
769
+ )
794
770
 
795
- Nothing ->
771
+ Just notFoundReason ->
796
772
  render404Page config
797
- -- TODO do I need sharedDataResult here?
798
773
  Nothing
774
+ -- TODO do I need sharedDataResult?
775
+ --(Result.toMaybe sharedDataResult)
799
776
  isDevServer
800
- (Path.fromString path)
801
- NotFoundReason.NoMatchingRoute
802
- --Err error ->
803
- -- [ error ]
804
- -- |> ToJsPayload.Errors
805
- -- |> Effect.SendSinglePage
777
+ serverRequestPayload.path
778
+ notFoundReason
779
+ |> BackendTask.succeed
806
780
  )
807
- (apiHandler.matchesToResponse
808
- (renderRequest
809
- |> RenderRequest.maybeRequestPayload
810
- |> Maybe.withDefault Json.Encode.null
811
- )
812
- path
813
- )
814
- globalHeadTags
815
-
816
- RenderRequest.NotFound notFoundPath ->
817
- (BackendTask.map2
818
- (\_ _ ->
819
- render404Page config
820
- Nothing
821
- --(Result.toMaybe sharedDataResult)
822
- --model
823
- isDevServer
824
- notFoundPath
825
- NotFoundReason.NoMatchingRoute
781
+
782
+ RenderRequest.Api ( path, ApiRoute apiHandler ) ->
783
+ BackendTask.map2
784
+ (\response _ ->
785
+ case response of
786
+ Just okResponse ->
787
+ { body = okResponse
788
+ , staticHttpCache = Dict.empty -- TODO do I need to serialize the full cache here, or can I handle that from the JS side?
789
+ , statusCode = 200
790
+ }
791
+ |> ToJsPayload.SendApiResponse
792
+ |> Effect.SendSinglePage
793
+
794
+ Nothing ->
795
+ render404Page config
796
+ -- TODO do I need sharedDataResult here?
797
+ Nothing
798
+ isDevServer
799
+ (Path.fromString path)
800
+ NotFoundReason.NoMatchingRoute
801
+ --Err error ->
802
+ -- [ error ]
803
+ -- |> ToJsPayload.Errors
804
+ -- |> Effect.SendSinglePage
805
+ )
806
+ (apiHandler.matchesToResponse
807
+ (renderRequest
808
+ |> RenderRequest.maybeRequestPayload
809
+ |> Maybe.withDefault Json.Encode.null
826
810
  )
827
- (BackendTask.succeed [])
828
- globalHeadTags
829
- -- TODO is there a way to resolve sharedData but get it as a Result if it fails?
830
- --config.sharedData
811
+ path
831
812
  )
832
- )
813
+ globalHeadTags
814
+
815
+ RenderRequest.NotFound notFoundPath ->
816
+ (BackendTask.map2
817
+ (\_ _ ->
818
+ render404Page config
819
+ Nothing
820
+ --(Result.toMaybe sharedDataResult)
821
+ --model
822
+ isDevServer
823
+ notFoundPath
824
+ NotFoundReason.NoMatchingRoute
825
+ )
826
+ (BackendTask.succeed [])
827
+ globalHeadTags
828
+ -- TODO is there a way to resolve sharedData but get it as a Result if it fails?
829
+ --config.sharedData
830
+ )
833
831
 
834
832
  initialModel : Model route
835
833
  initialModel =