diff --git a/.changeset/proud-guests-bake.md b/.changeset/proud-guests-bake.md new file mode 100644 index 000000000000..9788786a9469 --- /dev/null +++ b/.changeset/proud-guests-bake.md @@ -0,0 +1,5 @@ +--- +"astro": patch +--- + +Adds an error during the build phase in case `i18n.routing.prefixDefaultLocale` is set to `true` and the index page is missing. diff --git a/packages/astro/src/core/errors/errors-data.ts b/packages/astro/src/core/errors/errors-data.ts index cb9a430b5a2f..fb463a77fe1c 100644 --- a/packages/astro/src/core/errors/errors-data.ts +++ b/packages/astro/src/core/errors/errors-data.ts @@ -1001,6 +1001,13 @@ export const MissingLocale = { `The locale/path \`${locale}\` does not exist in the configured \`i18n.locales\`.`, } satisfies ErrorData; +export const MissingIndexForInternationalization = { + name: 'MissingIndexForInternationalizationError', + title: 'Index page not found.', + message: (src: string) => + `Astro couldn't find the index URL. This index page is required to create a redirect from the index URL to the index URL of the default locale. \nCreate an index page in \`${src}\``, +} satisfies ErrorData; + /** * @docs * @description diff --git a/packages/astro/src/core/routing/manifest/create.ts b/packages/astro/src/core/routing/manifest/create.ts index fcb31c1cc9b8..191e13417604 100644 --- a/packages/astro/src/core/routing/manifest/create.ts +++ b/packages/astro/src/core/routing/manifest/create.ts @@ -18,6 +18,8 @@ import { SUPPORTED_MARKDOWN_FILE_EXTENSIONS } from '../../constants.js'; import { removeLeadingForwardSlash, slash } from '../../path.js'; import { resolvePages } from '../../util.js'; import { getRouteGenerator } from './generator.js'; +import { AstroError } from '../../errors/index.js'; +import { MissingIndexForInternationalization } from '../../errors/errors-data.js'; const require = createRequire(import.meta.url); interface Item { @@ -513,6 +515,21 @@ export function createRouteManifest( }); const i18n = settings.config.i18n; if (i18n) { + // First we check if the user doesn't have an index page. + if (i18n.routing === 'prefix-always') { + let index = routes.find((route) => route.route === '/'); + if (!index) { + let relativePath = path.relative( + fileURLToPath(settings.config.root), + fileURLToPath(new URL('pages', settings.config.srcDir)) + ); + throw new AstroError({ + ...MissingIndexForInternationalization, + message: MissingIndexForInternationalization.message(relativePath), + }); + } + } + // In this block of code we group routes based on their locale // A map like: locale => RouteData[] diff --git a/packages/astro/test/fixtures/i18n-routing-prefix-always/src/pages/index.astro b/packages/astro/test/fixtures/i18n-routing-prefix-always/src/pages/index.astro new file mode 100644 index 000000000000..e69de29bb2d1