Skip to content

[Image Resizing] Add documentation example showing how to use next/image #672

Description

@GregBrimble

Unfortunately, they aren't taking any PRs to add additional built-in loaders, but next/image is a popular component capable of using external resizing services to render an image.

Happy to do the PR if it's of interest. It should just be what follows, but I haven't properly tested yet.

import Image from 'next/image'

const normalizeSrc = (src: string): string => {
  return src[0] === '/' ? src.slice(1) : src
}

const cloudflareLoader = ({ src, width, quality }) => {
  const params = [`width=${width}`]
  if (quality) {
    params.push(`quality=${quality}`)
  }
  const paramsString = params.join(',')
  return `/cdn-cgi/image/${paramsString}/${normalizeSrc(src)}`
}

const MyImage = (props) => {
  return (
    <Image
      loader={myLoader}
      src="/me.png"
      alt="Picture of the author"
      width={500}
      height={500}
    />
  )
}

Metadata

Metadata

Assignees

Labels

Type

No type

Fields

No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions