0

I've had a problem with mdbreact package, because when i am adding some components like buttons, etc. I am getting error: "Window is not defined". I made a research and found that window object is defined only on the client side. Is it possible way to add Bootstrap to NextJS???

Jee Mok
  • 6,157
  • 8
  • 47
  • 80
Marcin Warzybok
  • 357
  • 4
  • 16

2 Answers2

0

In case you want to use bootstrap cdn, your can simply use _app.js or _document.js files in nextjs.

Here is how i linked bootstrap cdn to my nextjs project via _document.js

import Document, { Html, Head, Main, NextScript } from "next/document";

class MyDocument extends Document {
    render() {
        return (
            <Html>
                <Head>
                    <link
                        href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/css/bootstrap.min.css"
                        rel="stylesheet"
                        integrity="sha384-giJF6kkoqNQ00vy+HMDP7azOuL0xtbfIcaT9wjKHr8RbDVddVHyTfAAsrekwKmP1"
                        crossorigin="anonymous"
                    />
                </Head>
                <body>
                    <Main />
                    <NextScript />
                </body>
            </Html>
        );
    }
}

export default MyDocument;

To know more about _document.js click here.

You can also use _app.js in a similar way. for more information about _app.js check this out.

Hemant
  • 1,127
  • 1
  • 10
  • 18
-3

Updated answer (2020)

react-bootstrap supports SSR as of v1.0

Source: https://github.com/react-bootstrap/react-bootstrap/issues/3569


Outdated answer (2018)

At the time of writing, there is only one "clean" way: using Dynamic Components to disable SSR for those component.

import dynamic from 'next/dynamic'
const ButtonWithNoSSR = dynamic(import('react-bootstrap/lib/Button'), {
  ssr: false
})

The "dirty" way would be to make react-bootstrap support SSR by mocking the window object.

Clément Prévost
  • 8,000
  • 2
  • 36
  • 51