Skip to content

Conversation

@ascorbic
Copy link
Contributor

@ascorbic ascorbic commented May 8, 2025

Changes

Adds support for returning a cacheHint object with entries and collections. This can be used to suggest cache tags or ttl for the data, and in future could be integrated with route caching

Testing

Docs

Added new error for invalid cache hints

@changeset-bot
Copy link

changeset-bot bot commented May 8, 2025

⚠️ No Changeset found

Latest commit: 9c8beb8

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) docs pr labels May 8, 2025
`**${String(collection)}${String(entryId)}** data does not match collection schema.`,
...error.errors.map((zodError) => zodError.message),
`**${String(collection)}${String(entryId)}** data does not match collection schema.\n`,
...error.errors.map((zodError) => ` **${zodError.path.join('.')}**: ${zodError.message}`),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

When adding the new error I noticed that these were missing the path, which could cause unhelpful errors

Copy link
Member

Choose a reason for hiding this comment

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

2 things:

  1. Why do you need String(...) when the type is already string?
  2. I think the formatting of the error should happen where the throw happens rather than inside the error data, see https://github.com/withastro/astro/blob/main/packages/astro/src/core/errors/README.md#shape

Copy link
Member

Choose a reason for hiding this comment

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

Can you provide two screenshots: one before and after the change?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is a very old error - I just added the zodError.path.join('.') bit. I followed the same pattern for the one I added. You're right the string cast isn't needed, but I think that standardising the formatting of the zod error does make sense

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Before:
image

After:
image

See the field name title has been added

Copy link
Member

Choose a reason for hiding this comment

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

Ah sorry I didn't notice it was an old one

Copy link
Member

@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.

Can you update the description of the PR and let us know what's your plan with the tests?

* **maxAge**: Expected number, received string
* @description
* The loader for a live content collection returned an invalid cache hint.
* Make sure that if you are using a cache hint, it is a valid object.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
* Make sure that if you are using a cache hint, it is a valid object.
* Make sure that your cache hint object is properly formatted.

Just two quick things jump to mind:

  • if an invalid cache hint was received, then presumably they are using a cache hint? So we can avoid that extra wording? (Or at least, safe enough? If someone reacts, "Wait, I didn't think I provided a cache hint..." then this message is also helpful to show them where to look for their error.)

  • "It's invalid. Make it valid" could be a tiny bit more helpful if instead of "make it valid", we can prompt with what's likely to make it invalid, or at the very least, not duplicate "valid" which can feel like a "duh!" kind of error message. 😅 Both tags and maxAge appear to be optional, so it's probably not e.g. that one is missing, but something like that the type or syntax was wrong?

The other alternative is to be more descriptive about the first sentence, like:

The loader for a live content collection could not determine a caching strategy (/how to cache your content). Make sure that your cache hint is a valid object.

I think either change to avoid duplication feels helpful!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I suppose what I was trying to get at was "Your hint is invalid. Fix it or remove it", because it's optional

Copy link
Contributor Author

Choose a reason for hiding this comment

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

How about Make sure that cacheHint is an object with the correct properties, or is undefined.

@ascorbic ascorbic merged commit c4fafc7 into live-loaders May 16, 2025
5 checks passed
@ascorbic ascorbic deleted the ll-cachehint branch May 16, 2025 09:44
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)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants