Skip to content

Docs update for includeFiles and excludeFiles options on netlify adapter #10760

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

Merged
merged 7 commits into from
Feb 13, 2025
84 changes: 84 additions & 0 deletions src/content/docs/en/guides/integrations-guide/netlify.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ category: adapter
i18nReady: true
---
import PackageManagerTabs from '~/components/tabs/PackageManagerTabs.astro'
import Since from '~/components/Since.astro';

This adapter allows Astro to deploy your [on-demand rendered routes](/en/guides/on-demand-rendering/) to [Netlify](https://www.netlify.com/).

Expand Down Expand Up @@ -244,6 +245,89 @@ With [fine-grained cache control](https://www.netlify.com/blog/swr-and-fine-grai
standard caching headers like `CDN-Cache-Control` or `Vary`.
Refer to the docs to learn about implementing e.g. time to live (TTL) or stale while revalidate (SWR) caching: https://docs.netlify.com/platform/caching

### Including or excluding files from Netlify Functions

When deploying an Astro site with on-demand rendering to Netlify, the generated functions automatically trace and include server dependencies. However, you may need to customize which files are included in your Netlify Functions.

#### `includeFiles`

<p>
**Type:** `string[]`<br />
**Default:** `[]`<br />
<Since v="5.3.0" />
</p>

The `includeFiles` property allows you to explicitly specify additional files that should be bundled with your function. This is useful for files that aren't automatically detected as dependencies, such as:
- Data files loaded using `fs` operations
- Configuration files
- Template files

Provide an array of additional files to include with file paths relative to your project's [`root`](/en/reference/configuration-reference/#root). Absolute paths may not work as expected.

```js title="astro.config.mjs" ins={8}
import { defineConfig } from 'astro/config';
import netlify from '@astrojs/netlify';

export default defineConfig({
// ...
output: 'server',
adapter: netlify({
includeFiles: ['./my-data.json'], // relative to `root`
}),
});
```

#### `excludeFiles`

<p>
**Type:** `string[]`<br />
**Default:** `[]`<br />
<Since v="5.3.0" />
</p>

You can use the `excludeFiles` property to prevent specific files from being bundled that would otherwise be included. This is helpful for:
- Reducing bundle size
- Excluding large binaries
- Preventing unwanted files from being deployed

Provide an array of specific files to exclude with file paths relative to your project's [`root`](/en/reference/configuration-reference/#root). Absolute paths may not work as expected.

```js title="astro.config.mjs" ins={8}
import { defineConfig } from 'astro/config';
import netlify from '@astrojs/netlify';

export default defineConfig({
// ...
output: 'server',
adapter: netlify({
excludeFiles: ['./src/some_big_file.jpg'], // relative to `root`
}),
});
```

#### Using glob patterns

Both `includeFiles` and `excludeFiles` support [glob patterns](/en/guides/imports/#glob-patterns) for matching multiple files:

```js title="astro.config.mjs" ins={8-9, 12-13}
import { defineConfig } from 'astro/config';
import netlify from '@astrojs/netlify';

export default defineConfig({
output: 'server',
adapter: netlify({
includeFiles: [
'./data/**/*.json',
'./config/?(dev|prod).yml'
],
excludeFiles: [
'./node_modules/package/**/*',
'./src/**/*.test.js'
]
}),
});
```

## Examples

* The [Astro Netlify Edge Starter](https://github.com/sarahetter/astro-netlify-edge-starter) provides an example and a guide in the README.
Expand Down