diff --git a/lib/index.js b/lib/index.js
index 93cee47a4..4d7647242 100644
--- a/lib/index.js
+++ b/lib/index.js
@@ -1,5 +1,6 @@
const fs = require('fs');
const path = require('path');
+const serialize = require('serialize-javascript');
const route_manager = require('./route_manager.js');
const templates = require('./templates.js');
const create_app = require('./utils/create_app.js');
@@ -144,21 +145,31 @@ function get_route_handler(fn) {
if (mod.preload) {
const promise = Promise.resolve(mod.preload(req)).then(preloaded => {
+ const serialized = try_serialize(preloaded);
Object.assign(data, preloaded);
- return mod.render(data);
+
+ return { rendered: mod.render(data), serialized };
});
return templates.stream(res, 200, {
- main: client.main_file,
- html: promise.then(rendered => rendered.html),
- head: promise.then(({ head }) => `${head}`),
- styles: promise.then(({ css }) => (css && css.code ? `` : ''))
+ scripts: promise.then(({ serialized }) => {
+ const main = ``;
+
+ if (serialized) {
+ return `${main}`;
+ }
+
+ return main;
+ }),
+ html: promise.then(({ rendered }) => rendered.html),
+ head: promise.then(({ rendered }) => `${rendered.head}`),
+ styles: promise.then(({ rendered }) => (rendered.css && rendered.css.code ? `` : ''))
});
} else {
const { html, head, css } = mod.render(data);
const page = templates.render(200, {
- main: client.main_file,
+ scripts: ``,
html,
head: `${head}`,
styles: (css && css.code ? `` : '')
@@ -221,7 +232,7 @@ function get_not_found_handler(fn) {
title: 'Not found',
status: 404,
method: req.method,
- main: asset_cache.client.main_file,
+ scripts: ``,
url: req.url
}));
};
@@ -249,4 +260,12 @@ function compose_handlers(handlers) {
function read_json(file) {
return JSON.parse(fs.readFileSync(file, 'utf-8'));
+}
+
+function try_serialize(data) {
+ try {
+ return serialize(data);
+ } catch (err) {
+ return null;
+ }
}
\ No newline at end of file
diff --git a/lib/templates.js b/lib/templates.js
index 8260aa871..058325cdc 100644
--- a/lib/templates.js
+++ b/lib/templates.js
@@ -1,10 +1,22 @@
const fs = require('fs');
const glob = require('glob');
+const chalk = require('chalk');
const chokidar = require('chokidar');
+const framer = require('code-frame');
+const { locate } = require('locate-character');
const { dev } = require('./config.js');
let templates;
+function error(e) {
+ if (e.title) console.error(chalk.bold.red(e.title));
+ if (e.body) console.error(chalk.red(e.body));
+ if (e.url) console.error(chalk.cyan(e.url));
+ if (e.frame) console.error(chalk.grey(e.frame));
+
+ process.exit(1);
+}
+
function create_templates() {
templates = glob.sync('*.html', { cwd: 'templates' })
.map(file => {
@@ -12,7 +24,24 @@ function create_templates() {
const status = file.replace('.html', '').toLowerCase();
if (!/^[0-9x]{3}$/.test(status)) {
- throw new Error(`Bad template — should be a valid status code like 404.html, or a wildcard like 2xx.html`);
+ error({
+ title: `templates/${file}`,
+ body: `Bad template — should be a valid status code like 404.html, or a wildcard like 2xx.html`
+ });
+ }
+
+ const index = template.indexOf('%sapper.main%');
+ if (index !== -1) {
+ // TODO remove this in a future version
+ const { line, column } = locate(template, index, { offsetLine: 1 });
+ const frame = framer(template, line, column);
+
+ error({
+ title: `templates/${file}`,
+ body: `
\ No newline at end of file
+
diff --git a/test/app/templates/2xx.html b/test/app/templates/2xx.html
index e75cd1252..e1444c557 100644
--- a/test/app/templates/2xx.html
+++ b/test/app/templates/2xx.html
@@ -32,6 +32,6 @@
-
+ %sapper.scripts%