withastro / astro

The web framework for content-driven websites. ⭐️ Star to support our work!
https://astro.build
Other
46.44k stars 2.46k forks source link

i18n getAbsoluteLocaleUrl only respect parts of the astro.config.mjs config in vite tests #11175

Closed ascheucher-shopify-partner closed 4 months ago

ascheucher-shopify-partner commented 4 months ago

Astro Info

Astro                    v4.9.2
Node                     v18.19.1
System                   macOS (arm64)
Package Manager          pnpm
Output                   static
Adapter                  none
Integrations             @astrojs/preact
                         @astrojs/sitemap

If this issue only occurs in one browser, which browser is a problem?

not browser related

Describe the Bug

I am new to Astro and also learning TS along the way. Maybe it's just me. But I assume it's related with vite test setup, maybe also a bug.

To learn more about the getAbsoluteLocaleUrl(), I created a vitest.

Given my astro.config.mjs file:

import { defineConfig } from 'astro/config';
import preact from "@astrojs/preact";
import sitemap from "@astrojs/sitemap";

// https://docs.astro.build/en/reference/configuration-reference/
// https://astro.build/config
export default defineConfig({
  site: 'https://username.github.io', // TODO change this to the real domain for the sitemap
  base: '/astro-blog-tutorial',
  trailingSlash: "never",
  integrations: [
    preact(),
    sitemap({
      i18n: {
        defaultLocale: 'en',
        locales: {
          'en': 'en',
          'de': 'de'
        },
      }
    }),
  ],
  i18n: {
    defaultLocale: 'en',
    locales: ['en', 'de'],
    routing: {
      prefixDefaultLocale: true,
      redirectToDefaultLocale: true,
    }
  }
});

Called like this:

getAbsoluteLocaleUrl('en','ecommerce-insights/learning-astro')
getAbsoluteLocaleUrl('de','ecommerce-insights/learne-astro')

I would expect:

https://username.github.io/astro-blog-tutorial/en/ecommerce-insights/learning-astro/
https://username.github.io/astro-blog-tutorial/de/ecommerce-insights/lerne-astro/

But when I run the function in a vitest src/lib/i18n/utils.test.ts, they become (missing the astro-blog-tutorial base URL part, but includes the site configuration):

https://username.github.io/en/ecommerce-insights/learning-astro
https://username.github.io/de/ecommerce-insights/lerne-astro

I read loads of the Vite & Astro documentation and also dove into the source of Astro to see how the astro.config.mjs is loaded, but could not figure it out for the vitest bootstrapping.

But I really don't understand it's respecting the site config value, but not the base value.

An example can be found in the linked StackBlitz

Relevant files are:

To see the working example in the web pages, press the 'astro-blog-tutorial' link shown in the error message in the browser window. Wait for the redirect to finish. Then, you can click the links generated by getAbsoluteLocaleUrl and shown on the page.

To see the failing test:

What's the expected result?

I would expect to respect base & site astro.config.mjs values by getAbsoluteLocaleUrl(...) when run in a vitest, or none of the config.

Link to Minimal Reproducible Example

https://stackblitz.com/edit/github-yoalk6?file=src%2Fpages%2Fde%2Fpath%2Fpost-name-1%2Findex.astro

Participation

ematipico commented 4 months ago

@ascheucher-shopify-partner I am checking the repro you shared, and I can see correctly the URL:

de PATH: https://githubyoalk6-ikdi--4321--12d46890.local-credentialless.webcontainer.io/astro-blog-tutorial/en/path/post-name-1/
github-actions[bot] commented 4 months ago

Hello @ascheucher-shopify-partner. Please provide a minimal reproduction using a GitHub repository or StackBlitz. Issues marked with needs repro will be closed if they have no activity within 3 days.

ascheucher-shopify-partner commented 4 months ago

Hi @ematipico,

where did you see it correctly? On the pages it works flawless, only in the vitest it fails.

Steps to reproduce the problem: