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

Dispatch visit events on the initiator link or form #695

Merged
merged 6 commits into from
Sep 13, 2022
Merged
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
4 changes: 2 additions & 2 deletions src/core/drive/navigator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { Visit, VisitDelegate, VisitOptions } from "./visit"
import { PageSnapshot } from "./page_snapshot"

export type NavigatorDelegate = VisitDelegate & {
allowsVisitingLocationWithAction(location: URL, action?: Action): boolean
allowsVisitingLocation(location: URL, options: Partial<VisitOptions>): boolean
visitProposedToLocation(location: URL, options: Partial<VisitOptions>): Promise<void>
notifyApplicationAfterVisitingSamePageLocation(oldURL: URL, newURL: URL): void
}
Expand All @@ -24,7 +24,7 @@ export class Navigator {
}

proposeVisit(location: URL, options: Partial<VisitOptions> = {}) {
if (this.delegate.allowsVisitingLocationWithAction(location, options.action)) {
if (this.delegate.allowsVisitingLocation(location, options)) {
if (locationIsVisitable(location, this.view.snapshot.rootLocation)) {
return this.delegate.visitProposedToLocation(location, options)
} else {
Expand Down
7 changes: 6 additions & 1 deletion src/core/drive/visit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export type VisitOptions = {
shouldCacheSnapshot: boolean
frame?: string
acceptsStreamResponse: boolean
initiator: Element
}

const defaultOptions: VisitOptions = {
Expand All @@ -61,6 +62,7 @@ const defaultOptions: VisitOptions = {
updateHistory: true,
shouldCacheSnapshot: true,
acceptsStreamResponse: false,
initiator: document.documentElement,
}

export type VisitResponse = {
Expand All @@ -86,6 +88,7 @@ export class Visit implements FetchRequestDelegate {
readonly willRender: boolean
readonly updateHistory: boolean
readonly promise: Promise<void>
readonly initiator: Element

private resolvingFunctions!: ResolvingFunctions<void>

Expand Down Expand Up @@ -126,6 +129,7 @@ export class Visit implements FetchRequestDelegate {
updateHistory,
shouldCacheSnapshot,
acceptsStreamResponse,
initiator,
} = {
...defaultOptions,
...options,
Expand All @@ -142,6 +146,7 @@ export class Visit implements FetchRequestDelegate {
this.scrolled = !willRender
this.shouldCacheSnapshot = shouldCacheSnapshot
this.acceptsStreamResponse = acceptsStreamResponse
this.initiator = initiator
}

get adapter() {
Expand Down Expand Up @@ -222,7 +227,7 @@ export class Visit implements FetchRequestDelegate {
if (this.hasPreloadedResponse()) {
this.simulateRequest()
} else if (this.shouldIssueRequest() && !this.request) {
this.request = new FetchRequest(this, FetchMethod.get, this.location)
this.request = new FetchRequest(this, FetchMethod.get, this.location, undefined, this.initiator)
this.request.perform()
}
}
Expand Down
25 changes: 16 additions & 9 deletions src/core/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,13 +195,16 @@ export class Session
const action = this.getActionForLink(link)
const acceptsStreamResponse = link.hasAttribute("data-turbo-stream")

this.visit(location.href, { action, acceptsStreamResponse })
this.visit(location.href, { action, acceptsStreamResponse, initiator: link })
}

// Navigator delegate

allowsVisitingLocationWithAction(location: URL, action?: Action) {
return this.locationWithActionIsSamePage(location, action) || this.applicationAllowsVisitingLocation(location)
allowsVisitingLocation(location: URL, options: Partial<VisitOptions> = {}) {
return (
this.locationWithActionIsSamePage(location, options.action) ||
this.applicationAllowsVisitingLocation(location, options)
)
}

visitProposedToLocation(location: URL, options: Partial<VisitOptions>) {
Expand All @@ -215,7 +218,7 @@ export class Session
}
extendURLWithDeprecatedProperties(visit.location)
if (!visit.silent) {
this.notifyApplicationAfterVisitingLocation(visit.location, visit.action)
this.notifyApplicationAfterVisitingLocation(visit.location, visit.action, visit.initiator)
}
}

Expand Down Expand Up @@ -329,8 +332,8 @@ export class Session
return !event.defaultPrevented
}

applicationAllowsVisitingLocation(location: URL) {
const event = this.notifyApplicationBeforeVisitingLocation(location)
applicationAllowsVisitingLocation(location: URL, options: Partial<VisitOptions> = {}) {
const event = this.notifyApplicationBeforeVisitingLocation(location, options.initiator)
return !event.defaultPrevented
}

Expand All @@ -342,15 +345,19 @@ export class Session
})
}

notifyApplicationBeforeVisitingLocation(location: URL) {
notifyApplicationBeforeVisitingLocation(location: URL, element?: Element) {
return dispatch<TurboBeforeVisitEvent>("turbo:before-visit", {
target: element,
detail: { url: location.href },
cancelable: true,
})
}

notifyApplicationAfterVisitingLocation(location: URL, action: Action) {
return dispatch<TurboVisitEvent>("turbo:visit", { detail: { url: location.href, action } })
notifyApplicationAfterVisitingLocation(location: URL, action: Action, element?: Element) {
return dispatch<TurboVisitEvent>("turbo:visit", {
target: element,
detail: { url: location.href, action },
})
}

notifyApplicationBeforeCachingSnapshot() {
Expand Down
5 changes: 2 additions & 3 deletions src/http/fetch_request.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { FetchResponse } from "./fetch_response"
import { FrameElement } from "../elements/frame_element"
import { dispatch } from "../util"

export type TurboBeforeFetchRequestEvent = CustomEvent<{
Expand Down Expand Up @@ -62,7 +61,7 @@ export class FetchRequest {
readonly headers: FetchRequestHeaders
readonly url: URL
readonly body?: FetchRequestBody
readonly target?: FrameElement | HTMLFormElement | null
readonly target?: Element | null
readonly abortController = new AbortController()
private resolveRequestPromise = (_value: any) => {}

Expand All @@ -71,7 +70,7 @@ export class FetchRequest {
method: FetchMethod,
location: URL,
body: FetchRequestBody = new URLSearchParams(),
target: FrameElement | HTMLFormElement | null = null
target: Element | null = null
) {
this.delegate = delegate
this.method = method
Expand Down
13 changes: 13 additions & 0 deletions src/tests/functional/navigation_tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
nextBody,
nextEventNamed,
noNextEventNamed,
nextEventOnTarget,
pathname,
readEventLogs,
search,
Expand Down Expand Up @@ -381,3 +382,15 @@ test("test ignores links that target an iframe", async ({ page }) => {

assert.equal(pathname(page.url()), "/src/tests/fixtures/navigation.html")
})

test("test visit events are dispatched on the initiator", async ({ page }) => {
await page.click("#same-origin-unannotated-link")
await nextEventOnTarget(page, "same-origin-unannotated-link", "turbo:before-visit")
await nextEventOnTarget(page, "same-origin-unannotated-link", "turbo:visit")
})

test("test fetch events are dispatched on the initiator", async ({ page }) => {
await page.click("#same-origin-unannotated-link")
await nextEventOnTarget(page, "same-origin-unannotated-link", "turbo:before-fetch-request")
await nextEventOnTarget(page, "same-origin-unannotated-link", "turbo:before-fetch-response")
})