Skip to content

Commit

Permalink
Merge pull request #212 from seanpdoyle/frame-loading-lazy-anchor
Browse files Browse the repository at this point in the history
Navigate a lazy-loaded turbo-frame
  • Loading branch information
sstephenson committed Apr 7, 2021
2 parents 316ddac + 543b528 commit db96ea9
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/core/frames/frame_controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export class FrameController implements AppearanceObserverDelegate, FetchRequest
formSubmission?: FormSubmission
private resolveVisitPromise = () => {}
private connected = false
private hasBeenLoaded = false

constructor(element: FrameElement) {
this.element = element
Expand Down Expand Up @@ -53,7 +54,7 @@ export class FrameController implements AppearanceObserverDelegate, FetchRequest
}

sourceURLChanged() {
if (this.loadingStyle == FrameLoadingStyle.eager) {
if (this.loadingStyle == FrameLoadingStyle.eager || this.hasBeenLoaded) {
this.loadSourceURL()
}
}
Expand All @@ -76,6 +77,7 @@ export class FrameController implements AppearanceObserverDelegate, FetchRequest
this.element.loaded = this.visit(this.sourceURL)
this.appearanceObserver.stop()
await this.element.loaded
this.hasBeenLoaded = true
} catch (error) {
this.currentURL = previousURL
throw error
Expand Down
2 changes: 2 additions & 0 deletions src/tests/fixtures/frames/hello.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
<h1>Form</h1>
<turbo-frame id="hello">
<h2>Hello from a frame</h2>

<a href="/src/tests/fixtures/frames.html">Navigate #hello</a>
</turbo-frame>
</body>
</html>
14 changes: 14 additions & 0 deletions src/tests/functional/loading_tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,20 @@ export class LoadingTests extends TurboDriveTestCase {
this.assert.equal(await contents.getVisibleText(), "Hello from a frame")
}

async "test navigating a visible frame with loading=lazy navigates"() {
await this.clickSelector("#loading-lazy summary")
await this.nextBeat

const initialContents = await this.querySelector("#hello h2")
this.assert.equal(await initialContents.getVisibleText(), "Hello from a frame")

await this.clickSelector("#hello a")
await this.nextBeat

const navigatedContents = await this.querySelector("#hello h2")
this.assert.equal(await navigatedContents.getVisibleText(), "Frames: #hello")
}

async "test changing src attribute on a frame with loading=lazy defers navigation"() {
const frameContents = "#loading-lazy turbo-frame h2"
await this.nextBeat
Expand Down

0 comments on commit db96ea9

Please sign in to comment.