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

Allow users to define an async custom render function #782

Merged
merged 1 commit into from
Dec 14, 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/bardo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ export class Bardo {
readonly permanentElementMap: PermanentElementMap
readonly delegate: BardoDelegate

static preservingPermanentElements(
static async preservingPermanentElements(
delegate: BardoDelegate,
permanentElementMap: PermanentElementMap,
callback: () => void
) {
const bardo = new this(delegate, permanentElementMap)
bardo.enter()
callback()
await callback()
bardo.leave()
}

Expand Down
12 changes: 6 additions & 6 deletions src/core/drive/page_renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class PageRenderer extends Renderer<HTMLBodyElement, PageSnapshot> {

async render() {
if (this.willRender) {
this.replaceBody()
await this.replaceBody()
}
}

Expand Down Expand Up @@ -67,10 +67,10 @@ export class PageRenderer extends Renderer<HTMLBodyElement, PageSnapshot> {
await newStylesheetElements
}

replaceBody() {
this.preservingPermanentElements(() => {
async replaceBody() {
await this.preservingPermanentElements(async () => {
this.activateNewBody()
this.assignNewBody()
await this.assignNewBody()
})
}

Expand Down Expand Up @@ -120,8 +120,8 @@ export class PageRenderer extends Renderer<HTMLBodyElement, PageSnapshot> {
}
}

assignNewBody() {
this.renderElement(this.currentElement, this.newElement)
async assignNewBody() {
await this.renderElement(this.currentElement, this.newElement)
}

get newHeadStylesheetElements() {
Expand Down
4 changes: 2 additions & 2 deletions src/core/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ export abstract class Renderer<E extends Element, S extends Snapshot<E> = Snapsh
}
}

preservingPermanentElements(callback: () => void) {
Bardo.preservingPermanentElements(this, this.permanentElementMap, callback)
async preservingPermanentElements(callback: () => void) {
await Bardo.preservingPermanentElements(this, this.permanentElementMap, callback)
}

focusFirstAutofocusableElement() {
Expand Down
30 changes: 30 additions & 0 deletions src/tests/functional/rendering_tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,36 @@ test("test before-render event supports custom render function", async ({ page }
assert.equal(await customRendered.textContent(), "Custom Rendered", "renders with custom function")
})

test("test before-render event supports async custom render function", async ({ page }) => {
await page.evaluate(() => {
const nextEventLoopTick = () =>
new Promise<void>((resolve) => {
setTimeout(() => resolve(), 0)
})

addEventListener("turbo:before-render", (event) => {
const { detail } = event as CustomEvent
const { render } = detail
detail.render = async (currentElement: HTMLBodyElement, newElement: HTMLBodyElement) => {
await nextEventLoopTick()

newElement.insertAdjacentHTML("beforeend", `<span id="custom-rendered">Custom Rendered</span>`)
render(currentElement, newElement)
}
})

addEventListener("turbo:load", () => {
localStorage.setItem("renderedElement", document.getElementById("custom-rendered")?.textContent || "")
})
})
await page.click("#same-origin-link")
await nextEventNamed(page, "turbo:load")

const renderedElement = await page.evaluate(() => localStorage.getItem("renderedElement"))

assert.equal(renderedElement, "Custom Rendered", "renders with custom function")
})

test("test wont reload when tracked elements has a nonce", async ({ page }) => {
await page.click("#tracked-nonce-tag-link")
await nextBody(page)
Expand Down