Merge branch 'main' into bare-standalone-paths

This commit is contained in:
Rishi Raj Jain 2023-09-21 16:13:56 +05:30 committed by GitHub
commit 9288b1528a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
26 changed files with 84 additions and 13 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Do not throw an error for an empty collection directory.

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Fixed an issue where spaces and unicode characters in project path prevented middleware from running.

View file

@ -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.`
);
},
},
};
}

View file

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

View file

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

View file

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

View file

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

View file

@ -0,0 +1,9 @@
{
"name": "@test/content-collections-empty-dir",
"version": "0.0.0",
"private": true,
"type": "module",
"dependencies": {
"astro": "workspace:*"
}
}

View file

@ -0,0 +1,11 @@
import { z, defineCollection } from 'astro:content';
const blog = defineCollection({
schema: z.object({
title: z.string(),
}),
});
export const collections = {
blog,
};

View file

@ -0,0 +1,8 @@
---
import { getCollection } from 'astro:content'
const blogs = await getCollection("blogs")
// console.log(blogs)
---
<p>This should still render</p>

View file

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

View file

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