summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Tony @ Navillus <60468564+tony-navillus@users.noreply.github.com> 2021-06-28 13:22:15 +0200
committerGravatar GitHub <noreply@github.com> 2021-06-28 06:22:15 -0500
commitaa8605761b0758a9ca0a0963393b92c74eb39466 (patch)
tree329c85ad5273b99383fb9ae901ed5fce93193187
parent11cf22999d9eeb45d25596af481d8bc27fa931ec (diff)
downloadastro-aa8605761b0758a9ca0a0963393b92c74eb39466.tar.gz
astro-aa8605761b0758a9ca0a0963393b92c74eb39466.tar.zst
astro-aa8605761b0758a9ca0a0963393b92c74eb39466.zip
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
-rw-r--r--.changeset/orange-grapes-divide.md5
-rw-r--r--packages/astro/src/search.ts2
-rw-r--r--packages/astro/test/astro-collection.test.js14
-rw-r--r--packages/astro/test/fixtures/astro-collection/src/pages/$individuals.astro24
4 files changed, 44 insertions, 1 deletions
diff --git a/.changeset/orange-grapes-divide.md b/.changeset/orange-grapes-divide.md
new file mode 100644
index 000000000..1f0f890f5
--- /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 198ec73b0..ce3a57b95 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 058ebbbb6..4f685b355 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 000000000..97d5ec206
--- /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
+ };
+}
+---
+
+<div id="posts">
+{collection.data.map((post) => (
+ <article>
+ <h1>{post.title}</h1>
+ <a href={post.url}>Read more</a>
+ </article>
+))}
+</div> \ No newline at end of file