CSS media query compilation issue involving a specific PostCSS plugin bug.
The issue involves a bug in the CSS media query compilation process where parentheses are incorrectly dropped in `not` expressions. This requires understanding PostCSS and CSS Media Queries Level 4 syntax. The fix likely involves updating or patching the PostCSS plugin, but the exact scope depends on Next.js's internal PostCSS configuration.
https://codesandbox.io/p/devbox/fragrant-sky-dkr6k9
npx [email protected] nextjs-media-query-bug
cd nextjs-media-query-bug
yarn add @alma-oss/[email protected]
// postcss.config.mjs
export default {
plugins: {
autoprefixer: {
flexbox: 'no-2009',
},
},
};
// app/globals.css
@import '@alma-oss/spirit-web/css/utilities.css';
// app/page.tsx
export default function Home() {
return (
<div className="d-only-tablet-none">
This should be hidden on tablet (768px-1279px)
</div>
);
}
yarn build
IMO, Next.js 16.1.5 bundles postcss-preset-env v7.4.3 internally, which uses @csstools/postcss-media-minmax to transpile CSS Media Queries Level 4 range syntax. The bundled version has a bug where it drops the outer parentheses around not expressions when combined with and operators, violating the CSS Media Queries Level 4 specification.
The CSS code before postprocessing:
@media (width >= 768px) and (width < 1280px) { … }
Current behavior after postprocessing.
@media (min-width: 768px) and not (min-width: 1280px) { ... }
Expected behavior after postprocessing.
@media (min-width: 768px) and (not (min-width: 1280px)) { ... }
Claim this issue to let others know you're working on it. You'll earn 20 points when you complete it!