Skip to content

Commit

Permalink
Merge branch 'chore/upgrade-vite' of https://github.com/florian-lefeb…
Browse files Browse the repository at this point in the history
…vre/astro into chore/upgrade-vite
  • Loading branch information
florian-lefebvre committed Dec 19, 2023
2 parents 71bba24 + c444e73 commit e98c6ad
Show file tree
Hide file tree
Showing 21 changed files with 291 additions and 32 deletions.
5 changes: 5 additions & 0 deletions .changeset/breezy-hairs-yell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/react': patch
---

Prevents unsupported `forwardRef` components created by Preact from being rendered by React
5 changes: 5 additions & 0 deletions .changeset/eight-sheep-nail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/node': patch
---

Fix typo in @astrojs/node README
5 changes: 5 additions & 0 deletions .changeset/friendly-tables-worry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

update import created for `astro create netlify`
5 changes: 5 additions & 0 deletions .changeset/modern-humans-think.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fixes importing dev toolbar apps from integrations on Windows
5 changes: 5 additions & 0 deletions .changeset/real-bags-hope.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fix Astro failing to build on certain exotic platform that reports their CPU count incorrectly
5 changes: 5 additions & 0 deletions .changeset/selfish-rings-occur.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Correctly handle the error in case the middleware throws a runtime error
5 changes: 5 additions & 0 deletions .changeset/silly-cycles-clap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fixes an issue where redirects did not replace slugs when the target of the redirect rule was not a verbatim route in the project.
5 changes: 5 additions & 0 deletions .changeset/warm-bats-eat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fixes incorrect hoisted script paths when custom rollup output file names are configured
2 changes: 1 addition & 1 deletion examples/ssr/src/pages/api/cart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { APIContext } from 'astro';
import { userCartItems } from '../../models/session';

export function GET({ cookies }: APIContext) {
let userId = cookies.get('user-id').value;
let userId = cookies.get('user-id')?.value;

if (!userId || !userCartItems.has(userId)) {
return Response.json({ items: [] });
Expand Down
2 changes: 1 addition & 1 deletion packages/astro/src/@types/astro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -677,7 +677,7 @@ export interface AstroUserConfig {
* [See our Server-side Rendering guide](https://docs.astro.build/en/guides/server-side-rendering/) for more on SSR, and [our deployment guides](https://docs.astro.build/en/guides/deploy/) for a complete list of hosts.
*
* ```js
* import netlify from '@astrojs/netlify/functions';
* import netlify from '@astrojs/netlify';
* {
* // Example: Build for Netlify serverless deployment
* adapter: netlify(),
Expand Down
2 changes: 1 addition & 1 deletion packages/astro/src/cli/add/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public-hoist-pattern[]=*lit*
`;

const OFFICIAL_ADAPTER_TO_IMPORT_MAP: Record<string, string> = {
netlify: '@astrojs/netlify/functions',
netlify: '@astrojs/netlify',
vercel: '@astrojs/vercel/serverless',
cloudflare: '@astrojs/cloudflare',
node: '@astrojs/node',
Expand Down
2 changes: 1 addition & 1 deletion packages/astro/src/core/build/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ export async function generatePages(opts: StaticBuildOptions, internals: BuildIn
.reduce((a, b) => a + b, 0);
const cpuCount = os.cpus().length;
const assetsCreationEnvironment = await prepareAssetsGenerationEnv(pipeline, totalCount);
const queue = new PQueue({ concurrency: cpuCount });
const queue = new PQueue({ concurrency: Math.max(cpuCount, 1) });

const assetsTimer = performance.now();
for (const [originalPath, transforms] of staticImageList) {
Expand Down
3 changes: 2 additions & 1 deletion packages/astro/src/core/build/plugins/plugin-manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,9 @@ function buildManifest(
if (route.prerender || !pageData) continue;
const scripts: SerializedRouteInfo['scripts'] = [];
if (pageData.hoistedScript) {
const shouldPrefixAssetPath = pageData.hoistedScript.type === 'external';
const hoistedValue = pageData.hoistedScript.value;
const value = hoistedValue.endsWith('.js') ? prefixAssetPath(hoistedValue) : hoistedValue;
const value = shouldPrefixAssetPath ? prefixAssetPath(hoistedValue) : hoistedValue;
scripts.unshift(
Object.assign({}, pageData.hoistedScript, {
value,
Expand Down
20 changes: 20 additions & 0 deletions packages/astro/src/core/errors/errors-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -780,6 +780,26 @@ export const LocalsNotAnObject = {
hint: 'If you tried to remove some information from the `locals` object, try to use `delete` or set the property to `undefined`.',
} satisfies ErrorData;

/**
* @docs
* @description
* Thrown in development mode when middleware throws an error while attempting to loading it.
*
* For example:
* ```ts
* import {defineMiddleware} from "astro:middleware";
* throw new Error("Error thrown while loading the middleware.")
* export const onRequest = defineMiddleware(() => {
* return "string"
* });
* ```
*/
export const MiddlewareCantBeLoaded = {
name: 'MiddlewareCantBeLoaded',
title: "Can't load the middleware.",
message: 'The middleware threw an error while Astro was trying to loading it.',
} satisfies ErrorData;

/**
* @docs
* @see
Expand Down
10 changes: 6 additions & 4 deletions packages/astro/src/core/middleware/loadMiddleware.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type { ModuleLoader } from '../module-loader/index.js';
import { MIDDLEWARE_MODULE_ID } from './vite-plugin.js';
import { MiddlewareCantBeLoaded } from '../errors/errors-data.js';
import { AstroError } from '../errors/index.js';

/**
* It accepts a module loader and the astro settings, and it attempts to load the middlewares defined in the configuration.
Expand All @@ -8,9 +10,9 @@ import { MIDDLEWARE_MODULE_ID } from './vite-plugin.js';
*/
export async function loadMiddleware(moduleLoader: ModuleLoader) {
try {
const module = await moduleLoader.import(MIDDLEWARE_MODULE_ID);
return module;
} catch {
return void 0;
return await moduleLoader.import(MIDDLEWARE_MODULE_ID);
} catch (error: any) {
const astroError = new AstroError(MiddlewareCantBeLoaded, undefined, { cause: error });
throw astroError;
}
}
9 changes: 8 additions & 1 deletion packages/astro/src/core/redirects/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,14 @@ export function redirectRouteGenerate(redirectRoute: RouteData, data: Params): s
if (typeof routeData !== 'undefined') {
return routeData?.generate(data) || routeData?.pathname || '/';
} else if (typeof route === 'string') {
return route;
// TODO: this logic is duplicated between here and manifest/create.ts
let target = route;
for (const param of Object.keys(data)) {
const paramValue = data[param]!;
target = target.replace(`[${param}]`, paramValue);
target = target.replace(`[...${param}]`, paramValue);
}
return target;
} else if (typeof route === 'undefined') {
return '/';
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export default function astroDevOverlay({ settings }: AstroPluginOptions): vite.
return `
export const loadDevOverlayPlugins = async () => {
return [${settings.devToolbarApps
.map((plugin) => `(await import('${plugin}')).default`)
.map((plugin) => `(await import(${JSON.stringify(plugin)})).default`)
.join(',')}];
};
`;
Expand Down
28 changes: 28 additions & 0 deletions packages/astro/test/redirects.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ describe('Astro.redirect', () => {
redirects: {
'/api/redirect': '/test',
'/external/redirect': 'https://example.com/',
// for example, the real file handling the target path may be called
// src/pages/not-verbatim/target1/[something-other-than-dynamic].astro
'/source/[dynamic]': '/not-verbatim/target1/[dynamic]',
// may be called src/pages/not-verbatim/target2/[abc]/[xyz].astro
'/source/[dynamic]/[route]': '/not-verbatim/target2/[dynamic]/[route]',
// may be called src/pages/not-verbatim/target3/[...rest].astro
'/source/[...spread]': '/not-verbatim/target3/[...spread]',
},
});
await fixture.build();
Expand Down Expand Up @@ -68,6 +75,27 @@ describe('Astro.redirect', () => {
const response = await app.render(request);
expect(response.status).to.equal(308);
});

it('Forwards params to the target path - single param', async () => {
const app = await fixture.loadTestAdapterApp();
const request = new Request('http://example.com/source/x');
const response = await app.render(request);
expect(response.headers.get('Location')).to.equal('/not-verbatim/target1/x');
});

it('Forwards params to the target path - multiple params', async () => {
const app = await fixture.loadTestAdapterApp();
const request = new Request('http://example.com/source/x/y');
const response = await app.render(request);
expect(response.headers.get('Location')).to.equal('/not-verbatim/target2/x/y');
});

it('Forwards params to the target path - spread param', async () => {
const app = await fixture.loadTestAdapterApp();
const request = new Request('http://example.com/source/x/y/z');
const response = await app.render(request);
expect(response.headers.get('Location')).to.equal('/not-verbatim/target3/x/y/z');
});
});
});

Expand Down
Loading

0 comments on commit e98c6ad

Please sign in to comment.