Skip to content

fix(i18n): review fallback system#9119

Merged
ematipico merged 4 commits intomainfrom
fix/fallback-routing
Nov 17, 2023
Merged

fix(i18n): review fallback system#9119
ematipico merged 4 commits intomainfrom
fix/fallback-routing

Conversation

@ematipico
Copy link
Member

@ematipico ematipico commented Nov 16, 2023

Changes

This PR closes #9108

The was a big flaw in the logic of the fallback, so I went back, and restored how the routes were stored before the introduction of the fallback logic.

The new change involves the introduction of fallbackRoutes as part of the RouteData object, very very similar to how we do it for redirectRoutes.

Although, now the generation of the pages is different, and each RouteData could create more than one page (itself + fallbackRoutes), hence I create a generator function called eachRouteInRouteData, which returns all the routes we need to create.

EDIT:

In SSR, the middleware already has the logic for doing the redirect, although we need to tell it somehow. So I updated the logic of app/index.ts.

The match function now considers' fallbackRoutes' when matching a route. However, when returning a Response, we set a status code 302 because we want the fallback to apply.

Example:

  • Fallback it to en
  • Request a /it/blog/article route
  • We match the route /en/blog/article, because it contains a fallback route that has a pathname that matches /it/blog/article
  • We then return a 302 status code. The middleware catches the status code and returns a redirect to /en/blog/article

Testing

I added a new case where we check the presence of a hoisted script.

The other rests should still pass.

Docs

@changeset-bot
Copy link

changeset-bot bot commented Nov 16, 2023

🦋 Changeset detected

Latest commit: 517ffa3

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 labels Nov 16, 2023
@ematipico ematipico force-pushed the fix/fallback-routing branch from 750c04e to 72dfaf7 Compare November 16, 2023 21:27
`[${staticPaths.length} ${label}]`
)}`
);
for (const route of eachRouteInRouteData(pageData)) {
Copy link
Member Author

Choose a reason for hiding this comment

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

Now we loop over all the routes: the main ones and the fallback routes associated to it

const matchedRoute = matchRoute(staticPath, opts.manifest);
return matchedRoute === route;
});
paths.push(
Copy link
Member Author

Choose a reason for hiding this comment

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

We chain all the generated routes all together

}

pipeline.getEnvironment().logger.debug('build', `Generating: ${pathname}`);
for (const route of eachRouteInRouteData(pageData)) {
Copy link
Member Author

@ematipico ematipico Nov 16, 2023

Choose a reason for hiding this comment

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

The only relevant change is that the whole logic is inside this loop now, and instead of doing pageData.route, we pass the route from this loop

@ematipico ematipico marked this pull request as ready for review November 16, 2023 21: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.

Setting a i18n.fallback option that maps to the defaultLocale leads to client-side js been removed.

3 participants