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
-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
+<%= text %>
+Other EJS pages with includes:
+<%= text %>
Other EJS pages with includes:
-