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: change strategy for route caching #10248

Merged
merged 2 commits into from
Feb 28, 2024
Merged

fix: change strategy for route caching #10248

merged 2 commits into from
Feb 28, 2024

Conversation

ematipico
Copy link
Member

@ematipico ematipico commented Feb 27, 2024

Changes

Closes #8241
Closes PLT-1716

This PR changes the key of our internal route caching. Previously, we were using RouteData#component, which used to cover most of the cases, although it seems too restrictive, because the same component can emit multiple paths.

This means that, given the minimum use case, the first time we hit /blog/world, we correctly render the route and cache its params, props and static paths using the /blog/[post].astro as key. When we attempt to render /fr/blog/world, we hit the cache because this path is rendered by the same component, although the static paths don't match, so we emit an error and return a 404.

With this change, we use RouteData#route as key. Given the previous use case, we cache the entry using the /blog/[post] key when rendering /blog/world. Then, when we attempt to render /fr/blog/world, RouteData#route value is /fr/blog/[post], which isn't inside the cache and we correctly resolve the route and its static paths.

Testing

I tested in a local project, and it works. The current tests should pass.

I tried to add an integration test, but I was receiving a strange error, I will investigate later.

Docs

N/A, bugfix

Copy link

changeset-bot bot commented Feb 27, 2024

🦋 Changeset detected

Latest commit: c3f1678

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 the pkg: astro Related to the core `astro` package (scope) label Feb 27, 2024
@ematipico ematipico force-pushed the fix/change-key-cache branch from a8f5242 to 83a4a0f Compare February 27, 2024 14:44
Comment on lines 105 to 106
if (this.mode === 'production' && this.cache[route.route]?.staticPaths) {
this.logger.warn(null, `Internal Warning: route cache overwritten. (${route.route})`);
Copy link
Member

@Fryuni Fryuni Feb 28, 2024

Choose a reason for hiding this comment

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

I think this will switch one conflict for another. Imagine a project with two integrations such that:

  • Integration A injects route /[...slug] pre-rendered that generates the paths /foo and /bar
  • Integration B injects route /[...slug] pre-rendered that generates the paths /baz and /qux
  • A src/pages/[...slug].astro file in the project that generates the path /home and /about

Those three are the same route, so they would conflict in the cache. Two integrations may define the same route for different components, and a single integration may define the same component for different routes.

Maybe the cache key should be the combination of both

Copy link
Member Author

Choose a reason for hiding this comment

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

Maybe the cache key should be the combination of both

I thought about it too! And thank you for the comment, it's good to see that I'm not the only one :) I'll apply the change

@ematipico
Copy link
Member Author

This c3f1678 (#10248) updates again the route caching key.

Copy link
Member

@Princesseuh Princesseuh left a comment

Choose a reason for hiding this comment

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

Seems good to me

@ematipico ematipico merged commit 8ae5d99 into main Feb 28, 2024
13 checks passed
@ematipico ematipico deleted the fix/change-key-cache branch February 28, 2024 11:59
@astrobot-houston astrobot-houston mentioned this pull request Feb 28, 2024
peng added a commit to peng/astro that referenced this pull request Mar 4, 2024
* main: (327 commits)
  [ci] format
  fix(node): listen on 0.0.0.0 if server.host is set to true (withastro#10282)
  [ci] format
  fix(dev): cosider `base` when special-casing `/_image` (withastro#10274)
  [ci] format
  update login flow to support Brave (withastro#10258)
  [ci] format
  improve link command (withastro#10257)
  Updates deprecated Node.js 16 github actions (withastro#10270)
  Fix Vitest check fail again (withastro#10266)
  [ci] format
  Adds auto completion of `astro:` events when adding or removing event listeners on `document` (withastro#10263)
  Update Vite to latest (withastro#10259)
  [ci] release (withastro#10236)
  [ci] format
  fix(i18n): localised index pages are overwritten (withastro#10250)
  fix: change strategy for route caching (withastro#10248)
  Fix TypeScript type definitions for `Code` component (withastro#10251)
  chang changeset (withastro#10253)
  Removes morph animations when setting transition:animate=none (withastro#10247)
  ...
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.

Astro v3: Recent injectRoute changes broke integration astro-i18n-aut
3 participants