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

i18n fallback : existing translated files are not built #10226

Closed
1 task
jsiot76 opened this issue Feb 24, 2024 · 3 comments · Fixed by #10250
Closed
1 task

i18n fallback : existing translated files are not built #10226

jsiot76 opened this issue Feb 24, 2024 · 3 comments · Fixed by #10250
Labels
- P4: important Violate documented behavior or significantly impacts performance (priority) feat: i18n Related to internalization (scope) regression

Comments

@jsiot76
Copy link

jsiot76 commented Feb 24, 2024

Astro Info

Astro                    v4.4.4
Node                     v20.10.0
System                   Linux (x64)
Package Manager          npm
Output                   static
Adapter                  none
Integrations             none

If this issue only occurs in one browser, which browser is a problem?

No response

Describe the Bug

When building a project, routing fallback is working fine for missing translations, generating redirection pages, excepted for index.astro page, redirection page is also built, even if index.astro file exists in translation folder.
All pages are correctly shown when running project in dev mode.
This appeared since 4.2.0 version.

I created a stackblitz reproductible example, base on astro fixture code: astro/packages/astro/test/fixtures/i18n-routing-fallback


├── dist/
│	└── blog
│	└── end
│	└── it
│	└── pt
|	|	└── blog
|	|	└── end
|	|	└── start
|	|	|	└── index.html : translated page
|	|	└── index.html : redirection page => it should be translated page
├── src/
│	├── pages/
│	│	└── blog
│	|	│	└── [id].astro
│	├── pt/
│	│	└── blog
│	|	|	└── [id].astro
│	|	└── index.astro : translated index page => generated as redirection page
│	|	|
│	|	└── start.astro
│	└── end.astro
│	└── index.astro
│	└── start.astro
 

What's the expected result?

index.astro page should be correctly built in [locale] folder instead of index.html redirection page.

├── dist/
│	└── blog
│	└── end
│	└── it
│	└── pt
|	|	└── blog
|	|	└── end
|	|	└── start
|	|	|	└── index.html : translated page
|	|	└── index.html : correct translated page
├── src/
│	├── pages/
│	│	└── blog
│	|	│	└── [id].astro
│	├── pt/
│	│	└── blog
│	|	|	└── [id].astro
│	|	└── index.astro
│	|	|
│	|	└── start.astro
│	└── end.astro
│	└── index.astro
│	└── start.astro

Link to Minimal Reproducible Example

https://stackblitz.com/edit/withastro-astro-mrykc9?file=src%2Fpages%2Findex.astro

Participation

  • I am willing to submit a pull request for this issue.
@github-actions github-actions bot added the needs triage Issue needs to be triaged label Feb 24, 2024
@jsiot76
Copy link
Author

jsiot76 commented Feb 24, 2024

my astro.config.mjs

import { defineConfig } from 'astro/config';

export default defineConfig({
  base: 'new-site',
  i18n: {
    defaultLocale: 'en',
    locales: ['en', 'pt', 'it'],
    fallback: {
      it: 'en',
      pt: 'en',
    },
  },
});

@log101
Copy link
Contributor

log101 commented Feb 25, 2024

My initial debugging shows that the /pt index page is actually built correctly first. But in the router explosion stage, /index.astro generates multiple /pt pages under it, and one of them returns a redirect response which in turn overwrites /pt/index.html file. I suppose the problem lies within the route explosion logic.

// Now we explode the routes. A route render itself, and it can render its fallbacks (i18n routing)
for (const route of eachRouteInRouteData(pageData)) {

I'll continue working on the problem.

@lilnasy lilnasy added - P4: important Violate documented behavior or significantly impacts performance (priority) regression and removed needs triage Issue needs to be triaged labels Feb 26, 2024
@log101
Copy link
Contributor

log101 commented Feb 26, 2024

I think I found the problem, the /pt/index.html is not overwritten once but twice after being built correctly.

First Overwrite

The eachRouteInRouteData function introduced in #9119 generates routes and their fallbacks as separate routes. So, in our case it generates three routes for the /src/pages/index.astro: [/, /it, /pt], the last two are fallback routes.

// Now we explode the routes. A route render itself, and it can render its fallbacks (i18n routing)
for (const route of eachRouteInRouteData(pageData)) {

But then getPathsForRoute also returns three routes!

if (route.pathname) {
paths.push(route.pathname);
builtPaths.add(route.pathname);
for (const virtualRoute of route.fallbackRoutes) {
if (virtualRoute.pathname) {
paths.push(virtualRoute.pathname);
builtPaths.add(virtualRoute.pathname);
}
}

As a result, all three paths are populated with /index.html content, which overwrites /pt/index.html.

Second Overwrite

Then, for loop continues to other routes generated by eachRouteInRouteData: /it and /pt. On /pt, because it is a fallback route, it overwrites it with a redirect. Which causes this issue.

My solution is to return only the original path at the getPathsForRoute function because generator function eachRouteInRouteData already handles fallback routes. And then when rendering fallback routes, we could check if there any matches in manifest routes. If there is a match we can skip rendering because it means there is already a non-fallback page for the path. But I'm not sure if this suits well with the fallback logic Astro has. My implentation had some failing tests, after fixing them I'll send the PR.

@ematipico ematipico added the feat: i18n Related to internalization (scope) label Feb 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
- P4: important Violate documented behavior or significantly impacts performance (priority) feat: i18n Related to internalization (scope) regression
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants