Skip to content

Commit 9e2b062

Browse files
committed
hot reload context vars
1 parent 170dd9e commit 9e2b062

File tree

5 files changed

+60
-22
lines changed

5 files changed

+60
-22
lines changed

.changeset/serious-months-report.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"htmldocs": patch
3+
---
4+
5+
hot reload context vars
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"use server";
2+
3+
import path from 'node:path';
4+
import fs from 'node:fs';
5+
import { DOCUMENT_SCHEMAS_DIR } from '../../utils/paths';
6+
import { JSONSchema7Definition } from 'json-schema';
7+
import logger from '~/lib/logger';
8+
9+
export async function getDocumentSchema(documentPath: string): Promise<JSONSchema7Definition | null> {
10+
const baseName = path.basename(documentPath, path.extname(documentPath));
11+
const schemaPath = path.join(
12+
DOCUMENT_SCHEMAS_DIR,
13+
baseName,
14+
`${baseName}.schema.json`
15+
);
16+
17+
try {
18+
if (fs.existsSync(schemaPath)) {
19+
const rawSchema = JSON.parse(await fs.promises.readFile(schemaPath, 'utf-8'));
20+
return rawSchema?.definitions?.ComponentProps || null;
21+
}
22+
} catch (error) {
23+
logger.warn('Failed to load schema:', error);
24+
}
25+
26+
return null;
27+
}

packages/htmldocs/src/app/contexts/documents.tsx

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import { getDocumentPathFromSlug } from "~/actions/get-document-path-from-slug";
1313
import { renderDocumentToPDF, RenderDocumentToPDFProps } from "~/actions/render-document-to-pdf";
1414
import { PageConfig } from "~/lib/types";
1515
import logger from "~/lib/logger";
16+
import { JSONSchema7Definition } from "json-schema";
17+
import { getDocumentSchema } from "~/actions/get-document-schema";
1618

1719
const DocumentsContext = createContext<
1820
| {
@@ -27,6 +29,7 @@ const DocumentsContext = createContext<
2729
renderDocumentToPDF: ({ url, ...props }: RenderDocumentToPDFProps) => Promise<Buffer | Error>;
2830
pageConfigs: Record<string, PageConfig>;
2931
setPageConfig: (documentPath: string, config: PageConfig) => void;
32+
documentSchemas: Record<string, JSONSchema7Definition>;
3033
}
3134
| undefined
3235
>(undefined);
@@ -54,6 +57,7 @@ export const DocumentsProvider = (props: {
5457
useState<Record<string, DocumentRenderingResult>>({});
5558

5659
const [pageConfigs, setPageConfigs] = useState<Record<string, PageConfig>>({});
60+
const [documentSchemas, setDocumentSchemas] = useState<Record<string, JSONSchema7Definition>>({});
5761

5862
const setPageConfig = (documentPath: string, config: PageConfig) => {
5963
setPageConfigs(prev => ({
@@ -62,6 +66,20 @@ export const DocumentsProvider = (props: {
6266
}));
6367
};
6468

69+
const loadSchema = async (documentPath: string) => {
70+
try {
71+
const schema = await getDocumentSchema(documentPath);
72+
if (schema) {
73+
setDocumentSchemas(prev => ({
74+
...prev,
75+
[documentPath]: schema
76+
}));
77+
}
78+
} catch (error) {
79+
logger.warn('Failed to load schema:', error);
80+
}
81+
};
82+
6583
if (process.env.NEXT_PUBLIC_IS_BUILDING !== "true") {
6684
// this will not change on runtime so it doesn't violate
6785
// the rules of hooks
@@ -112,6 +130,9 @@ export const DocumentsProvider = (props: {
112130
...map,
113131
[pathForChangedDocument]: renderingResult,
114132
}));
133+
134+
// Load or reload schema for the changed document
135+
await loadSchema(pathForChangedDocument);
115136
}
116137
}
117138
});
@@ -149,6 +170,7 @@ export const DocumentsProvider = (props: {
149170
renderDocumentToPDF,
150171
pageConfigs,
151172
setPageConfig,
173+
documentSchemas,
152174
}}
153175
>
154176
{props.children}

packages/htmldocs/src/app/preview/[...slug]/page.tsx

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
import { Suspense } from 'react';
22
import path from 'node:path';
3-
import fs from 'node:fs';
43
import { redirect } from 'next/navigation';
54
import { getDocumentPathFromSlug } from '~/actions/get-document-path-from-slug';
65
import { getDocumentsDirectoryMetadata } from '~/actions/get-documents-directory-metadata';
76
import { renderDocumentByPath } from '~/actions/render-document-by-path';
87
import { documentsDirectoryAbsolutePath } from '../../../utils/documents-directory-absolute-path';
98
import Home from '../../page';
109
import Preview from './preview';
11-
import { DOCUMENT_SCHEMAS_DIR } from '../../../utils/paths';
10+
import { getDocumentSchema } from '~/actions/get-document-schema';
1211

1312
export const dynamicParams = true;
1413

@@ -46,24 +45,7 @@ This is most likely not an issue with the preview server. Maybe there was a typo
4645
throw exception;
4746
}
4847

49-
// Load schema if it exists
50-
const baseName = path.basename(documentPath, path.extname(documentPath));
51-
const schemaPath = path.join(
52-
DOCUMENT_SCHEMAS_DIR,
53-
baseName,
54-
`${baseName}.schema.json`
55-
);
56-
57-
let schema = null;
58-
try {
59-
if (fs.existsSync(schemaPath)) {
60-
const rawSchema = JSON.parse(await fs.promises.readFile(schemaPath, 'utf-8'));
61-
schema = rawSchema?.definitions?.ComponentProps || null;
62-
}
63-
} catch (error) {
64-
console.warn('Failed to load schema:', error);
65-
}
66-
48+
const schema = await getDocumentSchema(documentPath);
6749
const documentRenderingResult = await renderDocumentByPath(documentPath);
6850

6951
if (

packages/htmldocs/src/app/preview/[...slug]/preview.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,22 @@ const Preview = ({
2828
documentPath,
2929
pathSeparator,
3030
renderingResult: initialRenderingResult,
31-
schema,
31+
schema: initialSchema,
3232
}: PreviewProps) => {
3333
const router = useRouter();
3434
const pathname = usePathname();
3535
const searchParams = useSearchParams();
3636

3737
const activeView = searchParams.get('view') ?? 'desktop';
38-
const { useDocumentRenderingResult, setPageConfig } = useDocuments();
38+
const { useDocumentRenderingResult, setPageConfig, documentSchemas } = useDocuments();
3939

4040
const renderingResult = useDocumentRenderingResult(
4141
documentPath,
4242
initialRenderingResult,
4343
);
4444

45+
const schema = documentSchemas[documentPath] || initialSchema;
46+
4547
const renderedDocumentMetadata = useRenderingMetadata(
4648
documentPath,
4749
renderingResult,

0 commit comments

Comments
 (0)