Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 6 additions & 1 deletion src/observers/appearance_observer.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@ export class AppearanceObserver {
constructor(delegate, element) {
this.delegate = delegate
this.element = element
this.intersectionObserver = new IntersectionObserver(this.intersect)
}

start() {
if (!this.started) {
this.started = true

if (!this.intersectionObserver) {
const rootMargin = this.element.getAttribute("data-turbo-lazy-root-margin") || "0px"
this.intersectionObserver = new IntersectionObserver(this.intersect, { rootMargin })
}

this.intersectionObserver.observe(this.element)
}
}
Expand Down
24 changes: 24 additions & 0 deletions src/tests/fixtures/frame_navigation_lazy.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Turbo</title>
<link rel="icon" href="data:image/x-icon;base64,AA">
<script src="/dist/turbo.es2017-umd.js" data-turbo-track="reload"></script>
<script src="/src/tests/fixtures/test.js"></script>
</head>
<body>
<div id="container">
<h1>Frame lazy test</h1>

<div style="height: calc(100vh*2);"></div>
<div id="load-frame-trigger"></div>
<div style="height: 100px;"></div>

<turbo-frame id="eager-loaded-frame" src="/src/tests/fixtures/frames/frame_for_eager.html" loading="lazy"
data-turbo-lazy-root-margin="100px">
<h2 style="margin: 0">Eager-loaded frame: Not Loaded</h2>
</turbo-frame>
</div>
</body>
</html>
11 changes: 11 additions & 0 deletions src/tests/functional/frame_navigation_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,17 @@ test("lazy-loaded frame promotes navigation", async ({ page }) => {
assert.equal(pathname(page.url()), "/src/tests/fixtures/frames/frame_for_eager.html")
})

test("lazy-loading frame with root margin", async ({ page }) => {
await page.goto("/src/tests/fixtures/frame_navigation_lazy.html")

assert.equal(await page.textContent("#eager-loaded-frame h2"), "Eager-loaded frame: Not Loaded")

await page.locator("#load-frame-trigger").evaluate(el => el.scrollIntoView(false))
await nextEventOnTarget(page, "eager-loaded-frame", "turbo:frame-load")

assert.equal(await page.textContent("#eager-loaded-frame h2"), "Eager-loaded frame: Loaded")
})

test("promoted frame navigation updates the URL before rendering", async ({ page }) => {
await page.goto("/src/tests/fixtures/tabs.html")

Expand Down