Skip to content

Commit

Permalink
fix(middleware): emit warning if next is called and nothing is retu…
Browse files Browse the repository at this point in the history
…rned (#7010)

* fix(middleware): emit warning if `next` is called and nothing is returned

* chore: add test case

* chore: changeset

* chore: restore code, ooops!

* chore: change logic

* change namespace
  • Loading branch information
ematipico authored May 5, 2023
1 parent 890c8b3 commit e9f0dd9
Show file tree
Hide file tree
Showing 12 changed files with 82 additions and 37 deletions.
5 changes: 5 additions & 0 deletions .changeset/large-seahorses-reply.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Call `next()` without return anything should work, with a warning
16 changes: 6 additions & 10 deletions examples/middleware/src/env.d.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
/// <reference types="astro/client" />
declare global {
namespace AstroMiddleware {
interface Locals {
user: {
name: string;
surname: string;
};
}
declare namespace App {
interface Locals {
user: {
name: string;
surname: string;
};
}
}

export {};
4 changes: 1 addition & 3 deletions examples/middleware/src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
18 changes: 0 additions & 18 deletions examples/middleware/src/pages/api/login.ts

This file was deleted.

11 changes: 8 additions & 3 deletions packages/astro/src/core/middleware/callMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,11 @@ export async function callMiddleware<R>(
});

let nextCalled = false;
let responseFunctionPromise: Promise<R> | undefined = undefined;
const next: MiddlewareNext<R> = async () => {
nextCalled = true;
return await responseFunction();
responseFunctionPromise = responseFunction();
return responseFunctionPromise;
};

let middlewarePromise = onRequest(apiContext, next);
Expand All @@ -74,8 +76,11 @@ export async function callMiddleware<R>(
/**
* 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') {
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
11 changes: 11 additions & 0 deletions packages/astro/test/fixtures/middleware-tailwind/astro.config.mjs
Original file line number Diff line number Diff line change
@@ -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,
}
});
9 changes: 9 additions & 0 deletions packages/astro/test/fixtures/middleware-tailwind/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "@test/middleware-tailwind",
"version": "0.0.0",
"private": true,
"dependencies": {
"astro": "workspace:*",
"@astrojs/tailwind": "workspace:*"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const onRequest = (_, next) => {
next();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<h1 class="text-blue-500 text-2xl font-bold">Hello world</h1>

<style>
p {
color: red;
}
</style>
24 changes: 22 additions & 2 deletions packages/astro/test/middleware.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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;
});
});
9 changes: 9 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 e9f0dd9

Please sign in to comment.