-
-
Notifications
You must be signed in to change notification settings - Fork 483
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
feat: add support for query params in canonical url #1274
Conversation
Hello, We're already using Trailing slashes should be covered? Thanks! |
Or even use URL but simplify a lot since URL object already contains parsed params ( But before focusing on details, we have to look at the big picture. My initial reaction was that we shouldn't introduce global option like that because we'll end up with a mess later, if we need to also introduce it per-page. And we can't just remove it later since that would be a breaking change. Though maybe global option like that would also be useful to support but then we should think of the best way to do that all together. One idea I can think of is passing those options as arguments to |
Well, Thanks! |
Hello @rwwagner90, Have you successfully implemented this feature? If not, I could try to take care. Thanks! |
@divine I have not had the time to dedicate to this. If you have time, and would like to implement it, please feel free, I would love the help! 😄 |
@divine I updated the PR. Can you please approve the workflow so the tests can run? |
Hello, I don't have permission to do that as I'm a just a contributor like you :). I have done the similar work as you but didn't have a chance to complete it as well! Can you also make canonical attibutes optional (true by default?). If you want I can publish my branch to take a look. Thanks! |
@divine it is optional and defaults to an empty array. |
@rchl can you approve the workflow please so we can see what tests do? |
Done. yarn test:e2e-ssr -t 'canonical SEO link' |
Hello, What I mean is to add an extra parameter to make canonical optional (default true): export function nuxtI18nHead ({ addCanonicalAttributes = true ... Later: function addCanonicalLinks (baseUrl, link) {
if (!addCanonicalAttributes) {
return
} And this logic might be better: let href = toAbsoluteUrl(canonicalPath, baseUrl)
if (currentRoute && addCanonicalQueries.length) {
const params = new URLSearchParams()
for (const name in addCanonicalQueries) {
const value = currentRoute.query[name]
if (Array.isArray(value)) {
value.forEach(v => params.append(name, v || ''))
} else {
params.append(name, value || '')
}
}
href = `${href}?${params.toString()}`
} What do you think @rchl? Thank! |
@divine making canonical optional entirely seems out of scope for this PR. This PR is to add the ability to specify which query params should be included in the canonical link. |
@rchl looks like it needs to be approved again, but I ran it locally and it seemed to be working. Please let me know if I need to make any further changes! |
I keep getting |
@rchl I have fixed the lint issues now, if you want to approve again. |
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.
The paragraph at the bottom of the https://i18n.nuxtjs.org/seo documentation page should also be updated as it got outdated with those changes
Co-authored-by: Rafał Chłodnicki <rchl2k@gmail.com>
Co-authored-by: Rafał Chłodnicki <rchl2k@gmail.com>
Why not? It won't take you a minute, anyways, I'll do on my own then. Thanks! |
@divine just because it is not related to this PR at all. This is to add support for adding query params to canonical urls. Once it is in, I would be glad to help with any additional options you think need to be added. |
Co-authored-by: Rafał Chłodnicki <rchl2k@gmail.com>
@rchl I tried to support setting queryString = queryString.replace('=undefined', '')
queryString = queryString.replace('=null', '') Let me know if you have any better ideas. I think checking that the value exists and is a string is maybe good, to avoid these hacks. I would assume people don't use query params with no value often? |
@rchl is there anything else I need to update here to get this PR in? |
The part where I asked to change the types of |
Also this is not addressed: #1274 (review) |
I'm also thinking about folding the new It feels a bit wrong now because I can do that change myself if you are feeling like I'm pushing too much. :) |
@rchl I see you pushed a change to fix the types, thanks! I could not figure it out 😬
Could you elaborate on what is needed here please? |
It says:
So it's obviously outdated now. Needs to be changed to mention how to use the new functionality to enable that. |
@rchl I updated those docs. We still need to address this issue #1274 (comment) |
Query strings which behavior is kinda weird and my suggestion would be to use something like https://github.com/sindresorhus/query-string which covers all of that problems. That means depending on another library as well... but at least it will make things stable. Forgot to add, that this should be configurable |
I feel we should just use the code as I wrote it, which strips null and undefined, but I don't feel super strongly. Technically, query params are always undefined if you don't define them, so I consider that an invalid edge case. |
@rchl @divine you seem to have strong opinions about including |
Sorry for the delay. |
@rwwagner90 please have a look and let me know if you are fine with my changes. |
@rchl looks good to me! 🚢 |
Thanks for merging @rchl! When do you think a new release will be out? |
This is a first attempt at adding support for including certain query params in the canonical url. Currently, it uses global options, but I am open to making it work on a page by page basis, I just wasn't sure how to hook that up.
Related to #912