Skip to content
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

index.md doesn't get loaded when using <ContentDoc> with dynamic routing. #1189

Closed
dankcatlord opened this issue May 31, 2022 · 4 comments · Fixed by #1224
Closed

index.md doesn't get loaded when using <ContentDoc> with dynamic routing. #1189

dankcatlord opened this issue May 31, 2022 · 4 comments · Fixed by #1224
Assignees

Comments

@dankcatlord
Copy link

dankcatlord commented May 31, 2022

Environment

Nuxt CLI v3.0.0-rc.3
RootDir: /home/projects/nuxt-starter-jx8vzt
Nuxt project info:

------------------------------
- Operating System: `Linux`
- Node Version:     `v16.14.2`
- Nuxt Version:     `3.0.0-rc.3`
- Package Manager:  `npm@7.17.0`
- Builder:          `vite`
- User Config:      `modules`
- Runtime Modules:  `@nuxt/content@2.0.1`
- Build Modules:    `-`
------------------------------

Reproduction

I reproduced the issue in stackblitz.
https://stackblitz.com/edit/nuxt-starter-jx8vzt

Describe the bug

When using dynamic routing, rather than loading the index.md file inside the directory pointed to by $route.path, the page loaded is the first one found alphabetically inside the directory pointed to by $route.path.

Additional context

As far as I know, this bug isn't in 2.0.0.

Logs

No response

@dankcatlord dankcatlord changed the title index.md doesn't get loaded at root when using <ContentDoc> with dynamic routing. index.md doesn't get loaded when using <ContentDoc> with dynamic routing. Jun 1, 2022
@Atinux
Copy link
Member

Atinux commented Jun 1, 2022

I think we shall for the path to be equal and not starting by when using ContentDoc @farnabaz

@itshizhan
Copy link
Contributor

the same questions

@qin-guan
Copy link

qin-guan commented Jun 2, 2022

The issue appears to be caused by this PR: #1143

@qin-guan
Copy link

qin-guan commented Jun 2, 2022

@Atinux From some rough experimenting, updating

https://github.com/nuxt/content/blob/main/src/runtime/composables/query.ts#L44

export function queryContent<T = ParsedContent> (query?: string | QueryBuilderParams, ...pathParts: string[]) {
  if (typeof query === 'string') {
    let path = withLeadingSlash(withoutTrailingSlash(joinURL(query, ...pathParts)))
    // escape regex special chars
-   path = path.replace(/[-[\]{}()*+.,^$\s]/g, '\\$&')
+   path = path.replace(/[-[\]{}()*+.,^$\s/]/g, '\\$&')

-   return createQuery<T>(queryFetch).where({ _path: new RegExp(`^${path}`) })
+   return createQuery<T>(queryFetch).where({ _path: new RegExp(`^${path}$`) })
  }

  return createQuery<T>(queryFetch, query)
}

to include / in path and $ in regex query fixes the issue. I am not sure of any unintended side effects this might cause, please advise if the changes are alright 😄 !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants