Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue#29, set default charset - second try #35

Merged
merged 3 commits into from
Feb 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

<a name="usage"></a>
## Usage
```js
Expand Down Expand Up @@ -91,6 +92,12 @@ and in ejs template files (for example templates/index.ejs) use something like:
<% include templates/header.ejs %>
```

<a name="note"></a>
## 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

Expand Down
12 changes: 7 additions & 5 deletions example-ejs-with-some-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down
13 changes: 7 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 || './')
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -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)
})
}

Expand All @@ -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)
})
}

Expand All @@ -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)
}
}
}
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
31 changes: 12 additions & 19 deletions tap-snapshots/test-with-snapshot.js-TAP.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,7 @@ exports[`test-with-snapshot.js TAP reply.view with ejs engine, template folder s
<html lang="en">
<head></head>
<body>
<header>
Sample header (ejs)
</header>

<p>text</p>
<footer>
Sample footer (html) - Back to <a href="/">Home</a>
</footer>

</body>
</html>

`

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`] = `
<!DOCTYPE html>
<html lang="en">
<head></head>
<body>
<p>Hello from EJS Templates</p>
<br/>
<div>
<p>Other EJS pages with includes:</p>
Expand All @@ -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`] = `
<!DOCTYPE html>
<html lang="en">
<head></head>
<body>
<p>Hello from EJS Templates</p>
<br/>
</body>
</html>

`

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`] = `
<!DOCTYPE html>
<html lang="en">
Expand Down
20 changes: 20 additions & 0 deletions templates/index-linking-other-pages.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!DOCTYPE html>
<html lang="en">
<head></head>
<body>
<p><%= text %></p>
<br/>
<div>
<p>Other EJS pages with includes:</p>
<ul>
<li>Normal page, <a href="/include-test">here</a></li>
<li>One include not exist, <a href="/include-one-include-missing-test">here</a>
(to raise errors)
</li>
<li>One attribute not exist, <a href="/include-one-attribute-missing-test">here</a>
(to raise errors)
</li>
</ul>
<div>
</body>
</html>
12 changes: 0 additions & 12 deletions templates/index.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,5 @@
<body>
<p><%= text %></p>
<br/>
<div>
<p>Other EJS pages with includes:</p>
<ul>
<li>Normal page, <a href="/include-test">here</a></li>
<li>One include not exist, <a href="/include-one-include-missing-test">here</a>
(to raise errors)
</li>
<li>One attribute not exist, <a href="/include-one-attribute-missing-test">here</a>
(to raise errors)
</li>
</ul>
<div>
</body>
</html>
5 changes: 3 additions & 2 deletions test-with-snapshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand All @@ -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)
Expand Down
27 changes: 14 additions & 13 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
Expand Down Expand Up @@ -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()
})
Expand Down Expand Up @@ -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()
})
Expand Down Expand Up @@ -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()
})
Expand Down Expand Up @@ -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()
})
Expand Down Expand Up @@ -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()
})
Expand Down Expand Up @@ -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('<html><head></head><body><h1>header</h1><div>text</div><div>footer</div></body></html>', body)
fastify.close()
})
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
})
Expand Down Expand Up @@ -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 => {
Expand All @@ -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)
Expand Down