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

[Solved] Minify HTML with EJS #2188

Closed
marceloboeira opened this issue Sep 4, 2014 · 20 comments
Closed

[Solved] Minify HTML with EJS #2188

marceloboeira opened this issue Sep 4, 2014 · 20 comments

Comments

@marceloboeira
Copy link

Why this doest work?

var minify = require('html-minifier').minify;
var ejs = require('ejs');
var parsing = function(path,options,fn) {
    ejs.renderFile(path, options, function(err, str){
      str = minify(str,{collapseWhitespace: true});
      return fn(err, str);
    });

};

module.exports.views = {
  engine: {
    ext: 'ejs',
    fn: parsing
  },
  layout: 'layout/default'
};
@sgress454
Copy link
Member

This forum is for reporting bugs in the Sails core; support questions should go in the Google Group or StackOverflow (where you've already posted this).

@marceloboeira
Copy link
Author

Alright 👍

@marceloboeira
Copy link
Author

@sgress454 the StackOverflow thread is closed, and I still have no answer. 👎

@sahanDissanayake
Copy link

@sgress454 Would be really nice to know how to do this.

As

https://groups.google.com/forum/#!topic/sailsjs/zSxv7uJEVrM
http://stackoverflow.com/questions/26051280/minify-all-of-sailsjs-served-html

There is no answer right now.

Thank you.

@marceloboeira
Copy link
Author

+1

@sgress454
Copy link
Member

@marceloboeira unfortunately I couldn't get that SO question re-opened, where I had basically answered you in the comments. The problem is that when you declare a default layout with EJS, Sails overrides your view configuration in order to use ejs-locals to incorporate layout support that was stripped out of Express. So you can set layout: false, and your code will work...almost. It won't have any layout, which is probably not what you want. Instead, you can use ejs-locals yourself:

var minify = require('html-minifier').minify;
var ejs = require('ejs-locals');
var parsing = function(path,options,fn) {
    options.locals = options.locals || {};
    options.locals._layoutFile = 'layout.ejs';
    ejs(path, options, function(err, str){
      str = minify(str,{collapseWhitespace: true, removeComments: true});
      return fn(err, str);
    });

};

module.exports.views = {
  engine: {
    ext: 'ejs',
    fn: parsing
  },
  layout: false
};

should do the trick.

@marceloboeira marceloboeira changed the title Minify HTML with EJS [Solved] Minify HTML with EJS Sep 26, 2014
@marceloboeira
Copy link
Author

@sgress454 thanks! It worked!

Only this line is different:

 options.locals._layoutFile = '/layout.ejs';

@sgress454
Copy link
Member

@marceloboeira Works both ways for me. In any case why don't you go ahead and post this as an answer on the new StackOverflow question!

@marceloboeira
Copy link
Author

@sgress454 alright!

@mewben
Copy link
Contributor

mewben commented Nov 6, 2014

👍 It worked... This would be super useful if enabled by default in production...

@sahanDissanayake
Copy link

Keep an eye out for the time it takes when everytime the page is requested and the time you receive the file

For me normal file was sent in 179ms and when minify was enabled it took up to 900ms

So there is a problem here.

@mewben
Copy link
Contributor

mewben commented Nov 6, 2014

@sahanDissanayake How do you test the time? Newbie here...

@sahanDissanayake
Copy link

@mewben If you are using Google Chrome use https://chrome.google.com/webstore/detail/postman-rest-client/fdmmgilgnpjigdojojpjoooidkmcomcm?hl=en

And then you can just enter the link and then you will see the results. Let me know

@mewben
Copy link
Contributor

mewben commented Nov 6, 2014

Seems ok to me... I'm using a fresh sails install in my local dev...

@mewben
Copy link
Contributor

mewben commented Nov 6, 2014

I mean no noticeable difference at all... without minification, I got 75ms... With minification, i got 65ms. on homepage...

@iswanj
Copy link

iswanj commented Mar 19, 2015

how can we use it for multiple layouts ?

@marceloboeira
Copy link
Author

@iswanj tricky question, ...

@marceloboeira
Copy link
Author

@mewben I should test several times the same page, I didn't have the time to test it yet.

@iswanj
Copy link

iswanj commented Mar 26, 2015

@marceloboeira I already solved that

var minify = require('html-minifier').minify;
var ejs = require('ejs-locals');
var parsing = function(path, options, fn) {
  options.locals = options.locals || {};

  // use default 'layout.ejs' if res.locals.layout is not set
  options.locals._layoutFile = '../' + options.layout;
  ejs(path, options, function(err, str) {
    str = minify(str, {collapseWhitespace: true, removeComments: true});
    return fn(err, str);
  });
};

module.exports.views = {
  engine: {
    ext: 'ejs',
    fn: parsing
  },
  layout: false

};

@marceloboeira
Copy link
Author

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

5 participants