Skip to content

Commit

Permalink
Adds quality setting to useOgImage()
Browse files Browse the repository at this point in the history
  • Loading branch information
cannikin committed Mar 1, 2024
1 parent 4b9d39c commit cc8ad07
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 16 deletions.
55 changes: 41 additions & 14 deletions packages/web/src/components/__tests__/ogImage.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import '@testing-library/jest-dom/jest-globals'

import { useOgImageUrl, OGIMAGE_DEFAULTS } from '../ogImage'
import { useOgImage, OGIMAGE_DEFAULTS } from '../ogImage'

const mockLocation = jest.fn()

Expand All @@ -10,7 +10,7 @@ jest.mock('@redwoodjs/router', () => {
}
})

describe('useOgImageUrl', () => {
describe('useOgImage', () => {
afterEach(() => {
jest.clearAllMocks()
})
Expand All @@ -22,7 +22,7 @@ describe('useOgImageUrl', () => {
searchParams: new URLSearchParams(),
})

const { url } = useOgImageUrl()
const { url } = useOgImage()

expect(url).toBe('http://localhost/user/1.png')
})
Expand All @@ -34,7 +34,7 @@ describe('useOgImageUrl', () => {
searchParams: new URLSearchParams(),
})

const { width } = useOgImageUrl()
const { width } = useOgImage()

expect(width).toBe(OGIMAGE_DEFAULTS.width)
})
Expand All @@ -46,19 +46,31 @@ describe('useOgImageUrl', () => {
searchParams: new URLSearchParams(),
})

const { height } = useOgImageUrl()
const { height } = useOgImage()

expect(height).toBe(OGIMAGE_DEFAULTS.height)
})

it('returns the default quality of the image', () => {
mockLocation.mockReturnValue({
origin: 'http://localhost/user/1',
pathname: '/user/1',
searchParams: new URLSearchParams(),
})

const { quality } = useOgImage()

expect(quality).toBe(OGIMAGE_DEFAULTS.quality)
})

it('returns all the props necessary to build the og:image meta tags', () => {
mockLocation.mockReturnValue({
origin: 'http://localhost/user/1',
pathname: '/user/1',
searchParams: new URLSearchParams(),
})

const { ogProps } = useOgImageUrl()
const { ogProps } = useOgImage()

expect(ogProps).toEqual({
image: ['http://localhost/user/1.png', { width: 1200, height: 630 }],
Expand All @@ -72,7 +84,7 @@ describe('useOgImageUrl', () => {
searchParams: new URLSearchParams(),
})

const { url } = useOgImageUrl()
const { url } = useOgImage()

expect(url).toBe('http://localhost/index.png')
})
Expand All @@ -84,7 +96,7 @@ describe('useOgImageUrl', () => {
searchParams: new URLSearchParams('foo=bar'),
})

const { url } = useOgImageUrl()
const { url } = useOgImage()

expect(url).toBe('http://localhost/about.png?foo=bar')
})
Expand All @@ -96,7 +108,7 @@ describe('useOgImageUrl', () => {
searchParams: new URLSearchParams(),
})

const { url } = useOgImageUrl({ extension: 'jpg' })
const { url } = useOgImage({ extension: 'jpg' })

expect(url).toBe('http://localhost/user/1/edit.jpg')
})
Expand All @@ -108,7 +120,7 @@ describe('useOgImageUrl', () => {
searchParams: new URLSearchParams(),
})

const { url, width, height } = useOgImageUrl({ width: 1000 })
const { url, width, height } = useOgImage({ width: 1000 })

expect(url).toBe('http://localhost/user/1.png?width=1000')
expect(width).toBe(1000)
Expand All @@ -122,30 +134,45 @@ describe('useOgImageUrl', () => {
searchParams: new URLSearchParams(),
})

const { url, width, height } = useOgImageUrl({ height: 500 })
const { url, width, height } = useOgImage({ height: 500 })

expect(url).toBe('http://localhost/user/1.png?height=500')
expect(width).toBe(OGIMAGE_DEFAULTS.width)
expect(height).toBe(500)
})

it('allows setting a custom quality', () => {
mockLocation.mockReturnValue({
origin: 'http://localhost/user/1',
pathname: '/user/1',
searchParams: new URLSearchParams(),
})

const { url, quality } = useOgImage({ quality: 50 })

expect(url).toBe('http://localhost/user/1.png?quality=50')
expect(quality).toBe(50)
})

it('merges existing query variables with custom ones', () => {
mockLocation.mockReturnValue({
origin: 'http://localhost/user/1',
pathname: '/user/1',
searchParams: new URLSearchParams('foo=bar'),
})

const { url, width, height } = useOgImageUrl({
extension: 'gif',
const { url, width, height, quality } = useOgImage({
extension: 'png',
width: 1024,
height: 768,
quality: 75,
})

expect(url).toBe(
'http://localhost/user/1.gif?foo=bar&width=1024&height=768'
'http://localhost/user/1.png?foo=bar&width=1024&height=768&quality=75'
)
expect(width).toBe(1024)
expect(height).toBe(768)
expect(quality).toBe(75)
})
})
12 changes: 10 additions & 2 deletions packages/web/src/components/ogImage.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
import { useLocation } from '@redwoodjs/router'

export type OgImageUrlOptions = {
extension?: 'png' | 'jpg' | 'jpeg' | 'gif'
extension?: 'png' | 'jpg'
width?: number
height?: number
quality?: number
}

export const OGIMAGE_DEFAULTS = {
extension: 'png',
width: 1200,
height: 630,
quality: 100,
}

export const useOgImageUrl = (options?: OgImageUrlOptions) => {
export const useOgImage = (options?: OgImageUrlOptions) => {
const { origin, pathname, searchParams } = useLocation()
const ext = options?.extension || OGIMAGE_DEFAULTS.extension
const width = options?.width
const height = options?.height
const quality = options?.quality
const output = [origin, `.${ext}`]

// special case if we're at the root, image is available at /index.ext
Expand All @@ -30,6 +33,9 @@ export const useOgImageUrl = (options?: OgImageUrlOptions) => {
if (height) {
searchParams.append('height', height.toString())
}
if (quality) {
searchParams.append('quality', quality.toString())
}

// only append search params if there are any, so we don't up with a trailing `?`
if (searchParams.size) {
Expand All @@ -40,6 +46,8 @@ export const useOgImageUrl = (options?: OgImageUrlOptions) => {
url: output.join(''),
width: width || OGIMAGE_DEFAULTS.width,
height: height || OGIMAGE_DEFAULTS.height,
quality: quality || OGIMAGE_DEFAULTS.quality,
extension: ext,
ogProps: {
image: [
output.join(''),
Expand Down

0 comments on commit cc8ad07

Please sign in to comment.