From 148b5b87690ea595c6febb9bc1ca74d90f594111 Mon Sep 17 00:00:00 2001
From: Martin Trapp <94928215+martrapp@users.noreply.github.com>
Date: Fri, 29 Sep 2023 20:03:19 +0200
Subject: [PATCH] Fix preloading stylesheets in view transitions (#8707)
* Fix preload of stylesheets (VT)
* Fix preload of stylesheets (VT)
---
.../fixtures/view-transitions/public/one.css | 3 +++
.../view-transitions/public/styles.css | 3 +++
.../src/components/Layout.astro | 1 +
.../view-transitions/src/pages/one.astro | 2 +-
packages/astro/e2e/view-transitions.test.js | 18 ++++++++++++++++++
packages/astro/src/transitions/router.ts | 4 +++-
6 files changed, 29 insertions(+), 2 deletions(-)
create mode 100644 packages/astro/e2e/fixtures/view-transitions/public/one.css
create mode 100644 packages/astro/e2e/fixtures/view-transitions/public/styles.css
diff --git a/packages/astro/e2e/fixtures/view-transitions/public/one.css b/packages/astro/e2e/fixtures/view-transitions/public/one.css
new file mode 100644
index 000000000..4a1589c4d
--- /dev/null
+++ b/packages/astro/e2e/fixtures/view-transitions/public/one.css
@@ -0,0 +1,3 @@
+#one {
+ background-color: whitesmoke;
+}
diff --git a/packages/astro/e2e/fixtures/view-transitions/public/styles.css b/packages/astro/e2e/fixtures/view-transitions/public/styles.css
new file mode 100644
index 000000000..37fc7c196
--- /dev/null
+++ b/packages/astro/e2e/fixtures/view-transitions/public/styles.css
@@ -0,0 +1,3 @@
+h1 {
+ color: blue
+}
diff --git a/packages/astro/e2e/fixtures/view-transitions/src/components/Layout.astro b/packages/astro/e2e/fixtures/view-transitions/src/components/Layout.astro
index 1dc1a1c24..ddafb98a9 100644
--- a/packages/astro/e2e/fixtures/view-transitions/src/components/Layout.astro
+++ b/packages/astro/e2e/fixtures/view-transitions/src/components/Layout.astro
@@ -18,6 +18,7 @@ const { link } = Astro.props as Props;
margin: auto;
}
+
diff --git a/packages/astro/e2e/fixtures/view-transitions/src/pages/one.astro b/packages/astro/e2e/fixtures/view-transitions/src/pages/one.astro
index cc57e76d8..d05973036 100644
--- a/packages/astro/e2e/fixtures/view-transitions/src/pages/one.astro
+++ b/packages/astro/e2e/fixtures/view-transitions/src/pages/one.astro
@@ -1,7 +1,7 @@
---
import Layout from '../components/Layout.astro';
---
-
+
Page 1
test
go to 2
diff --git a/packages/astro/e2e/view-transitions.test.js b/packages/astro/e2e/view-transitions.test.js
index a04b905af..31e3128f5 100644
--- a/packages/astro/e2e/view-transitions.test.js
+++ b/packages/astro/e2e/view-transitions.test.js
@@ -20,6 +20,20 @@ function scrollToBottom(page) {
});
}
+function collectPreloads(page) {
+ return page.evaluate(() => {
+ window.preloads = [];
+ const observer = new MutationObserver((mutations) => {
+ mutations.forEach((mutation) =>
+ mutation.addedNodes.forEach((node) => {
+ if (node.nodeName === 'LINK' && node.rel === 'preload') preloads.push(node.href);
+ })
+ );
+ });
+ observer.observe(document.head, { childList: true });
+ });
+}
+
test.describe('View Transitions', () => {
test('Moving from page 1 to page 2', async ({ page, astro }) => {
const loads = [];
@@ -170,11 +184,15 @@ test.describe('View Transitions', () => {
let p = page.locator('#one');
await expect(p, 'should have content').toHaveText('Page 1');
+ await collectPreloads(page);
+
// Go to page 2
await page.click('#click-two');
p = page.locator('#two');
await expect(p, 'should have content').toHaveText('Page 2');
await expect(p, 'imported CSS updated').toHaveCSS('font-size', '24px');
+ const preloads = await page.evaluate(() => window.preloads);
+ expect(preloads.length === 1 && preloads[0].endsWith('/two.css')).toBeTruthy();
});
test('astro:page-load event fires when navigating to new page', async ({ page, astro }) => {
diff --git a/packages/astro/src/transitions/router.ts b/packages/astro/src/transitions/router.ts
index 12ac5615d..1049f92d1 100644
--- a/packages/astro/src/transitions/router.ts
+++ b/packages/astro/src/transitions/router.ts
@@ -292,7 +292,9 @@ async function updateDOM(
// Do not preload links that are already on the page.
if (
!document.querySelector(
- `[${PERSIST_ATTR}="${el.getAttribute(PERSIST_ATTR)}"], link[rel=stylesheet]`
+ `[${PERSIST_ATTR}="${el.getAttribute(
+ PERSIST_ATTR
+ )}"], link[rel=stylesheet][href="${el.getAttribute('href')}"]`
)
) {
const c = document.createElement('link');