0
0
mirror of https://github.com/sveltejs/svelte.git synced 2024-11-30 00:46:29 +01:00

server-side <:Head>

This commit is contained in:
Rich Harris 2017-12-13 19:40:32 -05:00
parent 50654fab88
commit ed44f7d1fa
9 changed files with 21 additions and 19 deletions

View File

@ -110,14 +110,14 @@ export default function ssr(
components.add(component);
}
var result = { title: null, addComponent };
var result = { head: '', addComponent };
var html = ${name}._render(result, state, options);
var cssCode = Array.from(components).map(c => c.css && c.css.code).filter(Boolean).join('\\n');
return {
html,
title: result.title,
head: result.head,
css: { code: cssCode, map: null },
toString() {
return result.html;

View File

@ -2,17 +2,18 @@ import { SsrGenerator } from '../index';
import Block from '../Block';
import { Node } from '../../../interfaces';
import stringifyAttributeValue from './shared/stringifyAttributeValue';
import visit from '../visit';
export default function visitDocument(
generator: SsrGenerator,
block: Block,
node: Node
) {
throw new Error('TODO');
generator.append('${(__result.head += `');
// const title = node.attributes.find(attribute => attribute.type === 'Attribute' && attribute.name === 'title');
node.children.forEach((child: Node) => {
visit(generator, block, child);
});
// if (title) {
// generator.append('${(__result.title = `' + stringifyAttributeValue(block, title.value) + '`, "")}');
// }
generator.append('`, "")}');
}

View File

@ -11,14 +11,14 @@ SvelteComponent.render = function(state, options = {}) {
components.add(component);
}
var result = { title: null, addComponent };
var result = { head: '', addComponent };
var html = SvelteComponent._render(result, state, options);
var cssCode = Array.from(components).map(c => c.css && c.css.code).filter(Boolean).join('\n');
return {
html,
title: result.title,
head: result.head,
css: { code: cssCode, map: null },
toString() {
return result.html;

View File

@ -13,14 +13,14 @@ SvelteComponent.render = function(state, options = {}) {
components.add(component);
}
var result = { title: null, addComponent };
var result = { head: '', addComponent };
var html = SvelteComponent._render(result, state, options);
var cssCode = Array.from(components).map(c => c.css && c.css.code).filter(Boolean).join('\n');
return {
html,
title: result.title,
head: result.head,
css: { code: cssCode, map: null },
toString() {
return result.html;

View File

@ -1,6 +1,4 @@
export default {
solo: true,
data: {
adjective: 'custom'
},

View File

@ -59,7 +59,7 @@ describe("ssr", () => {
const data = tryToLoadJson(`${dir}/data.json`);
const { html, css, title } = component.render(data);
const { html, css, head } = component.render(data);
fs.writeFileSync(`${dir}/_actual.html`, html);
if (css.code) fs.writeFileSync(`${dir}/_actual.css`, css.code);
@ -70,10 +70,11 @@ describe("ssr", () => {
expectedCss.replace(/^\s+/gm, "")
);
if (fs.existsSync(`${dir}/title.txt`)) {
assert.equal(
title,
fs.readFileSync(`${dir}/title.txt`, 'utf-8')
if (fs.existsSync(`${dir}/_expected-head.html`)) {
fs.writeFileSync(`${dir}/_actual-head.html`, head);
assert.htmlEqual(
head,
fs.readFileSync(`${dir}/_expected-head.html`, 'utf-8')
);
}

View File

@ -0,0 +1,2 @@
<title>a custom title</title>

View File

@ -0,0 +1 @@
<title>a custom title</title>

View File

@ -1 +0,0 @@
a custom title