Unverified Commit faa4b992 authored by Joseph's avatar Joseph Committed by GitHub

docs: local images referenced by remote source (#92178)

It was discovered that one could let a remote source, safely-ish, load
local images, which has the benefit of providing, width, height, src,
and blur-* props which we could pass to next/image.

Closes:
https://linear.app/vercel/issue/DOC-6288/local-images-referenced-by-a-remote-source
parent 8b5dfa66
...@@ -117,6 +117,50 @@ export default function Page() { ...@@ -117,6 +117,50 @@ export default function Page() {
} }
``` ```
### Images without static imports
If you can't use a static `import` for your images, you can use a dynamic `import()` in a Server Component to still get automatic `width`, `height`, and `blurDataURL`:
```tsx filename="app/blog/[slug]/page.tsx" switcher
import Image from 'next/image'
async function PostImage({
imageFilename,
alt,
}: {
imageFilename: string
alt: string
}) {
const { default: image } = await import(
`../content/blog/images/${imageFilename}`
)
// image contains width, height, and blurDataURL
return <Image src={image} alt={alt} />
}
```
```jsx filename="app/blog/[slug]/page.js" switcher
import Image from 'next/image'
async function PostImage({ imageFilename, alt }) {
const { default: image } = await import(
`../content/blog/images/${imageFilename}`
)
// image contains width, height, and blurDataURL
return <Image src={image} alt={alt} />
}
```
If you have a [path alias](https://www.typescriptlang.org/tsconfig/#paths) configured (e.g. `@/`), you can use it instead of a relative path:
```tsx
const { default: image } = await import(
`@/content/blog/images/${imageFilename}`
)
```
The path must include a static prefix (like `../content/blog/images/`). Be as specific as possible, since **all** files matching that prefix are bundled. Only files in your specified directory are included, so external input cannot reach outside of it.
## Remote images ## Remote images
To use a remote image, you can provide a URL string for the `src` property. To use a remote image, you can provide a URL string for the `src` property.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment