withastro/astro · warning
Could not generate a valid regex from the remotePattern "${J
Error message
Could not generate a valid regex from the remotePattern "${JSON.stringify(pattern)}". Please check the syntax. What it means
The Netlify adapter converts each config.image.remotePatterns entry into a JavaScript regex so it can be written into Netlify's image CDN configuration (remotePatternToRegex). Before handing the regex off, it sanity-compiles it with new RegExp(); if construction throws, the pattern is unusable, this warning prints, and the pattern is skipped (returns undefined), so those remote images will not be optimized.
Source
Thrown at packages/integrations/netlify/src/index.ts:111
// Exact match
regexStr += `(${escapeRegex(pathname)})`;
}
} else {
// Default to matching any path
regexStr += '(\\/[^?#]*)?';
}
if (!regexStr.endsWith('.*)')) {
// Match query, but only if it's not already matched by the pathname
regexStr += '([?][^#]*)?';
}
// Anchor to end of string so .test() can't match a prefix
regexStr += '$';
try {
// nosemgrep: javascript.lang.security.audit.detect-non-literal-regexp.detect-non-literal-regexp
// This only validates the generated pattern before handing it to Netlify.
new RegExp(regexStr);
} catch {
logger.warn(
`Could not generate a valid regex from the remotePattern "${JSON.stringify(
pattern,
)}". Please check the syntax.`,
);
return undefined;
}
return regexStr;
}
function remoteImagesFromAstroConfig(
config: AstroConfig,
logger: AstroIntegrationLogger,
): string[] {
const remoteImages: string[] = [];
// Domains get a simple regex match
remoteImages.push(
...config.image.domains.map((domain) => `^https?:\/\/${escapeRegex(domain)}\/.*$`),
);View on GitHub (pinned to 3578d45d34)
Solutions
- Inspect the printed remotePattern JSON and fix field values: protocol like 'https', hostname optionally prefixed with '**.' or '*.', numeric port, pathname using only trailing '/**' or '/*' wildcards.
- Move simple hostnames into image.domains instead of remotePatterns when no path/port matching is needed.
- Test the pattern locally with a tiny regex rebuild, or just run astro build again to confirm no warning prints and the images are served through the CDN.
- If you need regex-level control, verify each segment escapes metacharacters yourself before putting the literal in config.
Example fix
// before (astro.config.mjs)
image: {
remotePatterns: [{ protocol: 'https', hostname: 'cdn.example.com', port: '80-443' }],
},
// after
image: {
remotePatterns: [{ protocol: 'https', hostname: 'cdn.example.com' }],
}, Defensive patterns
Strategy: validation
Validate before calling
const isValidRemotePattern = (p: {
protocol?: string;
hostname?: string;
port?: string | number;
pathname?: string;
}): boolean =>
(!p.protocol || /^[a-z]+$/i.test(p.protocol)) &&
(!p.hostname || /^\*?\*?\.?[a-z0-9.*-]+$/i.test(p.hostname)) &&
(p.port === undefined || /^\d+$/.test(String(p.port))) &&
(!p.pathname || /^\/.*/.test(p.pathname));
const allPatternsValid = config.image.remotePatterns.every(isValidRemotePattern); Type guard
interface RemotePattern {
protocol?: string;
hostname?: string;
port?: string | number;
pathname?: string;
}
const isRemotePattern = (p: unknown): p is RemotePattern =>
typeof p === 'object' && p !== null &&
Object.keys(p).every((k) => ['protocol', 'hostname', 'port', 'pathname'].includes(k)); Prevention
- Keep port numeric and omit it when unnecessary.
- Use only the supported wildcard forms: '**.' prefix on hostname, trailing '/**' or '/*' on pathname.
- Prefer image.domains for plain hostnames and reserve remotePatterns for path/port matching.
- Watch build logs for this warning after every image config change; a skipped pattern means unoptimized remote images.
When it happens
Trigger: An entry in image.remotePatterns whose fields produce an invalid regex: e.g. a port that is not numeric ('port: "80:90"' interpolated raw into ':80:90'), a protocol with regex metacharacters, or a malformed pattern object. The catch around new RegExp(regexStr) in remotePatternToRegex fires and the function returns undefined.
Common situations: Typos in remotePatterns objects; port given as a range or non-numeric string; assuming remotePatterns supports full regex syntax when it only supports the **. and /* wildcard conventions; copying patterns from next.config.js where semantics differ slightly.
Related errors
- ⚠️ Astro expected an SVG for "${transform.src}" but the sou
- ⚠️ Astro could not optimize image "${transform.src}". Sharp
- Auto-generating collections for folders in "src/content/" t
- ${colors.bold(plugin)} not applied.
- ${colors.bold(plugin[0])} not applied.
AI-assisted analysis of withastro/astro@3578d45d34 (2026-08-18).
Data as JSON: /api/errors/9b4ca550d006b9d1.
Report an issue: GitHub.