astro/packages/integrations/netlify
2022-06-27 21:22:26 +00:00
..
src [ci] format 2022-06-27 21:22:26 +00:00
test Enables eslint on the full repo and adds a rule for no only() tests (#3659) 2022-06-22 15:59:49 +00:00
.npmignore Ignore test folder in Netlify 2022-04-19 11:45:19 -04:00
CHANGELOG.md [ci] release (#3666) 2022-06-22 10:05:21 -04:00
package.json [ci] release (#3666) 2022-06-22 10:05:21 -04:00
README.md Adds support base64 encoding in Netlify Functions (#3592) 2022-06-15 19:49:09 +00:00
tsconfig.json [ci] format 2022-04-22 14:04:54 +00:00

@astrojs/netlify

Deploy your server-side rendered (SSR) Astro app to Netlify.

Use this adapter in your Astro configuration file, alongside a valid deployment URL:

import { defineConfig } from 'astro/config';
import netlify from '@astrojs/netlify/functions';

export default defineConfig({
	adapter: netlify(),
});

After you build your site the netlify/ folder will contain Netlify Functions in the netlify/functions/ folder.

Now you can deploy!

netlify deploy --build

Edge Functions

Netlify has two serverless platforms, Netlify Functions and Netlify Edge Functions. With Edge Functions your code is distributed closer to your users, lowering latency. You can use Edge Functions by changing the import in your astro configuration file:

import { defineConfig } from 'astro/config';
- import netlify from '@astrojs/netlify/functions';
+ import netlify from '@astrojs/netlify/edge-functions';

export default defineConfig({
	adapter: netlify(),
});

Configuration

dist

We build to a dist directory at the base of your project. To change this, use the dist option:

import { defineConfig } from 'astro/config';
import netlify from '@astrojs/netlify/functions';

export default defineConfig({
  adapter: netlify({
    dist: new URL('./dist/', import.meta.url)
  })
});

And then point to the dist in your netlify.toml:

[functions]
  directory = "dist/functions"

binaryMediaTypes

This option is only needed for the Functions adapter and is not needed for Edge Functions.

Netlify Functions sending binary data in the body need to be base64 encoded. The @astrojs/netlify/functions adapter handles this automatically based on the Content-Type header.

We check for common mime types for audio, image, and video files. To include specific mime types that should be treated as binary data, include the binaryMediaTypes option with a list of binary mime types.

import fs from 'node:fs';

export function get() {
	const buffer = fs.readFileSync('../image.jpg');

  // Return the buffer directly, @astrojs/netlify will base64 encode the body
  return new Response(buffer, {
    status: 200,
		headers: {
			'content-type': 'image/jpeg'
		}
  });
}