Skip to content

Commit

Permalink
Improving error messages for invalid client hydration directives (#2076)
Browse files Browse the repository at this point in the history
* Adding check to make sure the hydration directive is valid

* remove temp debug logging

* Adding a check for media query with client:media + small refactor

* adding changeset

Co-authored-by: Tony Sullivan <tony.sullivan@hyperlab.se>
  • Loading branch information
Tony Sullivan and Tony Sullivan committed Dec 1, 2021
1 parent bebb312 commit 920d3da
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/two-geckos-rhyme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Improving build validation and error messages for client hydration directives
13 changes: 13 additions & 0 deletions packages/astro/src/runtime/server/hydration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ export function serializeProps(value: any) {
});
}

const HydrationDirectives = ['load', 'idle', 'media', 'visible', 'only'];

interface ExtractedProps {
hydration: {
directive: string;
Expand Down Expand Up @@ -70,6 +72,17 @@ export function extractDirectives(inputProps: Record<string | number, any>): Ext
default: {
extracted.hydration.directive = key.split(':')[1];
extracted.hydration.value = value;

// throw an error if an invalid hydration directive was provided
if (HydrationDirectives.indexOf(extracted.hydration.directive) < 0) {
throw new Error(`Error: invalid hydration directive "${key}". Supported hydration methods: ${HydrationDirectives.map(d => `"client:${d}"`).join(', ')}`)
}

// throw an error if the query wasn't provided for client:media
if (extracted.hydration.directive === 'media' && typeof extracted.hydration.value !== 'string') {
throw new Error('Error: Media query must be provided for "client:media", similar to client:media="(max-width: 600px)"')
}

break;
}
}
Expand Down
30 changes: 30 additions & 0 deletions packages/astro/test/errors.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,36 @@ describe('Error display', () => {

// TODO: improve stacktrace
});

it('hydration error', async () => {
if (isMacOS) return;

const res = await fixture.fetch('/astro-hydration-error');

// 500 returned
expect(res.status).to.equal(500);

// error message contains error
const body = await res.text();

// error message contains error
expect(body).to.include('Error: invalid hydration directive');
});

it('client:media error', async () => {
if (isMacOS) return;

const res = await fixture.fetch('/astro-client-media-error');

// 500 returned
expect(res.status).to.equal(500);

// error message contains error
const body = await res.text();

// error message contains error
expect(body).to.include('Error: Media query must be provided');
});
});

describe('JS', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<h1>I shouldn’t be here</h1>

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
import SvelteDirectiveError from '../components/SvelteDirectiveError.svelte';
---

<div>
<SvelteDirectiveError client:media />
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
import SvelteDirectiveError from '../components/SvelteDirectiveError.svelte';
---

<div>
<SvelteDirectiveError client:loadm />
</div>

0 comments on commit 920d3da

Please sign in to comment.