Skip to content

Commit

Permalink
feat: Reduce build size by client side fetching garden json (#95)
Browse files Browse the repository at this point in the history
  • Loading branch information
ianhomer authored Aug 25, 2022
1 parent a9b43a2 commit 341633f
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 29 deletions.
3 changes: 3 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
out/
output/

packages/site/public/garden.json
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ node_modules/
out/
output/

packages/site/public/garden.json

*.tsbuildinfo
.env
.pnpm-debug.log
Expand Down
4 changes: 4 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
.next
gardens
out
output
pnpm-lock.yaml

packages/site/public/garden.json

6 changes: 4 additions & 2 deletions packages/garden/src/garden.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export interface Garden {
config: GardenConfig;
thing: (filename: string) => FileThing;
findBackLinks: (things: Things, name: string) => Array<Link>;
getMetaFilename: () => string;
meta: () => Promise<Things>;
load: () => Promise<Things>;
refresh: (filenameToPatch?: string) => Promise<Things>;
Expand All @@ -25,8 +26,8 @@ export interface GardenOptions {
allowGlobalMeta?: boolean;
directory?: string;
excludedDirectories?: string[];
hasMultiple?: boolean;
gardens?: { [key: string]: string };
hasMultiple?: boolean;
liveMeta?: boolean;
verbose?: boolean;
}
Expand All @@ -35,9 +36,9 @@ export interface GardenConfig {
allowGlobalMeta: boolean;
directory: string;
excludedDirectories: string[];
gardens: { [key: string]: string };
hasMultiple: boolean;
liveMeta: boolean;
gardens: { [key: string]: string };
verbose: boolean;
}

Expand Down Expand Up @@ -265,6 +266,7 @@ export const createGarden = (options: GardenOptions): Garden => {
findBackLinks: (things: Things, name: string) => {
return findBackLinks(things, name);
},
getMetaFilename: () => getMetaFilename(config),
thing: (filename: string) => {
return loadThing(config, filename);
},
Expand Down
2 changes: 2 additions & 0 deletions packages/site/public/robots.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
User-agent: *
Disallow: /
6 changes: 6 additions & 0 deletions packages/site/src/components/siteGarden.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { createGarden, toConfig } from "@garden/garden";

import options from "../../garden.config.js";

export const config = toConfig(options);
export const garden = createGarden(config);
47 changes: 27 additions & 20 deletions packages/site/src/pages/[[...name]].tsx
Original file line number Diff line number Diff line change
@@ -1,29 +1,26 @@
import {
createGarden,
findItemOrWanted,
findLinks,
findWantedThings,
getAllItems,
toConfig,
} from "@garden/garden";
import { findDeepLinks } from "@garden/graph";
import { Item, Link } from "@garden/types";
import { useState } from "react";
import { Item, Link, Things } from "@garden/types";
import { useEffect, useState } from "react";

import options from "../../garden.config.js";
import GraphDiagram from "../components/graph-diagram";
import { config, garden } from "../components/siteGarden";
import { useKey } from "../components/useKey";
import useWindowDimensions from "../components/useWindowDimensions";
import { createGraph } from "../lib/graph/graph";
import markdownToHtml from "../lib/markdownToHtml";

const config = toConfig(options);
const garden = createGarden(config);

function ItemPage({ item }) {
const { height, width } = useWindowDimensions();
const [depth, setDepth] = useState(2);
const [scale, setScale] = useState(1.3);
const [isLoading, setLoading] = useState(true);
const [data, setData] = useState<Things>({});

useKey((key) => setDepth(parseInt(key)), ["1", "2", "3", "4", "5"]);

Expand All @@ -33,6 +30,16 @@ function ItemPage({ item }) {
useKey(() => setScale(scale * 1.3), ["x"]);
useKey(() => setScale(scale * 1.5), ["z"]);

useEffect(() => {
fetch("/garden.json")
.then((res) => res.json())
.then((data) => {
setLoading(false);
console.log(data);
setData(data);
});
}, []);

return (
<>
<div className="container max-w-4xl px-4">
Expand All @@ -45,16 +52,18 @@ function ItemPage({ item }) {
))}
</ul>
</div>
<GraphDiagram
width={width}
height={height}
scale={scale}
graph={createGraph(
item.name,
item.garden,
findDeepLinks(item.garden, item.name, depth)
)}
/>
{!isLoading && data && (
<GraphDiagram
width={width}
height={height}
scale={scale}
graph={createGraph(
item.name,
data,
findDeepLinks(data, item.name, depth)
)}
/>
)}
</>
);
}
Expand All @@ -66,15 +75,13 @@ export async function getStaticProps({ params }) {
);
const links = await findLinks(garden, item);
const content = await markdownToHtml(item.content || "no content");
const things = await garden.load();

return {
props: {
item: {
...item,
links,
content,
garden: things,
},
},
};
Expand Down
19 changes: 12 additions & 7 deletions packages/site/src/scripts/pre-build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ const argv = yargs(hideBin(process.argv)).argv;
const garden = createGarden(config);

const refresh = (filenameToPatch?: string) =>
garden
.refresh(filenameToPatch)
.then((meta) => console.log(`refreshed ${Object.keys(meta).length} items`));
garden.refresh(filenameToPatch).then((meta) => {
console.log(`refreshed ${Object.keys(meta).length} items`);
fs.copyFileSync(garden.getMetaFilename(), "public/garden.json");
});

const cmdCallback = (error, stdout, stderr) => {
error && console.error(`exec error: ${error}`);
Expand All @@ -22,10 +23,14 @@ const cmdCallback = (error, stdout, stderr) => {
refresh();
};

if (!fs.existsSync(config.directory)) {
console.log(`Creating ${config.directory}`);
fs.mkdirSync(config.directory);
}
const mkdir = (directory: string) => {
if (!fs.existsSync(directory)) {
console.log(`Creating ${directory}`);
fs.mkdirSync(directory);
}
};

mkdir(config.directory);

if (config.hasMultiple) {
const gardens = config.gardens;
Expand Down

0 comments on commit 341633f

Please sign in to comment.