Fix Content Collections not loading config file when there are spaces 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`
This commit is contained in:
Happydev 2023-01-24 21:00:17 +01:00 committed by GitHub
parent e7f9696cea
commit 46b6e14265
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 95 additions and 1 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Fix Content Collections not loading config file when there are spaces in the folder tree

View file

@ -227,7 +227,8 @@ export async function loadContentConfig({
return undefined; return undefined;
} }
try { try {
unparsedConfig = await tempConfigServer.ssrLoadModule(contentPaths.config.pathname); const configPathname = fileURLToPath(contentPaths.config);
unparsedConfig = await tempConfigServer.ssrLoadModule(configPathname);
} catch (e) { } catch (e) {
throw e; throw e;
} finally { } finally {

View file

@ -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', () => { describe('SSR integration', () => {
let app; let app;

View file

@ -0,0 +1,9 @@
{
"name": "@test/content-with-spaces-in-folder-name",
"version": "0.0.0",
"private": true,
"dependencies": {
"astro": "workspace:*",
"@astrojs/mdx": "workspace:*"
}
}

View file

@ -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,
}

View file

@ -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>

View file

@ -1647,6 +1647,14 @@ importers:
'@astrojs/mdx': link:../../../../integrations/mdx '@astrojs/mdx': link:../../../../integrations/mdx
astro: link:../../.. astro: link:../../..
packages/astro/test/fixtures/content with spaces in folder name:
specifiers:
'@astrojs/mdx': workspace:*
astro: workspace:*
dependencies:
'@astrojs/mdx': link:../../../../integrations/mdx
astro: link:../../..
packages/astro/test/fixtures/content-collections: packages/astro/test/fixtures/content-collections:
specifiers: specifiers:
'@astrojs/mdx': workspace:* '@astrojs/mdx': workspace:*