Skip to content

fix(i18n): redirect the index when prefix-always is enabled#9006

Merged
ematipico merged 2 commits intofeat/i18n-routingfrom
fix/redirect-index
Nov 7, 2023
Merged

fix(i18n): redirect the index when prefix-always is enabled#9006
ematipico merged 2 commits intofeat/i18n-routingfrom
fix/redirect-index

Conversation

@ematipico
Copy link
Copy Markdown
Member

Changes

From the API bash session, it emerged that when we have the routing strategy prefix-always, we should always redirect the root index example.com to example.com/<defaultLocale>.

Not doing this will result in the index having a 404, which is a bad practice.

Relative change in the RFC: withastro/roadmap@26ed3c6

Testing

All the existing tests should pass, plus I added new ones to cover the case.

Docs

/cc @withastro/maintainers-docs Please review the change in the configuration description

@ematipico ematipico added the pr: docs A PR that includes documentation for review label Nov 6, 2023
@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Nov 6, 2023

⚠️ No Changeset found

Latest commit: 8aae148

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@github-actions github-actions bot added pkg: astro Related to the core `astro` package (scope) and removed pr: docs A PR that includes documentation for review labels Nov 6, 2023
return context.redirect(`${joinPaths(base, i18n.defaultLocale)}`);
}

// Astro can't know where the default locale is supposed to be, so it returns a 404 with no content.
Copy link
Copy Markdown
Member

@sarah11918 sarah11918 Nov 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just reminding ourselves that docs will have to be VERY LOUD about saying that your defaultLocale must be set, and even your default languages files need to be under a /[lang]/ folder.

Since there's no config option to indicate true to use this feature and it will just be enabled by default with the object, I'm just checking that the expected behaviour is:

  • if no i18n config object is found, then none of this routing strategy applies.
  • if an i18 config object is found, THEN all these rules kick in

I just want to make sure there's no way that a "normal" Astro site, not intending to use these features, could somehow 404 on the index page. 😄

Copy link
Copy Markdown
Member Author

@ematipico ematipico Nov 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, all the existing tests are passing, and there hasn't been a regression so far; this means that i18n routing rules are only triggered when opting in.

Plus, we have an extensive configuration validation suite for defaultLocale and locales.

This means that, for example, the docs website can update Astro without regressions because i18n routing is opt-in only.

Copy link
Copy Markdown
Member

@bluwy bluwy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. I know you mentioned that Chris prefers less/no redirects, perhaps would be nice to hear his thoughts on this too.

@sarah11918
Copy link
Copy Markdown
Member

sarah11918 commented Nov 6, 2023

FYI, Astro Docs redirects at index.astro, so Chris can weigh in but in this case, I believe it would be a Chris-approved strategy!

@ematipico ematipico requested a review from delucis November 6, 2023 14:01
@ematipico ematipico force-pushed the feat/i18n-routing branch 2 times, most recently from 0d09b79 to 185a1aa Compare November 6, 2023 15:22
Copy link
Copy Markdown
Member

@delucis delucis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @ematipico! Left a couple small comments and feedback re: how the routingStrategy option is named/structured.

Comment on lines +1444 to +1464
/**
* @docs
* @name experimental.i18n.routingStrategy
* @type {'prefix-always' | 'prefix-other-locales'}
* @default 'prefix-other-locales'
* @version 3.5.0
* @description
*
* Controls the routing strategy to determine your site URLs.
*
* - `prefix-other-locales`(default): Only non-default languages will display a language prefix. The `defaultLocale` will not show a language prefix.
* URLs will be of the form `example.com/[lang]/content/` for all non-default languages, but `example.com/content/` for the default locale.
* - `prefix-always`: All URLs will display a language prefix.
* URLs will be of the form `example.com/[lang]/content/` for every route, including the default language.
*
* Note: Astro requires all content to exist within a `/[lang]/` folder, even for the default language.
*/
routingStrategy: 'prefix-always' | 'prefix-other-locales';
};
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find the naming of these options not super clear. In particular “other” begs the question “other from WHAT?”

Got some context from @sarah11918 that the reason not to choose something simple like:

interface i18n {
	prefixAllLocales: boolean; // (default: false)
	// or
	prefixDefaultLocale: boolean; // (default: false)
	// or
	prefix: 'all-locales' | 'non-default-locales';
}

is because we want to expand this option later to also take control of a subdomain routing strategy (fr.example.com vs example.com/fr/).

In this case, I’d argue that on top the “other” being vague, “prefix” also doesn’t have an established meaning (is fr. a prefix in a subdomain?).

Maybe something like the following could work?

routingStrategy: 'subpath-all' | 'subpath-non-default' | 'subdomain-all' | 'subdomain-non-default';

So we have subpath vs subdomain to distinguish those and all vs non-default instead of always vs other to distinguish what subset uses the path/subdomain strategy.

A further alternative (assuming there aren’t other combinations I’m unaware of) would be:

interface i18n {
	routing: {
		prefixDefaultLocale?: boolean; // default: false
		strategy?: 'subdomain' | 'subpath'; // default: 'subpath' 
	}
}

That would avoid trying to shoehorn two separate concerns (do we prefix default locale? do we use subpath or subdomain?) into one enum string. This would be my preferred option I think.

Sorry if I’ve missed some context or discussion — this feature is so big I find it hard to get a clear picture.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @delucis , and don't worry if you missed the discussion/context.

That's excellent feedback, thank you. The reason why I used "prefix" is because it seems to be the most common term used in this context. Personally, I don't like it because it introduces a new terminology that isn't very clear in the space of "routing and internationalization".

I will have more context and information once domain support is clearer.

I would like to avoid breaking down the feature in too many flags because they are a footgun, and they become more difficult to maintain. They essentially become a matrix. In fact, base, trailingSlash and build.format are very difficult to maintain together.

I can see have at most two properties, as you suggested, but like this:

interface i18n {
	routingStrategy: {
		subUrl?: 'suburl-all' | 'suburl-non-default',
		subDomain?: 'subdomain-all' | 'subdomain-non-default'
	}
}

I used "URL" because that's what the users see, and "path" is misleading because it can be used for files too.

This could allow us to have support cases like this:

  • en.example.com/
  • en.example.com/en-au

FYI, if we agree on something, I don't plan to apply this change in this PR. I would like to apply these changes after we ship the experimental release.

What do you think?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’ll move this discussion to the RFC too 👍

@github-actions github-actions bot added the pr: docs A PR that includes documentation for review label Nov 7, 2023
@ematipico ematipico merged commit 35156e9 into feat/i18n-routing Nov 7, 2023
@ematipico ematipico deleted the fix/redirect-index branch November 7, 2023 14:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pkg: astro Related to the core `astro` package (scope) pr: docs A PR that includes documentation for review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants