From aa8605761b0758a9ca0a0963393b92c74eb39466 Mon Sep 17 00:00:00 2001 From: "Tony @ Navillus" <60468564+tony-navillus@users.noreply.github.com> Date: Mon, 28 Jun 2021 13:22:15 +0200 Subject: [PATCH] Fix collections regex (#557) * fix: :bug: Fixes bug #532 Matching for collection routes should look for exact filename matches * test: :white_check_mark: Adding test coverage to make sure collection routes are matched exactly * chore: Adding changeset --- .changeset/orange-grapes-divide.md | 5 ++++ packages/astro/src/search.ts | 2 +- packages/astro/test/astro-collection.test.js | 14 +++++++++++ .../src/pages/$individuals.astro | 24 +++++++++++++++++++ 4 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 .changeset/orange-grapes-divide.md create mode 100644 packages/astro/test/fixtures/astro-collection/src/pages/$individuals.astro diff --git a/.changeset/orange-grapes-divide.md b/.changeset/orange-grapes-divide.md new file mode 100644 index 0000000000..1f0f890f58 --- /dev/null +++ b/.changeset/orange-grapes-divide.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Updates collections to match URLs by exact template filename diff --git a/packages/astro/src/search.ts b/packages/astro/src/search.ts index 198ec73b00..ce3a57b95a 100644 --- a/packages/astro/src/search.ts +++ b/packages/astro/src/search.ts @@ -115,7 +115,7 @@ export function searchForPage(url: URL, astroConfig: AstroConfig): SearchResult function loadCollection(url: string, astroConfig: AstroConfig): { currentPage?: number; location: PageLocation } | undefined { const pages = glob('**/$*.astro', { cwd: fileURLToPath(astroConfig.pages), filesOnly: true }); for (const pageURL of pages) { - const reqURL = new RegExp('^/' + pageURL.replace(/\$([^/]+)\.astro/, '$1') + '/?(.*)'); + const reqURL = new RegExp('^/' + pageURL.replace(/\$([^/]+)\.astro/, '$1') + '(?:\/(.*)|\/?$)'); const match = url.match(reqURL); if (match) { let currentPage: number | undefined; diff --git a/packages/astro/test/astro-collection.test.js b/packages/astro/test/astro-collection.test.js index 058ebbbb65..4f685b3551 100644 --- a/packages/astro/test/astro-collection.test.js +++ b/packages/astro/test/astro-collection.test.js @@ -109,4 +109,18 @@ Collections('generates individual pages from a collection', async ({ runtime }) } }); +Collections('matches collection filename exactly', async ({ runtime }) => { + const result = await runtime.load('/individuals'); + if (result.error) throw new Error(result.error); + const $ = doc(result.contents); + + assert.ok($('#posts').length); + const urls = [ + ...$('#posts a').map(function () { + return $(this).attr('href'); + }), + ]; + assert.equal(urls, ['/post/nested/a', '/post/three', '/post/two', '/post/one']); +}); + Collections.run(); diff --git a/packages/astro/test/fixtures/astro-collection/src/pages/$individuals.astro b/packages/astro/test/fixtures/astro-collection/src/pages/$individuals.astro new file mode 100644 index 0000000000..97d5ec206f --- /dev/null +++ b/packages/astro/test/fixtures/astro-collection/src/pages/$individuals.astro @@ -0,0 +1,24 @@ +--- +const { collection } = Astro.props; + +export async function createCollection() { + return { + async data() { + let data = Astro.fetchContent('./post/**/*.md'); + data.sort((a, b) => new Date(b.date) - new Date(a.date)); + return data; + }, + + pageSize: 10 + }; +} +--- + +