Skip to content

Commit

Permalink
Introduce data-turbo-track="dynamic" (#1140)
Browse files Browse the repository at this point in the history
To track stylesheets and styles that we can dynamically remove when
navigating.

We introduced this behaviour in #1128
and thought it would be a good default to avoid full page reloads when
styles change.

However, it seems it's quite common for libraries to add stylesheets
and styles to the head that they want to keep around. For example, see

- #1133
- #1139

So let's make this behaviour opt-in by adding a `data-turbo-track="dynamic"`
attribute to stylesheets and styles that we want to dynamically remove when
they are no longer in a new page navigation.

This avoids any breaking changes for existing apps.
  • Loading branch information
Alberto Fernández-Capel committed Jan 25, 2024
1 parent 5902f3b commit 814099c
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 20 deletions.
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

0 comments on commit 814099c

Please sign in to comment.