Skip to content

Commit 85269e8

Browse files
Refactor loadJsonFile import and add new implementation
- Updated import paths for loadJsonFile in ReactOnRailsRSC and getReactServerComponent to use the new local loadJsonFile.ts module. - Removed the old loadJsonFile reference from package.json in react-on-rails. - Introduced a new loadJsonFile.ts file in react-on-rails-pro with functionality to load and cache JSON files. These changes improve modularity and ensure the correct loading of JSON files in the new package structure.
1 parent b97f05b commit 85269e8

File tree

4 files changed

+43
-3
lines changed

4 files changed

+43
-3
lines changed

packages/react-on-rails-pro/src/ReactOnRailsRSC.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import {
3131
StreamingTrackers,
3232
transformRenderStreamChunksToResultObject,
3333
} from './streamServerRenderedReactComponent.ts';
34-
import loadJsonFile from 'react-on-rails/loadJsonFile';
34+
import loadJsonFile from './loadJsonFile.ts';
3535

3636
let serverRendererPromise: Promise<ReturnType<typeof buildServerRenderer>> | undefined;
3737

packages/react-on-rails-pro/src/getReactServerComponent.server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import { BundleManifest } from 'react-on-rails-rsc';
1616
import { buildClientRenderer } from 'react-on-rails-rsc/client.node';
1717
import transformRSCStream from './transformRSCNodeStream.ts';
18-
import loadJsonFile from 'react-on-rails/loadJsonFile';
18+
import loadJsonFile from './loadJsonFile.ts';
1919
import type { RailsContextWithServerStreamingCapabilities } from 'react-on-rails/types';
2020

2121
type GetReactServerComponentOnServerProps = {
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright (c) 2025 Shakacode LLC
3+
*
4+
* This file is NOT licensed under the MIT (open source) license.
5+
* It is part of the React on Rails Pro offering and is licensed separately.
6+
*
7+
* Unauthorized copying, modification, distribution, or use of this file,
8+
* via any medium, is strictly prohibited without a valid license agreement
9+
* from Shakacode LLC.
10+
*
11+
* For licensing terms, please see:
12+
* https://github.com/shakacode/react_on_rails/blob/master/REACT-ON-RAILS-PRO-LICENSE.md
13+
*/
14+
15+
import * as path from 'path';
16+
import * as fs from 'fs/promises';
17+
18+
type LoadedJsonFile = Record<string, unknown>;
19+
const loadedJsonFiles = new Map<string, LoadedJsonFile>();
20+
21+
export default async function loadJsonFile<T extends LoadedJsonFile = LoadedJsonFile>(
22+
fileName: string,
23+
): Promise<T> {
24+
// Asset JSON files are uploaded to node renderer.
25+
// Renderer copies assets to the same place as the server-bundle.js and rsc-bundle.js.
26+
// Thus, the __dirname of this code is where we can find the manifest file.
27+
const filePath = path.resolve(__dirname, fileName);
28+
const loadedJsonFile = loadedJsonFiles.get(filePath);
29+
if (loadedJsonFile) {
30+
return loadedJsonFile as T;
31+
}
32+
33+
try {
34+
const file = JSON.parse(await fs.readFile(filePath, 'utf8')) as T;
35+
loadedJsonFiles.set(filePath, file);
36+
return file;
37+
} catch (error) {
38+
console.error(`Failed to load JSON file: ${filePath}`, error);
39+
throw error;
40+
}
41+
}

packages/react-on-rails/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@
5353
"./ReactOnRails.full": "./lib/ReactOnRails.full.js",
5454
"./handleError": "./lib/handleError.js",
5555
"./serverRenderUtils": "./lib/serverRenderUtils.js",
56-
"./loadJsonFile": "./lib/loadJsonFile.js",
5756
"./buildConsoleReplay": "./lib/buildConsoleReplay.js",
5857
"./ReactDOMServer": "./lib/ReactDOMServer.cjs"
5958
},

0 commit comments

Comments
 (0)