-
-
Notifications
You must be signed in to change notification settings - Fork 830
Add testing to the "don’t generate invalid <link rel="canonical"/> with missing href" solution #3501
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
base: stack/invalid-canonical
Are you sure you want to change the base?
Add testing to the "don’t generate invalid <link rel="canonical"/> with missing href" solution #3501
Conversation
Signed-off-by: Anders Kaseorg <[email protected]>
…s to add an option of not setting site
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
urlis required, and that url actually sets<link rel="canonical"/>to it, from what I logged in the console.
Ah, that could suggest the logic in utils/head.ts needs further changes potentially! The type there is accurate because it reflects the value Astro provides, and it always provides a value for url even when site is not set:
When building a site, prerendered routes will receive a URL based on the
siteandbaseoptions. Ifsiteis not configured, prerendered pages will receive alocalhostURL during builds as well. (Source: https://docs.astro.build/en/reference/api-reference/#url)
So it’s correct that the getRouteDataTestContext() always returns url, and if that means we don’t get the omission we’re looking for, we’ll need to do a bit more work in the head logic I guess. So writing a failing test for the canonical is a good start.
The changes to getRouteDataTestContext() look good 👍 I think we might eventually want to refactor it to use an options object to avoid the slightly awkward cases like getRouteDataTestContext(undefined, false) which would be clearer as getRouteDataTestContext({ setSite: false }). But we can do that in a follow-up PR where we’re not changing any other logic or tests.
P.S. I just updated this branch so it has everything from the target branch — you might want to make sure you pull before making your next changes.
…s to add an option of not setting site
…erc/starlight into add-tests-invalid-canonical
|
Ok, I had some git confusion there. Added the failing test! |
|
Btw I just wanted to add that I tested this manually by packing my local astro version (with the canonical changes) and comparing it to the current release, the invalid canonical and og:url did, in fact, disappear |
|
@delucis Could you please take a look at vitest.config.ts for head tests? I think this is the reason why the test fails. We keep getting in the head, no matter the test setup, because config has it by default. I noticed that because |
Ah yes, that makes sense! We were already testing that canonicals get deduplicated, so we set that in the test config to make sure the user version is used. I guess in this case we can instead add the new test to our “Basics” test folder. Those tests run with a minimal config, so should work in this case, I think? Thanks for figuring that out! |
Description
Continuation of #3420 and #3496
I have modified the
getRouteDataTestContext()function to make sure I can get test context without site set, same withgetTestHead(). The new parameter is optional, so it does not break any other usage of the functions.I also wrote a test checking that
<meta property="og:url"/>with missing content is not generated. The test passes.I wanted to write one more test checking
<link rel="canonical"/>in the same way (if site is not set, this element should not appear). However,getRouteDataTestContext()has a return type ofor, more specifically:
urlis required, and that url actually sets<link rel="canonical"/>to it, from what I logged in the console.I would kindly ask you to check if my test logic is alright, as I have not done a lot of testing before and would appreciate any help with figuring out what to do to be able to write the next test. I don't really want to mess with existing types and I don't know the codebase well enough yet to come up with something new.