Skip to content

fix: fix a resource a type error when using async _resolved function #7426

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 21 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 10 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
6 changes: 6 additions & 0 deletions .changeset/polite-singers-sing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@builder.io/qwik-city': patch
'@builder.io/qwik': patch
---

follow version 2 to fix this ts error
2 changes: 1 addition & 1 deletion packages/docs/src/routes/api/qwik/api.json
Original file line number Diff line number Diff line change
Expand Up @@ -2432,7 +2432,7 @@
}
],
"kind": "Interface",
"content": "```typescript\nexport interface ResourceProps<T> \n```\n\n\n<table><thead><tr><th>\n\nProperty\n\n\n</th><th>\n\nModifiers\n\n\n</th><th>\n\nType\n\n\n</th><th>\n\nDescription\n\n\n</th></tr></thead>\n<tbody><tr><td>\n\n[onPending?](#)\n\n\n</td><td>\n\n\n</td><td>\n\n() =&gt; [JSXOutput](#jsxoutput)\n\n\n</td><td>\n\n_(Optional)_\n\n\n</td></tr>\n<tr><td>\n\n[onRejected?](#)\n\n\n</td><td>\n\n\n</td><td>\n\n(reason: Error) =&gt; [JSXOutput](#jsxoutput)\n\n\n</td><td>\n\n_(Optional)_\n\n\n</td></tr>\n<tr><td>\n\n[onResolved](#)\n\n\n</td><td>\n\n\n</td><td>\n\n(value: T) =&gt; [JSXOutput](#jsxoutput)\n\n\n</td><td>\n\n\n</td></tr>\n<tr><td>\n\n[value](#)\n\n\n</td><td>\n\n`readonly`\n\n\n</td><td>\n\n[ResourceReturn](#resourcereturn)<!-- -->&lt;T&gt; \\| [Signal](#signal)<!-- -->&lt;Promise&lt;T&gt; \\| T&gt; \\| Promise&lt;T&gt;\n\n\n</td><td>\n\n\n</td></tr>\n</tbody></table>",
"content": "```typescript\nexport interface ResourceProps<T> \n```\n\n\n<table><thead><tr><th>\n\nProperty\n\n\n</th><th>\n\nModifiers\n\n\n</th><th>\n\nType\n\n\n</th><th>\n\nDescription\n\n\n</th></tr></thead>\n<tbody><tr><td>\n\n[onPending?](#)\n\n\n</td><td>\n\n\n</td><td>\n\n() =&gt; [JSXOutput](#jsxoutput)\n\n\n</td><td>\n\n_(Optional)_\n\n\n</td></tr>\n<tr><td>\n\n[onRejected?](#)\n\n\n</td><td>\n\n\n</td><td>\n\n(reason: Error) =&gt; [JSXOutput](#jsxoutput)\n\n\n</td><td>\n\n_(Optional)_\n\n\n</td></tr>\n<tr><td>\n\n[onResolved](#)\n\n\n</td><td>\n\n\n</td><td>\n\n(value: T) =&gt; [JSXOutput](#jsxoutput) \\| Promise&lt;[JSXOutput](#jsxoutput)<!-- -->&gt;\n\n\n</td><td>\n\n\n</td></tr>\n<tr><td>\n\n[value](#)\n\n\n</td><td>\n\n`readonly`\n\n\n</td><td>\n\n[ResourceReturn](#resourcereturn)<!-- -->&lt;T&gt; \\| [Signal](#signal)<!-- -->&lt;Promise&lt;T&gt; \\| T&gt; \\| Promise&lt;T&gt;\n\n\n</td><td>\n\n\n</td></tr>\n</tbody></table>",
"editUrl": "https://github.com/QwikDev/qwik/tree/main/packages/qwik/src/core/use/use-resource.ts",
"mdFile": "qwik.resourceprops.md"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/docs/src/routes/api/qwik/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -5089,7 +5089,7 @@ _(Optional)_

</td><td>

(value: T) =&gt; [JSXOutput](#jsxoutput)
(value: T) =&gt; [JSXOutput](#jsxoutput) \| Promise&lt;[JSXOutput](#jsxoutput)&gt;

</td><td>

Expand Down
4 changes: 2 additions & 2 deletions packages/qwik/src/core/qwik.core.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -882,11 +882,11 @@ export interface ResourcePending<T> {
// @public (undocumented)
export interface ResourceProps<T> {
// (undocumented)
onPending?: () => JSXOutput;
onPending?: () => JSXOutput | Promise<JSXOutput>;
// (undocumented)
onRejected?: (reason: Error) => JSXOutput;
// (undocumented)
onResolved: (value: T) => JSXOutput;
onResolved: (value: T) => JSXOutput | Promise<JSXOutput>;
// (undocumented)
readonly value: ResourceReturn<T> | Signal<Promise<T> | T> | Promise<T>;
}
Expand Down
30 changes: 30 additions & 0 deletions packages/qwik/src/core/render/ssr/render-ssr.unit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,25 @@ test('DelayResource', async () => {
);
});

test('AsyncResource', async () => {
await testSSR(
<body>
<ul>
<AsyncResource text="thing" />
</ul>
</body>,
`<html q:container="paused" q:version="dev" q:render="ssr-dev" q:base="" q:manifest-hash="test">
<body>
<ul>
<!--qv q:id=0 q:key=sX:-->
<div class="cmp"><!--qkssr-f--><span>thing</span>;</div>
<!--/qv-->
</ul>
</body>
</html>`
);
});

test('using promises with DelayResource', async () => {
await testSSR(
<body>
Expand Down Expand Up @@ -1882,6 +1901,17 @@ export const DelayResource = component$((props: { text: string; delay: number })
);
});

export const AsyncResource = component$((props: { text: string }) => {
const resource = useResource$<string>(() => {
return props.text;
});
return (
<div class="cmp">
<Resource value={resource} onResolved={async (value) => <span>{value}</span>} />;
</div>
);
});

export const NullCmp = component$(() => {
return null;
});
Expand Down
43 changes: 26 additions & 17 deletions packages/qwik/src/core/use/use-resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,8 @@ export const useResource$ = <T>(
/** @public */
export interface ResourceProps<T> {
readonly value: ResourceReturn<T> | Signal<Promise<T> | T> | Promise<T>;
onResolved: (value: T) => JSXOutput;
onPending?: () => JSXOutput;
onResolved: (value: T) => JSXOutput | Promise<JSXOutput>;
onPending?: () => JSXOutput | Promise<JSXOutput>;
Comment on lines +194 to +195
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand why this is needed. The resource holds a Promise, but the Resource component should just render without a Promise

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From what I know, it is correct for JavaScript, but it is not correct for TypeScript.
TypeScript will think that the function returns a Promise when async is added before the function.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you shouldn't add async before the function then

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

aaah ok now I get it, it's when the dev puts an async in the resource functions, ok. Hmm not sure if that's a good idea though - the render functions are just like inline components and we don't really support async inline components either. It works though :)

@Varixo thoughts?

onRejected?: (reason: Error) => JSXOutput;
}

Expand Down Expand Up @@ -252,10 +252,15 @@ export interface ResourceProps<T> {
*/
// </docs>
export const Resource = <T>(props: ResourceProps<T>): JSXOutput => {
const isBrowser = !isServerPlatform();
// Resource path
return jsx(Fragment, {
children: getResourceValueAsPromise(props),
});
};
function getResourceValueAsPromise<T>(props: ResourceProps<T>): Promise<JSXOutput> | JSXOutput {
const resource = props.value as ResourceReturnInternal<T> | Promise<T> | Signal<T>;
let promise: Promise<T> | undefined;
if (isResourceReturn(resource)) {
if (isResourceReturn(resource) && resource.value) {
const isBrowser = !isServerPlatform();
if (isBrowser) {
if (props.onRejected) {
resource.value.catch(() => {});
Expand All @@ -277,23 +282,27 @@ export const Resource = <T>(props: ResourceProps<T>): JSXOutput => {
return props.onResolved(resource._resolved!);
}
}
promise = resource.value;
return resource.value.then(
useBindInvokeContext(props.onResolved),
useBindInvokeContext(props.onRejected)
);
} else if (isPromise(resource)) {
promise = resource;
return resource.then(
useBindInvokeContext(props.onResolved),
useBindInvokeContext(props.onRejected)
);
} else if (isSignal(resource)) {
promise = Promise.resolve(resource.value);
return Promise.resolve(resource.value).then(
useBindInvokeContext(props.onResolved),
useBindInvokeContext(props.onRejected)
);
} else {
return props.onResolved(resource as T);
}

// Resource path
return jsx(Fragment, {
children: promise.then(
return Promise.resolve(resource as T).then(
useBindInvokeContext(props.onResolved),
useBindInvokeContext(props.onRejected)
),
});
};
);
}
}

export const _createResourceReturn = <T>(opts?: ResourceOptions): ResourceReturnInternal<T> => {
const resource: ResourceReturnInternal<T> = {
Expand Down