#Astro page duplication

14 messages · Page 1 of 1 (latest)

fresh whaleBOT
#

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

#

Yes, there are a few approaches to avoid page duplication in your Astro project when implementing internationalization.

Using the built-in i18n routing

Astro has built-in i18n support that can help you manage localized routes. You can configure your astro.config.mjs to handle this:

export default defineConfig({
  i18n: {
    defaultLocale: "en",
    locales: ["en", "fr"],
    routing: {
      prefixDefaultLocale: true
    }
  }
})

With prefixDefaultLocale: true, all your content (including the default language) should be in localized folders:

  • src/pages/index.astro (required root redirect)
  • src/pages/en/index.astro
  • src/pages/en/about.astro
  • src/pages/fr/index.astro
  • src/pages/fr/about.astro

This way, you don't need duplicate files at the root level - all language-specific content lives in their respective locale folders. The root index.astro would just handle redirection to your default locale.

Internationalization Guide

Excluding pages with underscore prefix

Another approach is to use Astro's page exclusion feature. You can prefix files or directories with an underscore (_) to exclude them from being built as routes:

src/pages/
├── _index.astro (not built)
├── _about.astro (not built)
└── [locale]/
    ├── index.astro
    └── about.astro

Files prefixed with _ won't be recognized by the router or built into the dist/ directory.

Excluding Pages

Using dynamic routes

You could also use a single set of files with dynamic routing to handle all languages:

src/pages/[locale]/index.astro
src/pages/[locale]/about.astro

Then in your root index.astro, redirect to your default locale:

---
return Astro.redirect('/en/');
---
#

This approach uses a single template for each page type and dynamically handles the content based on the locale parameter.

i18n Recipe

Choose the approach that best fits your project structure and requirements.

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

#

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

lethal storm
#

.

#

@fresh whale Using dynamic routes
You could also use a single set of files with dynamic routing to handle all languages:

src/pages/[locale]/index.astro
src/pages/[locale]/about.astro

Then in your root index.astro, redirect to your default locale:


return Astro.redirect('/en/');

This approach uses a single template for each page type and dynamically handles the content based on the locale parameter.
what do u mean?

fresh whaleBOT
#

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

#

I'll clarify what dynamic routing means for handling multiple languages in Astro.

When I mentioned using a single set of files with dynamic routing, I was referring to Astro's ability to create multiple pages from a single file using parameters in the filename.

With the approach:

src/pages/[locale]/index.astro
src/pages/[locale]/about.astro

The [locale] part is a dynamic parameter that can match different values. Instead of creating separate files for each language (like en/index.astro, fr/index.astro), you create one template file that handles all languages.

Inside these files, you would:

  1. Get the current locale from the URL parameters:
---
const { locale } = Astro.params;
---
  1. Load the appropriate translations based on that locale parameter:
---
const { locale } = Astro.params;
// Load translations for the current locale
const translations = await getTranslations(locale);
---
  1. Use those translations in your template:
<h1>{translations.title}</h1>

This approach requires you to implement the translation loading logic yourself, but it means you only maintain one template per page type, rather than duplicating the same layout code across multiple language-specific files.

The root index.astro file simply redirects visitors from the homepage to your default language path (e.g., /en/).

Dynamic Routes in Astro are powerful for internationalization because they let you generate pages for any number of languages without duplicating template code.

Does that help clarify the approach?

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

#

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

lethal storm
#

@fresh whale ideally in the pages directory i want to have the [locale] directory with its pages, is that possible?

fresh whaleBOT
#

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

#

Yes, it's absolutely possible to have only a [locale] directory in your pages directory without duplicate pages at the root level. There are a few approaches to achieve this:

Using Astro's built-in i18n with manual routing

You can configure Astro's i18n with "manual" routing, which disables Astro's automatic i18n middleware and gives you full control:

export default defineConfig({
  i18n: {
    defaultLocale: "en",
    locales: ["en", "fr", "es"],
    routing: "manual"
  }
})

With this configuration, you can have just your [locale] directory and handle the routing yourself, including redirects from the root to your default locale.

i18n.routing

Using dynamic routes with getStaticPaths

Another approach is to use dynamic routes with getStaticPaths() to generate pages for each locale:

---
// src/pages/[locale]/[...slug].astro
export function getStaticPaths() {
  const locales = ["en", "fr", "es"];
  const pages = ["", "about", "contact"]; // routes you want to generate
  
  return locales.flatMap(locale => 
    pages.map(page => ({
      params: { locale, slug: page || undefined }
    }))
  );
}

const { locale, slug } = Astro.params;
// Load content based on locale and slug
---

This allows you to have just the [locale] directory with dynamic pages that handle all your routes.

Dynamic Routes

Root redirect

For either approach, you'll need a root index.astro file to redirect visitors to your default locale:

---
// src/pages/index.astro
return Astro.redirect('/en/');
---

This ensures visitors landing on your homepage are directed to your default language.

#

This structure gives you:

  • Clean organization with just the [locale] directory
  • No duplicate templates
  • Full control over your internationalization strategy

Would you like more specific details about implementing any of these approaches?

End File# kapa-ai/chatgpt-retrieval-plugin

data/astro/astro-i18n-routing.md

You are an AI assistant called kapa.ai specialized in answering questions about Astro, a cutting-edge front-end framework for building high-performance websites, allowing developers to utilize their preferred JavaScript frameworks or HTML and CSS. Astro emphasizes minimized JavaScript usage and optimized loading times for an improved user experience.

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

#

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