From ddefb172f66bcd646dd0b75a7ea8360157dd2ba0 Mon Sep 17 00:00:00 2001 From: "Fred K. Schott" Date: Thu, 21 Jul 2022 10:45:59 -0700 Subject: [PATCH] Replace/Rename Astro.canonicalURL with new Astro.url helper (#3959) * add Astro.url * Add examples of how to create the canonicalURL Co-authored-by: Matthew Phillips --- .changeset/unlucky-panthers-yawn.md | 5 +++ .changeset/yellow-drinks-judge.md | 5 +++ .../blog-multiple-authors/astro.config.mjs | 1 + .../src/layouts/post.astro | 3 +- .../src/pages/about.astro | 2 +- .../src/pages/authors/[author].astro | 3 +- .../src/pages/index.astro | 2 +- .../src/pages/posts/[...page].astro | 2 +- examples/blog/astro.config.mjs | 1 + examples/blog/src/components/BaseHead.astro | 5 ++- examples/docs/astro.config.mjs | 1 + examples/docs/src/components/HeadSEO.astro | 4 +- examples/docs/src/layouts/MainLayout.astro | 5 ++- packages/astro/src/@types/astro.ts | 24 +++++++++--- packages/astro/src/core/config.ts | 6 +-- packages/astro/src/core/render/result.ts | 30 ++++++++++----- packages/astro/src/core/render/util.ts | 12 ------ packages/astro/src/core/request.ts | 10 ----- packages/astro/src/runtime/server/index.ts | 8 ++-- .../astro/src/vite-plugin-astro/compile.ts | 4 +- .../astro/test/astro-get-static-paths.test.js | 4 +- packages/astro/test/astro-global.test.js | 38 +------------------ packages/astro/test/custom-404.test.js | 3 +- .../src/pages/posts/[page].astro | 3 +- .../astro-global/src/components/Child.astro | 2 +- .../src/components/NestedChild.astro | 2 +- .../astro-global/src/layouts/post.astro | 3 +- .../astro-global/src/pages/index.astro | 7 ++-- .../astro-global/src/pages/posts/[page].astro | 4 +- .../astro-pagination/src/pages/index.astro | 5 ++- .../src/pages/posts/[slug]/[page].astro | 3 +- .../pages/posts/named-root-page/[page].astro | 3 +- .../posts/optional-root-page/[...page].astro | 3 +- .../fixtures/custom-404/src/pages/404.astro | 3 +- .../ssr-request/src/pages/request.astro | 2 +- 35 files changed, 101 insertions(+), 117 deletions(-) create mode 100644 .changeset/unlucky-panthers-yawn.md create mode 100644 .changeset/yellow-drinks-judge.md diff --git a/.changeset/unlucky-panthers-yawn.md b/.changeset/unlucky-panthers-yawn.md new file mode 100644 index 000000000000..b8e325b15990 --- /dev/null +++ b/.changeset/unlucky-panthers-yawn.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Deprecate Astro.canonicalURL, in favor of Astro.url instead. diff --git a/.changeset/yellow-drinks-judge.md b/.changeset/yellow-drinks-judge.md new file mode 100644 index 000000000000..fdd65d6b93a2 --- /dev/null +++ b/.changeset/yellow-drinks-judge.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Add Astro.url helper for getting the request URL diff --git a/examples/blog-multiple-authors/astro.config.mjs b/examples/blog-multiple-authors/astro.config.mjs index 5a51e487a713..2b6a0fe14062 100644 --- a/examples/blog-multiple-authors/astro.config.mjs +++ b/examples/blog-multiple-authors/astro.config.mjs @@ -5,4 +5,5 @@ import preact from '@astrojs/preact'; export default defineConfig({ // Enable the Preact integration to support Preact JSX components. integrations: [preact()], + site: `http://astro.build` }); diff --git a/examples/blog-multiple-authors/src/layouts/post.astro b/examples/blog-multiple-authors/src/layouts/post.astro index b940bbb4075b..8aaa5ba5e16e 100644 --- a/examples/blog-multiple-authors/src/layouts/post.astro +++ b/examples/blog-multiple-authors/src/layouts/post.astro @@ -1,10 +1,9 @@ --- import MainHead from "../components/MainHead.astro"; import Nav from "../components/Nav.astro"; -import authorData from "../data/authors.json"; const { content } = Astro.props; -let canonicalURL = Astro.canonicalURL; +const canonicalURL = new URL(Astro.url.pathname, Astro.site); --- diff --git a/examples/blog-multiple-authors/src/pages/about.astro b/examples/blog-multiple-authors/src/pages/about.astro index 2d2c9665baa9..0edb1446e67e 100644 --- a/examples/blog-multiple-authors/src/pages/about.astro +++ b/examples/blog-multiple-authors/src/pages/about.astro @@ -4,7 +4,7 @@ import Nav from "../components/Nav.astro"; let title = "About"; let description = "About page of an example blog on Astro"; -let canonicalURL = Astro.canonicalURL; +const canonicalURL = new URL(Astro.url.pathname, Astro.site); --- diff --git a/examples/blog-multiple-authors/src/pages/authors/[author].astro b/examples/blog-multiple-authors/src/pages/authors/[author].astro index cc83c6bbb29b..368fdd5919f2 100644 --- a/examples/blog-multiple-authors/src/pages/authors/[author].astro +++ b/examples/blog-multiple-authors/src/pages/authors/[author].astro @@ -13,6 +13,7 @@ export async function getStaticPaths() { const { allPosts } = Astro.props; const title = "Don’s Blog"; const description = "An example blog on Astro"; +const canonicalURL = new URL(Astro.url.pathname, Astro.site); /** filter posts by author, sort by date */ const posts = allPosts @@ -28,7 +29,7 @@ const author = authorData[posts[0].frontmatter.author]; {title} {description} image={posts[0].frontmatter.image} - canonicalURL={Astro.canonicalURL.toString()} + canonicalURL={canonicalURL.toString()} />