Skip to content

Commit

Permalink
Merge pull request #1606 from sveltejs/gh-1605
Browse files Browse the repository at this point in the history
provide more helpful error if SSR component tries to render non-SSR component
  • Loading branch information
Rich-Harris authored Jul 21, 2018
2 parents cc93595 + e9b588e commit 593bb03
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
17 changes: 10 additions & 7 deletions src/compile/nodes/Component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -564,12 +564,12 @@ export default class Component extends Node {
.concat(bindingProps)
.join(', ')} }`;

const isDynamicComponent = this.name === 'svelte:component';

const expression = (
this.name === 'svelte:self' ? this.compiler.name :
(isDynamicComponent ? `((${this.expression.snippet}) || @missingComponent)` :
`%components-${this.name}`)
this.name === 'svelte:self'
? this.compiler.name
: this.name === 'svelte:component'
? `((${this.expression.snippet}) || @missingComponent)`
: `%components-${this.name}`
);

this.bindings.forEach(binding => {
Expand All @@ -583,7 +583,10 @@ export default class Component extends Node {
}
}

conditions.push(`!('${binding.name}' in ctx)`);
conditions.push(
`!('${binding.name}' in ctx)`,
`${expression}.data`
);

const { name } = getObject(binding.value.node);

Expand All @@ -598,7 +601,7 @@ export default class Component extends Node {
`);
});

let open = `\${${expression}._render(__result, ${props}`;
let open = `\${@validateSsrComponent(${expression}, '${this.name}')._render(__result, ${props}`;

const options = [];
options.push(`store: options.store`);
Expand Down
11 changes: 10 additions & 1 deletion src/shared/ssr.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,13 @@ export function each(items, assign, fn) {

export const missingComponent = {
_render: () => ''
};
};

export function validateSsrComponent(component, name) {
if (!component || !component._render) {
if (name === 'svelte:component') name += 'this={...}';
throw new Error(`<${name}> is not a valid SSR component. You may need to review your build config to ensure that dependencies are compiled, rather than imported as pre-compiled modules`);
}

return component;
}

0 comments on commit 593bb03

Please sign in to comment.