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

canonicalUrl doesn't match in dev and prod #4547

Closed
1 task
kevinzunigacuellar opened this issue Aug 29, 2022 · 3 comments
Closed
1 task

canonicalUrl doesn't match in dev and prod #4547

kevinzunigacuellar opened this issue Aug 29, 2022 · 3 comments

Comments

@kevinzunigacuellar
Copy link
Member

What version of astro are you using?

1.1.1

Are you using an SSR adapter? If so, which one?

None

What package manager are you using?

npm

What operating system are you using?

Mac

Describe the Bug

When making a canonical URL:

const canonicalURL = new URL(Astro.url.pathname, Astro.site);

dev server will output a URL without a trailing slash

<link rel="canonical" href="https://example.com/blog">

while build will generate a URL with trailing slash

<link rel="canonical" href="https://example.com/blog/">

Link to Minimal Reproducible Example

https://stackblitz.com/edit/github-s2tqe2

Participation

  • I am willing to submit a pull request for this issue.
@MoustaphaDev
Copy link
Member

MoustaphaDev commented Aug 30, 2022

This is expected. In dev mode the url trailing slash handling is forgiving by default and doesn't add trailing slashes to the routes urls. However, at build time trailing slashes are added by default

Quoting the docs:

Setting build.format controls what Astro.url is set to during the build. When it is:

  • directory - The Astro.url.pathname will include a trailing slash to mimic folder behavior; ie /foo/.

@kevinzunigacuellar
Copy link
Member Author

I see I was expecting both dev and prod to match. Also, I just realized that the Astro.url already contains the pathname and the site so the canonical URL can be written as

const canonicalUrl = new URL(Astro.url).href

or even just

<link rel="canonical" href={Astro.url} />

Thank you I am not quite sure if this is an issue now 😅 but I think it would be a better DX to match outputs both in dev and prod. Feel free to close this issue.

@bluwy
Copy link
Member

bluwy commented Aug 30, 2022

I think this is expected at the meantime as showed by @MoustaphaDev. For context, this change was made in #4352, and the reason it's not made consistent in dev is to prevent a breaking change. It's certainly something we can align in the future, but for now it's something to keep in mind. Closing the issue for now.

@bluwy bluwy closed this as not planned Won't fix, can't repro, duplicate, stale Aug 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants