Skip to content

Commit

Permalink
fix(i18n): emit an error when the index isn't found (#9678)
Browse files Browse the repository at this point in the history
* fix(i18n): emit an error when the index isn't found

* changeset

* Update .changeset/proud-guests-bake.md

Co-authored-by: Sarah Rainsberger <[email protected]>

* rename

* Update packages/astro/src/core/errors/errors-data.ts

Co-authored-by: Florian Lefebvre <[email protected]>

---------

Co-authored-by: Sarah Rainsberger <[email protected]>
Co-authored-by: Florian Lefebvre <[email protected]>
  • Loading branch information
3 people authored Jan 12, 2024
1 parent 810f7b2 commit 091097e
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 0 deletions.
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 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
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
Empty file.

0 comments on commit 091097e

Please sign in to comment.