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): fallback index when routing is prefix-always #9032

Merged
merged 2 commits into from
Nov 9, 2023

Conversation

ematipico
Copy link
Member

Changes

This PR fixes a case where the routingStrategy is prefix-always and the src/pages/index.astro isn't present.

The routing strategy is pretty clear about what it does (now):

example.com/ will redirect to `example.com/en

This works fine if src/pages/index.astro is present but it won't if the file isn't there.

This PR fixes the issue in dev and SSG (SSR is already handled):

  • dev was a pretty easy fix
  • SSG leverages the same fallback logic to create a fake "fallback" route, so we can emit a index.html file that redirects to /en

Testing

I deleted the src/pages/index.astro from the fixture of prefix-always and the assertions should still be valid.

Docs

Copy link

changeset-bot bot commented Nov 9, 2023

⚠️ No Changeset found

Latest commit: dcacacf

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 the pkg: astro Related to the core `astro` package (scope) label Nov 9, 2023
Comment on lines +529 to +559
if (i18n.routingStrategy === 'prefix-always') {
// we attempt to retrieve the index page of the default locale
const defaultLocaleRoutes = routesByLocale.get(i18n.defaultLocale);
if (defaultLocaleRoutes) {
const indexDefaultRoute = defaultLocaleRoutes.find((routeData) => {
// it should be safe to assume that an index page has "index" in their name
return routeData.component.includes('index');
});
if (indexDefaultRoute) {
// we found the index of the default locale, now we create a root index that will redirect to the index of the default locale
const pathname = '/';
const route = '/';

const segments = removeLeadingForwardSlash(route)
.split(path.posix.sep)
.filter(Boolean)
.map((s: string) => {
validateSegment(s);
return getParts(s, route);
});
routes.push({
...indexDefaultRoute,
pathname,
route,
segments,
pattern: getPattern(segments, config),
type: 'fallback',
});
}
}
}
Copy link
Member Author

@ematipico ematipico Nov 9, 2023

Choose a reason for hiding this comment

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

This is the additional code. The rest was just moved to be like this:

if (i18n) {
	// new block
	if (i18n.routingStrategy === 'prefix-always') {}

	if (i18n.falklback) {}
}

@ematipico ematipico merged commit 4e9f171 into main Nov 9, 2023
13 checks passed
@ematipico ematipico deleted the fix/allow-bypass-root-url branch November 9, 2023 14:32
natemoo-re pushed a commit that referenced this pull request Nov 22, 2023
* fix(i18n): fallback index when routing is prefix-always

* chore: add comment as per feedback
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)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants