-
Notifications
You must be signed in to change notification settings - Fork 86
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Markdown files cannot be found in netlify function environment #153
Comments
hey @MejanH thanks for opening this! happy to help. however, need a little bit more info from you. the fastest/easiest way for me to help you is for you to open a repo that reproduces this issue and that i can deploy myself. from the deploy logs you shared, i don't see a markdown dir under pages which means, wherever you have it, it's excluded from what next-on-netlify or next as a framework would handle. let me know! |
Thanks for the reply. For the demo, I just followed the next.js learn pages. At build time there is no problem with 'fs". But I don't why it's not working on view. |
hey, i spent a bit of time trying to get also, i can tell from your logs in your original post that what you have deviates from |
Hi, I have created a repo as you said. tried with
JSON error after publishing
|
@MejanH ok WEW finally got this deployed and diagnosed! so here's what's going on: because these pages are trying to use ISR (see this is something we can/should address, but for now, if you want this to Just Work, don't use ISR/revalidate. it will work as a plain SSG'd/prerendered page. thanks for opening that separate repo for me and let me know if you have any other questions!! i'm going to change the title if that's cool to be a little more specific to the issue :) |
Thanks for the help. Will wait for this feature. |
@MejanH hey! we pushed up a temporary solution for this supported in the plugin and wanted to let you know since you were the very first to surface this issue! i will note that this is a temporary, unstable solution. we're also working on a greater, platform-wide solution for this type of file inclusion. if you'd like access to the short term next-specific plugin solution, feel free to email me [email protected] or dm on twitter @levlinds. otherwise, i'll leave this issue open until the longer term solution rolls out! <3 |
Reaching out to you on twitter for this magical temporary solution! |
the platform solution is finally out!!! https://docs.netlify.com/configure-builds/file-based-configuration/#functions please go ahead and give it a shot! |
Thanks for the good news @lindsaylevine! Is this also intended to work with Server-side Rendered pages using |
@coreyaus hey! yes it should work with getServerSideProps! mind sharing how you're reading these files in your page, where the files are in your project, and how you configured your netlify.toml? that'd help me figure out why it's not working for you! :) |
Thanks for the prompt response @lindsaylevine! I've got this up and running now. Essentially I found that import matter from "gray-matter";
export const getServerSideProps = async ({ params }) => {
const fs = require("fs");
const path = require("path");
const directoryPath = path.join(process.cwd(), "_posts");
const pageSlugs = fs.readdirSync(directoryPath);
const allPosts = pageSlugs.map((slug) => {
const fullPath = path.join(directoryPath, slug);
const fileContents = fs.readFileSync(fullPath, "utf8");
const document = matter(fileContents);
return { slug, ...document };
});
return {
props: { ...params, allPosts },
};
}; The revised version that does work across localhost, Netlify and Vercel borrows heavily from this repository: https://github.com/cassidoo/next-netlify-blog-starter and looks more like the below, where the import matter from "gray-matter";
import getPosts from "path/to/utils/file";
export const getServerSideProps = async ({ params }) => {
const allPosts = ((context) => {
return getPosts(context);
})(require.context("../../posts", true, /\.md$/));
return {
props: { ...params, allPosts },
};
}; For that code to work you also need to run config.module.rules.push({
test: /\.md$/,
use: "raw-loader",
}); It would be handy to have Anyway, mostly just sharing in case these notes help anyone else (and also as a plug for all the excellent Netlify start repos that exist like the one linked in this comment!) 😄 PS: might also be worth posting the good news about the latest platform solution for this problem here too: https://answers.netlify.com/t/how-to-access-files-from-nextjs-api-function/35347/2 |
@coreyaus hey corey! per @eduardoboucas aka the engineer behind thanks so much for sharing your notes regardless, we appreciate it and i'm sure other users will too!! i also left a comment on that community thread 👍 |
@lindsaylevine I tried, 3.4.0 My workaround has been using How is Do you have any idea when this will be generally available and where would be the best way to find out when it's ready? - I ask specially as this seems as "small" feature in netlify's nextjs support scope thanks in advance! |
@juanpprieto hey there! my apologies for not circling back here!!! my teammate @eduardoboucas just recently shed some light on that question here. per his most recent comment, it's something we'll be rolling out soon. you can subscribe to #359 to see when eduardo gives that release update! |
@juanpprieto the fix that @lindsaylevine mentioned has now been rolled out. Can you please check if it works for you? Thanks! |
Thanks @eduardoboucas. Unfortunately, I haven't had any luck. I just tried this on "@netlify/plugin-nextjs": "3.4.2", but I'm either not accessing .jsons correctly or it's not working. netlify.toml
.json & getStaticPropsMy .jsons live on
Console logging NotesI inspected the Any idea what I may be doing wrong? P.S reading from the filesystem works fine on every context except (isPreview === true && NODE_ENV === 'production') |
Hi @juanpprieto. The path should be relative to the project root, so if the files live under [functions]
node_bundler = "esbuild"
included_files = ["public/json/collections/*.json"] And to read them you can try: const collection = await fs.readFile(path.resolve(`public/json/collections/${params.handle}.json`), 'utf8') Let me know if this helps. |
Game changer! 🔥🔥🔥 Thanks! @eduardoboucas |
Hi. First of all, Thanks for the plugin.
I am getting errors with my markdown blog. Pages and components seem to be working perfectly (tested with sample). but not working with the markdown directory. It's giving me an error related to my file location.
Deploy Log
The text was updated successfully, but these errors were encountered: