Skip to content

Commit

Permalink
Merge branch 'main' into feat/react-clientside-identifier-prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
SudoCat authored Aug 14, 2023
2 parents b551829 + d1f7143 commit 30806ca
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/odd-plants-tie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Add support for non-awaited imports to the Image component and `getImage`
5 changes: 5 additions & 0 deletions .changeset/olive-queens-drum.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Add second type argument to the AstroGlobal type to type Astro.self. This change will ultimately allow our editor tooling to provide props completions and intellisense for `<Astro.self />`
4 changes: 3 additions & 1 deletion packages/astro/client-base.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ declare module 'astro:assets' {
* This is functionally equivalent to using the `<Image />` component, as the component calls this function internally.
*/
getImage: (
options: import('./dist/assets/types.js').ImageTransform
options:
| import('./dist/assets/types.js').ImageTransform
| import('./dist/assets/types.js').UnresolvedImageTransform
) => Promise<import('./dist/assets/types.js').GetImageResult>;
getConfiguredImageService: typeof import('./dist/assets/index.js').getConfiguredImageService;
Image: typeof import('./components/Image.astro').default;
Expand Down
8 changes: 5 additions & 3 deletions packages/astro/src/@types/astro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,10 @@ export interface CLIFlags {
*
* [Astro reference](https://docs.astro.build/reference/api-reference/#astro-global)
*/
export interface AstroGlobal<Props extends Record<string, any> = Record<string, any>>
extends AstroGlobalPartial,
export interface AstroGlobal<
Props extends Record<string, any> = Record<string, any>,
Self = AstroComponentFactory
> extends AstroGlobalPartial,
AstroSharedContext<Props> {
/**
* A full URL object of the request URL.
Expand Down Expand Up @@ -217,7 +219,7 @@ export interface AstroGlobal<Props extends Record<string, any> = Record<string,
*
* [Astro reference](https://docs.astro.build/en/guides/api-reference/#astroself)
*/
self: AstroComponentFactory;
self: Self;
/** Utility functions for modifying an Astro component’s slotted children
*
* [Astro reference](https://docs.astro.build/en/reference/api-reference/#astroslots)
Expand Down
25 changes: 20 additions & 5 deletions packages/astro/src/assets/internal.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import type { AstroSettings } from '../@types/astro.js';
import { AstroError, AstroErrorData } from '../core/errors/index.js';
import { isLocalService, type ImageService } from './services/service.js';
import type { GetImageResult, ImageMetadata, ImageTransform } from './types.js';
import type {
GetImageResult,
ImageMetadata,
ImageTransform,
UnresolvedImageTransform,
} from './types.js';

export function injectImageEndpoint(settings: AstroSettings) {
settings.injectedRoutes.push({
Expand Down Expand Up @@ -37,7 +42,7 @@ export async function getConfiguredImageService(): Promise<ImageService> {
}

export async function getImage(
options: ImageTransform,
options: ImageTransform | UnresolvedImageTransform,
serviceConfig: Record<string, any>
): Promise<GetImageResult> {
if (!options || typeof options !== 'object') {
Expand All @@ -48,9 +53,19 @@ export async function getImage(
}

const service = await getConfiguredImageService();

// If the user inlined an import, something fairly common especially in MDX, await it for them
const resolvedOptions: ImageTransform = {
...options,
src:
typeof options.src === 'object' && 'then' in options.src
? (await options.src).default
: options.src,
};

const validatedOptions = service.validateOptions
? await service.validateOptions(options, serviceConfig)
: options;
? await service.validateOptions(resolvedOptions, serviceConfig)
: resolvedOptions;

let imageURL = await service.getURL(validatedOptions, serviceConfig);

Expand All @@ -60,7 +75,7 @@ export async function getImage(
}

return {
rawOptions: options,
rawOptions: resolvedOptions,
options: validatedOptions,
src: imageURL,
attributes:
Expand Down
6 changes: 5 additions & 1 deletion packages/astro/src/assets/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ export interface ImageMetadata {
orientation?: number;
}

export type UnresolvedImageTransform = Omit<ImageTransform, 'src'> & {
src: Promise<{ default: ImageMetadata }>;
};

/**
* Options accepted by the image transformation service.
*/
Expand Down Expand Up @@ -93,7 +97,7 @@ export type LocalImageProps<T> = ImageSharedProps<T> & {
* <Image src={myImage} alt="..."></Image>
* ```
*/
src: ImageMetadata;
src: ImageMetadata | Promise<{ default: ImageMetadata }>;
/**
* Desired output format for the image. Defaults to `webp`.
*
Expand Down
13 changes: 13 additions & 0 deletions packages/astro/test/core-image.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,19 @@ describe('astro:image', () => {
})
).to.be.true;
});

it('supports inlined imports', async () => {
let res = await fixture.fetch('/inlineImport');
let html = await res.text();
$ = cheerio.load(html);

let $img = $('img');
expect($img).to.have.a.lengthOf(1);

let src = $img.attr('src');
res = await fixture.fetch(src);
expect(res.status).to.equal(200);
});
});

describe('vite-isms', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
import { getImage } from "astro:assets";
const optimizedImage = await getImage({src: import('../assets/penguin1.jpg')})
---

<img src={optimizedImage.src} {...optimizedImage.attributes} />
2 changes: 1 addition & 1 deletion packages/integrations/netlify/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ Once you run `astro build` there will be a `dist/_redirects` file. Netlify will

By default, all pages will be rendered on first visit and the rendered result will be reused for every subsequent visit until you redeploy. To set a revalidation time, call the [`runtime.setBuildersTtl(ttl)` local](https://docs.astro.build/en/guides/middleware/#locals) with the duration (in seconds).

As an example, for the following snippet, Netlify will store the rendered HTML for 45 seconds.
The following example sets a revalidation time of 45, causing Netlify to store the rendered HTML for 45 seconds.

```astro
---
Expand Down

0 comments on commit 30806ca

Please sign in to comment.