Next.js: How to get static assets from within getStaticProps

ぃ、小莉子 提交于 2021-02-05 07:27:30

问题


I am using Netlify CMS. I want to import all the slides for a carousel into my component. I made a collection called slider and added a few slides. That created two markdown files (one for each slide) in public/content/slider/. I would like to import them all into an iteratable object so I can build the carousel.

Because I have a webpack loader set up for markdown files, I can import a single markdown file no problem, like this:

import post from '../public/content/posts/[post name].md

But when I try to use require.context, require-context, or import fs, it's no good. So I decide to try requiring those libs from within getStaticProps. But __dirname in getStaticProps is /, the root of my computer's filesystem.

All the getStaticProps examples use data fetching. I'm missing some info. How can I import all the markdown files in the /slides/ folder?


回答1:


This is a known issue in Next.js, __dirname incorrectly resolves to / as it stands.

The workaround is to use process.cwd() instead.



来源:https://stackoverflow.com/questions/65860421/next-js-how-to-get-static-assets-from-within-getstaticprops

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!