question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to make slugs follow file structure?

See original GitHub issue

I’m trying to make the paths (slugs? I get confused over terminology sometimes) of my content follow the file structure under the /content directory, but I can’t get it quite right. You can see my code at https://codesandbox.io/s/uy5lo?file=/nuxt.config.js. This is built on Nuxt 2.13 with target:static.

This is the behavior I hope to achieve:

directory/file  |  resulting path
----------------+----------------
/content        |  
  /hello.md     |  /hello
  /foo.md       |  /foo
  /foo          |
    /bar.md     |  /foo/bar

…or alternatively, this would be fine too:

directory/file  |  resulting path
----------------+----------------
/content        |  
  /hello.md     |  /hello
  /foo          |
    /index.md   |  /foo
    /bar.md     |  /foo/bar

To make this work, I created a _.vue route which fetches content with the {deep: true}. This seems to work fine. I also added the following hook:

  hooks: {
    'content:file:beforeInsert': (document) => {
      // Just strip the leading slash off document.path.
      document.slug = document.path.replace(/^\//, '')
    }
  }

While this works for /hello and /foo/bar, it does not work for /foo. The foo.md file seems to clash with the directory of the same name. I did read in the documentation that “If path […] is a directory it will return an Array”, which sounds like an explanation, but it doesn’t provide a solution.

What did I miss? Thanks for your assistance.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:5
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
benjamincanaccommented, Jul 2, 2020

Hi @marcvangend

Could you try with:

const [ doc ] = await $content('', { deep: true }).where({ path: route.path.replace(/\/$/, '') }).fetch()

With this you can also remove your hook.

2reactions
benjamincanaccommented, Jul 6, 2020

@marcvangend The first argument of $content can actually be nothing because it defaults to /: https://content.nuxtjs.org/fetching#contentpath-options, so you can do:

await $content({ deep: true }).where({ path: `/${path}` }).fetch()

I agree with you, you should be able to query a file with the same name as a directory, I’ve added this to our roadmap, thanks for your feedback!

Read more comments on GitHub >

github_iconTop Results From Across the Web

What the Heck are Content Slugs and How Do I Use Them?
Gatsby's default behavior is to use the file name as the slug, with the URL directory being the path. Overriding that on a...
Read more >
URL Slugs: How to Create SEO-Friendly URLs (10 Easy Steps)
This post contains my personal ten-step process for creating URL slugs that searchers and search engines LOVE. It's a super simple method that ......
Read more >
Slug Compiler | Heroku Dev Center
Slugs are compressed and pre-packaged copies of an application, optimized for distribution by the Heroku dyno manager. Minimize slug size ...
Read more >
URL slugs - Squarespace Help Center
To change the blog page slug, follow the steps above to change a page URL. Tap Post settings, then tap Save.
Read more >
Permalinks & Slugs – WordPress.com Support
Slugs are the URL-friendly names of your posts, pages, categories, and tags. ... you the ability to create a custom URL structure for...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found