# Babylon + Next.js Setup Optimized for SEO, Load Speed & Developer Experience

**URL:** <https://forum.babylonjs.com/t/babylon-next-js-setup-optimized-for-seo-load-speed-developer-experience/27412>\
**Category:** Tutorials and tips\
**Tags:** documentation, tutorial, npm\
**Created:** [February 6, 2022, 9:56pm UTC](https://forum.babylonjs.com/t/babylon-next-js-setup-optimized-for-seo-load-speed-developer-experience/27412 "2022-02-06T21:56:05Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![ecoin](https://avatars.discourse-cdn.com/v4/letter/e/8e7dd6/32.png) [@ecoin](https://forum.babylonjs.com/u/ecoin)\
**Post date:** [February 6, 2022, 9:56pm UTC](https://forum.babylonjs.com/t/babylon-next-js-setup-optimized-for-seo-load-speed-developer-experience/27412/1 "2022-02-06T21:56:05Z")

</div>

Hi everyone!

According to many questions posted in this forum, people were having problems with getting a working setup of Babylon and Next.js.

So here is the guide on [How to Setup Babylon + Next.js Production Build Optimized for SEO & Speed](https://3designer.app/best/babylon-nextjs-setup).

It comes with a proven starter template repository on [GitHub](https://github.com/ecoinomist/babylon-next) (tested on MacOS, Linux).

@note: Windows users should install this template using `yarn`, not `npm` because it will probably produce errors with `npm`. If you haven’t installed yarn, do this first:

```auto
// it doesnt work if using npm install --global yarn (according to some report)
npm i yarn

```

**Key Features:**

1. **SEO** Friendly
2. Load Fast to achieve **“A” Grade Page Speed** score
3. **Async Load** Babylon to improve UX (user experience)
4. Code Splitting with **Tree Shaking** to reduce bundle size
5. An option to write in pure **Javascript** or **Typescript** (or mixing both)
6. Intuitive developer experience with **HMR** (Hot Module Reload)
7. **Jest Test** working setup
8. Modular Architecture for a **flexible** , **maintainable** and **scalable** app
9. **Production** ready with detailed bundle analyzer.

If there is anything you’d like to add, plz let me know.

---

<div class="post-metadata">

**Author:** ![brianzinn](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/brianzinn/32/28013_2.png) [@brianzinn](https://forum.babylonjs.com/u/brianzinn)\
**Post date:** [February 7, 2022, 3:19am UTC](https://forum.babylonjs.com/t/babylon-next-js-setup-optimized-for-seo-load-speed-developer-experience/27412/2 "2022-02-07T03:19:16Z")

</div>

Thanks for sharing. I was wondering why you export lots of things in ‘client/babylon.js’ instead of just importing what you need in the views.

---

<div class="post-metadata">

**Author:** ![ecoin](https://avatars.discourse-cdn.com/v4/letter/e/8e7dd6/32.png) [@ecoin](https://forum.babylonjs.com/u/ecoin)\
**Post date:** [February 7, 2022, 8:31am UTC](https://forum.babylonjs.com/t/babylon-next-js-setup-optimized-for-seo-load-speed-developer-experience/27412/3 "2022-02-07T08:31:40Z")

</div>

> [@brianzinn](#):
>
> export lots of things in ‘client/babylon.js’

The philosophy is to make it newbie friendly, and pro happy.

A newbie might go in and try to show the boundingBox, and not know that Babylon requires ‘@babylonjs/core/Rendering/boundingBoxRenderer’ for it to work.

Whereas by the time you get to code pruning optimization for production, chances are you already know what you are doing. You can easily comment out the exports you do not need, like explained in the Doc block inside that file.

For ease if use, I left common exports that are frequently used (at least from my personal experience).

Also in production build, it seems Webpack will strip out all unused exports anyway (though I haven’t tested this completely).

---

<div class="post-metadata">

**Author:** ![ecoin](https://avatars.discourse-cdn.com/v4/letter/e/8e7dd6/32.png) [@ecoin](https://forum.babylonjs.com/u/ecoin)\
**Post date:** [February 12, 2022, 2:18pm UTC](https://forum.babylonjs.com/t/babylon-next-js-setup-optimized-for-seo-load-speed-developer-experience/27412/4 "2022-02-12T14:18:58Z")

</div>

> [@brianzinn](#):
>
> instead of just importing what you need in the views.

Just realized I might have misunderstood your question. All Babylon imports should come from a single **babylon.js** for these reasons:

- Abstract away internal dependencies of Babylon packages
- Make your project framework agnostic, so that you can easily swap/combine any 3D library in the future (Three.js for example, though I see no reason of doing that 😉)
- Easy to maintain (example: if a polyfill is need for WebGPU, do you not have to hunt down all views importing Babylon. Or perhaps you want to replace the default CSG with a [cooler one](https://github.com/wwwtyro/sdf-csg), like you mentioned.)

---

<div class="post-metadata">

**Author:** ![labris](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/labris/32/12576_2.png) [@labris](https://forum.babylonjs.com/u/labris)\
**Post date:** [February 12, 2022, 3:07pm UTC](https://forum.babylonjs.com/t/babylon-next-js-setup-optimized-for-seo-load-speed-developer-experience/27412/5 "2022-02-12T15:07:26Z")

</div>

Here is what I’ve got after `yarn` and `yarn dev`

 ![image](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/e/2/e2ec9456d9fdcbfb7a3c46e02dcf36ed47aa742a.png)

---

<div class="post-metadata">

**Author:** ![ecoin](https://avatars.discourse-cdn.com/v4/letter/e/8e7dd6/32.png) [@ecoin](https://forum.babylonjs.com/u/ecoin)\
**Post date:** [February 12, 2022, 3:35pm UTC](https://forum.babylonjs.com/t/babylon-next-js-setup-optimized-for-seo-load-speed-developer-experience/27412/6 "2022-02-12T15:35:39Z")

</div>

That’s because you don’t have `localhost:3000/ru` page setup yet.

The example **next.config.js** was mean’t to show you how to configure a multilingual website, but for each language, you need an actual `page`.

Please check the link about [localisation](https://nextjs.org/docs/advanced-features/i18n-routing) routing inside it.

Strangely, though, it works on mine, even without any page. Did you change anything to the repository? Can you post `yarn.lock` file?

---

<div class="post-metadata">

**Author:** ![labris](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/labris/32/12576_2.png) [@labris](https://forum.babylonjs.com/u/labris)\
**Post date:** [February 12, 2022, 3:39pm UTC](https://forum.babylonjs.com/t/babylon-next-js-setup-optimized-for-seo-load-speed-developer-experience/27412/7 "2022-02-12T15:39:33Z")

</div>

Actually I don’t need any localization at all.  
But when I open localhost:3000 it opens at localhost:3000/ru with errors.  
Is there a way to use your template straight out of the box?

I did fork and then `yarn` and `yarn dev`. Didn’t touch anything in files.

---

<div class="post-metadata">

**Author:** ![ecoin](https://avatars.discourse-cdn.com/v4/letter/e/8e7dd6/32.png) [@ecoin](https://forum.babylonjs.com/u/ecoin)\
**Post date:** [February 12, 2022, 3:40pm UTC](https://forum.babylonjs.com/t/babylon-next-js-setup-optimized-for-seo-load-speed-developer-experience/27412/8 "2022-02-12T15:40:46Z")

</div>

I see the problem. It’s because your comp has `ru` set by default. But the default template has no `ru` page setup, and Next.js being too smart, tried to redirect you to a non-existing page.

I updated git. Please pull and try.

---

<div class="post-metadata">

**Author:** ![labris](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/labris/32/12576_2.png) [@labris](https://forum.babylonjs.com/u/labris)\
**Post date:** [February 12, 2022, 4:06pm UTC](https://forum.babylonjs.com/t/babylon-next-js-setup-optimized-for-seo-load-speed-developer-experience/27412/9 "2022-02-12T16:06:12Z")

</div>

This is what I’ve got after pull, yarn and yarn dev:

 ![image](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/3/a/3a15497820d8eb7e6c2e3ed3c53523b1d32f5b9f.png)

---

<div class="post-metadata">

**Author:** ![ecoin](https://avatars.discourse-cdn.com/v4/letter/e/8e7dd6/32.png) [@ecoin](https://forum.babylonjs.com/u/ecoin)\
**Post date:** [February 12, 2022, 4:13pm UTC](https://forum.babylonjs.com/t/babylon-next-js-setup-optimized-for-seo-load-speed-developer-experience/27412/10 "2022-02-12T16:13:07Z")

</div>

What is your **yarn** , **node.js** version and OS?

Can you attach yarn.log or any error log?

This was only tested to work in Node.js v12.22.1. as stated, on MacOS. It could be problem with the **yarn** itself.

Try to remove this file: `/babylon-next/repos/web/components/Preload.js` - it is not used, only for reference, but that’s the only place the `next/router` was imported.

You can verify this yourself by searching for `next/router` in the entire repo, it’s not used anywhere. Perhaps you have stale cache from another next.js setup in your yarn.

Try this then:

```auto
yarn reset

```

---

<div class="post-metadata">

**Author:** ![labris](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/labris/32/12576_2.png) [@labris](https://forum.babylonjs.com/u/labris)\
**Post date:** [February 12, 2022, 4:27pm UTC](https://forum.babylonjs.com/t/babylon-next-js-setup-optimized-for-seo-load-speed-developer-experience/27412/11 "2022-02-12T16:27:28Z")

</div>

1. Deleted Preload.js - no success
2. yarn reset - the same

Every time it also opens rimraf/bin.js file and don’t do anything until I close this file.

I think I need to downgrade node.js version to 12.22.1 (currently use the latest), probably it will help…

---

<div class="post-metadata">

**Author:** ![ecoin](https://avatars.discourse-cdn.com/v4/letter/e/8e7dd6/32.png) [@ecoin](https://forum.babylonjs.com/u/ecoin)\
**Post date:** [February 12, 2022, 4:30pm UTC](https://forum.babylonjs.com/t/babylon-next-js-setup-optimized-for-seo-load-speed-developer-experience/27412/12 "2022-02-12T16:30:12Z")

</div>

> [@labris](#):
>
> downgrade node.js version

[NVM](https://github.com/nvm-sh/nvm) would be good for this purpose if you are not using it yet. Next.js is quite picky about Node versions, hence I explicitly mentioned the tested Node version that I currently use myself in production.

This is one of the reasons many people struggle with Next.js setup. But the upside is there is much less headache after you get the initial setup working, compared to CRA - easy to start, hard to customize.

---

<div class="post-metadata">

**Author:** ![ecoin](https://avatars.discourse-cdn.com/v4/letter/e/8e7dd6/32.png) [@ecoin](https://forum.babylonjs.com/u/ecoin)\
**Post date:** [February 12, 2022, 4:40pm UTC](https://forum.babylonjs.com/t/babylon-next-js-setup-optimized-for-seo-load-speed-developer-experience/27412/13 "2022-02-12T16:40:13Z")

</div>

> [@labris](#):
>
> Every time it also opens rimraf/bin.js file and don’t do anything until I close this file.

That means this package is not compatible with your OS, probably Windows. You can reset yarn cache directly:

```auto
yarn cache clean && yarn

```

---

<div class="post-metadata">

**Author:** ![labris](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/labris/32/12576_2.png) [@labris](https://forum.babylonjs.com/u/labris)\
**Post date:** [February 12, 2022, 4:46pm UTC](https://forum.babylonjs.com/t/babylon-next-js-setup-optimized-for-seo-load-speed-developer-experience/27412/14 "2022-02-12T16:46:29Z")

</div>

OK, seems that this one is not for Windows 🙂  
I’ll check further when will be on Mac.

---

<div class="post-metadata">

**Author:** ![ecoin](https://avatars.discourse-cdn.com/v4/letter/e/8e7dd6/32.png) [@ecoin](https://forum.babylonjs.com/u/ecoin)\
**Post date:** [February 12, 2022, 4:49pm UTC](https://forum.babylonjs.com/t/babylon-next-js-setup-optimized-for-seo-load-speed-developer-experience/27412/15 "2022-02-12T16:49:08Z")

</div>

I mean the package used for script `yarn reset`.

But the boilerplate should work in Windows, because that `rimraf` package is only used as extra developer tool for ease of use.

If it’s because of `rimraf` that you are having problems to get it running in Windows, then we can remove it from the template. I do not have Windows, so if you could test that would be great.

---

<div class="post-metadata">

**Author:** ![labris](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/labris/32/12576_2.png) [@labris](https://forum.babylonjs.com/u/labris)\
**Post date:** [February 12, 2022, 4:52pm UTC](https://forum.babylonjs.com/t/babylon-next-js-setup-optimized-for-seo-load-speed-developer-experience/27412/16 "2022-02-12T16:52:38Z")

</div>

Well, with node 12.22.1 I had the same problems.

---

<div class="post-metadata">

**Author:** ![ecoin](https://avatars.discourse-cdn.com/v4/letter/e/8e7dd6/32.png) [@ecoin](https://forum.babylonjs.com/u/ecoin)\
**Post date:** [February 12, 2022, 4:57pm UTC](https://forum.babylonjs.com/t/babylon-next-js-setup-optimized-for-seo-load-speed-developer-experience/27412/17 "2022-02-12T16:57:10Z")

</div>

Please try these manually then:

- In `/package.json`, delete lines: 34, 32, 28
- delete `/yarn.lock`
- delete `/node_modules`
- delete `/repos/web/.next`
- delete `/repos/web/node_modules`

```auto
yarn cache clean
yarn
yarn dev

```

If still not working, it must be yarn, or maybe Next.js itself does not work in WIndows lol. My yarn version is: 1.22.10

---

<div class="post-metadata">

**Author:** ![ecoin](https://avatars.discourse-cdn.com/v4/letter/e/8e7dd6/32.png) [@ecoin](https://forum.babylonjs.com/u/ecoin)\
**Post date:** [February 12, 2022, 5:11pm UTC](https://forum.babylonjs.com/t/babylon-next-js-setup-optimized-for-seo-load-speed-developer-experience/27412/18 "2022-02-12T17:11:58Z")

</div>

> [@labris](#):
>
> 12.22.1 I had the same problems.

The boilerplate assumed that you have Next.js installed in your machine, but if not, you should try official doc first [Docs | Next.js](https://nextjs.org/docs/getting-started)

See if you can actually start any Next.js project to begin with.

---

<div class="post-metadata">

**Author:** ![labris](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/labris/32/12576_2.png) [@labris](https://forum.babylonjs.com/u/labris)\
**Post date:** [February 12, 2022, 6:41pm UTC](https://forum.babylonjs.com/t/babylon-next-js-setup-optimized-for-seo-load-speed-developer-experience/27412/19 "2022-02-12T18:41:36Z")

</div>

Should I try to use first

```auto
yarn create next-app

```

before yarn dev (or even before yarn)?

---

<div class="post-metadata">

**Author:** ![ecoin](https://avatars.discourse-cdn.com/v4/letter/e/8e7dd6/32.png) [@ecoin](https://forum.babylonjs.com/u/ecoin)\
**Post date:** [February 12, 2022, 6:56pm UTC](https://forum.babylonjs.com/t/babylon-next-js-setup-optimized-for-seo-load-speed-developer-experience/27412/20 "2022-02-12T18:56:23Z")

</div>

That Getting Started will create an entirely new Next.js project, so I suggest you cd out of babylon project first.

```auto
yarn create next-app # this will create new folder in current WORK_DIR/next-app 

```

This is to test if your machine can successfully install and run Next.js from the official doc. So we can eliminate where your bug is coming from.

[Next page](https://forum.babylonjs.com/t/babylon-next-js-setup-optimized-for-seo-load-speed-developer-experience/27412.md?page=2)
