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

Introduce data-turbo-track="dynamic" #1140

Merged
merged 1 commit into from
Jan 25, 2024
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
14 changes: 5 additions & 9 deletions src/core/drive/page_renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export class PageRenderer extends Renderer {
await newStylesheetElements

if (this.willRender) {
this.removeUnusedHeadStylesheetElements()
this.removeUnusedDynamicStylesheetElements()
}
}

Expand Down Expand Up @@ -111,8 +111,8 @@ export class PageRenderer extends Renderer {
}
}

removeUnusedHeadStylesheetElements() {
for (const element of this.unusedHeadStylesheetElements) {
removeUnusedDynamicStylesheetElements() {
for (const element of this.unusedDynamicStylesheetElements) {
document.head.removeChild(element)
}
}
Expand Down Expand Up @@ -182,13 +182,9 @@ export class PageRenderer extends Renderer {
await this.renderElement(this.currentElement, this.newElement)
}

get unusedHeadStylesheetElements() {
get unusedDynamicStylesheetElements() {
return this.oldHeadStylesheetElements.filter((element) => {
return !(element.hasAttribute("data-turbo-permanent") ||
// Trix dynamically adds styles to the head that we want to keep around which have a
// `data-tag-name` attribute. Long term we should moves those styles to Trix's CSS file
// but for now we'll just skip removing them
element.hasAttribute("data-tag-name"))
return element.getAttribute("data-turbo-track") === "dynamic"
})
}

Expand Down
2 changes: 0 additions & 2 deletions src/core/drive/progress_bar.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,6 @@ export class ProgressBar {

createStylesheetElement() {
const element = document.createElement("style")
element.id = ProgressBarID
element.setAttribute("data-turbo-permanent", "")
element.type = "text/css"
element.textContent = ProgressBar.defaultCSS
if (this.cspNonce) {
Expand Down
11 changes: 8 additions & 3 deletions src/tests/fixtures/stylesheets/left.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@
<meta charset="utf-8" />
<title>Left</title>
<script src="/dist/turbo.es2017-umd.js" data-turbo-track="reload"></script>
<link rel="stylesheet" type="text/css" href="/src/tests/fixtures/stylesheets/common.css">
<link rel="stylesheet" type="text/css" href="/src/tests/fixtures/stylesheets/left.css">
<link rel="stylesheet" type="text/css" href="/src/tests/fixtures/stylesheets/common.css" data-turbo-track="dynamic">
<link rel="stylesheet" type="text/css" href="/src/tests/fixtures/stylesheets/left.css" data-turbo-track="dynamic">

<style>
<style data-turbo-track="dynamic">
body { margin-left: 20px; }
.left { margin-left: 20px; }
</style>

<script type="text/javascript">
document.head.insertAdjacentHTML("beforeend", `<style id="added-style">body{background:red}</style>`)
document.head.insertAdjacentHTML("beforeend", `<link id="added-link" rel="stylesheet" type="text/css" href="/src/tests/fixtures/test.css">`)
</script>
</head>

<body></body>
Expand Down
6 changes: 3 additions & 3 deletions src/tests/fixtures/stylesheets/right.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
<meta charset="utf-8" />
<title>Right</title>
<script src="/dist/turbo.es2017-umd.js" data-turbo-track="reload"></script>
<link rel="stylesheet" type="text/css" href="/src/tests/fixtures/stylesheets/common.css">
<link rel="stylesheet" type="text/css" href="/src/tests/fixtures/stylesheets/right.css">
<link rel="stylesheet" type="text/css" href="/src/tests/fixtures/stylesheets/common.css" data-turbo-track="dynamic">
<link rel="stylesheet" type="text/css" href="/src/tests/fixtures/stylesheets/right.css" data-turbo-track="dynamic">

<style>
<style data-turbo-track="dynamic">
body { margin-right: 20px; }
.right { margin-right: 20px; }
</style>
Expand Down
9 changes: 6 additions & 3 deletions src/tests/functional/drive_stylesheet_merging_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,22 @@ test.beforeEach(async ({ page }) => {
await page.goto("/src/tests/fixtures/stylesheets/left.html")
})

test("navigating removes unused style elements", async ({ page }) => {
assert.ok(await hasSelector(page, 'style[id="turbo-progress-bar"]'))
test("navigating removes unused dynamically tracked style elements", async ({ page }) => {
assert.ok(await hasSelector(page, 'style[id="added-style"]'))
assert.ok(await hasSelector(page, 'link[id="added-link"]'))

await page.locator("#go-right").click()
await nextBody(page)

assert.ok(await hasSelector(page, 'style[id="turbo-progress-bar"]'))
assert.ok(await hasSelector(page, 'link[rel=stylesheet][href="/src/tests/fixtures/stylesheets/common.css"]'))
assert.ok(await hasSelector(page, 'link[rel=stylesheet][href="/src/tests/fixtures/stylesheets/right.css"]'))
assert.notOk(await hasSelector(page, 'link[rel=stylesheet][href="/src/tests/fixtures/stylesheets/left.css"]'))
assert.equal(await getComputedStyle(page, "body", "backgroundColor"), "rgb(0, 128, 0)")
assert.equal(await getComputedStyle(page, "body", "color"), "rgb(0, 128, 0)")

assert.ok(await hasSelector(page, 'style[id="added-style"]'))
assert.ok(await hasSelector(page, 'link[id="added-link"]'))

assert.ok(await cssClassIsDefined(page, "right"))
assert.notOk(await cssClassIsDefined(page, "left"))
assert.equal(await getComputedStyle(page, "body", "marginLeft"), "0px")
Expand Down