Skip to content

Commit

Permalink
Docs for better Next.js integration (#300)
Browse files Browse the repository at this point in the history
  • Loading branch information
robmadole authored Nov 8, 2019
1 parent 0adb686 commit 0a17caa
Show file tree
Hide file tree
Showing 7 changed files with 7,966 additions and 0 deletions.
94 changes: 94 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
* [Basic](#basic)
* [Advanced](#advanced)
* [TypeScript](#typescript)
- [Integrating with other tools and frameworks](#integrating-with-other-tools-and-frameworks)
* [ext.js](#extjs)
- [Frequent questions](#frequent-questions)
* [How do I import the same icon from two different styles?](#how-do-i-import-the-same-icon-from-two-different-styles)
* [I don't think tree-shaking is working; got any advice?](#i-dont-think-tree-shaking-is-working-got-any-advice)
Expand Down Expand Up @@ -531,6 +533,98 @@ They are re-exported from both `@fortawesome/fontawesome-svg-core` and
make importing more convenient in some cases. Refer to the `index.d.ts` in any
module to see which types it exports.
## Integrating with other tools and frameworks
### ext.js
Next.js projects will experience an icon that is very large when the page first
loads. The reason this occurs is that the necessary CSS has not been loaded
before the icon is displayed.
To fix this we need to make sure the CSS for Font Awesome is bundled with the
Next.js app.
Install `@zeit/next-css`:
```
npm install --save-dev @zeit-css
```
You may want to use `--save` instead of `--save-dev` depending on how your `package.json` is organized.
Create or edit `next.config.js` in the root of your project:
```javascript
const withCSS = require('@zeit/next-css')
module.exports = withCSS({
/* config options here */
})
```
Create or edit `./pages/_app.js`:
```javascript
import React from 'react'
import App, { Container } from 'next/app'
import { config } from '@fortawesome/fontawesome-svg-core'
import '@fortawesome/fontawesome-svg-core/styles.css' // Import the CSS
config.autoAddCss = false // Tell Font Awesome to skip adding the CSS automatically since it's being imported above

class MyApp extends App {
render() {
const { Component, pageProps } = this.props
return <Component {...pageProps} />
}
}

export default MyApp
```
You may also wish to include your library calls in the `_app.js` code.
```javascript
import React from 'react'
import App, { Container } from 'next/app'

import { config, library } from '@fortawesome/fontawesome-svg-core'
import '@fortawesome/fontawesome-svg-core/styles.css'
config.autoAddCss = false

import { faBars, faUser } from '@fortawesome/free-solid-svg-icons'
import { faTwitter, faFacebook } from '@fortawesome/free-brands-svg-icons'

library.add(faBars, faUser, faTwitter, faFacebook)

class MyApp extends App {
render() {
const { Component, pageProps } = this.props
return <Component {...pageProps} />
}
}

export default MyApp
```
You can also use [explicit import](#explicit-import) instead of using the `library`.
Create a new page:
```javascript
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
import { faThumbsUp } from '@fortawesome/free-solid-svg-icons'

const Index = () => (
<div>
<p>
<FontAwesomeIcon icon={faThumbsUp} /> Hello Next.js
</p>
</div>
)

export default Index
```
## Frequent questions
### How do I import the same icon from two different styles?
Expand Down
2 changes: 2 additions & 0 deletions examples/nextjs-app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.next

This comment has been minimized.

Copy link
@alexooua

alexooua Mar 15, 2020

csac

node_modules
3 changes: 3 additions & 0 deletions examples/nextjs-app/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const withCSS = require('@zeit/next-css')

module.exports = withCSS({})
Loading

0 comments on commit 0a17caa

Please sign in to comment.