Skip to content

[Bug]: Graph fails to load when `SPACESHIP_BASE="/" #24

@justbry

Description

@justbry

Describe the bug

Issue Description

The graph visualization on the landing page fails to load when the SPACESHIP_BASE environment variable or base config option is set to "/" (root path).

Root Cause

The bug is in the GraphView.astro component's URL construction logic (line 59-60). When base="/", the current implementation creates a malformed URL:

Current code:

const base = this.getAttribute("base");
const slug = this.getAttribute("slug");
const url = base ? `/${base.replace(/^\/+|\/+$/g, '')}/_spaceship/graph/${slug ?? "index"}.json` : `/_spaceship/graph/${slug ?? "index"}.json`;

What happens when base="/":

  1. base.replace(/^\/+|\/+$/g, '') strips leading/trailing slashes from "/", resulting in an empty string ""
  2. The ternary condition base ? ... is still truthy (because base is "/", not null or undefined)
  3. The URL becomes: /${""}/_spaceship/graph/index.json///_spaceship/graph/index.json
  4. This triple-slash URL is invalid and causes the fetch to fail
  5. No graph data loads, so the graph doesn't render

Expected Behavior

When base="/", the graph should load from /_spaceship/graph/index.json (same as when base is undefined).

Proposed Fix

Modify the URL construction to check the normalized base path instead of the raw base attribute:

const base = this.getAttribute("base");
const slug = this.getAttribute("slug");
const normalizedBase = base ? base.replace(/^\/+|\/+$/g, '') : '';
const url = normalizedBase 
  ? `/${normalizedBase}/_spaceship/graph/${slug ?? "index"}.json` 
  : `/_spaceship/graph/${slug ?? "index"}.json`;

This ensures:

  • When base="/"normalizedBase="" → URL is /_spaceship/graph/index.json
  • When base="/subpath"normalizedBase="subpath" → URL is /subpath/_spaceship/graph/index.json
  • When base is undefined/null → normalizedBase="" → URL is /_spaceship/graph/index.json

File to Change

File: components/GraphView/GraphView.astro
Lines: 59-60

Before:

async loadData(): Promise<GraphViewData> {
  const base = this.getAttribute("base");
  const slug = this.getAttribute("slug");
  const url = base ? `/${base.replace(/^\/+|\/+$/g, '')}/_spaceship/graph/${slug ?? "index"}.json` : `/_spaceship/graph/${slug ?? "index"}.json`;

  const response = await fetch(url);
  return response.json();
}

After:

async loadData(): Promise<GraphViewData> {
  const base = this.getAttribute("base");
  const slug = this.getAttribute("slug");
  const normalizedBase = base ? base.replace(/^\/+|\/+$/g, '') : '';
  const url = normalizedBase 
    ? `/${normalizedBase}/_spaceship/graph/${slug ?? "index"}.json` 
    : `/_spaceship/graph/${slug ?? "index"}.json`;

  const response = await fetch(url);
  return response.json();
}

How to Reproduce

  1. Set SPACESHIP_BASE="/" in your .env file, or set base: "/" in website.config.json
  2. Run the dev server
  3. Navigate to the landing page
  4. Observe that the graph visualization doesn't load
  5. Check browser console/network tab - you'll see a failed fetch to ///_spaceship/graph/index.json

Environment

  • astro-spaceship version: (latest from node_modules)
  • Astro version: 5.12.8
  • Browser: Any

This explanation provides:

  1. Clear problem description - what fails and when
  2. Root cause analysis - why the bug occurs with code walkthrough
  3. Proposed solution - exact code changes needed
  4. Reproduction steps - how to verify the bug
  5. Expected vs actual behavior - what should happen

Would you like me to format this as a GitHub issue, pull request description, or another format?

To Reproduce

Steps to Reproduce

  1. Set SPACESHIP_BASE="/" in your .env file or "base": "/" in website.config.json
  2. Run npm run dev or bun dev
  3. Open the landing page in your browser
  4. Observe that the graph visualization area is empty/doesn't load
  5. Check browser DevTools → Network tab for a failed request to ///_spaceship/graph/index.json (note the invalid triple slash)

Expected: Graph should load from /_spaceship/graph/index.json and display nodes/connections

Actual: Graph fails to load due to malformed URL

Expected behavior

No response

Screenshots

No response

Additional context

No response

Code of Conduct

  • I agree to follow this project's Code of Conduct

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions