Skip to content

Commit

Permalink
Prevent FOUC with production links in ViewTransitions (#7756)
Browse files Browse the repository at this point in the history
* Prevent FOUC with production links in ViewTransitions

* Adding changeset

* Move up so it works with fallback too
  • Loading branch information
matthewp committed Jul 21, 2023
1 parent f79ab0b commit 274e675
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/cool-deers-dream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fixes case where there is FOUC caused by stylesheets not loaded
10 changes: 10 additions & 0 deletions packages/astro/components/ViewTransitions.astro
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@ const { fallback = 'animate' } = Astro.props as Props;
doc.documentElement.dataset.astroTransition = dir;
const swap = () => document.documentElement.replaceWith(doc.documentElement);

// Wait on links to finish, to prevent FOUC
const links = Array.from(doc.querySelectorAll('head link[rel=stylesheet]')).map(link => new Promise(resolve => {
const c = link.cloneNode();
['load', 'error'].forEach(evName => c.addEventListener(evName, resolve));
document.head.append(c);
}));
if(links.length) {
await Promise.all(links);
}

if (fallback === 'animate') {
let isAnimating = false;
addEventListener('animationstart', () => (isAnimating = true), { once: true });
Expand Down
3 changes: 3 additions & 0 deletions packages/astro/e2e/fixtures/view-transitions/public/two.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#two {
font-size: 24px;
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
---
import { ViewTransitions } from 'astro:transitions';
interface Props {
link?: string;
}
const { link } = Astro.props as Props;
---
<html>
<head>
<title>Testing</title>
{link ? <link rel="stylesheet" href={link} > : ''}
<ViewTransitions />
</head>
<body>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
import Layout from '../components/Layout.astro';
---
<Layout>
<Layout link="/two.css">
<p id="two">Page 2</p>
</Layout>
17 changes: 17 additions & 0 deletions packages/astro/e2e/view-transitions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,21 @@ test.describe('View Transitions', () => {
p = page.locator('#one');
await expect(p, 'should have content').toHaveText('Page 1');
});

test('Stylesheets in the head are waited on', async ({ page, astro }) => {
page.addListener('console', data => {
console.log(data)
})

// Go to page 1
await page.goto(astro.resolveUrl('/one'));
let p = page.locator('#one');
await expect(p, 'should have content').toHaveText('Page 1');

// 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');
});
});

0 comments on commit 274e675

Please sign in to comment.