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

feat: enable vercel image optimization #8667

Merged
merged 6 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/flat-clocks-listen.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/adapter-vercel': minor
---

feat: expose vercel image optimization config in adapter config
14 changes: 13 additions & 1 deletion documentation/docs/25-build-and-deploy/90-adapter-vercel.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import adapter from '@sveltejs/adapter-vercel';
export default {
kit: {
adapter: adapter({
// see the 'Deployment configuration' section below
// see below for options that can be set here
})
}
};
Expand Down Expand Up @@ -64,6 +64,18 @@ And the following option apply to serverless functions:

If your functions need to access data in a specific region, it's recommended that they be deployed in the same region (or close to it) for optimal performance.

## Image Optimization

You may set the `images` config to control how Vercel builds your images. See the [image configuration reference](https://vercel.com/docs/build-output-api/v3/configuration#images) for full details. As an example, you may set:
benmccann marked this conversation as resolved.
Show resolved Hide resolved

```
{
sizes: [640, 828, 1200, 1920, 3840],
formats: ['image/avif', 'image/webp'],
minimumCacheTTL: 300
}
```

## Incremental Static Regeneration

Vercel supports [Incremental Static Regeneration](https://vercel.com/docs/concepts/incremental-static-regeneration/overview) (ISR), which provides the performance and cost advantages of prerendered content with the flexibility of dynamically rendered content.
Expand Down
26 changes: 26 additions & 0 deletions packages/adapter-vercel/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ export interface ServerlessConfig {
* If `true`, this route will always be deployed as its own separate function
*/
split?: boolean;

/**
* https://vercel.com/docs/build-output-api/v3/configuration#images
*/
images?: ImagesConfig;

/**
* [Incremental Static Regeneration](https://vercel.com/docs/concepts/incremental-static-regeneration/overview) configuration.
* Serverless only.
Expand All @@ -53,6 +59,26 @@ export interface ServerlessConfig {
| false;
}

type ImageFormat = 'image/avif' | 'image/webp';

type RemotePattern = {
protocol?: 'http' | 'https';
hostname: string;
port?: string;
pathname?: string;
};

type ImagesConfig = {
benmccann marked this conversation as resolved.
Show resolved Hide resolved
sizes: number[];
domains: string[];
remotePatterns?: RemotePattern[];
minimumCacheTTL?: number; // seconds
formats?: ImageFormat[];
dangerouslyAllowSVG?: boolean;
contentSecurityPolicy?: string;
contentDispositionType?: string;
};

export interface EdgeConfig {
/**
* Whether to use [Edge Functions](https://vercel.com/docs/concepts/functions/edge-functions) or [Serverless Functions](https://vercel.com/docs/concepts/functions/serverless-functions)
Expand Down
15 changes: 11 additions & 4 deletions packages/adapter-vercel/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const plugin = function (defaults = {}) {
functions: `${dir}/functions`
};

const static_config = static_vercel_config(builder);
const static_config = static_vercel_config(builder, defaults);

builder.log.minor('Generating serverless function...');

Expand Down Expand Up @@ -368,14 +368,20 @@ function write(file, data) {
}

// This function is duplicated in adapter-static
/** @param {import('@sveltejs/kit').Builder} builder */
function static_vercel_config(builder) {
/**
* @param {import('@sveltejs/kit').Builder} builder
* @param {import('.').Config} config
*/
function static_vercel_config(builder, config) {
/** @type {any[]} */
const prerendered_redirects = [];

/** @type {Record<string, { path: string }>} */
const overrides = {};

/** @type {import('./index').ImagesConfig} */
const images = config.images;

for (const [src, redirect] of builder.prerendered.redirects) {
prerendered_redirects.push({
src,
Expand Down Expand Up @@ -421,7 +427,8 @@ function static_vercel_config(builder) {
handle: 'filesystem'
}
],
overrides
overrides,
images
};
}

Expand Down
Loading