34
loading...
This website collects cookies to deliver better user experience
// /pages/Index.js
import { add } from '../components/math.js'
console.log(add(15, 15)); // 30
// /components/math.js
export function add(a, b) {
return a + b;
}
function add(a, b) {
return a + b;
}
console.log(add(15, 15)); // 30
gatsby build
. However, it does so in a specific manner. For example:npm install @loadable/component
# or use yarn
yarn add @loadable/component
import Hero from '../components/Hero'
import Services from '../components/Services'
export default function LandingPage() {
return (
<div>
<Hero />
<Services />
</div>
)
}
import loadable from '@loadable/component'
import Hero from '../components/Hero'
const Services = loadable(() => import('../components/Services'))
export default function LandingPage() {
return (
<div>
<Hero />
<Services />
</div>
)
}
Services.js
will now be loaded in its own bundle, separate from the one containing Hero.js
. As you see in this example, one method of using loadable-components for a single page site is importing content above the fold normally, and lazy loading content below it.import loadable from '@loadable/component'
import Hero from '../components/Hero'
const Services = loadable(() => import('../components/Services'), {
fallback: <div>Loading...</div>,
})
export default function LandingPage() {
return (
<div>
<Hero />
<Services />
</div>
)
}