-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(vercel): Use Sharp in dev instead of Squoosh by default (#8445)
* feat(vercel): Use Sharp in dev instead of Squoosh by default * fix(build): * nit: adjust with feedback * fix: imports * Update packages/integrations/vercel/README.md Co-authored-by: Sarah Rainsberger <[email protected]> * docs: small change in other part of the README --------- Co-authored-by: Sarah Rainsberger <[email protected]>
- Loading branch information
1 parent
6c6f1ae
commit 9138037
Showing
14 changed files
with
357 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@astrojs/vercel': major | ||
--- | ||
|
||
Adds a configuration option `devImageService` to choose which of the built-in image services to use in development. Defaults to `sharp`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -137,7 +137,7 @@ export default defineConfig({ | |
**Available for:** Serverless, Static | ||
**Added in:** `@astrojs/[email protected]` | ||
|
||
When enabled, an [Image Service](https://docs.astro.build/en/reference/image-service-reference/) powered by the Vercel Image Optimization API will be automatically configured and used in production. In development, a built-in Squoosh-based service will be used instead. | ||
When enabled, an [Image Service](https://docs.astro.build/en/reference/image-service-reference/) powered by the Vercel Image Optimization API will be automatically configured and used in production. In development, the image service specified by [`devImageService`](#devimageservice) will be used instead. | ||
|
||
```js | ||
// astro.config.mjs | ||
|
@@ -172,6 +172,30 @@ import astroLogo from '../assets/logo.png'; | |
/> | ||
``` | ||
|
||
### devImageService | ||
|
||
**Type:** `'sharp' | 'squoosh' | string`<br> | ||
**Available for:** Serverless, Static | ||
**Added in:** `@astrojs/[email protected]` | ||
**Default**: 'sharp' | ||
|
||
Allows you to configure which image service to use in development when [imageService](#imageservice) is enabled. This can be useful if you cannot install Sharp's dependencies on your development machine, but using another image service like Squoosh would allow you to preview images in your dev environment. Build is unaffected and will always use Vercel's Image Optimization. | ||
|
||
It can also be set to any arbitrary value in order to use a custom image service instead of Astro's built-in ones. | ||
|
||
```js | ||
import { defineConfig } from 'astro/config'; | ||
import vercel from '@astrojs/vercel/serverless'; | ||
|
||
export default defineConfig({ | ||
output: 'server', | ||
adapter: vercel({ | ||
imageService: true, | ||
devImageService: 'squoosh', | ||
}), | ||
}); | ||
``` | ||
|
||
### includeFiles | ||
|
||
**Type:** `string[]`<br> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
packages/integrations/vercel/src/image/shared-dev-service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import type { LocalImageService } from 'astro'; | ||
import { sharedValidateOptions } from './shared.js'; | ||
|
||
export const baseDevService: Omit<LocalImageService, 'transform'> = { | ||
validateOptions: (options, serviceOptions) => | ||
sharedValidateOptions(options, serviceOptions.service.config, 'development'), | ||
getURL(options) { | ||
const fileSrc = typeof options.src === 'string' ? options.src : options.src.src; | ||
|
||
const searchParams = new URLSearchParams(); | ||
searchParams.append('href', fileSrc); | ||
|
||
options.width && searchParams.append('w', options.width.toString()); | ||
options.quality && searchParams.append('q', options.quality.toString()); | ||
|
||
return '/_image?' + searchParams; | ||
}, | ||
parseURL(url) { | ||
const params = url.searchParams; | ||
|
||
if (!params.has('href')) { | ||
return undefined; | ||
} | ||
|
||
const transform = { | ||
src: params.get('href')!, | ||
width: params.has('w') ? parseInt(params.get('w')!) : undefined, | ||
quality: params.get('q'), | ||
}; | ||
|
||
return transform; | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
packages/integrations/vercel/src/image/squoosh-dev-service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import type { LocalImageService } from 'astro'; | ||
import squooshService from 'astro/assets/services/squoosh'; | ||
import { baseDevService } from './shared-dev-service.js'; | ||
|
||
const service: LocalImageService = { | ||
...baseDevService, | ||
getHTMLAttributes(options, serviceOptions) { | ||
const { inputtedWidth, ...props } = options; | ||
|
||
// If `validateOptions` returned a different width than the one of the image, use it for attributes | ||
if (inputtedWidth) { | ||
props.width = inputtedWidth; | ||
} | ||
|
||
return squooshService.getHTMLAttributes | ||
? squooshService.getHTMLAttributes(props, serviceOptions) | ||
: {}; | ||
}, | ||
transform(inputBuffer, transform, serviceOptions) { | ||
// NOTE: Hardcoding webp here isn't accurate to how the Vercel Image Optimization API works, normally what we should | ||
// do is setup a custom endpoint that sniff the user's accept-content header and serve the proper format based on the | ||
// user's Vercel config. However, that's: a lot of work for: not much. The dev service is inaccurate to the prod service | ||
// in many more ways, this is one of the less offending cases and is, imo, okay, erika - 2023-04-27 | ||
transform.format = transform.src.endsWith('svg') ? 'svg' : 'webp'; | ||
|
||
// The base squoosh service works the same way as the Vercel Image Optimization API, so it's a safe fallback in local | ||
return squooshService.transform(inputBuffer, transform, serviceOptions); | ||
}, | ||
}; | ||
|
||
export default service; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.