Merge pull request #60 from dillonkearns/dont-preload-unknown-paths

Don't prefetch unknown paths.
This commit is contained in:
Dillon Kearns 2020-01-30 09:40:40 -08:00 committed by GitHub
commit 04236bf2d4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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(( app.ports.toJsPort.subscribe((
/** @type { HeadTag[] } headTags */ headTags /** @type { { head: HeadTag[], allRoutes: string[] } } */ fromElm
) => { ) => {
appendTag({ appendTag({
name: "meta", name: "meta",
@ -37,9 +37,12 @@ function loadContentAndInitializeApp(/** @type { init: any } */ mainElmModule)
["content", `elm-pages v${elmPagesVersion}`] ["content", `elm-pages v${elmPagesVersion}`]
] ]
}); });
console.log('allRoutes', fromElm.allRoutes);
window.allRoutes = fromElm.allRoutes;
if (navigator.userAgent.indexOf("Headless") >= 0) { if (navigator.userAgent.indexOf("Headless") >= 0) {
headTags.forEach(headTag => { fromElm.head.forEach(headTag => {
appendTag(headTag); appendTag(headTag);
}); });
} else { } else {
@ -54,7 +57,7 @@ function loadContentAndInitializeApp(/** @type { init: any } */ mainElmModule)
}); });
} }
function setupLinkPrefetching() { function setupLinkPrefetching(allRoutes) {
new MutationObserver(observeFirstRender).observe(document.body, { new MutationObserver(observeFirstRender).observe(document.body, {
attributes: true, attributes: true,
childList: true, childList: true,
@ -109,7 +112,7 @@ function setupLinkPrefetchingHelp(
) { ) {
const links = document.querySelectorAll("a"); const links = document.querySelectorAll("a");
links.forEach(link => { links.forEach(link => {
// console.log(link.pathname); console.log(link.pathname);
link.addEventListener("mouseenter", function(event) { link.addEventListener("mouseenter", function(event) {
if ( if (
event && event &&
@ -127,10 +130,13 @@ function setupLinkPrefetchingHelp(
function prefetchIfNeeded(/** @type {HTMLAnchorElement} */ target) { function prefetchIfNeeded(/** @type {HTMLAnchorElement} */ target) {
if (target.host === window.location.host) { if (target.host === window.location.host) {
if (prefetchedPages.includes(target.pathname)) { if (prefetchedPages.includes(target.pathname)) {
// console.log("Already preloaded", target.href); console.log("Already preloaded", target.href);
} else { } else if (!allRoutes.includes(target.pathname)) {
console.log("Not a known route, skipping preload", target.pathname);
}
else {
prefetchedPages.push(target.pathname); prefetchedPages.push(target.pathname);
// console.log("Preloading...", target.pathname); console.log("Preloading...", target.pathname);
const link = document.createElement("link"); const link = document.createElement("link");
link.setAttribute("as", "fetch"); 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 : List String -> String -> String -> List (Head.Tag pathKey) -> Json.Encode.Value
encodeHeads canonicalSiteUrl currentPagePath head = encodeHeads allRoutes canonicalSiteUrl currentPagePath head =
Json.Encode.list (Head.toJson 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 type Msg userMsg metadata view
@ -389,7 +392,8 @@ type Phase
update : update :
String List String
-> String
-> ->
(List ( PagePath pathKey, metadata ) (List ( PagePath pathKey, metadata )
-> ->
@ -416,7 +420,7 @@ update :
-> Msg userMsg metadata view -> Msg userMsg metadata view
-> ModelDetails userModel metadata view -> ModelDetails userModel metadata view
-> ( ModelDetails userModel metadata view, Cmd (AppMsg userMsg 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 case msg of
AppMsg appMsg -> AppMsg appMsg ->
case appMsg of case appMsg of
@ -469,7 +473,7 @@ update canonicalSiteUrl viewFunction pathKey onPageChangeMsg toJsPort document u
headFn pagePath frontmatter viewResult.staticData headFn pagePath frontmatter viewResult.staticData
|> Result.map .head |> Result.map .head
|> Result.toMaybe |> Result.toMaybe
|> Maybe.map (encodeHeads canonicalSiteUrl model.url.path) |> Maybe.map (encodeHeads allRoutes canonicalSiteUrl model.url.path)
|> Maybe.map toJsPort |> Maybe.map toJsPort
ContentCache.NeedContent string metadata -> ContentCache.NeedContent string metadata ->
@ -623,8 +627,14 @@ application config =
noOpUpdate = noOpUpdate =
\userMsg userModel -> \userMsg userModel ->
( userModel, Cmd.none ) ( userModel, Cmd.none )
allRoutes =
config.content
|> List.map Tuple.first
|> List.map (String.join "/")
|> List.map (\route -> "/" ++ route)
in 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.mapFirst Model
|> Tuple.mapSecond (Cmd.map AppMsg) |> Tuple.mapSecond (Cmd.map AppMsg)