diff --git a/index.js b/index.js index f89b1338..67601391 100644 --- a/index.js +++ b/index.js @@ -7,10 +7,13 @@ module.exports = function pagesInit( let prefetchedPages = [window.location.pathname]; document.addEventListener("DOMContentLoaded", function() { + httpGet(`${window.location.origin}${window.location.pathname}/content.json`, function (/** @type JSON */ contentJson) { + let app = mainElmModule.init({ flags: { secrets: null, isPrerendering: navigator.userAgent.indexOf("Headless") >= 0, + contentJson } }); @@ -34,6 +37,9 @@ module.exports = function pagesInit( document.dispatchEvent(new Event("prerender-trigger")); }); + + }) + }); function setupLinkPrefetching() { @@ -131,3 +137,14 @@ module.exports = function pagesInit( document.getElementsByTagName("head")[0].appendChild(meta); } }; + +function httpGet(/** @type string */ theUrl, /** @type Function */ callback) +{ + var xmlHttp = new XMLHttpRequest(); + xmlHttp.onreadystatechange = function() { + if (xmlHttp.readyState == 4 && xmlHttp.status == 200) + callback(JSON.parse(xmlHttp.responseText)); + } + xmlHttp.open("GET", theUrl, true); // true for asynchronous + xmlHttp.send(null); +} diff --git a/src/Pages/ContentCache.elm b/src/Pages/ContentCache.elm index cfced161..826cb044 100644 --- a/src/Pages/ContentCache.elm +++ b/src/Pages/ContentCache.elm @@ -111,9 +111,10 @@ pagesWithErrors cache = init : Document metadata view -> Content + -> Maybe (ContentJson String) -> ContentCache metadata view -init document content = - parseMetadata document content +init document content maybeInitialPageContent = + parseMetadata maybeInitialPageContent document content |> List.map (\tuple -> Tuple.mapSecond @@ -149,39 +150,44 @@ createBuildError path decodeError = parseMetadata : - Document metadata view + Maybe (ContentJson String) + -> Document metadata view -> List ( List String, { extension : String, frontMatter : String, body : Maybe String } ) -> List ( List String, Result String (Entry metadata view) ) -parseMetadata document content = +parseMetadata maybeInitialPageContent document content = content |> List.map - (Tuple.mapSecond - (\{ frontMatter, extension, body } -> - let - maybeDocumentEntry = - Document.get extension document - in - case maybeDocumentEntry of - Just documentEntry -> - frontMatter - |> documentEntry.frontmatterParser - |> Result.map - (\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 - ) + (\( path, { frontMatter, extension, body } ) -> + let + maybeDocumentEntry = + Document.get extension document + in + case maybeDocumentEntry of + Just documentEntry -> + frontMatter + |> documentEntry.frontmatterParser + |> Result.map + (\metadata -> + let + renderer = + \value -> + parseContent extension value document + in + case maybeInitialPageContent of + Just initialPageContent -> + Parsed metadata + { body = renderer initialPageContent.body + , staticData = initialPageContent.staticData + } - Nothing -> - Err ("Could not find extension '" ++ extension ++ "'") - ) + Nothing -> + NeedContent extension metadata + ) + |> Tuple.pair path + + Nothing -> + Err ("Could not find extension '" ++ extension ++ "'") + |> Tuple.pair path ) @@ -327,8 +333,8 @@ lazyLoad document url cacheResult = |> Task.map (\downloadedContent -> update cacheResult - (\thing -> - parseContent extension thing document + (\value -> + parseContent extension value document ) url downloadedContent diff --git a/src/Pages/Internal/Platform.elm b/src/Pages/Internal/Platform.elm index 575c6055..84672ec7 100644 --- a/src/Pages/Internal/Platform.elm +++ b/src/Pages/Internal/Platform.elm @@ -217,6 +217,12 @@ type alias Flags = Decode.Value +type alias ContentJson = + { body : String + , staticData : Dict String String + } + + init : pathKey -> String @@ -249,7 +255,18 @@ init : init pathKey canonicalSiteUrl document toJsPort viewFn content initUserModel flags url key = let contentCache = - ContentCache.init document content + ContentCache.init document content contentJson + + contentJson = + flags + |> Decode.decodeValue (Decode.field "contentJson" contentJsonDecoder) + |> Result.toMaybe + + contentJsonDecoder : Decode.Decoder ContentJson + contentJsonDecoder = + Decode.map2 ContentJson + (Decode.field "body" Decode.string) + (Decode.field "staticData" (Decode.dict Decode.string)) in case contentCache of Ok okCache -> diff --git a/src/Pages/Internal/Platform/Cli.elm b/src/Pages/Internal/Platform/Cli.elm index ba87f3fb..a0e74544 100644 --- a/src/Pages/Internal/Platform/Cli.elm +++ b/src/Pages/Internal/Platform/Cli.elm @@ -160,7 +160,7 @@ cliApplication : cliApplication cliMsgConstructor narrowMsg toModel fromModel config = let contentCache = - ContentCache.init config.document config.content + ContentCache.init config.document config.content Nothing siteMetadata = contentCache