From 88e4a4df3a95e79ba67cc39fc7490743eea226f9 Mon Sep 17 00:00:00 2001 From: Dillon Kearns Date: Mon, 27 Jan 2020 21:40:30 -0800 Subject: [PATCH] Don't prefetch unknown paths. --- index.js | 20 +++++++++++++------- src/Pages/Internal/Platform.elm | 24 +++++++++++++++++------- 2 files changed, 30 insertions(+), 14 deletions(-) diff --git a/index.js b/index.js index a2582bd6..69e01338 100644 --- a/index.js +++ b/index.js @@ -28,7 +28,7 @@ function loadContentAndInitializeApp(/** @type { init: any } */ mainElmModule) }); app.ports.toJsPort.subscribe(( - /** @type { HeadTag[] } headTags */ headTags + /** @type { { head: HeadTag[], allRoutes: string[] } } */ fromElm ) => { appendTag({ name: "meta", @@ -37,9 +37,12 @@ function loadContentAndInitializeApp(/** @type { init: any } */ mainElmModule) ["content", `elm-pages v${elmPagesVersion}`] ] }); + console.log('allRoutes', fromElm.allRoutes); + window.allRoutes = fromElm.allRoutes; + if (navigator.userAgent.indexOf("Headless") >= 0) { - headTags.forEach(headTag => { + fromElm.head.forEach(headTag => { appendTag(headTag); }); } else { @@ -54,7 +57,7 @@ function loadContentAndInitializeApp(/** @type { init: any } */ mainElmModule) }); } -function setupLinkPrefetching() { +function setupLinkPrefetching(allRoutes) { new MutationObserver(observeFirstRender).observe(document.body, { attributes: true, childList: true, @@ -109,7 +112,7 @@ function setupLinkPrefetchingHelp( ) { const links = document.querySelectorAll("a"); links.forEach(link => { - // console.log(link.pathname); + console.log(link.pathname); link.addEventListener("mouseenter", function(event) { if ( event && @@ -127,10 +130,13 @@ function setupLinkPrefetchingHelp( function prefetchIfNeeded(/** @type {HTMLAnchorElement} */ target) { if (target.host === window.location.host) { if (prefetchedPages.includes(target.pathname)) { - // console.log("Already preloaded", target.href); - } else { + console.log("Already preloaded", target.href); + } else if (!allRoutes.includes(target.pathname)) { + console.log("Not a known route, skipping preload", target.pathname); + } + else { prefetchedPages.push(target.pathname); - // console.log("Preloading...", target.pathname); + console.log("Preloading...", target.pathname); const link = document.createElement("link"); link.setAttribute("as", "fetch"); diff --git a/src/Pages/Internal/Platform.elm b/src/Pages/Internal/Platform.elm index 010cae7a..cc757863 100644 --- a/src/Pages/Internal/Platform.elm +++ b/src/Pages/Internal/Platform.elm @@ -351,9 +351,12 @@ init pathKey canonicalSiteUrl document toJsPort viewFn content initUserModel fla ) -encodeHeads : String -> String -> List (Head.Tag pathKey) -> Json.Encode.Value -encodeHeads canonicalSiteUrl currentPagePath head = - Json.Encode.list (Head.toJson canonicalSiteUrl currentPagePath) head +encodeHeads : List String -> String -> String -> List (Head.Tag pathKey) -> Json.Encode.Value +encodeHeads allRoutes canonicalSiteUrl currentPagePath head = + Json.Encode.object + [ ( "head", Json.Encode.list (Head.toJson canonicalSiteUrl currentPagePath) head ) + , ( "allRoutes", Json.Encode.list Json.Encode.string allRoutes ) + ] type Msg userMsg metadata view @@ -389,7 +392,8 @@ type Phase update : - String + List String + -> String -> (List ( PagePath pathKey, metadata ) -> @@ -416,7 +420,7 @@ update : -> Msg userMsg metadata view -> ModelDetails userModel metadata view -> ( ModelDetails userModel metadata view, Cmd (AppMsg userMsg metadata view) ) -update canonicalSiteUrl viewFunction pathKey onPageChangeMsg toJsPort document userUpdate msg model = +update allRoutes canonicalSiteUrl viewFunction pathKey onPageChangeMsg toJsPort document userUpdate msg model = case msg of AppMsg appMsg -> case appMsg of @@ -469,7 +473,7 @@ update canonicalSiteUrl viewFunction pathKey onPageChangeMsg toJsPort document u headFn pagePath frontmatter viewResult.staticData |> Result.map .head |> Result.toMaybe - |> Maybe.map (encodeHeads canonicalSiteUrl model.url.path) + |> Maybe.map (encodeHeads allRoutes canonicalSiteUrl model.url.path) |> Maybe.map toJsPort ContentCache.NeedContent string metadata -> @@ -623,8 +627,14 @@ application config = noOpUpdate = \userMsg userModel -> ( userModel, Cmd.none ) + + allRoutes = + config.content + |> List.map Tuple.first + |> List.map (String.join "/") + |> List.map (\route -> "/" ++ route) in - update config.canonicalSiteUrl config.view config.pathKey config.onPageChange config.toJsPort config.document userUpdate msg model + update allRoutes config.canonicalSiteUrl config.view config.pathKey config.onPageChange config.toJsPort config.document userUpdate msg model |> Tuple.mapFirst Model |> Tuple.mapSecond (Cmd.map AppMsg)