Skip to content

Commit

Permalink
Use data-turbo-permanent to keep styles around
Browse files Browse the repository at this point in the history
And use it to keep the progress bar styles around, instead of relying
on an id.
  • Loading branch information
afcapel committed Jan 18, 2024
1 parent 8fbb573 commit cfd6edb
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
16 changes: 8 additions & 8 deletions src/core/drive/page_renderer.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { activateScriptElement, waitForLoad } from "../../util"
import { Renderer } from "../renderer"
import { ProgressBarID } from "./progress_bar"

export class PageRenderer extends Renderer {
static renderElement(currentElement, newElement) {
Expand Down Expand Up @@ -114,12 +113,7 @@ export class PageRenderer extends Renderer {

removeUnusedHeadStylesheetElements() {
for (const element of this.unusedHeadStylesheetElements) {
// Trix dynamically adds styles to the head that we want to keep around
// Long term we should moves those styles to Trix's CSS file
// but for now we'll just skip removing them
if (!element.dataset.tagName) {
document.head.removeChild(element)
}
document.head.removeChild(element)
}
}

Expand Down Expand Up @@ -189,7 +183,13 @@ export class PageRenderer extends Renderer {
}

get unusedHeadStylesheetElements() {
return this.oldHeadStylesheetElements.filter((element) => element.id !== ProgressBarID)
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-page-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-page-name"))
})
}

get oldHeadStylesheetElements() {
Expand Down
1 change: 1 addition & 0 deletions src/core/drive/progress_bar.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ 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
3 changes: 3 additions & 0 deletions src/tests/functional/drive_stylesheet_merging_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ test.beforeEach(async ({ page }) => {
})

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

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"]'))
Expand Down

0 comments on commit cfd6edb

Please sign in to comment.