Home
Support Forums

Do Gatsby functions work with the included_files property?

I’ve given Gatsby functions a test drive since they work with Netlify. I’ve got the majority of the functionality for my lambda function working, but I’m hung up on one thing…including additional files that the function can use.

My function is designed to accept a POST request from a contact form on a website and then send an HTML email to an email address. To create the body of the email, I’m using EJS so that I can have an HTML template, fill in some variables received from the contact form, and then send it through Mailgun.

functions
└── sendmail
    ├── email_templates
    │   └── contact.ejs
    └── sendmail.ts

When I implement this with Netlify, I use the included_files property as described in this article.

I do it like this in netlify.toml.

[functions."sendmail"]
  included_files = ["functions/sendmail/email_templates/*.ejs"]

However, I can’t seem to get those included_files working with the Gatsby function.

Does anyone know if the “included_files” property works with Gatsby functions?

Hi @dmorda,

I do believe it should work. What happens if you add it like:

[functions]
  included_files = ["functions/sendmail/email_templates/*.ejs"]

instead of how you have it now - just to eliminate possible causes.

No luck. When I run it locally the Gatsby function outputs the following:

.cache/functions/
├── manifest.json
└── sendmail
    └── sendmail.js

As you’ll notice, the included files are not present. However, when I run the same function re-purposed for Netlify, the included files do show up locally:

.netlify/
└── functions-serve
    └── sendmail
        ├── sendmail.js
        └── src
            └── functions
                └── sendmail
                    ├── email_templates
                    │   └── contact.ejs
                    ├── sendmail.js
                    └── sendmail.js.map

Here is my netlify.toml file and the output that shows up the for the gatsby function on Netlify.

netlify.toml

[build]
  command = "npm run build"
  publish = "public/"
  functions = "functions"

[functions]
  included_files = ["src/api/sendmail/email_templates/*.ejs"]

[[plugins]]
package = "@netlify/plugin-gatsby"

Here’s the output from the gatsby function on Netlify.

12:29:04 PM: 74e298a1 INFO   /var/task/functions/gatsby/functions/sendmail/email_templates/contact.ejs
12:29:04 PM: 74e298a1 ERROR  - Error rendering EJS template for a form named 'contact'.

Hi @dmorda,

Thank you for sharing this. I’ve pinged the developers to see what they have to say about this.

Hi @dmorda
The included_files aren’t copied into .cache/functions, which is generated by Gatsby. The function generated by Netlify won’t be called “sendmail”, but something like __gatsby-handler depending on the version of your build plugin. If you run netlify build locally it will create a zipfile in .netlify/functions called something like __gatsby-handler.zip. If you look inside there, either by running zipinfo or by unzipping it and opening the folder in an IDE or somewhere else that shows hidden folders, and see what files are included. You’ll also see it in functions-serve if you run netlify dev.

1 Like

Thanks for the clarification @ascorbic, I should have tried the build step myself and explored the generated file. Although I somewhat like the idea of using the “included_files” better, I ended up sorting this out as a Gatsby function.

I simplified my email templates into a single one flexible enough to handle any email that I need to generated. I then used an “import” statement that specifies that the raw-loader should be used.

import emailTemplate from '!raw-loader!./email_templates/default.ejs'

Now the template is bundled into the Gatsby function as a dependency and that seemed to do the trick.

1 Like

Glad everything is working for you now, @dmorda :netliconfetti: Thanks for outlining the exact steps you took, this will definitely be beneficial for future Forums members who encounter something similar.