-
Notifications
You must be signed in to change notification settings - Fork 432
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pre populate cache with views based on links with
turbo_preload
att…
…ribute (#552) * (feat) add preloader * (internal) add test stub * (internal) restructure preloading into the view * (fix) still handle cold boots * (feat) add some test cases * (fix) tweak tests so I can atleast run them (still failing) * (feat) be asyncy awaity * refine anchor type earlier so we know it will have a href attr * change attribute from data-turbo-preload to rel='preload' * feat almost working tests * (fix) revert to turbo_preload * (fix) properly handle preloader start * simplify element selector * (fix) hopefully test suite is now green * (fix) oops * make sure we wait sufficiently long enough for preloading to happen
- Loading branch information
Showing
11 changed files
with
183 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { Navigator } from "./navigator" | ||
import { PageSnapshot } from "./page_snapshot" | ||
import { SnapshotCache } from "./snapshot_cache" | ||
|
||
export interface PreloaderDelegate { | ||
readonly navigator: Navigator | ||
} | ||
|
||
export class Preloader { | ||
readonly delegate: PreloaderDelegate | ||
readonly selector: string = 'a[data-turbo-preload]' | ||
|
||
constructor(delegate: PreloaderDelegate) { | ||
this.delegate = delegate | ||
} | ||
|
||
get snapshotCache(): SnapshotCache { | ||
return this.delegate.navigator.view.snapshotCache | ||
} | ||
|
||
start() { | ||
if (document.readyState === 'loading') { | ||
return document.addEventListener('DOMContentLoaded', () => { | ||
this.preloadOnLoadLinksForView(document.body) | ||
}); | ||
} else { | ||
this.preloadOnLoadLinksForView(document.body) | ||
} | ||
} | ||
|
||
preloadOnLoadLinksForView(element: Element) { | ||
for (const link of element.querySelectorAll<HTMLAnchorElement>(this.selector)) { | ||
this.preloadURL(link) | ||
} | ||
} | ||
|
||
async preloadURL(link: HTMLAnchorElement) { | ||
const location = new URL(link.href) | ||
|
||
if (this.snapshotCache.has(location)) { | ||
return | ||
} | ||
|
||
try { | ||
const response = await fetch(location.toString(), { headers: { 'VND.PREFETCH': 'true', 'Accept': 'text/html' } }) | ||
const responseText = await response.text() | ||
const snapshot = PageSnapshot.fromHTMLString(responseText) | ||
|
||
this.snapshotCache.put(location, snapshot) | ||
} catch(_) { | ||
// If we cannot preload that is ok! | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
|
||
<head> | ||
<meta charset="utf-8"> | ||
<title>Page With Preloading Frame</title> | ||
<script src="/dist/turbo.es2017-umd.js" data-turbo-track="reload"></script> | ||
</head> | ||
|
||
<body> | ||
<turbo-frame id="menu" src="/src/tests/fixtures/frames/preloading.html"></turbo-frame> | ||
</body> | ||
|
||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<turbo-frame id="menu"> | ||
<a href="/src/tests/fixtures/preloaded.html" id="frame_preload_anchor" data-turbo-preload="true">Visit preloaded | ||
page</a> | ||
</turbo-frame> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
|
||
<head> | ||
<meta charset="utf-8"> | ||
<title>Page That Links to Preloading Page</title> | ||
<script src="/dist/turbo.es2017-umd.js" data-turbo-track="reload"></script> | ||
</head> | ||
|
||
<body> | ||
<a href="/src/tests/fixtures/preloading.html" id="hot_preload_anchor">Next page has preloading</a> | ||
</body> | ||
|
||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
|
||
<head> | ||
<meta charset="utf-8"> | ||
<title>Preloaded Page</title> | ||
<script src="/dist/turbo.es2017-umd.js" data-turbo-track="reload"></script> | ||
</head> | ||
|
||
<body> | ||
<div> | ||
This page was hopefully preloaded | ||
</div> | ||
</body> | ||
|
||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
|
||
<head> | ||
<meta charset="utf-8"> | ||
<title>Preloading Page</title> | ||
<script src="/dist/turbo.es2017-umd.js" data-turbo-track="reload"></script> | ||
</head> | ||
|
||
<body> | ||
<a href="/src/tests/fixtures/preloaded.html" id="preload_anchor" data-turbo-preload="true"> | ||
Visit preloaded page | ||
</a> | ||
</body> | ||
|
||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { TurboDriveTestCase } from "../helpers/turbo_drive_test_case" | ||
|
||
export class PreloaderTests extends TurboDriveTestCase { | ||
async "test preloads snapshot on initial load"() { | ||
// contains `a[rel="preload"][href="http://localhost:9000/src/tests/fixtures/preloaded.html"]` | ||
await this.goToLocation("/src/tests/fixtures/preloading.html") | ||
await this.nextBeat | ||
|
||
this.assert.ok(await this.remote.execute(() => { | ||
const preloadedUrl = "http://localhost:9000/src/tests/fixtures/preloaded.html" | ||
const cache = window.Turbo.session.preloader.snapshotCache.snapshots | ||
|
||
return preloadedUrl in cache | ||
})) | ||
} | ||
|
||
async "test preloads snapshot on page visit"() { | ||
// contains `a[rel="preload"][href="http://localhost:9000/src/tests/fixtures/preloading.html"]` | ||
await this.goToLocation("/src/tests/fixtures/hot_preloading.html") | ||
|
||
// contains `a[rel="preload"][href="http://localhost:9000/src/tests/fixtures/preloaded.html"]` | ||
await this.clickSelector("#hot_preload_anchor") | ||
await this.waitUntilSelector("#preload_anchor") | ||
await this.nextBeat | ||
|
||
this.assert.ok(await this.remote.execute(() => { | ||
const preloadedUrl = "http://localhost:9000/src/tests/fixtures/preloaded.html" | ||
const cache = window.Turbo.session.preloader.snapshotCache.snapshots | ||
|
||
return preloadedUrl in cache | ||
})) | ||
} | ||
|
||
async "test navigates to preloaded snapshot from frame"() { | ||
// contains `a[rel="preload"][href="http://localhost:9000/src/tests/fixtures/preloaded.html"]` | ||
await this.goToLocation("/src/tests/fixtures/frame_preloading.html") | ||
await this.waitUntilSelector("#frame_preload_anchor") | ||
await this.nextBeat | ||
|
||
this.assert.ok(await this.remote.execute(() => { | ||
const preloadedUrl = "http://localhost:9000/src/tests/fixtures/preloaded.html" | ||
const cache = window.Turbo.session.preloader.snapshotCache.snapshots | ||
|
||
return preloadedUrl in cache | ||
})) | ||
} | ||
} | ||
|
||
PreloaderTests.registerSuite() |