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

Dynamically Rendering with Node.js Emails is Slow #430

Closed
flolu opened this issue Apr 15, 2021 · 13 comments
Closed

Dynamically Rendering with Node.js Emails is Slow #430

flolu opened this issue Apr 15, 2021 · 13 comments

Comments

@flolu
Copy link

flolu commented Apr 15, 2021

I want to use Maizzle to dynamically render emails with Node.js.
For example to include the username in the content.

Is there a recommended approach to do this? Because my current implementation is very slow:

app.get('*', async (req, res) => {
  const template = await fsPromises.readFile(
    path.join(basePath, 'src/templates', `${req.path}.html`),
  )

  const configPath = path.join(basePath, './config.production')
  const {html} = await Maizzle.render(template.toString(), {
    maizzle: require(configPath),
  })

  res.send(html)
})

...rendering one email on my Kubernetes cluster takes 18 seconds.
I guess it's because TaildwindCSS needs to be compiled every time.

There must be a better way?!

@flolu
Copy link
Author

flolu commented Apr 15, 2021

For now I am using a slightly ugly workaround:

template.html

<div>@{{username}}</div>

Will compile to

<div>{{username}}</div>

when running maizzle build production

And in my Node.js I just do

template.replace('{{username}}', 'some_user_name')

@cossssmin
Copy link
Member

Wow 18 seconds definitely doesn't sound right.

Can you please let me know:

  • Maizzle version
  • contents of your template and layout files
  • what were you doing with {{username}} before deciding to go with @{{username}} (does that data come from somewhere, maybe that's the cause of the delay?)

Ideally if you could provide a repo, even a private one, it'd be much easier for me to help.

@cossssmin
Copy link
Member

Also, check the gotchas, you should provide a Tailwind config and environment name if you want it to work as the maizzle build command does.

@naumf
Copy link

naumf commented Apr 20, 2021

For me it was caused by the latest version that comes with tailwind JIT (maybe it affects older versions too) if the purge option in tailwind.config.js is set to an array.
https://github.com/maizzle/framework/releases/tag/v3.4.0
It solves if purge is omitted or set to undefined when templates are rendered via Maizzle.render, so rendering time goes from ~15s to ~2s.
When rendered via maizzle's cli serve or build commands, the purge option is set as per the example and the rendering time is pretty fast.

@cossssmin
Copy link
Member

cossssmin commented Apr 20, 2021

For me it was caused by the latest version that comes with tailwind JIT (maybe it affects older versions too) if the purge option in tailwind.config.js is set to an array.

Yeah, mentioned this here in #422 (comment).

You need env: 'production' in the maizzle object so that process.env.NODE_ENV is set to production and Tailwind purging works properly (even in array syntax).

Try this for example, it should log a number <2000 (ms):

// render-test.js

const Maizzle = require('@maizzle/framework')

const start = new Date()

Maizzle.render(
  `your html`,
  {
    maizzle: {
      env: 'production', // sets NODE_ENV = production
      inlineCSS: {
        enabled: true,
      },
    },
    tailwind: {
      css: '@tailwind utilities;',
      config: require('./tailwind.config'),
    }
  }
)
.then(() => console.log(new Date() - start))
$ node render-test

@naumf
Copy link

naumf commented Apr 20, 2021

You need env: 'production' in the maizzle object so that process.env.NODE_ENV is set to production and Tailwind purging works properly (even in array syntax).

Yes, it works.
Thanks.

@cossssmin
Copy link
Member

@flolu does this solve your issue?

@flolu
Copy link
Author

flolu commented Apr 21, 2021

@cossssmin I solved it like this: https://github.com/flolu/maizzle-node-example
It's super fast because all templates are already built.

@flolu flolu closed this as completed Apr 21, 2021
@claudiu-clearpoint
Copy link

With the Maizzle version 3.6.1, env: 'production' and without purge or with but undefined or array and is rendering it in 5 sec which is less than 18sec but is still slow.
I also noticed the warnings:
warn - You have enabled the JIT engine which is currently in preview.
warn - Preview features are not covered by semver, may introduce breaking changes, and can change at any time.

@cossssmin
Copy link
Member

If you use Tailwind's JIT, you need to specify the purge key. The warnings are coming from Tailwind, you can disable JIT if you prefer.

@claudiu-clearpoint
Copy link

claudiu-clearpoint commented Aug 6, 2021

can you use nodejs Maizzle.render function without mode jit? that would be great, all examples were using jit. maybe will be good to have a more detailed documentation and examples for render

@cossssmin
Copy link
Member

You can pass a Tailwind config object to the render function, inside of which you can disable JIT:

https://maizzle.com/docs/nodejs/#options

@claudiu-clearpoint
Copy link

without mode jit will be 23 sec. probably the 5 sec is the best performance for the render function but because is too long, the alternative solution of the render function from flolu seems to be the only solution as it will be under 1 sec

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

No branches or pull requests

4 participants