-
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.
- Loading branch information
Showing
9 changed files
with
178 additions
and
145 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@30aa5339af59f53d5452b29eebde196a1df66a16/" | ||
}, | ||
"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,145 @@ | ||
import type { Section } from "deco/blocks/section.ts"; | ||
import { SectionContext } from "deco/components/section.tsx"; | ||
import { asResolved, context } 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: await ctx.get<Section>(section), | ||
}; | ||
} | ||
|
||
const resolvingMatchers: Record<string, boolean> = {}; | ||
const resolvedSection = await ctx.get<Section>(section, { | ||
propagateOptions: true, | ||
hooks: { | ||
onPropsResolveStart: ( | ||
resolve, | ||
_props, | ||
resolver, | ||
_resolveType, | ||
ctx, | ||
) => { | ||
if (resolvingMatchers[ctx.resolveId]) { | ||
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") { | ||
// deno-lint-ignore no-explicit-any | ||
return undefined as any; | ||
} | ||
return resolve(); | ||
}, | ||
}, | ||
}); | ||
|
||
return { | ||
loading: shouldRender ? "eager" : "lazy", | ||
section: { | ||
Component: resolvedSection.LoadingFallback ?? | ||
defaultFallbackFor(resolvedSection.metadata?.component ?? "unknown"), | ||
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), | ||
}); | ||
|
||
export default Lazy; |
Oops, something went wrong.