Skip to content

Commit

Permalink
Merge branch 'main' into update-to-svelte-5
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy committed Nov 13, 2024
2 parents 3cae7b2 + 3b3bc9b commit 732176f
Show file tree
Hide file tree
Showing 32 changed files with 102 additions and 58 deletions.
2 changes: 1 addition & 1 deletion examples/basics/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
"astro": "astro"
},
"dependencies": {
"astro": "^4.16.11"
"astro": "^4.16.12"
}
}
2 changes: 1 addition & 1 deletion examples/blog/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@
"@astrojs/mdx": "^3.1.9",
"@astrojs/rss": "^4.0.9",
"@astrojs/sitemap": "^3.2.1",
"astro": "^4.16.11"
"astro": "^4.16.12"
}
}
2 changes: 1 addition & 1 deletion examples/component/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
],
"scripts": {},
"devDependencies": {
"astro": "^4.16.11"
"astro": "^4.16.12"
},
"peerDependencies": {
"astro": "^4.0.0"
Expand Down
2 changes: 1 addition & 1 deletion examples/container-with-vitest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"test": "vitest run"
},
"dependencies": {
"astro": "^4.16.11",
"astro": "^4.16.12",
"@astrojs/react": "^3.6.2",
"react": "^18.3.1",
"react-dom": "^18.3.1",
Expand Down
2 changes: 1 addition & 1 deletion examples/framework-alpine/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@
"@astrojs/alpinejs": "^0.4.0",
"@types/alpinejs": "^3.13.10",
"alpinejs": "^3.14.3",
"astro": "^4.16.11"
"astro": "^4.16.12"
}
}
2 changes: 1 addition & 1 deletion examples/framework-lit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"dependencies": {
"@astrojs/lit": "^4.3.0",
"@webcomponents/template-shadowroot": "^0.2.1",
"astro": "^4.16.11",
"astro": "^4.16.12",
"lit": "^3.2.1"
}
}
2 changes: 1 addition & 1 deletion examples/framework-multiple/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"@astrojs/vue": "^4.5.2",
"@types/react": "^18.3.12",
"@types/react-dom": "^18.3.1",
"astro": "^4.16.11",
"astro": "^4.16.12",
"preact": "^10.24.3",
"react": "^18.3.1",
"react-dom": "^18.3.1",
Expand Down
2 changes: 1 addition & 1 deletion examples/framework-preact/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"dependencies": {
"@astrojs/preact": "^3.5.3",
"@preact/signals": "^1.3.0",
"astro": "^4.16.11",
"astro": "^4.16.12",
"preact": "^10.24.3"
}
}
2 changes: 1 addition & 1 deletion examples/framework-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"@astrojs/react": "^3.6.2",
"@types/react": "^18.3.12",
"@types/react-dom": "^18.3.1",
"astro": "^4.16.11",
"astro": "^4.16.12",
"react": "^18.3.1",
"react-dom": "^18.3.1"
}
Expand Down
2 changes: 1 addition & 1 deletion examples/framework-solid/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
},
"dependencies": {
"@astrojs/solid-js": "^4.4.2",
"astro": "^4.16.11",
"astro": "^4.16.12",
"solid-js": "^1.9.3"
}
}
2 changes: 1 addition & 1 deletion examples/framework-svelte/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
},
"dependencies": {
"@astrojs/svelte": "^5.7.3",
"astro": "^4.16.11",
"astro": "^4.16.12",
"svelte": "^5.1.16"
}
}
2 changes: 1 addition & 1 deletion examples/framework-vue/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
},
"dependencies": {
"@astrojs/vue": "^4.5.2",
"astro": "^4.16.11",
"astro": "^4.16.12",
"vue": "^3.5.12"
}
}
2 changes: 1 addition & 1 deletion examples/hackernews/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
},
"dependencies": {
"@astrojs/node": "^8.3.4",
"astro": "^4.16.11"
"astro": "^4.16.12"
}
}
2 changes: 1 addition & 1 deletion examples/integration/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
],
"scripts": {},
"devDependencies": {
"astro": "^4.16.11"
"astro": "^4.16.12"
},
"peerDependencies": {
"astro": "^4.0.0"
Expand Down
2 changes: 1 addition & 1 deletion examples/minimal/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
"astro": "astro"
},
"dependencies": {
"astro": "^4.16.11"
"astro": "^4.16.12"
}
}
2 changes: 1 addition & 1 deletion examples/portfolio/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
"astro": "astro"
},
"dependencies": {
"astro": "^4.16.11"
"astro": "^4.16.12"
}
}
2 changes: 1 addition & 1 deletion examples/ssr/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"dependencies": {
"@astrojs/node": "^8.3.4",
"@astrojs/svelte": "^5.7.3",
"astro": "^4.16.11",
"astro": "^4.16.12",
"svelte": "^5.1.16"
}
}
2 changes: 1 addition & 1 deletion examples/starlog/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"astro": "astro"
},
"dependencies": {
"astro": "^4.16.11",
"astro": "^4.16.12",
"sass": "^1.80.6",
"sharp": "^0.33.3"
}
Expand Down
2 changes: 1 addition & 1 deletion examples/toolbar-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@
"./app": "./dist/app.js"
},
"devDependencies": {
"astro": "^4.16.11"
"astro": "^4.16.12"
}
}
2 changes: 1 addition & 1 deletion examples/with-markdoc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
},
"dependencies": {
"@astrojs/markdoc": "^0.11.5",
"astro": "^4.16.11"
"astro": "^4.16.12"
}
}
2 changes: 1 addition & 1 deletion examples/with-mdx/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"dependencies": {
"@astrojs/mdx": "^3.1.9",
"@astrojs/preact": "^3.5.3",
"astro": "^4.16.11",
"astro": "^4.16.12",
"preact": "^10.24.3"
}
}
2 changes: 1 addition & 1 deletion examples/with-nanostores/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"dependencies": {
"@astrojs/preact": "^3.5.3",
"@nanostores/preact": "^0.5.2",
"astro": "^4.16.11",
"astro": "^4.16.12",
"nanostores": "^0.11.3",
"preact": "^10.24.3"
}
Expand Down
2 changes: 1 addition & 1 deletion examples/with-tailwindcss/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"@astrojs/mdx": "^3.1.9",
"@astrojs/tailwind": "^5.1.2",
"@types/canvas-confetti": "^1.6.4",
"astro": "^4.16.11",
"astro": "^4.16.12",
"autoprefixer": "^10.4.20",
"canvas-confetti": "^1.9.3",
"postcss": "^8.4.47",
Expand Down
2 changes: 1 addition & 1 deletion examples/with-vitest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"test": "vitest"
},
"dependencies": {
"astro": "^4.16.11",
"astro": "^4.16.12",
"vitest": "^2.1.4"
}
}
6 changes: 6 additions & 0 deletions packages/astro/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# astro

## 4.16.12

### Patch Changes

- [#12420](https://github.com/withastro/astro/pull/12420) [`acac0af`](https://github.com/withastro/astro/commit/acac0af53466f8a381ccdac29ed2ad735d7b4e79) Thanks [@ematipico](https://github.com/ematipico)! - Fixes an issue where the dev server returns a 404 status code when a user middleware returns a valid `Response`.

## 4.16.11

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/astro/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "astro",
"version": "4.16.11",
"version": "4.16.12",
"description": "Astro is a modern site builder with web best practices, performance, and DX front-of-mind.",
"type": "module",
"author": "withastro",
Expand Down
5 changes: 5 additions & 0 deletions packages/astro/src/core/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ export const REWRITE_DIRECTIVE_HEADER_KEY = 'X-Astro-Rewrite';

export const REWRITE_DIRECTIVE_HEADER_VALUE = 'yes';

/**
* This header is set by the no-op Astro middleware.
*/
export const NOOP_MIDDLEWARE_HEADER = 'X-Astro-Noop';

/**
* The name for the header used to help i18n middleware, which only needs to act on "page" and "fallback" route types.
*/
Expand Down
6 changes: 5 additions & 1 deletion packages/astro/src/core/middleware/noop-middleware.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import type { MiddlewareHandler } from '../../@types/astro.js';
import { NOOP_MIDDLEWARE_HEADER } from '../constants.js';

export const NOOP_MIDDLEWARE_FN: MiddlewareHandler = (_, next) => next();
export const NOOP_MIDDLEWARE_FN: MiddlewareHandler = (ctx, next) => {
ctx.request.headers.set(NOOP_MIDDLEWARE_HEADER, 'true');
return next();
};
19 changes: 12 additions & 7 deletions packages/astro/src/vite-plugin-astro-server/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type http from 'node:http';
import type { ComponentInstance, ManifestData, RouteData } from '../@types/astro.js';
import {
DEFAULT_404_COMPONENT,
NOOP_MIDDLEWARE_HEADER,
REROUTE_DIRECTIVE_HEADER,
REWRITE_DIRECTIVE_HEADER_KEY,
clientLocalsSymbol,
Expand Down Expand Up @@ -191,16 +192,11 @@ export async function handleRoute({

mod = preloadedComponent;

const isDefaultPrerendered404 =
matchedRoute.route.route === '/404' &&
matchedRoute.route.prerender &&
matchedRoute.route.component === DEFAULT_404_COMPONENT;

renderContext = await RenderContext.create({
locals,
pipeline,
pathname,
middleware: isDefaultPrerendered404 ? undefined : middleware,
middleware: isDefaultPrerendered404(matchedRoute.route) ? undefined : middleware,
request,
routeData: route,
});
Expand All @@ -213,10 +209,15 @@ export async function handleRoute({
response = await renderContext.render(mod);
isReroute = response.headers.has(REROUTE_DIRECTIVE_HEADER);
isRewrite = response.headers.has(REWRITE_DIRECTIVE_HEADER_KEY);
const statusCodedMatched = getStatusByMatchedRoute(matchedRoute);
statusCode = isRewrite
? // Ignore `matchedRoute` status for rewrites
response.status
: (getStatusByMatchedRoute(matchedRoute) ?? response.status);
: // Our internal noop middleware sets a particular header. If the header isn't present, it means that the user have
// their own middleware, so we need to return what the user returns.
!response.headers.has(NOOP_MIDDLEWARE_HEADER) && !isReroute
? response.status
: (statusCodedMatched ?? response.status);
} catch (err: any) {
const custom500 = getCustom500Route(manifestData);
if (!custom500) {
Expand Down Expand Up @@ -308,3 +309,7 @@ function getStatusByMatchedRoute(matchedRoute?: MatchedRoute) {
if (matchedRoute?.route.route === '/500') return 500;
return undefined;
}

function isDefaultPrerendered404(route: RouteData) {
return route.route === '/404' && route.prerender && route.component === DEFAULT_404_COMPONENT;
}
11 changes: 10 additions & 1 deletion packages/astro/test/fixtures/middleware space/src/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,13 @@ const third = defineMiddleware(async (context, next) => {
return next();
});

export const onRequest = sequence(first, second, third);
const fourth = defineMiddleware((context, next) => {
if (context.request.url.includes('/no-route-but-200')) {
return new Response("It's OK!", {
status: 200
});
}
return next()
})

export const onRequest = sequence(first, second, third, fourth);
15 changes: 15 additions & 0 deletions packages/astro/test/middleware.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ describe('Middleware in DEV mode', () => {
assert.equal($('title').html(), 'MiddlewareNoDataOrNextCalled');
});

it('should return 200 if the middleware returns a 200 Response', async () => {
const response = await fixture.fetch('/no-route-but-200');
assert.equal(response.status, 200);
const html = await response.text();
assert.match(html, /It's OK!/);
});

it('should allow setting cookies', async () => {
const res = await fixture.fetch('/');
assert.equal(res.headers.get('set-cookie'), 'foo=bar');
Expand Down Expand Up @@ -239,6 +246,14 @@ describe('Middleware API in PROD mode, SSR', () => {
assert.notEqual($('title').html(), 'MiddlewareNoDataReturned');
});

it('should return 200 if the middleware returns a 200 Response', async () => {
const request = new Request('http://example.com/no-route-but-200');
const response = await app.render(request);
assert.equal(response.status, 200);
const html = await response.text();
assert.match(html, /It's OK!/);
});

it('should correctly work for API endpoints that return a Response object', async () => {
const request = new Request('http://example.com/api/endpoint');
const response = await app.render(request);
Expand Down
Loading

0 comments on commit 732176f

Please sign in to comment.