Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: new lines in hydration response #199

Merged
merged 5 commits into from
Nov 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/hydrogen/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ and adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## Unreleased

- fix: new lines in hydration request break JSON.parse
- fix(#201): normalize POSIX separators to support windows
- fix: scroll to top on app first load

Expand Down
5 changes: 4 additions & 1 deletion packages/hydrogen/src/framework/Hydration/Cache.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,10 @@ function createManifestFromWirePayload(payload: string): WireManifest {
return payload.split('\n').reduce((memo, row) => {
const [key, ...values] = row.split(':');

memo[key] = JSON.parse(values.join(':'));
if (key) {
memo[key] = JSON.parse(values.join(':'));
}

return memo;
}, {} as Record<string, any>) as WireManifest;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ it('handles DOM elements', async () => {
expect(screen.getByText('hello')).toBeInTheDocument();
});

it('ignores new lines', async () => {
const tuples = [['$', 'div', null, {children: 'hello'}]];
const payload = `\nJ0:${JSON.stringify(tuples)}\n`;

render(await convertHydrationResponseToReactComponents(payload));

expect(screen.getByText('hello')).toBeInTheDocument();
});

it('handles DOM elements with arrays of children', async () => {
const tuples = [
[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {generateWireSyntaxFromRenderedHtml} from '../wire.server';

it('renders normal html elements', () => {
const input = `<div class="foo"><p id="bar">Hello!</p></div>`;
const output = `\nJ0:["$","div",null,{"className":"foo","children":["$","p",null,{"id":"bar","children":"Hello!"}]}]`;
const output = `J0:["$","div",null,{"className":"foo","children":["$","p",null,{"id":"bar","children":"Hello!"}]}]`;

expect(generateWireSyntaxFromRenderedHtml(input)).toBe(output);
});
Expand Down Expand Up @@ -98,7 +98,7 @@ J0:["$","div",null,{"className":"foo","children":["\\n ",["$","@1",null,{"sid
it('renders Client Components with nested props that have multiple children', () => {
const input = `<div class="foo"><p>Bar</p><div>Baz</div></div>`;

const output = `\nJ0:["$","div",null,{"className":"foo","children":[["$","p",null,{"key":0,"children":"Bar"}],["$","div",null,{"key":1,"children":"Baz"}]]}]`;
const output = `J0:["$","div",null,{"className":"foo","children":[["$","p",null,{"key":0,"children":"Bar"}],["$","div",null,{"key":1,"children":"Baz"}]]}]`;
frandiox marked this conversation as resolved.
Show resolved Hide resolved

expect(generateWireSyntaxFromRenderedHtml(input)).toBe(output);
});
Expand Down
2 changes: 1 addition & 1 deletion packages/hydrogen/src/framework/Hydration/wire.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export function generateWireSyntaxFromRenderedHtml(html: string) {
return `M${idx + 1}:${JSON.stringify(component)}`;
})
.join('\n') + `\nJ0:${JSON.stringify(wireModel)}`
);
).trim();
}

function isDomNode(item: any) {
Expand Down