From fa973ceef7612568af5b77d693bd2b3de11066e8 Mon Sep 17 00:00:00 2001 From: prototypa Date: Fri, 4 Nov 2022 01:34:32 +0100 Subject: [PATCH] Fix issue #55 Trailing Slash --- README.md | 1 + astro.config.mjs | 1 + src/config.mjs | 1 + src/pages/[...blog]/[...page].astro | 2 +- src/utils/permalinks.js | 5 ++++- 5 files changed, 8 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 2acef19..d3799bc 100644 --- a/README.md +++ b/README.md @@ -134,6 +134,7 @@ export const SITE = { origin: "https://example.com", basePathname: "/", // Change this if you need to deploy to Github Pages, for example + trailingSlash: false, // Generate permalinks with or without "/" at the end title: "Example - This is the homepage title of Example", description: "This is the homepage description of Example", diff --git a/astro.config.mjs b/astro.config.mjs index 5df316d..d5095d2 100644 --- a/astro.config.mjs +++ b/astro.config.mjs @@ -20,6 +20,7 @@ export default defineConfig({ // Astro uses this full URL to generate your sitemap and canonical URLs in your final build site: SITE.origin, base: SITE.basePathname, + trailingSlash: SITE.trailingSlash ? "always" : "never", output: 'static', diff --git a/src/config.mjs b/src/config.mjs index 7d9852f..242c5de 100644 --- a/src/config.mjs +++ b/src/config.mjs @@ -3,6 +3,7 @@ export const SITE = { origin: 'https://astrowind.vercel.app', basePathname: '/', + trailingSlash: false, title: 'AstroWind — Your website with Astro + Tailwind CSS', description: '🚀 AstroWind is a free and ready to start template to make your website using Astro and Tailwind CSS.', diff --git a/src/pages/[...blog]/[...page].astro b/src/pages/[...blog]/[...page].astro index ba27ce6..8781f91 100644 --- a/src/pages/[...blog]/[...page].astro +++ b/src/pages/[...blog]/[...page].astro @@ -25,7 +25,7 @@ const currentPage = page.currentPage ?? 1; const meta = { title: `Blog ${currentPage > 1 ? `— Page ${currentPage} ` : ''}— ${SITE.name}`, description: SITE.description, - canonical: getCanonical(getPermalink(page.url.current)), + canonical: getCanonical(getPermalink(BLOG?.blog?.pathname)), ogType: "blog", noindex: currentPage > 1 }; diff --git a/src/utils/permalinks.js b/src/utils/permalinks.js index 6e6c075..7e14a88 100644 --- a/src/utils/permalinks.js +++ b/src/utils/permalinks.js @@ -11,7 +11,10 @@ const trim = (str, ch) => { }; const trimSlash = (s) => trim(trim(s, '/')); -const createPath = (...params) => '/' + params.filter((el) => !!el).join('/'); +const createPath = (...params) => { + const paths = params.filter((el) => !!el).join('/'); + return '/' + paths + (SITE.trailingSlash && paths ? '/' : ''); +}; const basePathname = trimSlash(SITE.basePathname);