From 22152bc0c11bcefca2a545b331ebfe53cc0a0ec5 Mon Sep 17 00:00:00 2001 From: Emanuele Stoppa Date: Fri, 12 Jan 2024 14:14:26 +0000 Subject: [PATCH] fix(i18n): emit an error when the index isn't found (#9678) * fix(i18n): emit an error when the index isn't found * changeset * Update .changeset/proud-guests-bake.md Co-authored-by: Sarah Rainsberger * rename * Update packages/astro/src/core/errors/errors-data.ts Co-authored-by: Florian Lefebvre --------- Co-authored-by: Sarah Rainsberger Co-authored-by: Florian Lefebvre --- .changeset/proud-guests-bake.md | 5 +++++ packages/astro/src/core/errors/errors-data.ts | 7 +++++++ .../astro/src/core/routing/manifest/create.ts | 17 +++++++++++++++++ 3 files changed, 29 insertions(+) create mode 100644 .changeset/proud-guests-bake.md 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 22f399f9fc26..8aadd6e66c5e 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[]