Next-translate: Build Error when using "documentLang" in TypeScript

Created on 29 May 2020  路  9Comments  路  Source: vinissimus/next-translate

Hi,
i don't know why i didn't get this error earlier, but using v0.17.0-canary.1 and upwards if i use next-translate/documentLang in the _document.tsx i get the following error on build:

Console Output:
console.txt

If i remove the documentLang this error doesn't appear.

Seems the important part is Automatically optimizing pages .Error: Cannot find module '/Users/userx/projects/own/project/packages/project-web/i18n.json'.

Please let me know if i can/should provide more information :)

bug

Most helpful comment

@lone-cloud @dnepro I did a fix on 0.17.1-canary.1. Can you confirm that it works now with TypeScript?

After that, I'm going to take a look deeply on this issue https://github.com/vinissimus/next-translate/issues/88

Feel free to reopen this issue if it is still happening. I'm going to wait to release 0.17.1 after receiving your feedback about this 馃槉

All 9 comments

Same issue here. Everything works fine in next dev but fails on build. The weird thing is that, that file definitely exists in that location.

I tried to build and export the example of this repo https://github.com/vinissimus/next-translate/tree/master/examples/static-site and I can't reproduce the same issue.

@dnepro @lone-cloud Can you provide more info? Next.js version, if you are getting this error deploying to Vercel or just doing next build && next export? A reproducible example would be very useful 馃憦

Apparently, it doesn't seem to like this:

https://github.com/vinissimus/next-translate/blob/f711246235467a503311d7efcc5340c92ad19e7f/src/documentLang.js#L3

I see in the Next.js docs that their use path.join(process.cwd(), 'file') instead of path.resolve(process.cwd() + '/file')). Perhaps the problem could be here...

I'm on the latest Next.js. I'm not deploying to Vercel, but rather just building locally. The issues is reproducible on both linux and windows for me. I'm not able to resolve, nor join, to require a .json file in my project. In all other instances I import them.
I forked this lib and I am able to build all of the examples without problem. At this point I believe that this bug is only reproducible in Typescript Next.js projects. Note that me and @dnepro are both experiencing this in a ts project.

Hi, i've created a small demo app dnepro/wlt-demo, it's a minimal version of my current app - and it won't "really" work because i excluded the certificates for the server.

Steps to reproduce:

  1. cd packages/web && yarn
  2. yarn start ... it should build and run the server as expected
  3. use the documentLang in pages_/_document.tsx
  4. run yarn start again and the build should fail

(Edited: removed lerna step)

@lone-cloud @dnepro I did a fix on 0.17.1-canary.1. Can you confirm that it works now with TypeScript?

After that, I'm going to take a look deeply on this issue https://github.com/vinissimus/next-translate/issues/88

Feel free to reopen this issue if it is still happening. I'm going to wait to release 0.17.1 after receiving your feedback about this 馃槉

Nice, I can confirm that your fix worked for me. Good job!

Great Job, also works for me!
隆Muchas gracias! 馃憤

I did a a bit more testing and while the new implementation builds well, it didn't end up working out with my deployment process. I use https://github.com/danielcondemarin/serverless-next.js which doesn't allow an easy way to deploy a custom directory structure. The current documentLang implementation requires the i18n.json to be deployed to be able to handle SSR requests.

The boilerplatey way that I've been using continues to work because it inlines the i18n.json in the bundle:
```
import i18nConfig from 'i18n.json';

// Update document language
function documentLang({ __NEXT_DATA__ }) {
const { page } = __NEXT_DATA__;
const [, langQuery] = page.split('/');
const lang = i18nConfig.allLanguages.find((l) => l === langQuery);

return lang || i18nConfig.defaultLanguage;
}

@lone-cloud thanks for contributing. I added your code on 0.17.1-canary.2 pre-release.

Was this page helpful?
0 / 5 - 0 ratings