Skip to content

Commit

Permalink
Fixes client:only CSS in Svelte components (#4782)
Browse files Browse the repository at this point in the history
* Fixes client:only CSS in Svelte components

* Add changeset
  • Loading branch information
matthewp committed Sep 17, 2022
1 parent 1c36b0e commit 8f9463e
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/witty-suits-grab.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fixes client:only CSS in Svelte components
12 changes: 10 additions & 2 deletions packages/astro/src/core/build/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,21 @@ export function* getPageDatasByClientOnlyID(
): Generator<PageBuildData, void, unknown> {
const pagesByClientOnly = internals.pagesByClientOnly;
if (pagesByClientOnly.size) {
let pathname = `/@fs${prependForwardSlash(viteid)}`;
// 1. Try the viteid
let pageBuildDatas = pagesByClientOnly.get(viteid);

// 2. Try prepending /@fs
if(!pageBuildDatas) {
let pathname = `/@fs${prependForwardSlash(viteid)}`;
pageBuildDatas = pagesByClientOnly.get(pathname);
}

// 3. Remove the file extension
// BUG! The compiler partially resolves .jsx to remove the file extension so we have to check again.
// We should probably get rid of all `@fs` usage and always fully resolve via Vite,
// but this would be a bigger change.
if (!pageBuildDatas) {
pathname = `/@fs${prependForwardSlash(removeFileExtension(viteid))}`;
let pathname = `/@fs${prependForwardSlash(removeFileExtension(viteid))}`;
pageBuildDatas = pagesByClientOnly.get(pathname);
}
if (pageBuildDatas) {
Expand Down
12 changes: 12 additions & 0 deletions packages/astro/test/astro-client-only.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ describe('Client only components', () => {
expect(css).to.match(/Courier New/, 'Global styles are added');
});

it('Adds the CSS to the page - standalone svelte component', async () => {
const html = await fixture.readFile('/persistent-counter-standalone/index.html');
const $ = cheerioLoad(html);

expect($('head link[rel=stylesheet]')).to.have.a.lengthOf(1);

const href = $('link[rel=stylesheet]').attr('href');
const css = await fixture.readFile(href);

expect(css).to.match(/tomato/, 'Svelte styles are added');
});

it('Includes CSS from components that use CSS modules', async () => {
const html = await fixture.readFile('/css-modules/index.html');
const $ = cheerioLoad(html);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<script>
import './logResize';
let count = parseInt(localStorage.getItem('test:count')) || 0;
$: localStorage.setItem('test:count', count);
function add() {
count += 1;
}
function subtract() {
count -= 1;
}
</script>
<style>
button {
background: tomato;
}
</style>
<div class="counter">
<button on:click={subtract}>-</button>
<pre>{ count }</pre>
<button on:click={add}>+</button>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
import PersistentCounter from '../components/PersistentCounterStandalone.svelte';
---
<html>
<head>
<title>Client only pages - Only PersistentCounter, nothing else</title>
</head>
<body>
<!--
Do not add another test-case to this file. This is meant to test if only a single component is used.
-->

<PersistentCounter client:only />
</body>
</html>

0 comments on commit 8f9463e

Please sign in to comment.