diff --git a/.changeset/large-seahorses-reply.md b/.changeset/large-seahorses-reply.md new file mode 100644 index 000000000..9af8d8033 --- /dev/null +++ b/.changeset/large-seahorses-reply.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Call `next()` without return anything should work, with a warning diff --git a/examples/middleware/src/env.d.ts b/examples/middleware/src/env.d.ts index f2de6d45d..44f67965a 100644 --- a/examples/middleware/src/env.d.ts +++ b/examples/middleware/src/env.d.ts @@ -1,13 +1,9 @@ /// -declare global { - namespace AstroMiddleware { - interface Locals { - user: { - name: string; - surname: string; - }; - } +declare namespace App { + interface Locals { + user: { + name: string; + surname: string; + }; } } - -export {}; diff --git a/examples/middleware/src/middleware.ts b/examples/middleware/src/middleware.ts index 1c0bd855f..9871e9d01 100644 --- a/examples/middleware/src/middleware.ts +++ b/examples/middleware/src/middleware.ts @@ -63,9 +63,7 @@ const validation = defineMiddleware(async (context, next) => { return context.redirect('/admin'); } } - // we don't really care about awaiting the response in this case - next(); - return; + return next(); }); export const onRequest = sequence(validation, minifier); diff --git a/examples/middleware/src/pages/api/login.ts b/examples/middleware/src/pages/api/login.ts deleted file mode 100644 index fa3f7b59b..000000000 --- a/examples/middleware/src/pages/api/login.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { APIRoute } from 'astro'; - -export const post: APIRoute = async ({ request }) => { - const data = await request.formData(); - const username = data.get('username'); - const password = data.get('password'); - return new Response( - JSON.stringify({ - username, - password, - }), - { - headers: { - 'content-type': 'application/json', - }, - } - ); -}; diff --git a/packages/astro/src/core/middleware/callMiddleware.ts b/packages/astro/src/core/middleware/callMiddleware.ts index 5836786e6..98220bbb5 100644 --- a/packages/astro/src/core/middleware/callMiddleware.ts +++ b/packages/astro/src/core/middleware/callMiddleware.ts @@ -46,9 +46,11 @@ export async function callMiddleware( }); let nextCalled = false; + let responseFunctionPromise: Promise | undefined = undefined; const next: MiddlewareNext = async () => { nextCalled = true; - return await responseFunction(); + responseFunctionPromise = responseFunction(); + return responseFunctionPromise; }; let middlewarePromise = onRequest(apiContext, next); @@ -74,8 +76,11 @@ export async function callMiddleware( /** * Here we handle the case where `next` was called and returned nothing. */ - const responseResult = await responseFunction(); - return responseResult; + if (responseFunctionPromise) { + return responseFunctionPromise; + } else { + throw new AstroError(AstroErrorData.MiddlewareNotAResponse); + } } } else if (typeof value === 'undefined') { /** diff --git a/packages/astro/test/fixtures/middleware-dev/src/middleware.js b/packages/astro/test/fixtures/middleware-dev/src/middleware.js index 2a09552e7..89b036543 100644 --- a/packages/astro/test/fixtures/middleware-dev/src/middleware.js +++ b/packages/astro/test/fixtures/middleware-dev/src/middleware.js @@ -34,7 +34,7 @@ const third = defineMiddleware(async (context, next) => { } else if (context.request.url.includes('/does-nothing')) { return undefined; } - next(); + return next(); }); export const onRequest = sequence(first, second, third); diff --git a/packages/astro/test/fixtures/middleware-tailwind/astro.config.mjs b/packages/astro/test/fixtures/middleware-tailwind/astro.config.mjs new file mode 100644 index 000000000..e6b93533d --- /dev/null +++ b/packages/astro/test/fixtures/middleware-tailwind/astro.config.mjs @@ -0,0 +1,11 @@ +import { defineConfig } from 'astro/config'; +import tailwind from '@astrojs/tailwind'; + + +// https://astro.build/config +export default defineConfig({ + integrations: [tailwind()], + experimental: { + middleware: true, + } +}); diff --git a/packages/astro/test/fixtures/middleware-tailwind/package.json b/packages/astro/test/fixtures/middleware-tailwind/package.json new file mode 100644 index 000000000..e5714c8dd --- /dev/null +++ b/packages/astro/test/fixtures/middleware-tailwind/package.json @@ -0,0 +1,9 @@ +{ + "name": "@test/middleware-tailwind", + "version": "0.0.0", + "private": true, + "dependencies": { + "astro": "workspace:*", + "@astrojs/tailwind": "workspace:*" + } +} diff --git a/packages/astro/test/fixtures/middleware-tailwind/src/middleware.js b/packages/astro/test/fixtures/middleware-tailwind/src/middleware.js new file mode 100644 index 000000000..b51370b7f --- /dev/null +++ b/packages/astro/test/fixtures/middleware-tailwind/src/middleware.js @@ -0,0 +1,3 @@ +export const onRequest = (_, next) => { + next(); +} \ No newline at end of file diff --git a/packages/astro/test/fixtures/middleware-tailwind/src/pages/index.astro b/packages/astro/test/fixtures/middleware-tailwind/src/pages/index.astro new file mode 100644 index 000000000..038b18ac9 --- /dev/null +++ b/packages/astro/test/fixtures/middleware-tailwind/src/pages/index.astro @@ -0,0 +1,7 @@ +

Hello world

+ + \ No newline at end of file diff --git a/packages/astro/test/middleware.test.js b/packages/astro/test/middleware.test.js index 784a32c30..7a1d6843e 100644 --- a/packages/astro/test/middleware.test.js +++ b/packages/astro/test/middleware.test.js @@ -77,8 +77,6 @@ describe('Middleware in DEV mode', () => { describe('Middleware in PROD mode, SSG', () => { /** @type {import('./test-utils').Fixture} */ let fixture; - /** @type {import('./test-utils').PreviewServer} */ - let previewServer; before(async () => { fixture = await loadFixture({ @@ -200,3 +198,25 @@ describe('Middleware API in PROD mode, SSR', () => { expect($('title').html()).to.not.equal('MiddlewareNoDataReturned'); }); }); + +describe('Middleware with tailwind', () => { + /** @type {import('./test-utils').Fixture} */ + let fixture; + + before(async () => { + fixture = await loadFixture({ + root: './fixtures/middleware-tailwind/', + }); + await fixture.build(); + }); + + it('should correctly emit the tailwind CSS file', async () => { + const html = await fixture.readFile('/index.html'); + const $ = cheerio.load(html); + const bundledCSSHREF = $('link[rel=stylesheet][href^=/_astro/]').attr('href'); + const bundledCSS = (await fixture.readFile(bundledCSSHREF.replace(/^\/?/, '/'))) + .replace(/\s/g, '') + .replace('/n', ''); + expect(bundledCSS.includes('--tw-content')).to.be.true; + }); +}); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 5a2055992..8d8c3410f 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -2771,6 +2771,15 @@ importers: specifier: workspace:* version: link:../../.. + packages/astro/test/fixtures/middleware-tailwind: + dependencies: + '@astrojs/tailwind': + specifier: workspace:* + version: link:../../../../integrations/tailwind + astro: + specifier: workspace:* + version: link:../../.. + packages/astro/test/fixtures/multiple-renderers: dependencies: '@test/astro-renderer-one':