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 all 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
4 changes: 2 additions & 2 deletions packages/docs/src/routes/api/qwik/api.json
Original file line number Diff line number Diff line change
Expand Up @@ -1774,7 +1774,7 @@
}
],
"kind": "Function",
"content": "> This API is provided as an alpha preview for developers and may change based on feedback that we receive. Do not use this API in a production environment.\n> \n\n> Warning: This API is now obsolete.\n> \n> This is no longer needed as the preloading happens automatically in qrl-class.ts. Leave this in your app for a while so it uninstalls existing service workers, but don't use it for new projects.\n> \n\n\n```typescript\nPrefetchServiceWorker: (opts: {\n base?: string;\n scope?: string;\n path?: string;\n verbose?: boolean;\n fetchBundleGraph?: boolean;\n nonce?: string;\n}) => JSXNode<'script'>\n```\n\n\n<table><thead><tr><th>\n\nParameter\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\nopts\n\n\n</td><td>\n\n{ base?: string; scope?: string; path?: string; verbose?: boolean; fetchBundleGraph?: boolean; nonce?: string; }\n\n\n</td><td>\n\n\n</td></tr>\n</tbody></table>\n**Returns:**\n\n[JSXNode](#jsxnode)<!-- -->&lt;'script'&gt;",
"content": "> This API is provided as an alpha preview for developers and may change based on feedback that we receive. Do not use this API in a production environment.\n> \n\n> Warning: This API is now obsolete.\n> \n> This is no longer needed as the preloading happens automatically in qrl-class.ts. Leave this in your app for a while so it uninstalls existing service workers, but don't use it for new projects.\n> \n\n\n```typescript\nPrefetchServiceWorker: (opts: {\n base?: string;\n scope?: string;\n path?: string;\n verbose?: boolean;\n fetchBundleGraph?: boolean;\n nonce?: string;\n}) => JSXNode<'script'>\n```\n\n\n<table><thead><tr><th>\n\nParameter\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\nopts\n\n\n</td><td>\n\n{ base?: string; scope?: string; path?: string; verbose?: boolean; fetchBundleGraph?: boolean; nonce?: string; }\n\n\n</td><td>\n\n\n</td></tr>\n</tbody></table>\n**Returns:**\n\nJSXNode&lt;'script'&gt;",
"editUrl": "https://github.com/QwikDev/qwik/tree/main/packages/qwik/src/core/components/prefetch.ts",
"mdFile": "qwik.prefetchserviceworker.md"
},
Expand Down 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) \\| Promise&lt;[JSXOutput](#jsxoutput)<!-- -->&gt;\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) \\| Promise&lt;[JSXOutput](#jsxoutput)<!-- -->&gt;\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
8 changes: 4 additions & 4 deletions packages/docs/src/routes/api/qwik/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -3651,7 +3651,7 @@ opts
</tbody></table>
**Returns:**

[JSXNode](#jsxnode)&lt;'script'&gt;
JSXNode&lt;'script'&gt;

[Edit this section](https://github.com/QwikDev/qwik/tree/main/packages/qwik/src/core/components/prefetch.ts)

Expand Down Expand Up @@ -5059,7 +5059,7 @@ Description

</td><td>

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

</td><td>

Expand All @@ -5074,7 +5074,7 @@ _(Optional)_

</td><td>

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

</td><td>

Expand All @@ -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
6 changes: 3 additions & 3 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;
onRejected?: (reason: Error) => JSXOutput | Promise<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
38 changes: 38 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 @@ -1550,6 +1550,25 @@ test('issue 4283', async () => {
);
});

test('AsyncResource', async () => {
await testSSR(
<body>
<ul>
<AsyncResource text="thing" delay={500} />
</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="cmp1"><!--qkssr-f--><span>1</span>;</div>
<!--/qv-->
</ul>
</body>
</html>`
);
});

// TODO
// Merge props on host
// - host events
Expand Down Expand Up @@ -1882,6 +1901,25 @@ export const DelayResource = component$((props: { text: string; delay: number })
);
});

export const AsyncResource = component$((props: { text: string; delay: number }) => {
const resource = useResource$<string>(async ({ track }) => {
track(() => props.text);
await delay(props.delay);
return props.text;
});
return (
<div class="cmp1">
<Resource
value={resource}
onResolved={async () => <span>1</span>}
onRejected={async () => <span>1</span>}
onPending={async () => <span>1</span>}
/>
;
</div>
);
});

export const NullCmp = component$(() => {
return null;
});
Expand Down
58 changes: 36 additions & 22 deletions packages/qwik/src/core/use/use-resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,9 @@ 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;
onRejected?: (reason: Error) => 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 | Promise<JSXOutput>;
}

// <docs markdown="../readme.md#useResource">
Expand Down Expand Up @@ -252,48 +252,62 @@ 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)) {
const isBrowser = !isServerPlatform();
if (isBrowser) {
if (props.onRejected) {
resource.value.catch(() => {});
if (resource._state === 'rejected') {
return props.onRejected(resource._error!);
return Promise.resolve(resource._error!).then(useBindInvokeContext(props.onRejected));
}
}
if (props.onPending) {
const state = resource._state;
if (state === 'resolved') {
return props.onResolved(resource._resolved!);
return Promise.resolve(resource._resolved!).then(useBindInvokeContext(props.onResolved));
} else if (state === 'pending') {
return props.onPending();
return Promise.resolve().then(useBindInvokeContext(props.onPending));
} else if (state === 'rejected') {
throw resource._error;
}
}
if (untrack(() => resource._resolved) !== undefined) {
return props.onResolved(resource._resolved!);
return Promise.resolve(resource._resolved!).then(useBindInvokeContext(props.onResolved));
}
}
promise = resource.value;
const value = resource.value;
if (value) {
return value.then(
useBindInvokeContext(props.onResolved),
useBindInvokeContext(props.onRejected)
);
} else {
// this is temporary value until the `runResource` is executed and promise is assigned to the value
return Promise.resolve(undefined);
}
} 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