fix(middleware): emit warning if next
is called and nothing is returned (#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
This commit is contained in:
parent
890c8b3ea7
commit
e9f0dd9b47
12 changed files with 82 additions and 37 deletions
5
.changeset/large-seahorses-reply.md
Normal file
5
.changeset/large-seahorses-reply.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
'astro': patch
|
||||
---
|
||||
|
||||
Call `next()` without return anything should work, with a warning
|
16
examples/middleware/src/env.d.ts
vendored
16
examples/middleware/src/env.d.ts
vendored
|
@ -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 {};
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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',
|
||||
},
|
||||
}
|
||||
);
|
||||
};
|
|
@ -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);
|
||||
|
@ -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') {
|
||||
/**
|
||||
|
|
|
@ -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
packages/astro/test/fixtures/middleware-tailwind/astro.config.mjs
vendored
Normal file
11
packages/astro/test/fixtures/middleware-tailwind/astro.config.mjs
vendored
Normal file
|
@ -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
packages/astro/test/fixtures/middleware-tailwind/package.json
vendored
Normal file
9
packages/astro/test/fixtures/middleware-tailwind/package.json
vendored
Normal file
|
@ -0,0 +1,9 @@
|
|||
{
|
||||
"name": "@test/middleware-tailwind",
|
||||
"version": "0.0.0",
|
||||
"private": true,
|
||||
"dependencies": {
|
||||
"astro": "workspace:*",
|
||||
"@astrojs/tailwind": "workspace:*"
|
||||
}
|
||||
}
|
3
packages/astro/test/fixtures/middleware-tailwind/src/middleware.js
vendored
Normal file
3
packages/astro/test/fixtures/middleware-tailwind/src/middleware.js
vendored
Normal file
|
@ -0,0 +1,3 @@
|
|||
export const onRequest = (_, next) => {
|
||||
next();
|
||||
}
|
7
packages/astro/test/fixtures/middleware-tailwind/src/pages/index.astro
vendored
Normal file
7
packages/astro/test/fixtures/middleware-tailwind/src/pages/index.astro
vendored
Normal file
|
@ -0,0 +1,7 @@
|
|||
<h1 class="text-blue-500 text-2xl font-bold">Hello world</h1>
|
||||
|
||||
<style>
|
||||
p {
|
||||
color: red;
|
||||
}
|
||||
</style>
|
|
@ -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;
|
||||
});
|
||||
});
|
||||
|
|
|
@ -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':
|
||||
|
|
Loading…
Reference in a new issue