askorama / orama

🌌 Fast, dependency-free, full-text and vector search engine with typo tolerance, filters, facets, stemming, and more. Works with any JavaScript runtime, browser, server, service!

Home Page:https://docs.askorama.ai

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Crashes on Astro when using an Image Service

okikio opened this issue · comments

Describe the bug

When you use a image service in Astro, a-la

import { defineConfig, squooshImageService } from 'astro/config';
import orama from "@orama/plugin-astro";

// https://astro.build/config
export default defineConfig({
  site: "https://astro.build",
  image: {
    service: squooshImageService()
  },
  output: "hybrid",
  integrations: [
    orama({
      // We can generate more than one DB, with different configurations
      mydb: {
        // Required. Only pages matching this path regex will be indexed
        pathMatcher: /.*/,

        // Optional. 'english' by default
        language: 'english',

        // Optional. ['body'] by default. Use it to constraint what is used to
        // index a page.
        contentSelectors: ['h1', 'main']
      }
    }),
  ],
});

The specific bug you get is this

...
ENOENT: no such file or directory, open 'undefineddist/about/index.html'
  Stack trace:
    at readFileSync (node:fs:456:20)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async withTakingALongTimeMsg (file:///workspace/astro/node_modules/.pnpm/astro@4.2.4_sass@1.70.0_typescript@5.3.3/node_modules/astro/dist/integrations/index.js:23:18)
    at async AstroBuilder.build (file:///workspace/astro/node_modules/.pnpm/astro@4.2.4_sass@1.70.0_typescript@5.3.3/node_modules/astro/dist/core/build/index.js:149:5)
    at async build (file:///workspace/astro/node_modules/.pnpm/astro@4.2.4_sass@1.70.0_typescript@5.3.3/node_modules/astro/dist/core/build/index.js:47:3)
    at async runCommand (file:///workspace/astro/node_modules/.pnpm/astro@4.2.4_sass@1.70.0_typescript@5.3.3/node_modules/astro/dist/cli/index.js:127:7)

I've reduced the issue to be due to the ways Orama selects the base URL for the built HTML files when analyzing, in fact I find the approach kind of odd since Astro now supports the baseURL directly via dir, I'm assuming this was due to an older version of Astro.

The way the Astro orama plugin currently selects the baseURL is basically to look through all the routes of the build including endpoint routes (which do NOT have a destination URL), the orama plugin then selects the first route and checks the destination URL, but Astro automatically pushes the image endpoint routes to being the first route when using an image service, which messes up the baseURL Orama uses, thus the fix is to just rely on Astro to give us the baseURL.

The solution for this issue is to just use the baseURL Astro provides in the astro:build:done hook like so const basePath = dir.pathname.slice(isWindows ? 1 : 0);

Hopefully, this helps https://docs.astro.build/en/reference/integrations-reference/#hooks:~:text=%27-,astro%3Abuild%3Adone%27%3F%3A%20(options%3A%20%7B%20dir%3A%20URL,-%3B%20routes%3A%20RouteData%5B%5D%3B%20logger

To Reproduce

  1. Install Astro & the Astro Orama Plugin
  2. Use an image service
  3. Error when running astro build

Expected behavior

I expected the correct baseURL should've been used

Environment Info

OS: Ubuntu 22.04
Node: 21
@orama/plugin-astro: 2.0.2

Affected areas

Data Insertion, Environment/OS

Additional context

This is a small bug in the way orama determines the baseURL when used in Astro. I'll create a quick PR and add it to the issue.

PR #618