summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Martin Trapp <94928215+martrapp@users.noreply.github.com> 2023-09-29 20:03:19 +0200
committerGravatar GitHub <noreply@github.com> 2023-09-29 20:03:19 +0200
commit148b5b87690ea595c6febb9bc1ca74d90f594111 (patch)
tree4c898817ab77b3f9caa73ed52c2372fe477395c6
parent345808170fce783ddd3c9a4035a91fa64dcc5f46 (diff)
downloadastro-148b5b87690ea595c6febb9bc1ca74d90f594111.tar.gz
astro-148b5b87690ea595c6febb9bc1ca74d90f594111.tar.zst
astro-148b5b87690ea595c6febb9bc1ca74d90f594111.zip
Fix preloading stylesheets in view transitions (#8707)
* Fix preload of stylesheets (VT) * Fix preload of stylesheets (VT)
-rw-r--r--packages/astro/e2e/fixtures/view-transitions/public/one.css3
-rw-r--r--packages/astro/e2e/fixtures/view-transitions/public/styles.css3
-rw-r--r--packages/astro/e2e/fixtures/view-transitions/src/components/Layout.astro1
-rw-r--r--packages/astro/e2e/fixtures/view-transitions/src/pages/one.astro2
-rw-r--r--packages/astro/e2e/view-transitions.test.js18
-rw-r--r--packages/astro/src/transitions/router.ts4
6 files changed, 29 insertions, 2 deletions
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;
}
</style>
+ <link rel="stylesheet" href="/style.css">
<ViewTransitions />
<DarkMode />
<meta name="script-executions" content="0">
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';
---
-<Layout>
+<Layout link="/one.css">
<p id="one">Page 1</p>
<a id="click-one" href="#test">test</a>
<a id="click-two" href="/two">go to 2</a>
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');