diff --git a/README.md b/README.md index a0047b0a..cb488339 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,7 @@ The data has been taken with: `autocannon -c 100 -d 5 -p 10 localhost:3000` ``` npm install point-of-view --save ``` + ## Usage ```js @@ -91,6 +92,12 @@ and in ejs template files (for example templates/index.ejs) use something like: <% include templates/header.ejs %> ``` + +## Note + +By default views are served with the mime type 'text/html; charset=utf-8', +but you can specify a different value using the type function of reply, or by specifying the desired charset in the property 'charset' in the opts object given to the plugin. + ## Acknowledgements diff --git a/example-ejs-with-some-options.js b/example-ejs-with-some-options.js index 429d71c0..b55a8847 100644 --- a/example-ejs-with-some-options.js +++ b/example-ejs-with-some-options.js @@ -13,23 +13,25 @@ fastify.register(require('./index'), { templates: templatesFolder, options: { filename: resolve(templatesFolder) - } + }, + charset: 'utf-8' // sample usage, but specifying the same value already used as default }) fastify.get('/', (req, reply) => { - reply.type('text/html; charset=utf-8').view('index', data) + // reply.type('text/html; charset=utf-8').view('index-linking-other-pages', data) // sample for specifying with type + reply.view('index-linking-other-pages', data) }) fastify.get('/include-test', (req, reply) => { - reply.type('text/html; charset=utf-8').view('index-with-includes', data) + reply.view('index-with-includes', data) }) fastify.get('/include-one-include-missing-test', (req, reply) => { - reply.type('text/html; charset=utf-8').view('index-with-includes-one-missing', data) + reply.view('index-with-includes-one-missing', data) }) fastify.get('/include-one-attribute-missing-test', (req, reply) => { - reply.type('text/html; charset=utf-8').view('index-with-includes-and-attribute-missing', data) + reply.view('index-with-includes-and-attribute-missing', data) }) fastify.listen(3000, err => { diff --git a/index.js b/index.js index 2d7a9963..a48fc29a 100644 --- a/index.js +++ b/index.js @@ -19,6 +19,7 @@ function fastifyView (fastify, opts, next) { return } + const charset = opts.charset || 'utf-8' const engine = opts.engine[type] const options = opts.options || {} const templatesDir = resolve(opts.templates || './') @@ -59,7 +60,7 @@ function fastifyView (fastify, opts, next) { lru.set(page, compiledPage) if (!that.res.getHeader('content-type')) { - that.header('Content-Type', 'text/html') + that.header('Content-Type', 'text/html; charset=' + charset) } let cachedPage try { @@ -84,7 +85,7 @@ function fastifyView (fastify, opts, next) { if (toHtml && prod) { if (!this.res.getHeader('content-type')) { - this.header('Content-Type', 'text/html') + this.header('Content-Type', 'text/html; charset=' + charset) } this.send(toHtml(data)) return @@ -110,7 +111,7 @@ function fastifyView (fastify, opts, next) { page = getPage(page, 'ejs') engine(join(templatesDir, page), confs, (err, html) => { if (err) return this.send(err) - this.header('Content-Type', 'text/html').send(html) + this.header('Content-Type', 'text/html; charset=' + charset).send(html) }) } @@ -124,7 +125,7 @@ function fastifyView (fastify, opts, next) { page = getPage(page, 'njk') env.render(join(templatesDir, page), data, (err, html) => { if (err) return this.send(err) - this.header('Content-Type', 'text/html').send(html) + this.header('Content-Type', 'text/html; charset=' + charset).send(html) }) } @@ -148,7 +149,7 @@ function fastifyView (fastify, opts, next) { function send (that) { return function _send (err, html) { if (err) return that.send(err) - that.header('Content-Type', 'text/html').send(html) + that.header('Content-Type', 'text/html; charset=' + charset).send(html) } } } @@ -163,7 +164,7 @@ function fastifyView (fastify, opts, next) { if (toHtml && prod) { if (!this.res.getHeader('content-type')) { - this.header('Content-Type', 'text/html') + this.header('Content-Type', 'text/html; charset=' + charset) } this.send(toHtml(data)) return diff --git a/package.json b/package.json index 1fdbc3c7..36076e82 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,8 @@ "example": "node example.js", "example-with-options": "node example-ejs-with-some-options.js", "test-with-snapshot": "standard && cross-env TAP_SNAPSHOT=1 tap test-with-snapshot.js", - "test": "standard && tap test.js test-with-snapshot.js" + "test-base": "standard && tap test.js", + "test": "npm run test-base && npm run test-with-snapshot" }, "repository": { "type": "git", diff --git a/tap-snapshots/test-with-snapshot.js-TAP.test.js b/tap-snapshots/test-with-snapshot.js-TAP.test.js index 475e3536..ce63c5b8 100644 --- a/tap-snapshots/test-with-snapshot.js-TAP.test.js +++ b/tap-snapshots/test-with-snapshot.js-TAP.test.js @@ -10,26 +10,7 @@ exports[`test-with-snapshot.js TAP reply.view with ejs engine, template folder s -
- Sample header (ejs) -
-

text

- - - - - -` - -exports[`test-with-snapshot.js TAP reply.view with ejs engine, templates with folder specified, include files and attributes; requires TAP snapshots enabled; home > output 1`] = ` - - - - -

Hello from EJS Templates


Other EJS pages with includes:

@@ -48,6 +29,18 @@ exports[`test-with-snapshot.js TAP reply.view with ejs engine, templates with fo ` +exports[`test-with-snapshot.js TAP reply.view with ejs engine, templates with folder specified, include files and attributes; requires TAP snapshots enabled; home > output 1`] = ` + + + + +

Hello from EJS Templates

+
+ + + +` + exports[`test-with-snapshot.js TAP reply.view with ejs engine, templates with folder specified, include files and attributes; requires TAP snapshots enabled; page with includes > output 1`] = ` diff --git a/templates/index-linking-other-pages.ejs b/templates/index-linking-other-pages.ejs new file mode 100644 index 00000000..466a8e55 --- /dev/null +++ b/templates/index-linking-other-pages.ejs @@ -0,0 +1,20 @@ + + + + +

<%= text %>

+
+
+

Other EJS pages with includes:

+ +
+ + diff --git a/templates/index.ejs b/templates/index.ejs index 466a8e55..30761bd9 100644 --- a/templates/index.ejs +++ b/templates/index.ejs @@ -4,17 +4,5 @@

<%= text %>


-
-

Other EJS pages with includes:

-
    -
  • Normal page, here
  • -
  • One include not exist, here - (to raise errors) -
  • -
  • One attribute not exist, here - (to raise errors) -
  • -
-
diff --git a/test-with-snapshot.js b/test-with-snapshot.js index b23a5eb9..7c91b7ea 100644 --- a/test-with-snapshot.js +++ b/test-with-snapshot.js @@ -27,7 +27,8 @@ test('reply.view with ejs engine, template folder specified, include files (ejs }) fastify.get('/', (req, reply) => { - reply.type('text/html; charset=utf-8').view('index-with-includes', data) + reply.type('text/html; charset=utf-8').view('index-linking-other-pages', data) // sample for specifying with type + // reply.view('index-linking-other-pages', data) }) fastify.listen(0, err => { @@ -43,7 +44,7 @@ test('reply.view with ejs engine, template folder specified, include files (ejs t.strictEqual(response.headers['content-length'], '' + body.length) let content = null - ejs.renderFile(templatesFolder + '/index-with-includes.ejs', data, options, function (err, str) { + ejs.renderFile(templatesFolder + '/index-linking-other-pages.ejs', data, options, function (err, str) { content = str t.error(err) t.strictEqual(content.length, body.length) diff --git a/test.js b/test.js index 7b50f66d..32dedba2 100644 --- a/test.js +++ b/test.js @@ -119,7 +119,7 @@ test('reply.view with ejs engine and custom templates folder', t => { t.error(err) t.strictEqual(response.statusCode, 200) t.strictEqual(response.headers['content-length'], '' + body.length) - t.strictEqual(response.headers['content-type'], 'text/html') + t.strictEqual(response.headers['content-type'], 'text/html; charset=utf-8') t.strictEqual(ejs.render(fs.readFileSync('./templates/index.ejs', 'utf8'), data), body) fastify.close() }) @@ -153,7 +153,7 @@ test('reply.view with ejs engine and full path templates folder', t => { t.error(err) t.strictEqual(response.statusCode, 200) t.strictEqual(response.headers['content-length'], '' + body.length) - t.strictEqual(response.headers['content-type'], 'text/html') + t.strictEqual(response.headers['content-type'], 'text/html; charset=utf-8') t.strictEqual(ejs.render(fs.readFileSync('./templates/index.ejs', 'utf8'), data), body) fastify.close() }) @@ -186,7 +186,7 @@ test('reply.view with ejs engine', t => { t.error(err) t.strictEqual(response.statusCode, 200) t.strictEqual(response.headers['content-length'], '' + body.length) - t.strictEqual(response.headers['content-type'], 'text/html') + t.strictEqual(response.headers['content-type'], 'text/html; charset=utf-8') t.strictEqual(ejs.render(fs.readFileSync('./templates/index.ejs', 'utf8'), data), body) fastify.close() }) @@ -219,7 +219,7 @@ test('reply.view with pug engine', t => { t.error(err) t.strictEqual(response.statusCode, 200) t.strictEqual(response.headers['content-length'], '' + body.length) - t.strictEqual(response.headers['content-type'], 'text/html') + t.strictEqual(response.headers['content-type'], 'text/html; charset=utf-8') t.strictEqual(pug.render(fs.readFileSync('./templates/index.pug', 'utf8'), data), body) fastify.close() }) @@ -252,7 +252,7 @@ test('reply.view with handlebars engine', t => { t.error(err) t.strictEqual(response.statusCode, 200) t.strictEqual(response.headers['content-length'], '' + body.length) - t.strictEqual(response.headers['content-type'], 'text/html') + t.strictEqual(response.headers['content-type'], 'text/html; charset=utf-8') t.strictEqual(handlebars.compile(fs.readFileSync('./templates/index.html', 'utf8'))(data), body) fastify.close() }) @@ -285,7 +285,7 @@ test('reply.view with marko engine', t => { t.error(err) t.strictEqual(response.statusCode, 200) t.strictEqual(response.headers['content-length'], '' + body.length) - t.strictEqual(response.headers['content-type'], 'text/html') + t.strictEqual(response.headers['content-type'], 'text/html; charset=utf-8') t.strictEqual(marko.load('./templates/index.marko').renderToString(data), body) fastify.close() }) @@ -384,7 +384,7 @@ test('reply.view with ejs-mate engine', t => { t.error(err) t.strictEqual(response.statusCode, 200) t.strictEqual(response.headers['content-length'], '' + body.length) - t.strictEqual(response.headers['content-type'], 'text/html') + t.strictEqual(response.headers['content-type'], 'text/html; charset=utf-8') t.strictEqual('

header

text
footer
', body) fastify.close() }) @@ -418,7 +418,7 @@ test('reply.view with nunjucks engine and custom templates folder', t => { t.error(err) t.strictEqual(response.statusCode, 200) t.strictEqual(response.headers['content-length'], '' + body.length) - t.strictEqual(response.headers['content-type'], 'text/html') + t.strictEqual(response.headers['content-type'], 'text/html; charset=utf-8') // Global Nunjucks templates dir changed here. t.strictEqual(nunjucks.render('./index.njk', data), body) fastify.close() @@ -453,7 +453,7 @@ test('reply.view with nunjucks engine and full path templates folder', t => { t.error(err) t.strictEqual(response.statusCode, 200) t.strictEqual(response.headers['content-length'], '' + body.length) - t.strictEqual(response.headers['content-type'], 'text/html') + t.strictEqual(response.headers['content-type'], 'text/html; charset=utf-8') // Global Nunjucks templates dir changed here. t.strictEqual(nunjucks.render('./index.njk', data), body) fastify.close() @@ -488,7 +488,7 @@ test('reply.view with nunjucks engine and includeViewExtension is true', t => { t.error(err) t.strictEqual(response.statusCode, 200) t.strictEqual(response.headers['content-length'], '' + body.length) - t.strictEqual(response.headers['content-type'], 'text/html') + t.strictEqual(response.headers['content-type'], 'text/html; charset=utf-8') // Global Nunjucks templates dir is `./` here. t.strictEqual(nunjucks.render('./templates/index.njk', data), body) fastify.close() @@ -523,7 +523,7 @@ test('reply.view with ejs engine and includeViewExtension property as true', t = t.error(err) t.strictEqual(response.statusCode, 200) t.strictEqual(response.headers['content-length'], '' + body.length) - t.strictEqual(response.headers['content-type'], 'text/html') + t.strictEqual(response.headers['content-type'], 'text/html; charset=utf-8') t.strictEqual(ejs.render(fs.readFileSync('./templates/index.ejs', 'utf8'), data), body) fastify.close() }) @@ -552,7 +552,8 @@ test('reply.view with ejs engine, template folder specified, include files (ejs }) fastify.get('/', (req, reply) => { - reply.type('text/html; charset=utf-8').view('index-with-includes', data) + reply.type('text/html; charset=utf-8').view('index-linking-other-pages', data) // sample for specifying with type + // reply.view('index-with-includes', data) }) fastify.listen(0, err => { @@ -568,7 +569,7 @@ test('reply.view with ejs engine, template folder specified, include files (ejs t.strictEqual(response.headers['content-length'], '' + body.length) let content = null - ejs.renderFile(templatesFolder + '/index-with-includes.ejs', data, options, function (err, str) { + ejs.renderFile(templatesFolder + '/index-linking-other-pages.ejs', data, options, function (err, str) { content = str t.error(err) t.strictEqual(content.length, body.length)