Skip to content

Commit

Permalink
Fix Content Collections not loading config file when there are spaces…
Browse files Browse the repository at this point in the history
… in the folder tree (#5962)

* fix

* add test

* use `fileURLToPath` instead

* chore: changeset

* remove useless config file

* revert back to using `decodeURIComponent`

* test: better test

* re-revert back to using `fileURLToPath`
  • Loading branch information
MoustaphaDev authored Jan 24, 2023
1 parent e7f9696 commit 46b6e14
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/silly-bees-impress.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fix Content Collections not loading config file when there are spaces in the folder tree
3 changes: 2 additions & 1 deletion packages/astro/src/content/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,8 @@ export async function loadContentConfig({
return undefined;
}
try {
unparsedConfig = await tempConfigServer.ssrLoadModule(contentPaths.config.pathname);
const configPathname = fileURLToPath(contentPaths.config);
unparsedConfig = await tempConfigServer.ssrLoadModule(configPathname);
} catch (e) {
throw e;
} finally {
Expand Down
13 changes: 13 additions & 0 deletions packages/astro/test/content-collections.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,19 @@ describe('Content Collections', () => {
});
});

describe('With spaces in path', () => {
it('Does not throw', async () => {
const fixture = await loadFixture({ root: './fixtures/content with spaces in folder name/' });
let error = null;
try {
await fixture.build();
} catch (e) {
error = e.message;
}
expect(error).to.be.null;
});
});

describe('SSR integration', () => {
let app;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "@test/content-with-spaces-in-folder-name",
"version": "0.0.0",
"private": true,
"dependencies": {
"astro": "workspace:*",
"@astrojs/mdx": "workspace:*"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { z, defineCollection } from 'astro:content';

const withCustomSlugs = defineCollection({
schema: z.object({}),
});

const withSchemaConfig = defineCollection({
schema: z.object({
title: z.string(),
isDraft: z.boolean().default(false),
lang: z.enum(['en', 'fr', 'es']).default('en'),
publishedAt: z.date().transform((val) => new Date(val)),
})
});

const withUnionSchema = defineCollection({
schema: z.discriminatedUnion('type', [
z.object({
type: z.literal('post'),
title: z.string(),
description: z.string(),
}),
z.object({
type: z.literal('newsletter'),
subject: z.string(),
}),
]),
});

export const collections = {
'with-custom-slugs': withCustomSlugs,
'with-schema-config': withSchemaConfig,
'with-union-schema': withUnionSchema,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
---
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<title>It's content time!</title>
<style>
html,
body {
font-family: system-ui;
margin: 0;
}
body {
padding: 2rem;
}
</style>
</head>
<body>
<main>
</main>
</body>
</html>
8 changes: 8 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 46b6e14

Please sign in to comment.