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(vanity): add support for vanity name in url #68

Merged
merged 4 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ Return the url as a string.

Specify the number of pixels to pad the image.

### `vanityName(value)`

Specify a vanity name for the image. This is useful for SEO purposes.

### Deprecated: `minWidth(pixels)`, `maxWidth(pixels)`, `minHeight(pixels)`, `maxHeight(pixels)`

Specifies min/max dimensions when cropping.
Expand Down
5 changes: 5 additions & 0 deletions src/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,11 @@ export class ImageUrlBuilder {
return this.withOptions({pad})
}

// Vanity URL for more SEO friendly URLs
vanityName(value: string) {
return this.withOptions({vanityName: value})
}

// Gets the url based on the submitted parameters
url() {
return urlForImage(this.options)
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export type ImageUrlBuilderOptions = Partial<SanityProjectDetails> & {
saturation?: number
auto?: AutoMode
pad?: number
vanityName?: string
}

export type ImageUrlBuilderOptionsWithAliases = ImageUrlBuilderOptions & {
Expand Down
5 changes: 3 additions & 2 deletions src/urlForImage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,9 @@ export default function urlForImage(options: ImageUrlBuilderOptions): string {
// eslint-disable-next-line complexity
function specToImageUrl(spec: ImageUrlBuilderOptionsWithAsset) {
const cdnUrl = (spec.baseUrl || 'https://cdn.sanity.io').replace(/\/+$/, '')
const filename = `${spec.asset.id}-${spec.asset.width}x${spec.asset.height}.${spec.asset.format}`
const baseUrl = `${cdnUrl}/images/${spec.projectId}/${spec.dataset}/${filename}`
const vanityStub = spec.vanityName ? `/${spec.vanityName}` : ''
const filename = `${spec.asset.id}-${spec.asset.width}x${spec.asset.height}.${spec.asset.format}${vanityStub}`
const baseUrl = `${cdnUrl}/images/${spec.projectId}/${spec.dataset}/${filename}`

const params = []

Expand Down
2 changes: 2 additions & 0 deletions test/__snapshots__/builder.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,5 @@ exports[`builder skips hotspot/crop if focal point specified 1`] = `"https://cdn
exports[`builder sub zero top/left 1`] = `"w=1000&h=805"`;

exports[`builder toString() aliases url() 1`] = `"https://cdn.sanity.io/images/zp7mbokg/production/Tb9Ew8CXIwaY6R1kjMvI0uRR-2000x3000.jpg?w=100&h=80"`;

exports[`builder vanity name 1`] = `"https://cdn.sanity.io/images/zp7mbokg/production/Tb9Ew8CXIwaY6R1kjMvI0uRR-2000x3000.jpg/moo"`;
11 changes: 8 additions & 3 deletions test/builder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,11 @@ const cases = [
url: stripPath(urlFor.image(noHotspotImage()).dpr(1).url()),
},

{
name: 'vanity name',
url: urlFor.image(noHotspotImage()).vanityName('moo').url()
},

{
name: 'sub zero top/left',
url: stripPath(
Expand Down Expand Up @@ -175,7 +180,7 @@ const cases = [
.flipHorizontal()
.flipVertical()
.fit('crop')
.pad(40)
.pad(40)
.url()
),
},
Expand Down Expand Up @@ -203,15 +208,15 @@ const cases = [
.flipVertical()
.fit('crop')
.crop('center')
.pad(40)
.pad(40)
.url()
),
},
]

describe('builder', () => {
cases.forEach((testCase) => {
test(testCase.name, () => {
test(testCase.name, () => {
expect(testCase.url).toMatchSnapshot()
})
})
Expand Down