Skip to content

Commit

Permalink
Initial prototype to fetch static data.
Browse files Browse the repository at this point in the history
  • Loading branch information
dillonkearns committed Oct 16, 2019
1 parent e46c1fa commit 40089da
Show file tree
Hide file tree
Showing 10 changed files with 108 additions and 67 deletions.
64 changes: 47 additions & 17 deletions elm-package/src/Pages/ContentCache.elm
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import Dict exposing (Dict)
import Html exposing (Html)
import Html.Attributes as Attr
import Http
import Json.Decode
import Json.Decode as Decode
import Mark
import Mark.Error
import Pages.Document as Document exposing (Document)
Expand Down Expand Up @@ -48,9 +48,9 @@ type alias ContentCacheInner metadata view =

type Entry metadata view
= NeedContent String metadata
| Unparsed String metadata String
| Unparsed String metadata (ContentJson String)
-- TODO need to have an UnparsedMarkup entry type so the right parser is applied
| Parsed metadata (Result ParseError view)
| Parsed metadata (ContentJson (Result ParseError view))


type alias ParseError =
Expand Down Expand Up @@ -91,8 +91,13 @@ pagesWithErrors cache =
|> List.filterMap
(\( path, value ) ->
case value of
Parsed metadata (Err parseError) ->
Just ( path, parseError )
Parsed metadata { body } ->
case body of
Err parseError ->
Just ( path, parseError )

_ ->
Nothing

_ ->
Nothing
Expand Down Expand Up @@ -151,13 +156,16 @@ parseMetadata document content =
|> documentEntry.frontmatterParser
|> Result.map
(\metadata ->
case body of
Just presentBody ->
Parsed metadata
(parseContent extension presentBody document)

Nothing ->
NeedContent extension metadata
-- TODO do I need to handle this case?
-- case body of
-- Just presentBody ->
-- Parsed metadata
-- { body = parseContent extension presentBody document
-- , staticData = ""
-- }
--
-- Nothing ->
NeedContent extension metadata
)

Nothing ->
Expand Down Expand Up @@ -330,14 +338,15 @@ lazyLoad document url cacheResult =
Task.succeed cacheResult


httpTask : Url -> Task Http.Error (ContentJson String)
httpTask url =
Http.task
{ method = "GET"
, headers = []
, url =
Url.Builder.absolute
((url.path |> String.split "/" |> List.filter (not << String.isEmpty))
++ [ "content.txt"
++ [ "content.json"
]
)
[]
Expand All @@ -359,17 +368,32 @@ httpTask url =
Err (Http.BadStatus metadata.statusCode)

Http.GoodStatus_ metadata body ->
Ok body
body
|> Decode.decodeString contentJsonDecoder
|> Result.mapError (\err -> Http.BadBody (Decode.errorToString err))
)
, timeout = Nothing
}


type alias ContentJson body =
{ body : body
, staticData : Decode.Value
}


contentJsonDecoder : Decode.Decoder (ContentJson String)
contentJsonDecoder =
Decode.map2 ContentJson
(Decode.field "body" Decode.string)
(Decode.field "staticData" Decode.value)


update :
ContentCache metadata view
-> (String -> Result ParseError view)
-> Url
-> String
-> ContentJson String
-> ContentCache metadata view
update cacheResult renderer url rawContent =
case cacheResult of
Expand All @@ -381,11 +405,17 @@ update cacheResult renderer url rawContent =
entry

Just (Unparsed extension metadata content) ->
Parsed metadata (renderer content)
Parsed metadata
{ body = renderer content.body
, staticData = content.staticData
}
|> Just

Just (NeedContent extension metadata) ->
Parsed metadata (renderer rawContent)
Parsed metadata
{ body = renderer rawContent.body
, staticData = rawContent.staticData
}
|> Just

Nothing ->
Expand Down
67 changes: 31 additions & 36 deletions elm-package/src/Pages/Platform.elm
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import Head
import Html exposing (Html)
import Html.Attributes
import Http
import Json.Decode
import Json.Decode as Decode
import Json.Encode
import List.Extra
import Mark
Expand Down Expand Up @@ -55,7 +55,7 @@ mainView :
}
->
( StaticHttp.Request
, String
, Decode.Value
->
Result String
{ view :
Expand Down Expand Up @@ -102,7 +102,7 @@ pageViewOrError :
}
->
( StaticHttp.Request
, String
, Decode.Value
->
Result String
{ view :
Expand Down Expand Up @@ -138,19 +138,9 @@ pageViewOrError pathKey viewFn model cache =
{ path = pagePath, frontmatter = metadata }
|> Tuple.second
)
dummyInputString

-- pageView =
-- viewFn
-- (cache
-- |> Result.map (ContentCache.extractMetadata pathKey)
-- |> Result.withDefault []
-- -- TODO handle error better
-- )
-- { path = pagePath, frontmatter = metadata }
-- |> .view
viewResult.staticData
in
case viewResult of
case viewResult.body of
Ok viewList ->
case viewFnResult of
Ok okViewFn ->
Expand Down Expand Up @@ -199,7 +189,7 @@ view :
}
->
( StaticHttp.Request
, String
, Decode.Value
->
Result String
{ view :
Expand Down Expand Up @@ -275,7 +265,7 @@ init :
}
->
( StaticHttp.Request
, String
, Decode.Value
->
Result String
{ view :
Expand Down Expand Up @@ -311,33 +301,38 @@ init pathKey canonicalSiteUrl document toJsPort viewFn content initUserModel fla
( Just pagePath, Just frontmatter ) ->
let
headFnResult =
(viewFn
viewFn
(ContentCache.extractMetadata pathKey okCache)
{ path = pagePath
, frontmatter = frontmatter
}
|> Tuple.second
)
""" 123456789 """

-- """ 123456789 """
-- "asdfasdf"
-- |> .head
in
case headFnResult |> Result.map .head of
Ok head ->
Cmd.batch
[ head
|> encodeHeads canonicalSiteUrl url.path
|> toJsPort
, userCmd |> Cmd.map UserMsg
, contentCache
|> ContentCache.lazyLoad document url
|> Task.attempt UpdateCache
]

Err error ->
Debug.todo error
Cmd.batch
[ userCmd |> Cmd.map UserMsg
, contentCache
|> ContentCache.lazyLoad document url
|> Task.attempt UpdateCache
]

-- case headFnResult |> Result.map .head of
-- Ok head ->
-- Cmd.batch
-- [ head
-- |> encodeHeads canonicalSiteUrl url.path
-- |> toJsPort
-- , userCmd |> Cmd.map UserMsg
-- , contentCache
-- |> ContentCache.lazyLoad document url
-- |> Task.attempt UpdateCache
-- ]
--
-- Err error ->
-- Debug.todo error
-- Cmd.none
_ ->
Cmd.none
Expand Down Expand Up @@ -493,7 +488,7 @@ application :
}
->
( StaticHttp.Request
, String
, Decode.Value
->
Result String
{ view :
Expand Down Expand Up @@ -565,7 +560,7 @@ cliApplication :
}
->
( StaticHttp.Request
, String
, Decode.Value
->
Result String
{ view :
Expand Down
2 changes: 1 addition & 1 deletion examples/docs/src/Main.elm
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ view :
}
->
( StaticHttp.Request
, String
, Decode.Value
->
Result String
{ view :
Expand Down
10 changes: 7 additions & 3 deletions generator/src/add-files-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,16 @@ module.exports = class AddFilesPlugin {
// but I found the example code for it here:
// https://github.com/jantimon/html-webpack-plugin/blob/35a154186501fba3ecddb819b6f632556d37a58f/index.js#L470-L478

const filename = path.join(file.baseRoute, "content.txt");
const filename = path.join(file.baseRoute, "content.json");
compilation.fileDependencies.add(filename);
const rawContents = JSON.stringify({
body: file.content,
staticData: { stargazers_count: 85 }
});

compilation.assets[filename] = {
source: () => file.content,
size: () => file.content.length
source: () => rawContents,
size: () => rawContents.length
};
});
});
Expand Down
4 changes: 2 additions & 2 deletions generator/src/elm-file-constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ application :
}
->
( StaticHttp.Request
, String
, Json.Decode.Value
->
Result String
{ view :
Expand Down Expand Up @@ -201,7 +201,7 @@ application :
}
->
( StaticHttp.Request
, String
, Json.Decode.Value
->
Result String
{ view :
Expand Down
13 changes: 12 additions & 1 deletion generator/src/generate-raw-content.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = function(markdown, markup, includeBody) {
return `content : List ( List String, { extension: String, frontMatter : String, body : Maybe String } )
return `content : List ( List String, { extension: String, frontMatter : String, body : Maybe String, staticData : Maybe String } )
content =
[ ${markdown.concat(markup).map(entry => toEntry(entry, includeBody))}
]`;
Expand All @@ -17,6 +17,7 @@ function toEntry(entry, includeBody) {
( [${fullPath.join(", ")}]
, { frontMatter = """${entry.metadata}
""" , body = ${body(entry, includeBody)}
, staticData = ${staticData(entry, includeBody)}
, extension = "${entry.extension}"
} )
`;
Expand All @@ -31,3 +32,13 @@ function body(entry, includeBody) {
return `Nothing`;
}
}

function staticData(entry, includeBody) {
if (includeBody) {
return `Just """${entry.staticData.replace(/\\/g, "\\\\")}
"""
`;
} else {
return `Nothing`;
}
}
7 changes: 4 additions & 3 deletions generator/src/service-worker-template.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ workbox.core.skipWaiting();
workbox.core.clientsClaim();
workbox.precaching.precacheAndRoute(self.__precacheManifest);
workbox.routing.registerNavigationRoute(
workbox.precaching.getCacheKeyForURL("/index.html"), {
workbox.precaching.getCacheKeyForURL("/index.html"),
{
blacklist: [/admin/]
}
);
Expand Down Expand Up @@ -54,8 +55,8 @@ self.addEventListener("install", event => {
// TODO store content in a cache that is hashed based on the webpack bundle hash,
// then delete the old cache on activate
const contentUrls = [
// "/blog/content.txt",
// "/blog/types-over-conventions/content.txt"
// "/blog/content.json",
// "/blog/types-over-conventions/content.json"
];
const coreUrls = ["/main.js"];
const preloadContent = caches
Expand Down
2 changes: 1 addition & 1 deletion generator/src/template.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="preload" href="content.txt" as="fetch" crossorigin />
<link rel="preload" href="content.json" as="fetch" crossorigin />
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<script>
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ module.exports = function pagesInit(
link.setAttribute("as", "fetch");

link.setAttribute("rel", "prefetch");
link.setAttribute("href", origin + target.pathname + "/content.txt");
link.setAttribute("href", origin + target.pathname + "/content.json");
document.head.appendChild(link);
}
}
Expand Down

0 comments on commit 40089da

Please sign in to comment.