-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Async Rendering: A new approach ⚡ (#742)
* async rendering * async rendering * Fix loading loaders on resolveStart Signed-off-by: Marcos Candeia <[email protected]> * Do not resolve loaders props Signed-off-by: Marcos Candeia <[email protected]> * Fix nul check Signed-off-by: Marcos Candeia <[email protected]> * Do not invoke loaders Signed-off-by: Marcos Candeia <[email protected]> * Resolve empty props Signed-off-by: Marcos Candeia <[email protected]> * Rollback deferred to old version Signed-off-by: Marcos Candeia <[email protected]> * Fix deferred checks Signed-off-by: Marcos Candeia <[email protected]> --------- Signed-off-by: Marcos Candeia <[email protected]> Co-authored-by: Marcos Candeia <[email protected]>
- Loading branch information
Showing
9 changed files
with
180 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,7 @@ | |
"std/": "https://deno.land/[email protected]/", | ||
"partytown/": "https://deno.land/x/[email protected]/", | ||
"deco-sites/std/": "https://denopkg.com/deco-sites/[email protected]/", | ||
"deco/": "https://cdn.jsdelivr.net/gh/deco-cx/deco@1.78.0/" | ||
"deco/": "https://cdn.jsdelivr.net/gh/deco-cx/deco@1.80.0/" | ||
}, | ||
"lock": false, | ||
"tasks": { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,5 @@ | ||
import { Section } from "deco/mod.ts"; | ||
import { __DECO_FBT } from "../website/handlers/fresh.ts"; | ||
export const __DECO_FBT = "__decoFBT"; | ||
|
||
export const shouldForceRender = <Ctx extends { isBot?: boolean }>( | ||
{ ctx, searchParams }: { ctx: Ctx; searchParams: URLSearchParams }, | ||
): boolean => ctx.isBot || searchParams.get(__DECO_FBT) === "0"; | ||
|
||
export const renderSection = ({ Component, props }: Section) => ( | ||
<Component {...props} /> | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
import type { Section } from "deco/blocks/section.ts"; | ||
import { SectionContext } from "deco/components/section.tsx"; | ||
import { asResolved, context, isDeferred } from "deco/mod.ts"; | ||
import { useContext } from "preact/hooks"; | ||
import { shouldForceRender } from "../../../utils/deferred.ts"; | ||
import type { AppContext } from "../../mod.ts"; | ||
|
||
const useSectionContext = () => useContext(SectionContext); | ||
|
||
interface Props { | ||
/** @label hidden */ | ||
section: Section; | ||
|
||
/** | ||
* @description htmx/Deferred.tsx prop | ||
* @hide true | ||
*/ | ||
loading?: "eager" | "lazy"; | ||
} | ||
|
||
const defaultFallbackFor = (section: string) => () => ( | ||
<div | ||
style={{ | ||
height: "50vh", | ||
width: "100%", | ||
display: "flex", | ||
flexDirection: "column", | ||
gap: "0.5rem", | ||
justifyContent: "center", | ||
alignItems: "center", | ||
textAlign: "center", | ||
}} | ||
> | ||
{!context.isDeploy && ( | ||
<> | ||
<div> | ||
Async Rendering not implemented for section{" "} | ||
<span style={{ fontSize: "1rem", fontWeight: 600 }}>{section}</span> | ||
</div> | ||
<div style={{ fontSize: "0.75rem" }}> | ||
If you are a developer, export a{" "} | ||
<span style={{ fontWeight: 600 }}>LoadingFallback</span>{" "} | ||
component in this section. To learn more, check out our{" "} | ||
<a | ||
style={{ fontWeight: 600, textDecoration: "underline" }} | ||
href="https://deco.cx/en/blog/async-rendering#:~:text=Customizing%20Loading%20States%20Made%20Easy%3A" | ||
> | ||
guide | ||
</a> | ||
</div> | ||
<div style={{ fontSize: "0.75rem" }}> | ||
If you are NOT a developer, you can tweak Async Rendering. To learn | ||
more, check out our{" "} | ||
<a | ||
style={{ fontWeight: 600, textDecoration: "underline" }} | ||
href="https://deco.cx/en/blog/async-render-default#:~:text=Q%3A%20Can%20I%20disable%20the%20async%20render%3F" | ||
> | ||
blog post | ||
</a> | ||
</div> | ||
</> | ||
)} | ||
</div> | ||
); | ||
|
||
export const loader = async (props: Props, req: Request, ctx: AppContext) => { | ||
const { section, loading } = props; | ||
const url = new URL(req.url); | ||
|
||
const shouldRender = loading === "eager" || shouldForceRender({ | ||
ctx, | ||
searchParams: url.searchParams, | ||
}); | ||
|
||
if (shouldRender) { | ||
return { | ||
loading: "eager", | ||
section: isDeferred<Section>(section) ? await section() : section, | ||
}; | ||
} | ||
|
||
const resolvingMatchers: Record<string, boolean> = {}; | ||
const resolvedSection = isDeferred<Section>(section) | ||
? await section({}, { | ||
propagateOptions: true, | ||
hooks: { | ||
onResolveStart: (resolve, _props, resolver, _resolveType, ctx) => { | ||
if (ctx.resolveId in resolvingMatchers) { | ||
return resolve(); | ||
} | ||
if (resolver?.type === "loaders") { | ||
// deno-lint-ignore no-explicit-any | ||
return undefined as any; | ||
} | ||
return resolve(); | ||
}, | ||
onPropsResolveStart: ( | ||
resolve, | ||
_props, | ||
resolver, | ||
_resolveType, | ||
ctx, | ||
) => { | ||
if (ctx.resolveId in resolvingMatchers) { | ||
return resolve(); | ||
} | ||
if (resolver?.type === "matchers") { // matchers should not have a timeout. | ||
const id = crypto.randomUUID(); | ||
resolvingMatchers[id] = true; | ||
return resolve(id); | ||
} | ||
if (resolver?.type === "loaders") { | ||
return Promise.resolve([]); | ||
} | ||
return resolve(); | ||
}, | ||
}, | ||
}) | ||
: section; | ||
|
||
return { | ||
loading: shouldRender ? "eager" : "lazy", | ||
section: { | ||
Component: resolvedSection.LoadingFallback ?? | ||
defaultFallbackFor(resolvedSection.metadata?.component ?? "unknown"), | ||
metadata: resolvedSection.metadata, | ||
props: {}, | ||
}, | ||
}; | ||
}; | ||
|
||
type SectionProps = Awaited<ReturnType<typeof loader>>; | ||
|
||
function Lazy({ section, loading }: SectionProps) { | ||
const ctx = useSectionContext(); | ||
|
||
if (!ctx) { | ||
throw new Error("Missing SectionContext"); | ||
} | ||
|
||
if (loading === "lazy") { | ||
return ( | ||
<ctx.FallbackWrapper props={{ loading: "eager" }}> | ||
<section.Component {...section.props} /> | ||
</ctx.FallbackWrapper> | ||
); | ||
} | ||
|
||
return <section.Component {...section.props} />; | ||
} | ||
|
||
export const onBeforeResolveProps = (props: Props) => ({ | ||
...props, | ||
section: asResolved(props.section, true), | ||
}); | ||
|
||
export default Lazy; |
Oops, something went wrong.