diff --git a/.changeset/hungry-dancers-hug.md b/.changeset/hungry-dancers-hug.md new file mode 100644 index 000000000..43b994ef0 --- /dev/null +++ b/.changeset/hungry-dancers-hug.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Do not throw an error for an empty collection directory. diff --git a/.changeset/tidy-kiwis-lick.md b/.changeset/tidy-kiwis-lick.md new file mode 100644 index 000000000..14a5284dd --- /dev/null +++ b/.changeset/tidy-kiwis-lick.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fixed an issue where spaces and unicode characters in project path prevented middleware from running. diff --git a/packages/astro/src/content/runtime.ts b/packages/astro/src/content/runtime.ts index 7b8654ee7..eeaa60e6c 100644 --- a/packages/astro/src/content/runtime.ts +++ b/packages/astro/src/content/runtime.ts @@ -1,5 +1,6 @@ import type { MarkdownHeading } from '@astrojs/markdown-remark'; import { ZodIssueCode, string as zodString } from 'zod'; +import type { AstroIntegration } from '../@types/astro.js'; import { AstroError, AstroErrorData } from '../core/errors/index.js'; import { prependForwardSlash } from '../core/path.js'; import { @@ -55,10 +56,7 @@ export function createGetCollection({ } else if (collection in dataCollectionToEntryMap) { type = 'data'; } else { - throw new AstroError({ - ...AstroErrorData.CollectionDoesNotExistError, - message: AstroErrorData.CollectionDoesNotExistError.message(collection), - }); + return warnOfEmptyCollection(collection); } const lazyImports = Object.values( type === 'content' @@ -392,3 +390,16 @@ type PropagatedAssetsModule = { function isPropagatedAssetsModule(module: any): module is PropagatedAssetsModule { return typeof module === 'object' && module != null && '__astroPropagation' in module; } + +function warnOfEmptyCollection(collection: string): AstroIntegration { + return { + name: 'astro-collection', + hooks: { + 'astro:server:start': ({ logger }) => { + logger.warn( + `The collection **${collection}** does not exist or is empty. Ensure a collection directory with this name exists.` + ); + }, + }, + }; +} diff --git a/packages/astro/src/core/build/plugins/plugin-middleware.ts b/packages/astro/src/core/build/plugins/plugin-middleware.ts index 47ff4b863..22d3f795b 100644 --- a/packages/astro/src/core/build/plugins/plugin-middleware.ts +++ b/packages/astro/src/core/build/plugins/plugin-middleware.ts @@ -25,7 +25,7 @@ export function vitePluginMiddleware( async resolveId(id) { if (id === MIDDLEWARE_MODULE_ID) { const middlewareId = await this.resolve( - `${opts.settings.config.srcDir.pathname}/${MIDDLEWARE_PATH_SEGMENT_NAME}` + `${decodeURI(opts.settings.config.srcDir.pathname)}${MIDDLEWARE_PATH_SEGMENT_NAME}` ); if (middlewareId) { resolvedMiddlewareId = middlewareId.id; diff --git a/packages/astro/src/core/middleware/loadMiddleware.ts b/packages/astro/src/core/middleware/loadMiddleware.ts index 9a7f3e4bc..b8528eb4b 100644 --- a/packages/astro/src/core/middleware/loadMiddleware.ts +++ b/packages/astro/src/core/middleware/loadMiddleware.ts @@ -12,7 +12,7 @@ export async function loadMiddleware( srcDir: AstroSettings['config']['srcDir'] ) { // can't use node Node.js builtins - let middlewarePath = srcDir.pathname + '/' + MIDDLEWARE_PATH_SEGMENT_NAME; + let middlewarePath = `${decodeURI(srcDir.pathname)}${MIDDLEWARE_PATH_SEGMENT_NAME}`; try { const module = await moduleLoader.import(middlewarePath); return module; diff --git a/packages/astro/test/content-collections.test.js b/packages/astro/test/content-collections.test.js index e39d7da5e..082a7c0eb 100644 --- a/packages/astro/test/content-collections.test.js +++ b/packages/astro/test/content-collections.test.js @@ -244,6 +244,22 @@ describe('Content Collections', () => { }); }); + describe('With empty collections directory', () => { + it('Handles the empty directory correclty', async () => { + const fixture = await loadFixture({ + root: './fixtures/content-collections-empty-dir/', + }); + let error; + try { + await fixture.build(); + } catch (e) { + error = e.message; + } + expect(error).to.be.undefined; + // TODO: try to render a page + }); + }); + describe('SSR integration', () => { let app; diff --git a/packages/astro/test/featuresSupport.test.js b/packages/astro/test/featuresSupport.test.js index fba8da475..c25d9d5f8 100644 --- a/packages/astro/test/featuresSupport.test.js +++ b/packages/astro/test/featuresSupport.test.js @@ -8,7 +8,7 @@ describe('Adapter', () => { it("should error if the adapter doesn't support edge middleware", async () => { try { fixture = await loadFixture({ - root: './fixtures/middleware-dev/', + root: './fixtures/middleware space/', output: 'server', build: { excludeMiddleware: true, @@ -32,7 +32,7 @@ describe('Adapter', () => { it("should error if the adapter doesn't support split build", async () => { try { fixture = await loadFixture({ - root: './fixtures/middleware-dev/', + root: './fixtures/middleware space/', output: 'server', build: { split: true, diff --git a/packages/astro/test/fixtures/content-collections-empty-dir/package.json b/packages/astro/test/fixtures/content-collections-empty-dir/package.json new file mode 100644 index 000000000..a811df071 --- /dev/null +++ b/packages/astro/test/fixtures/content-collections-empty-dir/package.json @@ -0,0 +1,9 @@ +{ + "name": "@test/content-collections-empty-dir", + "version": "0.0.0", + "private": true, + "type": "module", + "dependencies": { + "astro": "workspace:*" + } +} diff --git a/packages/astro/test/fixtures/content-collections-empty-dir/src/content/config.ts b/packages/astro/test/fixtures/content-collections-empty-dir/src/content/config.ts new file mode 100644 index 000000000..991994433 --- /dev/null +++ b/packages/astro/test/fixtures/content-collections-empty-dir/src/content/config.ts @@ -0,0 +1,11 @@ +import { z, defineCollection } from 'astro:content'; + +const blog = defineCollection({ + schema: z.object({ + title: z.string(), + }), +}); + +export const collections = { + blog, +}; diff --git a/packages/astro/test/fixtures/content-collections-empty-dir/src/pages/index.astro b/packages/astro/test/fixtures/content-collections-empty-dir/src/pages/index.astro new file mode 100644 index 000000000..4b5e0ffb8 --- /dev/null +++ b/packages/astro/test/fixtures/content-collections-empty-dir/src/pages/index.astro @@ -0,0 +1,8 @@ +--- +import { getCollection } from 'astro:content' + +const blogs = await getCollection("blogs") +// console.log(blogs) +--- + +

This should still render

diff --git a/packages/astro/test/fixtures/middleware-dev/package.json b/packages/astro/test/fixtures/middleware space/package.json similarity index 100% rename from packages/astro/test/fixtures/middleware-dev/package.json rename to packages/astro/test/fixtures/middleware space/package.json diff --git a/packages/astro/test/fixtures/middleware-dev/src/middleware.js b/packages/astro/test/fixtures/middleware space/src/middleware.js similarity index 100% rename from packages/astro/test/fixtures/middleware-dev/src/middleware.js rename to packages/astro/test/fixtures/middleware space/src/middleware.js diff --git a/packages/astro/test/fixtures/middleware-dev/src/pages/404.astro b/packages/astro/test/fixtures/middleware space/src/pages/404.astro similarity index 100% rename from packages/astro/test/fixtures/middleware-dev/src/pages/404.astro rename to packages/astro/test/fixtures/middleware space/src/pages/404.astro diff --git a/packages/astro/test/fixtures/middleware-dev/src/pages/api/endpoint.js b/packages/astro/test/fixtures/middleware space/src/pages/api/endpoint.js similarity index 100% rename from packages/astro/test/fixtures/middleware-dev/src/pages/api/endpoint.js rename to packages/astro/test/fixtures/middleware space/src/pages/api/endpoint.js diff --git a/packages/astro/test/fixtures/middleware-dev/src/pages/broken-500.astro b/packages/astro/test/fixtures/middleware space/src/pages/broken-500.astro similarity index 100% rename from packages/astro/test/fixtures/middleware-dev/src/pages/broken-500.astro rename to packages/astro/test/fixtures/middleware space/src/pages/broken-500.astro diff --git a/packages/astro/test/fixtures/middleware-dev/src/pages/broken-locals.astro b/packages/astro/test/fixtures/middleware space/src/pages/broken-locals.astro similarity index 100% rename from packages/astro/test/fixtures/middleware-dev/src/pages/broken-locals.astro rename to packages/astro/test/fixtures/middleware space/src/pages/broken-locals.astro diff --git a/packages/astro/test/fixtures/middleware-dev/src/pages/clone.astro b/packages/astro/test/fixtures/middleware space/src/pages/clone.astro similarity index 100% rename from packages/astro/test/fixtures/middleware-dev/src/pages/clone.astro rename to packages/astro/test/fixtures/middleware space/src/pages/clone.astro diff --git a/packages/astro/test/fixtures/middleware-dev/src/pages/does-nothing.astro b/packages/astro/test/fixtures/middleware space/src/pages/does-nothing.astro similarity index 100% rename from packages/astro/test/fixtures/middleware-dev/src/pages/does-nothing.astro rename to packages/astro/test/fixtures/middleware space/src/pages/does-nothing.astro diff --git a/packages/astro/test/fixtures/middleware-dev/src/pages/index.astro b/packages/astro/test/fixtures/middleware space/src/pages/index.astro similarity index 100% rename from packages/astro/test/fixtures/middleware-dev/src/pages/index.astro rename to packages/astro/test/fixtures/middleware space/src/pages/index.astro diff --git a/packages/astro/test/fixtures/middleware-dev/src/pages/lorem.astro b/packages/astro/test/fixtures/middleware space/src/pages/lorem.astro similarity index 100% rename from packages/astro/test/fixtures/middleware-dev/src/pages/lorem.astro rename to packages/astro/test/fixtures/middleware space/src/pages/lorem.astro diff --git a/packages/astro/test/fixtures/middleware-dev/src/pages/not-interested.astro b/packages/astro/test/fixtures/middleware space/src/pages/not-interested.astro similarity index 100% rename from packages/astro/test/fixtures/middleware-dev/src/pages/not-interested.astro rename to packages/astro/test/fixtures/middleware space/src/pages/not-interested.astro diff --git a/packages/astro/test/fixtures/middleware-dev/src/pages/redirect.astro b/packages/astro/test/fixtures/middleware space/src/pages/redirect.astro similarity index 100% rename from packages/astro/test/fixtures/middleware-dev/src/pages/redirect.astro rename to packages/astro/test/fixtures/middleware space/src/pages/redirect.astro diff --git a/packages/astro/test/fixtures/middleware-dev/src/pages/rewrite.astro b/packages/astro/test/fixtures/middleware space/src/pages/rewrite.astro similarity index 100% rename from packages/astro/test/fixtures/middleware-dev/src/pages/rewrite.astro rename to packages/astro/test/fixtures/middleware space/src/pages/rewrite.astro diff --git a/packages/astro/test/fixtures/middleware-dev/src/pages/second.astro b/packages/astro/test/fixtures/middleware space/src/pages/second.astro similarity index 100% rename from packages/astro/test/fixtures/middleware-dev/src/pages/second.astro rename to packages/astro/test/fixtures/middleware space/src/pages/second.astro diff --git a/packages/astro/test/middleware.test.js b/packages/astro/test/middleware.test.js index 9ca4841d4..81f167647 100644 --- a/packages/astro/test/middleware.test.js +++ b/packages/astro/test/middleware.test.js @@ -12,7 +12,7 @@ describe('Middleware in DEV mode', () => { before(async () => { fixture = await loadFixture({ - root: './fixtures/middleware-dev/', + root: './fixtures/middleware space/', }); devServer = await fixture.startDevServer(); }); @@ -116,7 +116,7 @@ describe('Middleware API in PROD mode, SSR', () => { before(async () => { fixture = await loadFixture({ - root: './fixtures/middleware-dev/', + root: './fixtures/middleware space/', output: 'server', adapter: testAdapter({}), }); @@ -223,7 +223,7 @@ describe('Middleware API in PROD mode, SSR', () => { it('the integration should receive the path to the middleware', async () => { fixture = await loadFixture({ - root: './fixtures/middleware-dev/', + root: './fixtures/middleware space/', output: 'server', build: { excludeMiddleware: true, @@ -275,7 +275,7 @@ describe('Middleware, split middleware option', () => { before(async () => { fixture = await loadFixture({ - root: './fixtures/middleware-dev/', + root: './fixtures/middleware space/', output: 'server', build: { excludeMiddleware: true, diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 1aebf6930..bd09af378 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -2367,6 +2367,12 @@ importers: specifier: workspace:* version: link:../../.. + packages/astro/test/fixtures/content-collections-empty-dir: + dependencies: + astro: + specifier: workspace:* + version: link:../../.. + packages/astro/test/fixtures/content-collections-empty-md-file: dependencies: astro: @@ -2847,7 +2853,7 @@ importers: specifier: workspace:* version: link:../../.. - packages/astro/test/fixtures/middleware-dev: + packages/astro/test/fixtures/middleware space: dependencies: astro: specifier: workspace:*