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

feat(i18n): expand fallback system #11677

Merged
merged 14 commits into from
Aug 28, 2024
Merged

feat(i18n): expand fallback system #11677

merged 14 commits into from
Aug 28, 2024

Conversation

ematipico
Copy link
Member

@ematipico ematipico commented Aug 12, 2024

Changes

This PR adds a new feature for i18n. A new configuration called i18n.routing.fallbackType is added. The value of this new field is "redirect" | "rewrite".

The "redirect" variant is the default value and matches the current behaviour of the fallback system. With "rewrite", we use the .rewrite() function that was stabilised two minors ago

This PR also fixes a case where RenderContent.pathname was incorrectly computed. The pathname isn't supposed to have the base in its URL. This led to some issues that were fixed in this PR.

Testing

I added new test for the rewrite.
I updated a test for the fix, which contained an incorrect behaviour.

Docs

withastro/docs#9133

Copy link

changeset-bot bot commented Aug 12, 2024

🦋 Changeset detected

Latest commit: a42d513

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

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

@github-actions github-actions bot added pkg: astro Related to the core `astro` package (scope) pr: docs A PR that includes documentation for review semver: minor Change triggers a `minor` release labels Aug 12, 2024
Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

This PR is blocked because it contains a minor changeset. A reviewer will merge this at the next release if approved.

@@ -1,3 +1,3 @@
---
return Astro.rewrite("/")
return Astro.rewrite("/base")
Copy link
Member Author

@ematipico ematipico Aug 12, 2024

Choose a reason for hiding this comment

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

This was an incorrect rewrite. The fix uncovered this, and I changed it. The fixture contains a base which means doing / should result to a 404 instead.

@ematipico ematipico marked this pull request as ready for review August 19, 2024 13:40
@sarah11918
Copy link
Member

Re: the patch changeset, is it possible that people tried to "work around" this bug, and may have to update their projects now accordingly?

e.g. Could someone have written return Astro.rewrite("/") and expect it to include the base, thinking that this is just how the feature worked, and now they need to add it in manually to preserve their existing behaviour? If so, we'd probably need to explain a little more in that changeset.

Am waiting to look at the rest of the docs too closely while I see Matthew still asking questions!

@ematipico
Copy link
Member Author

@sarah11918 I have updated the changeset regarding the bug fix.

@ematipico ematipico force-pushed the feature/fallback-rewrite branch 2 times, most recently from e0ba330 to 34df8db Compare August 21, 2024 13:25
@withastro withastro deleted a comment from github-actions bot Aug 21, 2024
Copy link
Member Author

@ematipico ematipico left a comment

Choose a reason for hiding this comment

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

!preview

.changeset/blue-pens-divide.md Outdated Show resolved Hide resolved
packages/astro/src/core/routing/rewrite.ts Outdated Show resolved Hide resolved
@ematipico ematipico requested a review from bluwy August 22, 2024 12:18
@@ -1665,6 +1665,20 @@ export interface AstroUserConfig {
* */
redirectToDefaultLocale?: boolean;

/**
* @docs
Copy link
Member

Choose a reason for hiding this comment

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

I have some questions first here @ematipico

Is this top-level i18n config? Is this entry in the correct place? Right now, it would be showing up between two i18n.routing.* entries.

Shouldn't it also be near i18n.fallback? And, this doesn't replace it, but is a separate, same-level configuration? (that's up around line 1567).

I think we should also see an example of both this and fallback configured together in one code sample, since I'm assuming they'd need to work together. Without any i18n.fallback configured, you default to showing 404s for pages that don't exist, so you can't have a strategy?

Copy link
Member Author

Choose a reason for hiding this comment

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

Is this top-level i18n config? Is this entry in the correct place? Right now, it would be showing up between two i18n.routing.* entries.

Shouldn't it also be near i18n.fallback? And, this doesn't replace it, but is a separate, same-level configuration? (that's up around line 1567).

It should be i18n.routing.fallbackType. I will update the code, because it's incorrect.

I think we should also see an example of both this and fallback configured together in one code sample, since I'm assuming they'd need to work together. Without any i18n.fallback configured, you default to showing 404s for pages that don't exist, so you can't have a strategy?

I updated the docs with an example here 2447fb9 (#11677)

Copy link
Member

@sarah11918 sarah11918 left a comment

Choose a reason for hiding this comment

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

This looks great @ematipico ! It's so exciting to see this feature continue to develop and allow people to control more and more!

I left some suggestions below for your consideration, so as always, correct what needs correcting and see what you think!

.changeset/blue-pens-divide.md Outdated Show resolved Hide resolved
.changeset/blue-pens-divide.md Outdated Show resolved Hide resolved
packages/astro/src/@types/astro.ts Show resolved Hide resolved
packages/astro/src/@types/astro.ts Outdated Show resolved Hide resolved
packages/astro/src/@types/astro.ts Outdated Show resolved Hide resolved
.changeset/lazy-feet-join.md Outdated Show resolved Hide resolved
.changeset/lazy-feet-join.md Outdated Show resolved Hide resolved
@sarah11918
Copy link
Member

Should this be added to the 4.15 milestone? I have it in mine for docs!

@ematipico ematipico added this to the 4.15 milestone Aug 23, 2024
Copy link
Member

@sarah11918 sarah11918 left a comment

Choose a reason for hiding this comment

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

This looks great @ematipico !

The content here is now good, but I think we could also update i18n.fallback itself now to make sure that there's reference to this additional configuration someone might make besides just setting that.

i18n.fallback is no longer just "the fallback strategy" because the strategy now combines these two (separated in diffrerent parts of the API) settings.

We could update the entry for i18n.fallback to be something like:

An option to configure the fallback language content when navigating to pages that do not exist (e.g. a translated page has not been created).

Use this object to declare a fallback locale route for each language you support. If no fallback is specified, then unavailable pages will return a 404.

You can also optionally configure i18n.routing.fallbackType, to select whether to execute this fallback content as a redirect (default) or a rewrite.

What do you think about something like this?

I will also note, entirely up to you to consider/ignore as you see fit:

Even though this is a routing action, having it as a configuration on i18n.routing instead of i18n.fallback feels like a clunky/awkward story for docs to tell. I think needing to cross-link these two settings, where one depends on having the other set, because these settings are in different parts of the API feels a bit strange.

If I got to wave my magic wand (and, admitting I know nothing about what's underlying here), i18n.fallback.type (or i18n.fallback.redirect as a boolean, if you're not expecting any other option here) feels like a much more natural thing for users to configure.

packages/astro/src/@types/astro.ts Outdated Show resolved Hide resolved
@ematipico
Copy link
Member Author

If I got to wave my magic wand (and, admitting I know nothing about what's underlying here), i18n.fallback.type (or i18n.fallback.redirect as a boolean, if you're not expecting any other option here) feels like a much more natural thing for users to configure.

I know, and I thought about it at the very beginning. However, i18n.fallback contains a list of locales and having a type doesn't fit in. That's why I changed it for i18n.fallbackType.

Copy link
Member

@sarah11918 sarah11918 left a comment

Choose a reason for hiding this comment

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

Just an extra space, but approving for docs! Thank you @ematipico !

.changeset/blue-pens-divide.md Outdated Show resolved Hide resolved
ematipico and others added 13 commits August 28, 2024 10:40
Co-authored-by: Bjorn Lu <bjornlu.dev@gmail.com>
Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>
Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>
Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>
Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>
@ematipico ematipico merged commit cb356a5 into main Aug 28, 2024
14 checks passed
@ematipico ematipico deleted the feature/fallback-rewrite branch August 28, 2024 11:16
@astrobot-houston astrobot-houston mentioned this pull request Aug 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs pr pkg: astro Related to the core `astro` package (scope) pr: docs A PR that includes documentation for review semver: minor Change triggers a `minor` release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants