Skip to content

Commit

Permalink
Merge branch 'main' into next
Browse files Browse the repository at this point in the history
  • Loading branch information
ematipico committed Oct 22, 2024
2 parents eb41d13 + d2b399d commit 57dedbf
Show file tree
Hide file tree
Showing 40 changed files with 197 additions and 72 deletions.
2 changes: 1 addition & 1 deletion benchmark/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@
"port-authority": "^2.0.1",
"pretty-bytes": "^6.1.1",
"sharp": "^0.33.3",
"tinyexec": "^0.3.0"
"tinyexec": "^0.3.1"
}
}
2 changes: 1 addition & 1 deletion examples/framework-alpine/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"dependencies": {
"@astrojs/alpinejs": "^0.4.0",
"@types/alpinejs": "^3.13.10",
"alpinejs": "^3.14.1",
"alpinejs": "^3.14.3",
"astro": "^5.0.0-beta.5"
}
}
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 @@
},
"dependencies": {
"astro": "^5.0.0-beta.5",
"sass": "^1.79.5",
"sass": "^1.80.3",
"sharp": "^0.33.3"
}
}
2 changes: 1 addition & 1 deletion examples/with-tailwindcss/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@
"autoprefixer": "^10.4.20",
"canvas-confetti": "^1.9.3",
"postcss": "^8.4.47",
"tailwindcss": "^3.4.13"
"tailwindcss": "^3.4.14"
}
}
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,15 @@
"@changesets/cli": "^2.27.9",
"@types/node": "^18.17.8",
"esbuild": "^0.21.5",
"eslint": "^9.12.0",
"eslint": "^9.13.0",
"eslint-plugin-regexp": "^2.6.0",
"globby": "^14.0.2",
"only-allow": "^1.2.1",
"prettier": "^3.3.3",
"prettier-plugin-astro": "^0.14.1",
"turbo": "^2.1.3",
"turbo": "^2.2.3",
"typescript": "~5.6.3",
"typescript-eslint": "^8.9.0"
"typescript-eslint": "^8.11.0"
},
"pnpm": {
"overrides": {
Expand Down
16 changes: 16 additions & 0 deletions packages/astro/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -1062,6 +1062,22 @@
- Updated dependencies [[`83a2a64`](https://github.com/withastro/astro/commit/83a2a648418ad30f4eb781d1c1b5f2d8a8ac846e)]:
- @astrojs/[email protected]

## 4.16.7

### Patch Changes

- [#12263](https://github.com/withastro/astro/pull/12263) [`e9e8080`](https://github.com/withastro/astro/commit/e9e8080a8139f898dcfa3c030f5ddaa98413c160) Thanks [@Fryuni](https://github.com/Fryuni)! - Fixes conflict between server islands and on-demand dynamic routes in the form of `/[...rest]` or `/[paramA]/[paramB]`.

- [#12279](https://github.com/withastro/astro/pull/12279) [`b781f88`](https://github.com/withastro/astro/commit/b781f8860c7d11e51fb60a0d6528bc88913ffc35) Thanks [@jsparkdev](https://github.com/jsparkdev)! - Update wrong error message

- [#12273](https://github.com/withastro/astro/pull/12273) [`c2ee963`](https://github.com/withastro/astro/commit/c2ee963cb6c0a65481be505848a7272d800f2f7b) Thanks [@ascorbic](https://github.com/ascorbic)! - Fixes an issue with some package managers where sites would not build if TypeScript was not installed.

- [#12235](https://github.com/withastro/astro/pull/12235) [`a75bc5e`](https://github.com/withastro/astro/commit/a75bc5e3068ed80366a03efbec78b3b0f8837516) Thanks [@ematipico](https://github.com/ematipico)! - Fixes a bug where Astro Actions couldn't redirect to the correct pathname when there was a rewrite involved.

- [#11839](https://github.com/withastro/astro/pull/11839) [`ff522b9`](https://github.com/withastro/astro/commit/ff522b96a01391a29b44f820dfcc2a2176d871e7) Thanks [@icaliman](https://github.com/icaliman)! - Fixes error when returning a top-level `null` from an Astro file frontmatter

- [#12272](https://github.com/withastro/astro/pull/12272) [`388d237`](https://github.com/withastro/astro/commit/388d2375b6900e6401e1c711087ee0b2176418dd) Thanks [@ascorbic](https://github.com/ascorbic)! - Correctly handles local images when using a base path in SSR

## 4.16.6

### Patch Changes
Expand Down
3 changes: 1 addition & 2 deletions packages/astro/e2e/actions-blog.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,7 @@ test.describe('Astro Actions - Blog', () => {
await expect(page).toHaveURL(astro.resolveUrl('/blog/'));
});

// TODO: fix regression #12201 and #12202
test.skip('Should redirect to the origin pathname when there is a rewrite', async ({
test('Should redirect to the origin pathname when there is a rewrite', async ({
page,
astro,
}) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/astro/e2e/fixtures/error-sass/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
"private": true,
"dependencies": {
"astro": "workspace:*",
"sass": "^1.79.5"
"sass": "^1.80.3"
}
}
2 changes: 1 addition & 1 deletion packages/astro/e2e/fixtures/errors/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"preact": "^10.24.3",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"sass": "^1.79.5",
"sass": "^1.80.3",
"solid-js": "^1.9.2",
"svelte": "^4.2.19",
"vue": "^3.5.12"
Expand Down
2 changes: 1 addition & 1 deletion packages/astro/e2e/fixtures/hmr/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
"private": true,
"devDependencies": {
"astro": "workspace:*",
"sass": "^1.79.5"
"sass": "^1.80.3"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
export const prerender = false;
---

<html>
<head>
<title>Conflicting route</title>
</head>
<body>
This route would conflict with the route generated for server islands.
<br />
This file is here so the tests break if that happens.
</body>
</html>
2 changes: 1 addition & 1 deletion packages/astro/e2e/fixtures/tailwindcss/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
"astro": "workspace:*",
"autoprefixer": "^10.4.20",
"postcss": "^8.4.47",
"tailwindcss": "^3.4.13"
"tailwindcss": "^3.4.14"
}
}
8 changes: 4 additions & 4 deletions packages/astro/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@
"@oslojs/encoding": "^1.1.0",
"@rollup/pluginutils": "^5.1.2",
"@types/cookie": "^0.6.0",
"acorn": "^8.12.1",
"acorn": "^8.13.0",
"aria-query": "^5.3.2",
"axobject-query": "^4.1.0",
"boxen": "8.0.1",
Expand Down Expand Up @@ -161,7 +161,7 @@
"rehype": "^13.0.2",
"semver": "^7.6.3",
"shiki": "^1.22.0",
"tinyexec": "^0.3.0",
"tinyexec": "^0.3.1",
"tsconfck": "^3.1.4",
"unist-util-visit": "^5.0.0",
"vfile": "^6.0.3",
Expand All @@ -180,7 +180,7 @@
},
"devDependencies": {
"@astrojs/check": "^0.9.4",
"@playwright/test": "^1.48.0",
"@playwright/test": "^1.48.1",
"@types/aria-query": "^5.0.4",
"@types/common-ancestor-path": "^1.0.2",
"@types/cssesc": "^3.0.2",
Expand Down Expand Up @@ -210,7 +210,7 @@
"rehype-toc": "^3.0.2",
"remark-code-titles": "^0.1.2",
"rollup": "^4.24.0",
"sass": "^1.79.5",
"sass": "^1.80.3",
"undici": "^6.20.1",
"unified": "^11.0.5",
"vitest": "^2.1.1"
Expand Down
8 changes: 7 additions & 1 deletion packages/astro/src/actions/runtime/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { decodeBase64, encodeBase64 } from '@oslojs/encoding';
import { yellow } from 'kleur/colors';
import { defineMiddleware } from '../../core/middleware/index.js';
import { getOriginPathname } from '../../core/routing/rewrite.js';
import type { MiddlewareNext } from '../../types/public/common.js';
import type { APIContext } from '../../types/public/context.js';
import { ACTION_QUERY_PARAMS } from '../consts.js';
Expand Down Expand Up @@ -29,7 +30,7 @@ export const onRequest = defineMiddleware(async (context, next) => {
if (context.request.method === 'POST') {
console.warn(
yellow('[astro:actions]'),
'POST requests should not be sent to prerendered pages. If you\'re using Actions, disable prerendering with `export const prerender = "false".',
"POST requests should not be sent to prerendered pages. If you're using Actions, disable prerendering with `export const prerender = false`.",
);
}
return next();
Expand Down Expand Up @@ -147,6 +148,11 @@ async function redirectWithResult({
return context.redirect(referer);
}

const referer = getOriginPathname(context.request);
if (referer) {
return context.redirect(referer);
}

return context.redirect(context.url.pathname);
}

Expand Down
6 changes: 6 additions & 0 deletions packages/astro/src/assets/endpoint/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ async function loadLocalImage(src: string, url: URL) {
fileUrl = pathToFileURL(removeQueryString(replaceFileSystemReferences(src)));
} else {
try {
// If the _image segment isn't at the start of the path, we have a base
const idx = url.pathname.indexOf('/_image');
if (idx > 0) {
// Remove the base path
src = src.slice(idx);
}
fileUrl = new URL('.' + src, outDir);
const filePath = fileURLToPath(fileUrl);

Expand Down
14 changes: 11 additions & 3 deletions packages/astro/src/content/types-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { bold, cyan } from 'kleur/colors';
import { type ViteDevServer, normalizePath } from 'vite';
import { type ZodSchema, z } from 'zod';
import { zodToJsonSchema } from 'zod-to-json-schema';
import { printNode, zodToTs } from 'zod-to-ts';
import { AstroError } from '../core/errors/errors.js';
import { AstroErrorData } from '../core/errors/index.js';
import type { Logger } from '../core/logger/core.js';
Expand Down Expand Up @@ -402,8 +401,17 @@ async function typeForCollection<T extends keyof ContentConfig['collections']>(
if (collection?.type === CONTENT_LAYER_TYPE) {
const schema = await getContentLayerSchema(collection, collectionKey);
if (schema) {
const ast = zodToTs(schema);
return printNode(ast.node);
try {
const zodToTs = await import('zod-to-ts');
const ast = zodToTs.zodToTs(schema);
return zodToTs.printNode(ast.node);
} catch (err: any) {
// zod-to-ts is sad if we don't have TypeScript installed, but that's fine as we won't be needing types in that case
if (err.message.includes("Cannot find package 'typescript'")) {
return 'any';
}
throw err;
}
}
}
return 'any';
Expand Down
3 changes: 1 addition & 2 deletions packages/astro/src/core/app/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import fs from 'node:fs';
import type { IncomingMessage, ServerResponse } from 'node:http';
import { Http2ServerResponse } from 'node:http2';
import type { RouteData } from '../../types/public/internal.js';
import { clientAddressSymbol } from '../constants.js';
import { deserializeManifest } from './common.js';
import { createOutgoingHttpHeaders } from './createOutgoingHttpHeaders.js';
import { App } from './index.js';
Expand All @@ -10,8 +11,6 @@ import type { SSRManifest, SerializedSSRManifest } from './types.js';

export { apply as applyPolyfills } from '../polyfill.js';

const clientAddressSymbol = Symbol.for('astro.clientAddress');

/**
* Allow the request body to be explicitly overridden. For example, this
* is used by the Express JSON middleware.
Expand Down
6 changes: 6 additions & 0 deletions packages/astro/src/core/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export const REROUTE_DIRECTIVE_HEADER = 'X-Astro-Reroute';
* This metadata is used to determine the origin of a Response. If a rewrite has occurred, it should be prioritised over other logic.
*/
export const REWRITE_DIRECTIVE_HEADER_KEY = 'X-Astro-Rewrite';

export const REWRITE_DIRECTIVE_HEADER_VALUE = 'yes';

/**
Expand Down Expand Up @@ -68,6 +69,11 @@ export const clientAddressSymbol = Symbol.for('astro.clientAddress');
*/
export const clientLocalsSymbol = Symbol.for('astro.locals');

/**
* Use this symbol to set and retrieve the original pathname of a request. This is useful when working with redirects and rewrites
*/
export const originPathnameSymbol = Symbol.for('astro.originPathname');

/**
* The symbol used as a field on the response object to keep track of streaming.
*
Expand Down
35 changes: 4 additions & 31 deletions packages/astro/src/core/render-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { callMiddleware } from './middleware/callMiddleware.js';
import { sequence } from './middleware/index.js';
import { renderRedirect } from './redirects/render.js';
import { type Pipeline, Slots, getParams, getProps } from './render/index.js';
import { copyRequest, setOriginPathname } from './routing/rewrite.js';

export const apiContextRoutesSymbol = Symbol.for('context.routes');

Expand Down Expand Up @@ -75,6 +76,7 @@ export class RenderContext {
Pick<RenderContext, 'locals' | 'middleware' | 'status' | 'props' | 'partial'>
>): Promise<RenderContext> {
const pipelineMiddleware = await pipeline.getMiddleware();
setOriginPathname(request, pathname);
return new RenderContext(
pipeline,
locals,
Expand Down Expand Up @@ -147,7 +149,7 @@ export class RenderContext {
if (payload instanceof Request) {
this.request = payload;
} else {
this.request = this.#copyRequest(newUrl, this.request);
this.request = copyRequest(newUrl, this.request);
}
this.isRewriting = true;
this.url = new URL(this.request.url);
Expand Down Expand Up @@ -247,7 +249,7 @@ export class RenderContext {
if (reroutePayload instanceof Request) {
this.request = reroutePayload;
} else {
this.request = this.#copyRequest(newUrl, this.request);
this.request = copyRequest(newUrl, this.request);
}
this.url = new URL(this.request.url);
this.cookies = new AstroCookies(this.request);
Expand Down Expand Up @@ -555,33 +557,4 @@ export class RenderContext {
if (!i18n) return;
return (this.#preferredLocaleList ??= computePreferredLocaleList(request, i18n.locales));
}

/**
* Utility function that creates a new `Request` with a new URL from an old `Request`.
*
* @param newUrl The new `URL`
* @param oldRequest The old `Request`
*/
#copyRequest(newUrl: URL, oldRequest: Request): Request {
if (oldRequest.bodyUsed) {
throw new AstroError(AstroErrorData.RewriteWithBodyUsed);
}
return new Request(newUrl, {
method: oldRequest.method,
headers: oldRequest.headers,
body: oldRequest.body,
referrer: oldRequest.referrer,
referrerPolicy: oldRequest.referrerPolicy,
mode: oldRequest.mode,
credentials: oldRequest.credentials,
cache: oldRequest.cache,
redirect: oldRequest.redirect,
integrity: oldRequest.integrity,
signal: oldRequest.signal,
keepalive: oldRequest.keepalive,
// https://fetch.spec.whatwg.org/#dom-request-duplex
// @ts-expect-error It isn't part of the types, but undici accepts it and it allows to carry over the body to a new request
duplex: 'half',
});
}
}
43 changes: 43 additions & 0 deletions packages/astro/src/core/routing/rewrite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import type { RewritePayload } from '../../types/public/common.js';
import type { AstroConfig } from '../../types/public/config.js';
import type { RouteData } from '../../types/public/internal.js';
import { shouldAppendForwardSlash } from '../build/util.js';
import { originPathnameSymbol } from '../constants.js';
import { AstroError, AstroErrorData } from '../errors/index.js';
import { appendForwardSlash, removeTrailingForwardSlash } from '../path.js';
import { DEFAULT_404_ROUTE } from './astro-designed-error-pages.js';

Expand Down Expand Up @@ -72,3 +74,44 @@ export function findRouteToRewrite({
}
}
}

/**
* Utility function that creates a new `Request` with a new URL from an old `Request`.
*
* @param newUrl The new `URL`
* @param oldRequest The old `Request`
*/
export function copyRequest(newUrl: URL, oldRequest: Request): Request {
if (oldRequest.bodyUsed) {
throw new AstroError(AstroErrorData.RewriteWithBodyUsed);
}
return new Request(newUrl, {
method: oldRequest.method,
headers: oldRequest.headers,
body: oldRequest.body,
referrer: oldRequest.referrer,
referrerPolicy: oldRequest.referrerPolicy,
mode: oldRequest.mode,
credentials: oldRequest.credentials,
cache: oldRequest.cache,
redirect: oldRequest.redirect,
integrity: oldRequest.integrity,
signal: oldRequest.signal,
keepalive: oldRequest.keepalive,
// https://fetch.spec.whatwg.org/#dom-request-duplex
// @ts-expect-error It isn't part of the types, but undici accepts it and it allows to carry over the body to a new request
duplex: 'half',
});
}

export function setOriginPathname(request: Request, pathname: string): void {
Reflect.set(request, originPathnameSymbol, encodeURIComponent(pathname));
}

export function getOriginPathname(request: Request): string | undefined {
const origin = Reflect.get(request, originPathnameSymbol);
if (origin) {
return decodeURIComponent(origin);
}
return undefined;
}
Loading

0 comments on commit 57dedbf

Please sign in to comment.