Image source policy and loaders
Scope: Next.js 16.x candidate, App Router, Cache Components disabled, TypeScript. Source walkthroughs and expected outcomes; no framework build or browser execution has been verified.
What you will learn
- Restrict remote sources
- Compare custom loader and optimizer
next.config.ts — optional remote policy, not active in fixture
import type { NextConfig } from 'next';const config: NextConfig = { images: { remotePatterns: [{ protocol: 'https', hostname: 'covers.example.com', port: '', pathname: '/catalog/**', search: '' }], maximumRedirects: 0, qualities: [75] } };export default config;A hypothetical HTTPS source on this host under /catalog/ with no query is eligible. The example domain is illustrative; do not contact it.
How it works
Choose explicit HTTPS protocol, hostname, port, path and query policy. Empty search means no query string. A pattern is an optimizer source restriction, not a user authorization system. Redirects require separate attention; for a tightly scoped remote service, consider disabling redirects and keep local-IP and SVG protections enabled. Next.js 16 uses a quality allowlist; the fixture explicitly allows 75. The older priority prop is deprecated in the 16 lane in favor of preload for appropriate cases, but do not preload every card. A static export cannot use the default runtime optimizer; use an appropriate custom loader or unoptimized images with a reviewed deployment design.
Optional loader function — URL construction only
export default function loader({ src, width, quality }: { src: string; width: number; quality?: number }) { if (!/^\/covers\/(moss|tide|fern)\.png$/.test(src) || !Number.isInteger(width) || width < 1 || width > 3840) throw new Error('Unsupported image request'); const url = new URL('https://covers.example.com/render'); url.searchParams.set('src', src); url.searchParams.set('w', String(width)); url.searchParams.set('q', String(75)); return url.toString();}The function returns a URL string for a fictional service, fixed to quality 75. It neither downloads nor optimizes an image. An actual service and configuration would be required.
Common mistake
Do not accept arbitrary user URLs, enable private-network fetching or remove all pattern constraints to fix a rejected image. Check exact protocol, host, path, port and query instead. Do not confuse a loader with authorization.
Try it yourself
Classify three hypothetical URLs against the shown rule: https://covers.example.com/catalog/a.png, http://covers.example.com/catalog/a.png, and https://covers.example.com/catalog/a.png?v=2. Only the first matches.
Further reading
Next lesson: Server Components by default.