datopian / flowershow

💐 Publish your obsidian digital garden or any markdown site easily and elegantly.
https://flowershow.app/
MIT License
832 stars 100 forks source link

Empty pages cause build error #45

Open olayway opened 2 years ago

olayway commented 2 years ago

Not sure if we should fix this or add a note somewhere in the docs, that there shouldn't be any empty pages in the content dir.

What happens?

Adding an empty page to the content dir (content/docs/roadmap.md in this case) results in a build error:

> Build error occurred
Error: Export encountered errors on following paths:
        /[[...slug]]: /docs/roadmap

image

Notes

This is because json file generated by Contentlayer for this page, has code value equal to an empty string.

{
  "layout": "docs",
  "body": {
    "raw": "",
    "code": ""
  },
  ...
  "type": "Page",
  "url": "roadmap"
}

Notes

The error is thrown in useMDXComponent hook imported from contentlayer/hooks. Since code in case of empty md files will be an empty string, the function created with new Function(...Object.keys, code) will have no body, so it will implicitly return undefined.

https://github.com/contentlayerdev/contentlayer/blob/9c19fbd349eb3c677753ac4c5b6323115e22d31c/packages/next-contentlayer/src/hooks/useMDXComponent.ts#L22-L30

Current workaround

const defaultCode = `
return {default: () => React.createElement('div', null, '')}
`;

const pageCode = body.code.length > 0 ? body.code : defaultCode;
const MDXPage = useMDXComponent(pageCode);
rufuspollock commented 1 year ago

@olayway just ran into this bug myself in building on life itself site. IMO this is a bug upstream in contentlayer - shouldn't it be ok with empty pages?

Bumping priority to medium as i suspect others will encounter this error and quite mysterious.

rufuspollock commented 1 year ago

@olayway can we open an issue for this upstream in contentlayer and then move this to blocked.

obstschale commented 1 year ago

I just started to build a new handbook using flowershow and ran into this bug. I did not anticipate that this could be caused by an empty page. I was using Umlauts (ä, ü, ö [german]), special charaters (?) and tried every combination to debug it.

I was deeply expecting that an empty page is not the problem. I would argue, this is a bug and should be fixed. It would have saved my quite some time.

olayway commented 1 year ago

@obstschale thank you for your comment and for reminding us about this issue! I've just committed a quick fix for it, so you and other users shouldn't encounter this bug anymore. Now the page will render correctly, just with no content 😀

obstschale commented 1 year ago

@olayway You surprise me again. Thank you in the name of future users who would have stumbled upon this bug. 🙇

olayway commented 1 year ago

Issue in Contentlayer repo: https://github.com/contentlayerdev/contentlayer/issues/370