简体   繁体   English

Next.js:如何从 getStaticProps 中获取 static 资产

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

I am using Netlify CMS.我正在使用 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.我制作了一个名为 slider 的集合并添加了一些幻灯片。 That created two markdown files (one for each slide) in public/content/slider/ .这在public/content/slider/中创建了两个 markdown 文件(每张幻灯片一个)。 I would like to import them all into an iteratable object so I can build the carousel.我想将它们全部导入一个可迭代的 object 以便我可以构建轮播。

Because I have a webpack loader set up for markdown files, I can import a single markdown file no problem, like this:因为我为 markdown 文件设置了 webpack 加载程序,所以我可以导入单个 markdown 文件没问题,如下所示:

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.但是当我尝试使用require.contextrequire-context或 import fs时,就不好了。 So I decide to try requiring those libs from within getStaticProps .所以我决定尝试从getStaticProps中要求这些库。 But __dirname in getStaticProps is / , the root of my computer's filesystem.但是getStaticProps中的__dirname/ ,我的计算机文件系统的根目录。

All the getStaticProps examples use data fetching.所有getStaticProps示例都使用数据获取。 I'm missing some info.我缺少一些信息。 How can I import all the markdown files in the /slides/ folder?如何导入/slides/文件夹中的所有 markdown 文件?

This is a known issue in Next.js , __dirname incorrectly resolves to / as it stands.这是__dirname中的一个已知问题,__ dirname 错误地解析为/

The workaround is to use process.cwd() instead.解决方法是改用process.cwd()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM