Skip to content

Commit

Permalink
Merge pull request #5 from dalurness/du/dynamicSolution
Browse files Browse the repository at this point in the history
fix dynamically rendered component
  • Loading branch information
dalurness authored Nov 8, 2024
2 parents b3acd3f + 9d25210 commit 114a1e2
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 14 deletions.
18 changes: 9 additions & 9 deletions src/components/DynamicSolution.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { lazy, Suspense } from "react";

// this component allows us to dynamically import components for question results that are unique to each specific question.

// https://vite.dev/guide/features.html#glob-import
let x: Record<string, astroHTML.JSX.Element> = import.meta.glob('./solutions/*', { eager: true, import: "default" })
export function DynamicSolution({ resultComponent }: { resultComponent: string }) {
// btw...be aware of footguns when engaging in jank
// See https://github.com/rollup/plugins/tree/master/packages/dynamic-import-vars#limitations for supported dynamic import formats
const Component = lazy(() => import(/* @vite-ignore */`./solutions/${resultComponent}`));
let path = `./solutions/${resultComponent}.tsx`
let Component = x[path]

if (!Component) return null

return (
<Suspense fallback={<div>Loading...</div>}>
<Component />
</Suspense>
);
<Component client:load />
)
};
6 changes: 1 addition & 5 deletions src/layouts/Day.astro
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,6 @@ const { day, title, resultComponent } = Astro.props;
</div>

<div class="flex flex-col max-w-screen-lg w-full mx-auto px-4 md:px-6 my-12">
{
resultComponent && (
<DynamicSolution resultComponent={resultComponent} client:load />
)
}
{resultComponent && <DynamicSolution resultComponent={resultComponent} />}
</div>
</Layout>

0 comments on commit 114a1e2

Please sign in to comment.