Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pre populate cache with views based on links with turbo_preload attribute #552

Merged
merged 16 commits into from
Jun 18, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat almost working tests
  • Loading branch information
hey-leon committed May 4, 2022
commit 9c62cf3d2255843fceff44287e2287d02085f23e
10 changes: 5 additions & 5 deletions src/core/drive/preloader.ts
Original file line number Diff line number Diff line change
@@ -8,20 +8,20 @@ export interface PreloaderDelegate {

export class Preloader {
readonly delegate: PreloaderDelegate
readonly element: Element
readonly selector: string = 'a[rel="preload"], a[rel="prefetch"]'
readonly selector: string = 'a[rel="preload"], a[rel="prefetch"], a[rel="next"]'

constructor(delegate: PreloaderDelegate, element: Element) {
constructor(delegate: PreloaderDelegate) {
this.delegate = delegate
this.element = element
}

get snapshotCache(): SnapshotCache {
return this.delegate.navigator.view.snapshotCache
}

start() {
this.preloadOnLoadLinksForView(this.element)
document.addEventListener("DOMContentLoaded", () => {
this.preloadOnLoadLinksForView(document.body)
})
}

preloadOnLoadLinksForView(element: Element) {
2 changes: 1 addition & 1 deletion src/core/session.ts
Original file line number Diff line number Diff line change
@@ -34,7 +34,7 @@ export class Session
{
readonly navigator = new Navigator(this)
readonly history = new History(this)
readonly preloader = new Preloader(this, document.body)
readonly preloader = new Preloader(this)
readonly view = new PageView(this, document.documentElement)
adapter: Adapter = new BrowserAdapter(this)

1 change: 1 addition & 0 deletions src/tests/fixtures/hot_preloading.html
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@
<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>
5 changes: 4 additions & 1 deletion src/tests/fixtures/preloaded.html
Original file line number Diff line number Diff line change
@@ -4,10 +4,13 @@
<head>
<meta charset="utf-8">
<title>Preloaded Page</title>
<script src="/dist/turbo.es2017-umd.js" data-turbo-track="reload"></script>
</head>

<body>
This page was hopefully preloaded
<div>
This page was hopefully preloaded
</div>
</body>

</html>
5 changes: 4 additions & 1 deletion src/tests/fixtures/preloading.html
Original file line number Diff line number Diff line change
@@ -4,10 +4,13 @@
<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" rel="preload">Visit preloaded page</a>
<a href="/src/tests/fixtures/preloaded.html" id="preload_anchor" rel="preload">
Visit preloaded page
</a>
</body>

</html>
29 changes: 20 additions & 9 deletions src/tests/functional/preloader_tests.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,34 @@
import { TurboDriveTestCase } from "../helpers/turbo_drive_test_case"

export class PreloaderTests extends TurboDriveTestCase {
async "test navigates to preloaded snapshot from cold start"() {
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.clickSelector("#preload_anchor")
this.assert.ok(await this.attributeForSelector("html", "data-turbo-preview"))

this.assert.ok(await this.remote.execute(() =>
"http://localhost:9000/src/tests/fixtures/preloaded.html" in window.Turbo.session.preloader.snapshotCache.snapshots
))
}

async "test navigates to preloaded snapshot from hot navigation"() {
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.clickSelector("#preload_anchor")
this.assert.ok(await this.attributeForSelector("html", "data-turbo-preview"))

this.assert.ok(await this.remote.execute(() =>
"http://localhost:9000/src/tests/fixtures/preloaded.html" in window.Turbo.session.preloader.snapshotCache.snapshots
))
}

async "test navigates to preloaded snapshot from eager frame"() {
await this.goToLocation("/src/tests/fixtures/frame_preloading.html")
await this.clickSelector("#frame_preload_anchor")
this.assert.ok(await this.attributeForSelector("html", "data-turbo-preview"))
// contains `a[rel="preload"][href="http://localhost:9000/src/tests/fixtures/preloaded.html"]`
await this.goToLocation("/src/tests/fixtures/preloading.html")

this.assert.ok(await this.remote.execute(() =>
"http://localhost:9000/src/tests/fixtures/preloaded.html" in window.Turbo.session.preloader.snapshotCache.snapshots
))
}
}