Repository: sveltejs/svelte (JavaScript)
Date: 2017-12-24 16:19:21+00:00
Task type: Bug Fix

Issue:
SSR component render() toString method returns undefined
When using `${app.render()}` in a template literal, I get undefined. That seems to be caused by a bug in `toString()`:
```
var result = { head: '', addComponent: addComponent };
...
var html = AddToCart._render(result, state, options);
return {
    html: html,
    head: result.head,
    css: { code: cssCode, map: null },
    toString: function toString() {
        return result.html;                <-- probably should return html
    }
};
```
Workaround: use `${app.render().html}` in template literals.
SSR component render() toString method returns undefined
When using `${app.render()}` in a template literal, I get undefined. That seems to be caused by a bug in `toString()`:
```
var result = { head: '', addComponent: addComponent };
...
var html = AddToCart._render(result, state, options);
return {
    html: html,
    head: result.head,
    css: { code: cssCode, map: null },
    toString: function toString() {
        return result.html;                <-- probably should return html
    }
};
```
Workaround: use `${app.render().html}` in template literals.


Issue discussion:

Solution patch:
diff --git a/src/generators/server-side-rendering/index.ts b/src/generators/server-side-rendering/index.ts
--- a/src/generators/server-side-rendering/index.ts
+++ b/src/generators/server-side-rendering/index.ts
@@ -124,7 +124,7 @@ export default function ssr(
 				head: result.head,
 				css: { code: cssCode, map: null },
 				toString() {
-					return result.html;
+					return html;
 				}
 			};
 		}


Test patch:
diff --git a/test/js/samples/ssr-no-oncreate-etc/expected-bundle.js b/test/js/samples/ssr-no-oncreate-etc/expected-bundle.js
--- a/test/js/samples/ssr-no-oncreate-etc/expected-bundle.js
+++ b/test/js/samples/ssr-no-oncreate-etc/expected-bundle.js
@@ -25,7 +25,7 @@ SvelteComponent.render = function(state, options = {}) {
 		head: result.head,
 		css: { code: cssCode, map: null },
 		toString() {
-			return result.html;
+			return html;
 		}
 	};
 };
diff --git a/test/js/samples/ssr-no-oncreate-etc/expected.js b/test/js/samples/ssr-no-oncreate-etc/expected.js
--- a/test/js/samples/ssr-no-oncreate-etc/expected.js
+++ b/test/js/samples/ssr-no-oncreate-etc/expected.js
@@ -27,7 +27,7 @@ SvelteComponent.render = function(state, options = {}) {
 		head: result.head,
 		css: { code: cssCode, map: null },
 		toString() {
-			return result.html;
+			return html;
 		}
 	};
 }
diff --git a/test/server-side-rendering/index.js b/test/server-side-rendering/index.js
--- a/test/server-side-rendering/index.js
+++ b/test/server-side-rendering/index.js
@@ -59,7 +59,11 @@ describe("ssr", () => {
 
 				const data = tryToLoadJson(`${dir}/data.json`);
 
-				const { html, css, head } = component.render(data);
+				const rendered = component.render(data);
+				const { html, css, head } = rendered;
+
+				// rendered.toString() === rendered.html
+				assert.equal(rendered, html);
 
 				fs.writeFileSync(`${dir}/_actual.html`, html);
 				if (css.code) fs.writeFileSync(`${dir}/_actual.css`, css.code);