Don't prefetch unknown paths.

This commit is contained in:
Dillon Kearns 2020-01-27 21:40:30 -08:00
parent 76e32ade44
commit 88e4a4df3a
2 changed files with 30 additions and 14 deletions

View File

@ -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");

View File

@ -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)