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

The Astro plugin is not compatible with Astro 5 #862

Open
alexisreina opened this issue Dec 18, 2024 · 3 comments
Open

The Astro plugin is not compatible with Astro 5 #862

alexisreina opened this issue Dec 18, 2024 · 3 comments

Comments

@alexisreina
Copy link

alexisreina commented Dec 18, 2024

Describe the bug

Astro 5 introduces breaking changes that affect this plugin. According to astro documentation:

"Astro v5.0 updates the shape of IntegrationRouteData.distURL to be undefined or an array of URLs. This fixes a previous error because a route can generate multiple files on disk, especially when using dynamic routes such as [slug] or [...slug]"

This change made to the distUrl used by the plugin prepareOramaDb causes the plugin to throw the following error before completing:

Cannot read properties of undefined (reading 'endsWith')
Location:
  /Users/.../node_modules/@orama/plugin-astro/dist/index.js:40:70

To Reproduce

  1. Create an astro 5 project
  2. Install and configure the orama plugin
  3. Build the project

Expected behavior

The porject builds and the orama db is created

Environment Info

OS: Macos 14.7.1
Node: v18.20.1

Affected areas

Initialization

Additional context

After reviewing the code looks like an easy fix that can be done backwards compatible so it won't require a major relase of the plugin

@alexisreina
Copy link
Author

alexisreina commented Dec 18, 2024

Changing the method from

async function prepareOramaDb(
  dbConfig: OramaOptions,
  pages: AstroPage[],
  routes: RouteData[],
  dir: URL
): Promise<Orama<PageIndexSchema, any, any, any>> {
  const contentConverter = compile({
    baseElements: {
      selectors: dbConfig.contentSelectors?.length ? dbConfig.contentSelectors : ['body']
    }
  })

  // All routes are in the same folder, we can use the first one to get the basePath
  const basePath = dir.pathname.slice(isWindows ? 1 : 0)
  const pathsToBeIndexed = pages
    .filter(({ pathname }) => dbConfig.pathMatcher.test(pathname))
    .map(({ pathname }) => {
      // Some pages like 404 are generated as 404.html while others are usually pageName/index.html
      const matchingPathname = routes
        .find((r) => r.distURL?.pathname.endsWith(pathname.replace(/\/$/, '') + '.html'))
        ?.distURL?.pathname?.slice(isWindows ? 1 : 0)
      return {
        pathname,
        generatedFilePath: matchingPathname ?? `${basePath}${pathname.replace(/\/+$/, '')}/index.html`
      }
    })
    .filter(({ generatedFilePath }) => !!generatedFilePath)

to the one below should do it, so instead of looking directly in distUlr we look in a distUlrs collection we have previously created

async function prepareOramaDb(
  dbConfig: OramaOptions,
  pages: AstroPage[],
  routes: RouteData[],
  dir: URL
): Promise<Orama<PageIndexSchema, any, any, any>> {
  const contentConverter = compile({
    baseElements: {
      selectors: dbConfig.contentSelectors?.length ? dbConfig.contentSelectors : ['body']
    }
  })

  // All routes are in the same folder, we can use the first one to get the basePath
  const basePath = dir.pathname.slice(isWindows ? 1 : 0)
  // Create a dist urls
  const distUrls = routes.flatMap(r => r.distURL);
  const pathsToBeIndexed = pages
    .filter(({ pathname }) => dbConfig.pathMatcher.test(pathname))
    .map(({ pathname }) => {
      // Some pages like 404 are generated as 404.html while others are usually pageName/index.html
      const matchingPathname = distUrls
        .find((url) => url?.pathname.endsWith(pathname.replace(/\/$/, '') + '.html'))?.pathname?.slice(isWindows ? 1 : 0)
      return {
        pathname,
        generatedFilePath: matchingPathname ?? `${basePath}${pathname.replace(/\/+$/, '')}/index.html`
      }
    })
    .filter(({ generatedFilePath }) => !!generatedFilePath)

@micheleriva
Copy link
Member

Hey @alexisreina thanks for opening this. Would you mind opening a PR with your proposed changes? We'd be glad to help debugging and landing the PR in the next release 🙏

@awhitford
Copy link

@micheleriva, I was trying to submit a PR for this, but my clone doesn't pass pnpm test.

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

No branches or pull requests

3 participants