I’m trying to load a mjml file as a string that’s then sent to SendGrid after being compiled but I’m having trouble deploying it along with my Netlify function.
I’ve also added this command to my build process to try to copy them in the correct folder for Netlify. cp -r ../email/src/templates dist/graphql/emails
However when running the call I get the error:
ENOENT: no such file or directory, open '/var/task/emails/welcome.mjml'
What’s the correct directory I need to copy these files into to ensure they’re deployed with my API?
import {template} from './templates.ts'
export async function getTemplate(name) {
return template
}
If you are fetching these files at runtime, perhaps consider using S3? Once you fetch the template, you can upload it to S3 and save the URL in your db. It’s probably a more scalable way of storing files - if you’re using lambdas or even k8s, storing files locally doesn’t guarantee its available in every instance (unless you mount some sort of shared storage)
I’m trying to avoid saving them as .js files because the source files are .mjml in the same monorepo and it would duplicate code or ruin my live-reload/dev experience when building them. I might just copy them the web/public folder and load them with the request npm module if there’s no way to save them locally.
I was under the impression if you upload files into a folder with the same name as the function it will copy them to the lambda as well. This is based off a Netlify employee’s posts here and the example here
Is there a difference between how services and functions are deployed? Not sure I follow, I’m sending emails on user registration, password reset, etc performed via GraphQL mutations.
Since lambda functions are self-contained, that would only work if your build pipeline copies each required file to your function folder and explicitly bundles it up. Our buildbot will not natively handle what you described, though if you bundle your function manually things should be doable.
We build your code and then hand it off to the build-bot, which zips up each Serverless Function (using zip it and ship it). Zip it and Ship It looks at the imports in each Serverless Function and only includes those, so, unless you’re importing these files they won’t be included.
The alternative is to take care of zip-it-and-ship-it yourself by doing the following:
If you do put in src/functions there is an issue (I think) where redwood dev server only doesn’t like functions in directories. So a flat structure like:
// sendMail.js
fs.readFileSync(./template.mjml)
Might work?
Edit: Actually because of the directory issue, it may not.
That makes way more sense now, thanks. I’m thinking a netlify build plugin would solve all of this pretty easily right? Will report back with my findings.
Yeah. It’s much easier to just use template literals like recommended with MJML but my app is heavily focused around emails and will likely have over 50 so a nice developer experience will be absolutely vital, plus I love the challenge. This way I get to keep hot reloading without duplicating any code. Appreciate all the support!
This is great . Thank you again for sharing on the forums, I love the idea of “community docs”. I’m going to change the title to make it a little more easily searchable!