angeloocana / gatsby-plugin-i18n

Multi language routes for Gatsby
434 stars 77 forks source link

Crash when placing posts in places other than `/src/pages`. #38

Open danmaq opened 6 years ago

danmaq commented 6 years ago

Hi, Thank you to create a nice plugin, I'm using it to create blogs.

Well, I found a bug in the gatsby-plugin-18n-tags package. If specified the posts in a directory other than /src/pages, the build will fail as shown below:

$ gatsby develop

success delete html and css files from previous builds — 0.007 s
success open and validate gatsby-config.js — 0.005 s
success copy gatsby files — 0.015 s
success onPreBootstrap — 1.253 s
error UNHANDLED EXCEPTION

  TypeError: Cannot read property 'langKey' of undefined

  - onCreateNode.js:43 
    [myproject]/[gatsby-plugin-i18n-tags]/onCreateNode.js:43:33

  - Array.map

  - onCreateNode.js:40 Object.onCreateNode
    [myproject]/[gatsby-plugin-i18n-tags]/onCreateNode.js:40:42

  - api-runner-node.js:110 runAPI
    [myproject]/[gatsby]/dist/utils/api-runner-node.js:110:36

  - api-runner-node.js:187 
    [myproject]/[gatsby]/dist/utils/api-runner-node.js:187:33

  - map.js:27 
    [myproject]/[async]/internal/map.js:27:9

  - eachOfLimit.js:64 replenish
    [myproject]/[async]/internal/eachOfLimit.js:64:17

  - eachOfLimit.js:49 iterateeCallback
    [myproject]/[async]/internal/eachOfLimit.js:49:17

  - onlyOnce.js:12 
    [myproject]/[async]/internal/onlyOnce.js:12:16

  - map.js:29 
    [myproject]/[async]/internal/map.js:29:13

  - util.js:16 tryCatcher
    [myproject]/[bluebird]/js/release/util.js:16:23

  - nodeify.js:23 Promise.successAdapter
    [myproject]/[bluebird]/js/release/nodeify.js:23:30

  - promise.js:566 Promise._settlePromise
    [myproject]/[bluebird]/js/release/promise.js:566:21

  - promise.js:606 Promise._settlePromiseCtx
    [myproject]/[bluebird]/js/release/promise.js:606:10

  - async.js:138 Async._drainQueue
    [myproject]/[bluebird]/js/release/async.js:138:12

  - async.js:143 Async._drainQueues
    [myproject]/[bluebird]/js/release/async.js:143:10

error An unexpected error occurred: "Command failed.
Exit code: 1
Command: sh
Arguments: -c gatsby develop
Directory: /Users/user/src/Repos/myproject
Output:
".
info If you think this is a bug, please open a bug report with the information provided in "/Users/user/src/Repos/myproject/yarn-error.log".
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
error An unexpected error occurred: "Command failed.
Exit code: 1
Command: sh
Arguments: -c yon run develop
Directory: /Users/user/src/Repos/myproject
Output:
".
info If you think this is a bug, please open a bug report with the information provided in "/Users/user/src/Repos/myproject/yarn-error.log".
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

I look like the type of the params passed to ptz-i18n.getSlugAndLang function was incorrect. The options parameter of this function seems to expects an plugins settings object, but gatsby-plugin-18n-tags.onCreateNode function passes options.langKeyForNull, ie a string.

Therefore, the function will misunderstand the posts location as /src/pages which is the default value, failing splitting of the path and slugAndLang constant has null.

And I think that this problem can probably be improved with the following a line only:

-     const slugAndLang = getSlugAndLang(options.langKeyForNull, node.fileAbsolutePath);
+     const slugAndLang = getSlugAndLang(options, node.fileAbsolutePath);
angeloocana commented 6 years ago

Thank you @danmaq ! Nice work!

Could you open a pull request with the fix?