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

Fix cms-kontent example Next/Image domain error #37188

Merged
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
2 changes: 1 addition & 1 deletion examples/cms-kontent/components/avatar.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Image from 'next/image'
import Image from '../components/image'

export default function Avatar({ name, picture }) {
return (
Expand Down
2 changes: 1 addition & 1 deletion examples/cms-kontent/components/cover-image.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import cn from 'classnames'
import Image from 'next/image'
import Image from '../components/image'
import Link from 'next/link'

export default function CoverImage({ title, src, slug }) {
Expand Down
32 changes: 32 additions & 0 deletions examples/cms-kontent/components/image.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import NextImage from 'next/image'
import { transformImageUrl } from '@kentico/kontent-delivery'

const KONTENT_ASSET_HOSTNAME_REGEX = /.kc-usercontent.com$/
Copy link
Member

@styfle styfle May 25, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the . be escaped? Perhaps you should use hostname.endsWith('.kc-usercontent.com') instead of a regexp

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

endsWith would be simpler, thanks 👍

I'll open another PR to update this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated in #37206


const getLoader = (src) => {
return srcIsKontentAsset(src) ? kontentImageLoader : undefined
}

const srcIsKontentAsset = (src) => {
try {
const { hostname } = new URL(src)
return KONTENT_ASSET_HOSTNAME_REGEX.test(hostname)
} catch {
return false
}
}

const kontentImageLoader = ({ src, width, quality = 100 }) => {
Copy link
Member

@styfle styfle May 25, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about quality 75 as default? Usually 100 is much larger than necessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated in #37206

return new transformImageUrl(src)
.withWidth(width)
.withQuality(quality)
.withCompression('lossless')
.withAutomaticFormat()
.getUrl()
}

export default function Image(props) {
const loader = getLoader(props.src)

return <NextImage {...props} loader={loader} />
}