diff options
author | 2022-07-21 10:45:59 -0700 | |
---|---|---|
committer | 2022-07-21 13:45:59 -0400 | |
commit | ddefb172f66bcd646dd0b75a7ea8360157dd2ba0 (patch) | |
tree | a43eb2a6a7cadf867e3f333e09ebac51974b1438 /examples | |
parent | d503c5bf3db39afeecb28522861ac5d78c919408 (diff) | |
download | astro-ddefb172f66bcd646dd0b75a7ea8360157dd2ba0.tar.gz astro-ddefb172f66bcd646dd0b75a7ea8360157dd2ba0.tar.zst astro-ddefb172f66bcd646dd0b75a7ea8360157dd2ba0.zip |
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 <matthew@skypack.dev>
Diffstat (limited to 'examples')
-rw-r--r-- | examples/blog-multiple-authors/astro.config.mjs | 1 | ||||
-rw-r--r-- | examples/blog-multiple-authors/src/layouts/post.astro | 3 | ||||
-rw-r--r-- | examples/blog-multiple-authors/src/pages/about.astro | 2 | ||||
-rw-r--r-- | examples/blog-multiple-authors/src/pages/authors/[author].astro | 3 | ||||
-rw-r--r-- | examples/blog-multiple-authors/src/pages/index.astro | 2 | ||||
-rw-r--r-- | examples/blog-multiple-authors/src/pages/posts/[...page].astro | 2 | ||||
-rw-r--r-- | examples/blog/astro.config.mjs | 1 | ||||
-rw-r--r-- | examples/blog/src/components/BaseHead.astro | 5 | ||||
-rw-r--r-- | examples/docs/astro.config.mjs | 1 | ||||
-rw-r--r-- | examples/docs/src/components/HeadSEO.astro | 4 | ||||
-rw-r--r-- | examples/docs/src/layouts/MainLayout.astro | 5 |
11 files changed, 18 insertions, 11 deletions
diff --git a/examples/blog-multiple-authors/astro.config.mjs b/examples/blog-multiple-authors/astro.config.mjs index 5a51e487a..2b6a0fe14 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 b940bbb40..8aaa5ba5e 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); --- <html lang={content.lang || "en"}> diff --git a/examples/blog-multiple-authors/src/pages/about.astro b/examples/blog-multiple-authors/src/pages/about.astro index 2d2c9665b..0edb1446e 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); --- <html lang="en"> diff --git a/examples/blog-multiple-authors/src/pages/authors/[author].astro b/examples/blog-multiple-authors/src/pages/authors/[author].astro index cc83c6bbb..368fdd591 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()} /> <style lang="scss"> diff --git a/examples/blog-multiple-authors/src/pages/index.astro b/examples/blog-multiple-authors/src/pages/index.astro index 91168ed43..8e47b8f15 100644 --- a/examples/blog-multiple-authors/src/pages/index.astro +++ b/examples/blog-multiple-authors/src/pages/index.astro @@ -12,7 +12,7 @@ import authorData from "../data/authors.json"; // All variables are available to use in the HTML template below. let title = "Don’s Blog"; let description = "An example blog on Astro"; -let canonicalURL = Astro.canonicalURL; +const canonicalURL = new URL(Astro.url.pathname, Astro.site); // Data Fetching: List all Markdown posts in the repo. let allPosts = await Astro.glob("./post/*.md"); diff --git a/examples/blog-multiple-authors/src/pages/posts/[...page].astro b/examples/blog-multiple-authors/src/pages/posts/[...page].astro index 8a6c9a730..3c6488855 100644 --- a/examples/blog-multiple-authors/src/pages/posts/[...page].astro +++ b/examples/blog-multiple-authors/src/pages/posts/[...page].astro @@ -32,7 +32,7 @@ export async function getStaticPaths({ paginate, rss }) { // page const title = "Don’s Blog"; const description = "An example blog on Astro"; -const { canonicalURL } = Astro; +const canonicalURL = new URL(Astro.url.pathname, Astro.site); const { page } = Astro.props; --- diff --git a/examples/blog/astro.config.mjs b/examples/blog/astro.config.mjs index 08916b1fe..39670fcbd 100644 --- a/examples/blog/astro.config.mjs +++ b/examples/blog/astro.config.mjs @@ -4,4 +4,5 @@ import preact from '@astrojs/preact'; // https://astro.build/config export default defineConfig({ integrations: [preact()], + site: `http://astro.build` }); diff --git a/examples/blog/src/components/BaseHead.astro b/examples/blog/src/components/BaseHead.astro index f7395b4e3..cc6f89ff1 100644 --- a/examples/blog/src/components/BaseHead.astro +++ b/examples/blog/src/components/BaseHead.astro @@ -6,6 +6,7 @@ export interface Props { description: string; } +const canonicalURL = new URL(Astro.url.pathname, Astro.site); const { title, description } = Astro.props; --- @@ -21,14 +22,14 @@ const { title, description } = Astro.props; <!-- Open Graph / Facebook --> <meta property="og:type" content="website" /> -<meta property="og:url" content={Astro.canonicalURL} /> +<meta property="og:url" content={canonicalURL} /> <meta property="og:title" content={title} /> <meta property="og:description" content={description} /> <meta property="og:image" content="https://astro.build/social.png" /> <!-- Twitter --> <meta property="twitter:card" content="summary_large_image" /> -<meta property="twitter:url" content={Astro.canonicalURL} /> +<meta property="twitter:url" content={canonicalURL} /> <meta property="twitter:title" content={title} /> <meta property="twitter:description" content={description} /> <meta property="twitter:image" content="https://astro.build/social.png" /> diff --git a/examples/docs/astro.config.mjs b/examples/docs/astro.config.mjs index 7ae8d6f7b..da05d8c36 100644 --- a/examples/docs/astro.config.mjs +++ b/examples/docs/astro.config.mjs @@ -10,4 +10,5 @@ export default defineConfig({ // Enable React for the Algolia search component. react(), ], + site: `http://astro.build` }); diff --git a/examples/docs/src/components/HeadSEO.astro b/examples/docs/src/components/HeadSEO.astro index 5d1a2bcb5..43a19c52b 100644 --- a/examples/docs/src/components/HeadSEO.astro +++ b/examples/docs/src/components/HeadSEO.astro @@ -5,7 +5,9 @@ export interface Props { site: any; canonicalURL: URL | string; } -const { content = {}, canonicalURL } = Astro.props; + +const canonicalURL = new URL(Astro.url.pathname, Astro.site); +const { content = {} } = Astro.props; const formattedContentTitle = content.title ? `${content.title} 🚀 ${SITE.title}` : SITE.title; const imageSrc = content?.image?.src ?? OPEN_GRAPH.image.src; const canonicalImageSrc = new URL(imageSrc, Astro.site); diff --git a/examples/docs/src/layouts/MainLayout.astro b/examples/docs/src/layouts/MainLayout.astro index e9812acdd..93ba323e4 100644 --- a/examples/docs/src/layouts/MainLayout.astro +++ b/examples/docs/src/layouts/MainLayout.astro @@ -9,7 +9,8 @@ import RightSidebar from "../components/RightSidebar/RightSidebar.astro"; import * as CONFIG from "../config"; const { content = {} } = Astro.props; -const currentPage = new URL(Astro.request.url).pathname; +const canonicalURL = new URL(Astro.url.pathname, Astro.site); +const currentPage = Astro.url.pathname; const currentFile = `src/pages${currentPage.replace(/\/$/, "")}.md`; const githubEditUrl = CONFIG.GITHUB_EDIT_URL && CONFIG.GITHUB_EDIT_URL + currentFile; --- @@ -17,7 +18,7 @@ const githubEditUrl = CONFIG.GITHUB_EDIT_URL && CONFIG.GITHUB_EDIT_URL + current <html dir={content.dir ?? "ltr"} lang={content.lang ?? "en-us"} class="initial"> <head> <HeadCommon /> - <HeadSEO {content} canonicalURL={Astro.canonicalURL} /> + <HeadSEO {content} canonicalURL={canonicalURL} /> <title>{content.title ? `${content.title} 🚀 ${CONFIG.SITE.title}` : CONFIG.SITE.title}</title> <style> body { |