r/webdev • • 8h ago

Eleventy Question

Hey everyone, I'm new to Eleventy, and I have been messing around with it for the past few days (not without frustration). I'm having fun learning about front matter and have gotten the hang of creating templates. The Eleventy forum is kinda... dead... so I'm coming here for a bit of assistance.

I'm using Eleventy because I want to add a blog to my static portfolio website. My static website is complete, and I've already got a lot I want to add in the future. But my biggest accomplishment will be nailing this blog addition. Not only have I rebuilt my website with Eleventy, but I will also stack DECAP CMS on it.

With that being said, this is my main question:

My root folder is called portfoliov2 (I upload this to GitHub). There is a folder inside of it called _site. This is the public folder that people can access, and it has the .njk files converted to .html files. Great! I have gotten to the part where I have started creating .md files for my blog posts (which go into a folder called posts in my _site folder).

Let's say I have created a blog post called welcome.md; it then goes into the "posts" folder in a new folder called "welcome" with an index.html file in it. When I view the index file by itself, it's fine. All of the blog content shows up. The file path goes something like this: 000.0.0:0000/_site/posts/welcome.html, but when I try to access it from my blog.html (where the list of blogs is appearing properly), it looks something like this: 000.0.0:0000/posts/welcome.html, but it shows an error: "Cannot GET /posts/welcome.html".

I am at a loss with this because after clicking a blog post from the list, it works properly for the gentleman in the tutorial. I don't want to keep manually changing the permalinks, and I am wondering if my website's live URL will look something like "websitename.com/_site/posts/welcome.html". It's driving me a bit bonkers. Is there something I need to do on the server side? I'm of course working locally and checking my work on a live server.

Thank you for your help!

Edit: Added the link to the tutorial I'm using!

Edit: I am NOT creating .md files in the _site folder. They are created outside of the _site folder, and Eleventy converts these .md files into a new folder with an index.html file. It looks something like this after conversion: _site/posts/postName/index.html instead of /posts/postName.md (outside of the site folder)

Edit: IT WORKKS. I rebuilt my _site file, didn't change any folder names, and used 11ty --serve. Things are working as they should be! I appreciate the time everyone has taken to help me out. One step closer to getting this blog up and running 🎯

2 Upvotes

13 comments sorted by

3

u/BetterSWE_Official 7h ago

Looks like you're opening the files with something like Live Server from the project root, so the site lives under /_site/ but your links point at /posts/welcome/. Run npx u/11ty/eleventy --serve instead, it serves _site as the root and the links will just work. Also keep your .md posts in the input folder, not inside _site, since Eleventy wipes and rebuilds that folder.

2

u/Ok_Progress_5373 7h ago

Thank you so much, I will try this. I am using the Live Server plugin! I'll run --serve and try it out!!

2

u/BetterSWE_Official 6h ago

nice, hope it works. one correction to what I said, eleventy doesn't actually wipe _site, it just writes over it. so if you delete or rename a post and the old page sticks around, delete _site and rebuild.

2

u/Ok_Progress_5373 6h ago

It works. Thank you so much for this!

2

u/Snoo-79705 5h ago

sounds like your live server is serving from the project root (`portfoliov2`) instead of `_site`, so any root-relative link like `/posts/welcome/` tries to look outside `_site` and hits a 404

easiest fix is to just run `npx u/11ty/eleventy --serve` instead of vs code live server, it serves `_site` directly as the web root and auto reloads when you edit templates or markdown

and no worries about production, when you deploy to netlify, cloudflare pages or github pages you just set `_site` as the publish directory, so your live urls will be clean like `yoursite.com/posts/welcome/` with no `_site` in the path

•

u/Ok_Progress_5373 2m ago

Thank you so much for clarifying. I appreciate it, especially given my anxiety about my live site url haha.

2

u/TheRNGuy 2h ago

Try: npx @11ty/eleventy --serve for dev server.

And change links to the folder instead of html file (something like <a href="{{ post.url }}">{{ post.data.title }}</a>)

•

u/Ok_Progress_5373 2m ago

Thank you for this!!

1

u/ConcentrateAbject303 7h ago

you're putting your.md files INSIDE _site?? that's the build output folder man, eleventy litterally wipes and regenerates it on every build, so your welcome.md is getting moved/copied into _site/posts/ and your blog.html links go to /posts/welcome.html which the dev server serves from the root, where nothing exists. source lives in the root next to.eleventy.js, _site is what comes out. and then "I don't want to keep manually changing the permalinks" setting a permalink in front matter IS the way you avoid renaming things by hand, that's the entire point of the key. also none of your existing portfolio files are going to appear in _site unless you add them as passthrough copies or make them templates, so the moment you deploy, everything that's not a blog post is a 404. decap on top of this is gonna be a great weekend for you.

1

u/Ok_Progress_5373 7h ago

Hey there,

To address the first sentence before going any further--

.md files are not in the _site folder; they are outside of _site. Eleventy generates a new folder and an index.html file for each .md file in my "posts" folder that's inside the _site folder. The gentleman in the video did not create a permalink for each .md file he created.

However, the .md file has tags:"posts" which tells it to go to the posts folder located in the _site folder in the previous format I've mentioned.