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

Proposal: pass isPreview value into the event detail in render events #926

Merged
merged 1 commit into from
Aug 24, 2023
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
6 changes: 5 additions & 1 deletion src/core/frames/frame_controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,11 @@ export class FrameController

// View delegate

allowsImmediateRender({ element: newFrame }: Snapshot<FrameElement>, options: ViewRenderOptions<FrameElement>) {
allowsImmediateRender(
{ element: newFrame }: Snapshot<FrameElement>,
_isPreview: boolean,
options: ViewRenderOptions<FrameElement>
) {
const event = dispatch<TurboBeforeFrameRenderEvent>("turbo:before-frame-render", {
target: this.element,
detail: { newFrame, ...options },
Expand Down
22 changes: 12 additions & 10 deletions src/core/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,16 @@ import { Preloader, PreloaderDelegate } from "./drive/preloader"
export type FormMode = "on" | "off" | "optin"
export type TimingData = unknown
export type TurboBeforeCacheEvent = CustomEvent
export type TurboBeforeRenderEvent = CustomEvent<{ newBody: HTMLBodyElement } & PageViewRenderOptions>
export type TurboBeforeRenderEvent = CustomEvent<
{ newBody: HTMLBodyElement; isPreview: boolean } & PageViewRenderOptions
>
export type TurboBeforeVisitEvent = CustomEvent<{ url: string }>
export type TurboClickEvent = CustomEvent<{ url: string; originalEvent: MouseEvent }>
export type TurboFrameLoadEvent = CustomEvent
export type TurboBeforeFrameRenderEvent = CustomEvent<{ newFrame: FrameElement } & FrameViewRenderOptions>
export type TurboFrameRenderEvent = CustomEvent<{ fetchResponse: FetchResponse }>
export type TurboLoadEvent = CustomEvent<{ url: string; timing: TimingData }>
export type TurboRenderEvent = CustomEvent
export type TurboRenderEvent = CustomEvent<{ isPreview: boolean }>
export type TurboVisitEvent = CustomEvent<{ url: string; action: Action }>

export class Session
Expand Down Expand Up @@ -276,8 +278,8 @@ export class Session
}
}

allowsImmediateRender({ element }: PageSnapshot, options: PageViewRenderOptions) {
const event = this.notifyApplicationBeforeRender(element, options)
allowsImmediateRender({ element }: PageSnapshot, isPreview: boolean, options: PageViewRenderOptions) {
const event = this.notifyApplicationBeforeRender(element, isPreview, options)
const {
defaultPrevented,
detail: { render },
Expand All @@ -290,9 +292,9 @@ export class Session
return !defaultPrevented
}

viewRenderedSnapshot(_snapshot: PageSnapshot, _isPreview: boolean) {
viewRenderedSnapshot(_snapshot: PageSnapshot, isPreview: boolean) {
this.view.lastRenderedLocation = this.history.location
this.notifyApplicationAfterRender()
this.notifyApplicationAfterRender(isPreview)
}

preloadOnLoadLinksForView(element: Element) {
Expand Down Expand Up @@ -348,15 +350,15 @@ export class Session
return dispatch<TurboBeforeCacheEvent>("turbo:before-cache")
}

notifyApplicationBeforeRender(newBody: HTMLBodyElement, options: PageViewRenderOptions) {
notifyApplicationBeforeRender(newBody: HTMLBodyElement, isPreview: boolean, options: PageViewRenderOptions) {
return dispatch<TurboBeforeRenderEvent>("turbo:before-render", {
detail: { newBody, ...options },
detail: { newBody, isPreview, ...options },
cancelable: true,
})
}

notifyApplicationAfterRender() {
return dispatch<TurboRenderEvent>("turbo:render")
notifyApplicationAfterRender(isPreview: boolean) {
return dispatch<TurboRenderEvent>("turbo:render", { detail: { isPreview } })
}

notifyApplicationAfterPageLoad(timing: TimingData = {}) {
Expand Down
4 changes: 2 additions & 2 deletions src/core/view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export interface ViewRenderOptions<E> {
}

export interface ViewDelegate<E extends Element, S extends Snapshot<E>> {
allowsImmediateRender(snapshot: S, options: ViewRenderOptions<E>): boolean
allowsImmediateRender(snapshot: S, isPreview: boolean, options: ViewRenderOptions<E>): boolean
preloadOnLoadLinksForView(element: Element): void
viewRenderedSnapshot(snapshot: S, isPreview: boolean): void
viewInvalidated(reason: ReloadReason): void
Expand Down Expand Up @@ -91,7 +91,7 @@ export abstract class View<

const renderInterception = new Promise((resolve) => (this.resolveInterceptionPromise = resolve))
const options = { resume: this.resolveInterceptionPromise, render: this.renderer.renderElement }
const immediateRender = this.delegate.allowsImmediateRender(snapshot, options)
const immediateRender = this.delegate.allowsImmediateRender(snapshot, isPreview, options)
if (!immediateRender) await renderInterception

await this.renderSnapshot(renderer)
Expand Down
10 changes: 10 additions & 0 deletions src/tests/functional/rendering_tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ test("test triggers before-render and render events", async ({ page }) => {
assert.equal(await newBody, await page.evaluate(() => document.body.outerHTML))
})

test("test includes isPreview in render event details", async ({ page }) => {
await page.click("#same-origin-link")

const { isPreview } = await nextEventNamed(page, "turbo:before-render")
assert.equal(isPreview, false)

await nextEventNamed(page, "turbo:render")
assert.equal(await isPreview, false)
})

test("test triggers before-render and render events for error pages", async ({ page }) => {
await page.click("#nonexistent-link")
const { newBody } = await nextEventNamed(page, "turbo:before-render")
Expand Down