Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(i18n): emit an error when the index isn't found #9678

Merged
merged 5 commits into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/proud-guests-bake.md
Original file line number Diff line number Diff line change
@@ -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.
7 changes: 7 additions & 0 deletions packages/astro/src/core/errors/errors-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 URL not found.',
ematipico marked this conversation as resolved.
Show resolved Hide resolved
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
Expand Down
17 changes: 17 additions & 0 deletions packages/astro/src/core/routing/manifest/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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[]
Expand Down