mirror of
https://github.com/withastro/astro.git
synced 2025-01-27 22:19:04 -05:00
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 <sarah@rainsberger.ca> * rename * Update packages/astro/src/core/errors/errors-data.ts Co-authored-by: Florian Lefebvre <contact@florian-lefebvre.dev> --------- Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca> Co-authored-by: Florian Lefebvre <contact@florian-lefebvre.dev>
This commit is contained in:
parent
810f7b23c8
commit
091097e60e
4 changed files with 29 additions and 0 deletions
5
.changeset/proud-guests-bake.md
Normal file
5
.changeset/proud-guests-bake.md
Normal file
|
@ -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.
|
|
@ -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
|
||||
|
|
|
@ -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[]
|
||||
|
|
0
packages/astro/test/fixtures/i18n-routing-prefix-always/src/pages/index.astro
vendored
Normal file
0
packages/astro/test/fixtures/i18n-routing-prefix-always/src/pages/index.astro
vendored
Normal file
Loading…
Add table
Reference in a new issue