withastro / astro

The web framework for content-driven websites. ⭐️ Star to support our work!
https://astro.build
Other
45.68k stars 2.4k forks source link

🐛 BUG: `fetchContent` unable to parse string literals and paths from variables #2026

Closed aFuzzyBear closed 2 years ago

aFuzzyBear commented 2 years ago

What version of astro are you using?

0.21.0

What package manager are you using?

npm

What operating system are you using?

linux - Stackblitz

Describe the Bug

This issue is raised as part of escalating the support issue :

Question about fetchContent

The issue relates to some of the behaviour that seems to be a bit different under the new compiler, the following patterns of behaviour do not work.

---
const route = '../docs/*.md'

const getFromRoute = Astro.fetchContent(route)
const getFromRouteFromLiteral = Astro.fetchContent(`${route}`)
const getFromRouteAsStrLiterals = Astro.fetchContent(`../docs/*.md`)
---

The following do

---
const getFromRouteSingleApostrophy = Astro.fetchContent('../docs/*.md')
const getFromRouteDoubleApostrophy = Astro.fetchContent("../docs/*.md")
---

Could this please be investigated further to clarify the position on variables and string literals with fetchContent.

Many Thanks,

Link to Minimal Reproducible Example

https://stackblitz.com/edit/github-qe8ifc-x993yc?file=src/pages/fetch.astro

FredKSchott commented 2 years ago

This isn't supported by Vite, unfortunately. But it shouldn't be silently failing! I wonder if there's a warning that we're accidentally hiding that would help.

As a workaround, you can always over-fetch and then filter after the fact with .filter() to remove the items you don't need.

aFuzzyBear commented 2 years ago

awesome, thanks @FredKSchott , this would need to be documented a bit clearer and lke you mentioned have some form of warning just to assist the dx. Ill relay this back to the support thread it was raised in.