Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { defineMiddleware, sequence } from 'astro:middleware';

const first = defineMiddleware(async (context, next) => {
if (context.request.url.includes('/virtual')) {
return new Response('<span>Virtual!!</span>', {
status: 200,
});
}
if (context.request.url.includes('/lorem')) {
context.locals.name = 'ipsum';
} else if (context.request.url.includes('/rewrite')) {
Expand Down Expand Up @@ -41,7 +46,7 @@ const first = defineMiddleware(async (context, next) => {
headers: response.headers,
});
} else if (context.url.pathname === '/prerendered/') {
context.locals.canBeReadDuringPrerendering = "yes they can!";
context.locals.canBeReadDuringPrerendering = 'yes they can!';
} else {
if (context.url.pathname === '/') {
context.cookies.set('foo', 'bar');
Expand Down
5 changes: 5 additions & 0 deletions packages/astro/test/middleware.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ describe('Middleware in DEV mode', () => {
assert.equal(json.post, 'works');
});
});

it('should render a virtual route', async () => {
const html = await fixture.fetch('/virtual').then((res) => res.text());
assert.equal(html.includes('Virtual!!'), true);
});
});

describe('Integration hooks with no user middleware', () => {
Expand Down