diff --git a/errors/css-global.md b/errors/css-global.md index 51f70f45f6193..0f2ff4fd84319 100644 --- a/errors/css-global.md +++ b/errors/css-global.md @@ -8,12 +8,27 @@ Global CSS cannot be used in files other than your [Custom ``](https://next #### Possible Ways to Fix It -Relocate all Global CSS imports to your [`pages/_app.js` file](https://nextjs.org/docs/advanced-features/custom-app). +To avoid conflicts, relocate all first-party Global CSS imports to your [`pages/_app.js` file](https://nextjs.org/docs/advanced-features/custom-app). Or, [update your component to use local CSS (Component-Level CSS) via CSS Modules](https://nextjs.org/docs/basic-features/built-in-css-support#adding-component-level-css). This is the preferred approach. #### Example +Consider the stylesheet named [`styles.css`](https://nextjs.org/docs/basic-features/built-in-css-support#adding-a-global-stylesheet) + +```jsx +//styles.css +body { + font-family: 'SF Pro Text', 'SF Pro Icons', 'Helvetica Neue', 'Helvetica', + 'Arial', sans-serif; + padding: 20px 20px 60px; + max-width: 680px; + margin: 0 auto; +} +``` + +Create a [`pages/_app.js` file](https://nextjs.org/docs/advanced-features/custom-app) if not already present. Then [`import`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import) the [`styles.css` file](https://nextjs.org/docs/basic-features/built-in-css-support#adding-a-global-stylesheet). + ```jsx // pages/_app.js import '../styles.css' diff --git a/packages/next/build/webpack/config/blocks/css/messages.ts b/packages/next/build/webpack/config/blocks/css/messages.ts index c06dc19eec3e7..787d46750e5ec 100644 --- a/packages/next/build/webpack/config/blocks/css/messages.ts +++ b/packages/next/build/webpack/config/blocks/css/messages.ts @@ -5,7 +5,7 @@ export function getGlobalImportError(file: string | null) { 'cannot' )} be imported from files other than your ${chalk.bold( 'Custom ' - )}. Please move all global CSS imports to ${chalk.cyan( + )}. Due to the Global nature of stylesheets, and to avoid conflicts, Please move all first-party global CSS imports to ${chalk.cyan( file ? file : 'pages/_app.js' )}. Or convert the import to Component-Level CSS (CSS Modules).\nRead more: https://nextjs.org/docs/messages/css-global` } diff --git a/test/integration/css/test/index.test.js b/test/integration/css/test/index.test.js index 0e8396297fbbf..305f94c5daef2 100644 --- a/test/integration/css/test/index.test.js +++ b/test/integration/css/test/index.test.js @@ -357,7 +357,7 @@ describe('CSS Support', () => { expect(stderr).toContain('Failed to compile') expect(stderr).toContain('styles/global.css') expect(stderr).toMatch( - /Please move all global CSS imports.*?pages(\/|\\)_app/ + /Please move all first-party global CSS imports.*?pages(\/|\\)_app/ ) expect(stderr).toMatch(/Location:.*pages[\\/]index\.js/) }) @@ -407,7 +407,7 @@ describe('CSS Support', () => { expect(stderr).toContain('Failed to compile') expect(stderr).toContain('styles/global.css') expect(stderr).toMatch( - /Please move all global CSS imports.*?pages(\/|\\)_app/ + /Please move all first-party global CSS imports.*?pages(\/|\\)_app/ ) expect(stderr).toMatch(/Location:.*pages[\\/]index\.js/) }) @@ -427,7 +427,7 @@ describe('CSS Support', () => { expect(code).not.toBe(0) expect(stderr).toContain('Failed to compile') expect(stderr).toContain('styles/global.css') - expect(stderr).toContain('Please move all global CSS imports') + expect(stderr).toContain('Please move all first-party global CSS imports') expect(stderr).toMatch(/Location:.*pages[\\/]index\.js/) }) }) diff --git a/test/integration/scss/test/index.test.js b/test/integration/scss/test/index.test.js index 9ed97695c6b68..f89ff0580644f 100644 --- a/test/integration/scss/test/index.test.js +++ b/test/integration/scss/test/index.test.js @@ -355,7 +355,7 @@ describe('SCSS Support', () => { expect(stderr).toContain('Failed to compile') expect(stderr).toContain('styles/global.scss') expect(stderr).toMatch( - /Please move all global CSS imports.*?pages(\/|\\)_app/ + /Please move all first-party global CSS imports.*?pages(\/|\\)_app/ ) expect(stderr).toMatch(/Location:.*pages[\\/]index\.js/) }) @@ -376,7 +376,7 @@ describe('SCSS Support', () => { expect(stderr).toContain('Failed to compile') expect(stderr).toContain('styles/global.scss') expect(stderr).toMatch( - /Please move all global CSS imports.*?pages(\/|\\)_app/ + /Please move all first-party global CSS imports.*?pages(\/|\\)_app/ ) expect(stderr).toMatch(/Location:.*pages[\\/]index\.js/) }) @@ -396,7 +396,7 @@ describe('SCSS Support', () => { expect(code).not.toBe(0) expect(stderr).toContain('Failed to compile') expect(stderr).toContain('styles/global.scss') - expect(stderr).toContain('Please move all global CSS imports') + expect(stderr).toContain('Please move all first-party global CSS imports') expect(stderr).toMatch(/Location:.*pages[\\/]index\.js/) }) })