forked from hotwired/turbo
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove unused stylesheets when navigating (hotwired#1128)
When navigating to another page, Turbo merges the `<head>` contents from the current and new pages, which results in a `<head>` containing the superset of both. For certain items, like scripts, this makes sense. We have no way to remove a running script. But that's not the case for styles: styles can be unloaded easily, and for the page to display properly, we need to do so. Otherwise styles kept in scope from a previous page could cause a page to render incorrectly. The common way to avoid this has been to use `data-turbo-track="reload"` to force a reload if styles change. This works, but it's a bit heavy-handed, causing full page reloads that could have been avoided. There are a couple of common cases where updating styles on the fly would be useful: - Deploying a CSS change. Clients should be able to pick up the change without having to reload. - Allowing pages to include their own specific styles, rather than bundle them all together for the whole site. This can reduce the size of the loaded CSS, and make it easier to avoid style conflicts.
- Loading branch information
1 parent
4d5ceaa
commit 8dbbc7d
Showing
12 changed files
with
140 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8"> | ||
<title>Additional assets</title> | ||
<link rel="stylesheet" type="text/css" href="/src/tests/fixtures/test.css"> | ||
<script src="/dist/turbo.es2017-umd.js" data-turbo-track="reload"></script> | ||
<script src="/src/tests/fixtures/test.js"></script> | ||
<script id="additional" src="/src/tests/fixtures/test.js"></script> | ||
</head> | ||
<body> | ||
<h1>Additional assets</h1> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
body { | ||
background-color: rgb(0, 0, 128); | ||
color: rgb(0, 128, 0); | ||
margin: 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
body { | ||
background-color: rgb(128, 0, 0); | ||
color: rgb(128, 0, 0); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<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"> | ||
|
||
<style> | ||
body { margin-left: 20px; } | ||
.left { margin-left: 20px; } | ||
</style> | ||
</head> | ||
|
||
<body></body> | ||
<h1>Left</h1> | ||
<p><a id="go-right" href="/src/tests/fixtures/stylesheets/right.html">go right</a></p> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
body { | ||
background-color: rgb(0, 128, 0); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<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"> | ||
|
||
<style> | ||
body { margin-right: 20px; } | ||
.right { margin-right: 20px; } | ||
</style> | ||
</head> | ||
|
||
<body></body> | ||
<h1>Right</h1> | ||
<p><a id="go-left" href="/src/tests/fixtures/stylesheets/left.html">go left</a></p> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { test } from "@playwright/test" | ||
import { assert } from "chai" | ||
import { cssClassIsDefined, getComputedStyle, hasSelector, nextBody } from "../helpers/page" | ||
|
||
test.beforeEach(async ({ page }) => { | ||
await page.goto("/src/tests/fixtures/stylesheets/left.html") | ||
}) | ||
|
||
test("navigating removes unused style elements", async ({ page }) => { | ||
await page.locator("#go-right").click() | ||
await nextBody(page) | ||
|
||
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 cssClassIsDefined(page, "right")) | ||
assert.notOk(await cssClassIsDefined(page, "left")) | ||
assert.equal(await getComputedStyle(page, "body", "marginLeft"), "0px") | ||
assert.equal(await getComputedStyle(page, "body", "marginRight"), "20px") | ||
}) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters