Skip to content

Commit a0774b3

Browse files
fix: solving #13577 (#13587)
* fix: solving #13577 * Update .changeset/yellow-dogs-say.md Co-authored-by: Martin Trapp <94928215+martrapp@users.noreply.github.com> --------- Co-authored-by: Martin Trapp <94928215+martrapp@users.noreply.github.com>
1 parent 5a0563d commit a0774b3

File tree

5 files changed

+21
-9
lines changed

5 files changed

+21
-9
lines changed

.changeset/yellow-dogs-say.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'astro': patch
3+
---
4+
5+
Fixes an issue with the client router where some attributes of the root element were not updated during swap, including the transition scope.

packages/astro/e2e/fixtures/view-transitions/src/pages/other-attributes.astro

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ lang="es"
66
style="background-color: green"
77
data-other-name="value"
88
data-astro-fake="value"
9-
data-astro-transition="downward">
9+
data-astro-transition="downward"
10+
data-astro-transition-scope="scope-y">
1011
<p id="heading">Page with other attributes</p>
1112
</AttributeLayout>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
import AttributedLayout from '../components/AttributedLayout.astro';
33
---
4-
<AttributedLayout lang="en" class="ugly">
4+
<AttributedLayout lang="en" class="ugly" data-astro-transition-scope="scope-x">
55
<p id="heading">Page with some attributes</p>
66
<a id="click-other-attributes" href="/other-attributes">Other attributes</a>
77
</AttributedLayout>

packages/astro/e2e/view-transitions.test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -739,6 +739,10 @@ test.describe('View Transitions', () => {
739739
await expect(h, 'should have content').toHaveAttribute('data-other-name', 'value');
740740
await expect(h, 'should have content').toHaveAttribute('data-astro-fake', 'value');
741741
await expect(h, 'should have content').toHaveAttribute('data-astro-transition', 'forward');
742+
await expect(h, 'should have swap rest of data-astro-* attributes').toHaveAttribute(
743+
'data-astro-transition-scope',
744+
'scope-y',
745+
);
742746
await expect(h, 'should be absent').not.toHaveAttribute('class', /.*/);
743747
});
744748

packages/astro/src/transitions/swap-functions.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ export type SavedFocus = {
66

77
const PERSIST_ATTR = 'data-astro-transition-persist';
88

9+
const NON_OVERRIDABLE_ASTRO_ATTRS = ['data-astro-transition', 'data-astro-transition-fallback'];
10+
911
const scriptsAlreadyRan = new Set<string>();
1012
export function detectScriptExecuted(script: HTMLScriptElement) {
1113
const key = script.src ? new URL(script.src, location.href).href : script.textContent!;
@@ -36,15 +38,15 @@ export function deselectScripts(doc: Document) {
3638
* swap attributes of the html element
3739
* delete all attributes from the current document
3840
* insert all attributes from doc
39-
* reinsert all original attributes that are named 'data-astro-*'
41+
* reinsert all original attributes that are referenced in NON_OVERRIDABLE_ASTRO_ATTRS'
4042
*/
41-
export function swapRootAttributes(doc: Document) {
42-
const html = document.documentElement;
43-
const astroAttributes = [...html.attributes].filter(
44-
({ name }) => (html.removeAttribute(name), name.startsWith('data-astro-')),
43+
export function swapRootAttributes(newDoc: Document) {
44+
const currentRoot = document.documentElement;
45+
const nonOverridableAstroAttributes = [...currentRoot.attributes].filter(
46+
({ name }) => (currentRoot.removeAttribute(name), NON_OVERRIDABLE_ASTRO_ATTRS.includes(name)),
4547
);
46-
[...doc.documentElement.attributes, ...astroAttributes].forEach(({ name, value }) =>
47-
html.setAttribute(name, value),
48+
[...newDoc.documentElement.attributes, ...nonOverridableAstroAttributes].forEach(
49+
({ name, value }) => currentRoot.setAttribute(name, value),
4850
);
4951
}
5052

0 commit comments

Comments
 (0)