#on pnpm run build iam getting error

8 messages · Page 1 of 1 (latest)

unkempt mulchBOT
#

Hi @ionic harbor. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

It looks like something in your build is calling new URL(...) with an invalid or missing base during prerendering. The docs show how Astro constructs URLs from the current request and how to safely build canonical or social URLs using Astro.url (and Astro.site as the base). Make sure any code that does new URL(...) during prerender uses a valid absolute base:

  • Prefer: new URL(Astro.url.pathname, Astro.site) for canonical URLs, or new URL('/path', Astro.url). [url]
  • If you previously used Astro.canonicalURL, that was removed; switch to the pattern above. [Removed canonicalURL]

Why this fails only at build:

  • During prerender, Astro.url is based on your site config. If site is not set, prerendered pages get a localhost URL; if any code does new URL(relativeOrEmpty) without a base, Node throws “Invalid URL.” [url]

What to check:

  1. In your layout (the stack points to Layout_e2BB4PZ7.mjs), find any new URL(...) calls and give them a proper base (Astro.url or Astro.site). [url]
  2. If you construct canonical URLs, use:
    ---
    const canonicalURL = new URL(Astro.url.pathname, Astro.site);
    ---
    <link rel="canonical" href={canonicalURL} />
    
    [Removed canonicalURL; url]
  3. Ensure your astro.config has site set if you rely on Astro.site when building absolute URLs. The docs note prerendered routes use site/base to build Astro.url. [url]
#

If, after fixing URL construction, you still hit a generic build failure, Vite’s “unknown error” guidance suggests opening an issue with a minimal repro. [Unknown Vite Error]

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@ionic harbor Kapa.ai is still learning and improving, please let me know how I did by reacting below.

ionic harbor
#

import tailwindcss from "@tailwindcss/vite";

import react from "@astrojs/react";

import cloudflare from "@astrojs/cloudflare";

export default defineConfig({
  i18n: {
    defaultLocale: "en",
    locales: ["en", "ar"],
    routing: {
      prefixDefaultLocale: false,
    },
  },

  vite: {
    plugins: [tailwindcss()],
  },

  integrations: [react()],
  adapter: cloudflare(),
});

@unkempt mulch this is my astro config

unkempt mulchBOT
#

Let me check the knowledge base for you again. One moment! :books:

#

Thanks! The “Invalid URL” during prerender likely comes from code in your layout/page doing new URL(...) without a valid absolute base while Astro is prerendering.

What to do:

  • If you build absolute URLs (canonical, social, etc.), always pass a base:

    ---
    // Requires site set in astro.config for consistent builds
    const canonicalURL = new URL(Astro.url.pathname, Astro.site);
    const socialImageURL = new URL('/images/preview.png', Astro.url);
    ---
    

    [url]

  • Set site in your astro.config so Astro.url/Astro.site resolve consistently during prerender:

    // astro.config.mjs
    import { defineConfig } from "astro/config";
    import tailwindcss from "@tailwindcss/vite";
    import react from "@astrojs/react";
    import cloudflare from "@astrojs/cloudflare";
    
    export default defineConfig({
      site: "<https://your-domain.com",>
      i18n: {
        defaultLocale: "en",
        locales: ["en", "ar"],
        routing: { prefixDefaultLocale: false },
      },
      vite: { plugins: [tailwindcss()] },
      integrations: [react()],
      adapter: cloudflare(),
    });
    

    Prerendered routes use site (and base) to construct Astro.url; without site, you may hit invalid URL cases if your code creates URLs without a base. [url]

  • If you previously relied on Astro.canonicalURL, replace it with the pattern above. [Removed Astro.canonicalURL]

Also, since you’re deploying to Cloudflare:

  • If you use any Node.js APIs, ensure pages are not prerendered and use node:* imports per the Cloudflare adapter docs. [Node.js compatibility]
#

If you still see the error, search your Layout component (the stack points to it) for new URL(...) and ensure every call has an absolute base (Astro.url or Astro.site). [url]

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: