Skip to content

Commit

Permalink
chore: cr
Browse files Browse the repository at this point in the history
  • Loading branch information
zllkjc committed Feb 21, 2025
1 parent 1403a54 commit 753e63c
Show file tree
Hide file tree
Showing 10 changed files with 226 additions and 197 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import type { Monitors } from '@modern-js/types';

export const defaultMonitors: Omit<Monitors, 'push'> = {
counter(name, ...args) {},
info(message, ...args) {},
debug(message, ...args) {},
trace(message, ...args) {},
warn(message, ...args) {},
error(message, ...args) {},
timing(name, dur, ...args) {},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { storage } from '@modern-js/runtime-utils/node';
import type { Monitors } from '@modern-js/types';
import { defaultMonitors } from './default';

export const getMonitors = (): Omit<Monitors, 'push'> => {
const storageContext = storage.useContext();
return storageContext.monitors || defaultMonitors;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { defaultMonitors } from './default';

export const getMonitors = () => {
return defaultMonitors;
};
111 changes: 57 additions & 54 deletions packages/runtime/plugin-runtime/src/core/server/stream/shared.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { OnError } from '@modern-js/app-tools';
import { run } from '@modern-js/runtime-utils/node';
import { storage } from '@modern-js/runtime-utils/node';
import { time } from '@modern-js/runtime-utils/time';
import { parseHeaders } from '@modern-js/runtime-utils/universal/request';
import type {
Expand Down Expand Up @@ -72,61 +72,64 @@ export function createRenderStreaming(
const createReadableStreamFromElement = await createReadableStreamPromise;
const headersData = parseHeaders(request);

return run(headersData, async () => {
const end = time();
const { runtimeContext, config, resource } = options;
const { onError, onTiming } = options;

const { htmlTemplate, entryName } = resource;

const ssrConfig = getSSRConfigByEntry(
entryName,
config.ssr,
config.ssrByEntries,
);

const RSCServerRoot = ({ children }: { children: React.ReactNode }) => {
return <>{children}</>;
};

let rootElement = wrapRuntimeContextProvider(
serverRoot,
Object.assign(runtimeContext, { ssr: true }),
);

rootElement = <RSCServerRoot>{rootElement}</RSCServerRoot>;

const stream = await createReadableStreamFromElement(
request,
rootElement,
{
config,
htmlTemplate,
runtimeContext,
ssrConfig,
return storage.run(
{ headers: headersData, monitors: options.monitors },
async () => {
const end = time();
const { runtimeContext, config, resource } = options;
const { onError, onTiming } = options;

const { htmlTemplate, entryName } = resource;

const ssrConfig = getSSRConfigByEntry(
entryName,
rscClientManifest: options.rscClientManifest,
rscSSRManifest: options.rscSSRManifest,
rscServerManifest: options.rscServerManifest,
rscRoot: options.rscRoot,
onShellReady() {
const cost = end();
onTiming(SSRTimings.RENDER_SHELL, cost);
},
onAllReady() {
const cost = end();
onTiming(SSRTimings.RENDER_HTML, cost);
},
onShellError(error) {
onError(error, SSRErrors.RENDER_SHELL);
},
onError(error) {
onError(error, SSRErrors.RENDER_STREAM);
config.ssr,
config.ssrByEntries,
);

const RSCServerRoot = ({ children }: { children: React.ReactNode }) => {
return <>{children}</>;
};

let rootElement = wrapRuntimeContextProvider(
serverRoot,
Object.assign(runtimeContext, { ssr: true }),
);

rootElement = <RSCServerRoot>{rootElement}</RSCServerRoot>;

const stream = await createReadableStreamFromElement(
request,
rootElement,
{
config,
htmlTemplate,
runtimeContext,
ssrConfig,
entryName,
rscClientManifest: options.rscClientManifest,
rscSSRManifest: options.rscSSRManifest,
rscServerManifest: options.rscServerManifest,
rscRoot: options.rscRoot,
onShellReady() {
const cost = end();
onTiming(SSRTimings.RENDER_SHELL, cost);
},
onAllReady() {
const cost = end();
onTiming(SSRTimings.RENDER_HTML, cost);
},
onShellError(error) {
onError(error, SSRErrors.RENDER_SHELL);
},
onError(error) {
onError(error, SSRErrors.RENDER_STREAM);
},
},
},
);
);

return stream;
});
return stream;
},
);
};
}
143 changes: 74 additions & 69 deletions packages/runtime/plugin-runtime/src/core/server/string/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { OnError, OnTiming } from '@modern-js/app-tools';
import { run } from '@modern-js/runtime-utils/node';
import { storage } from '@modern-js/runtime-utils/node';
import type { StaticHandlerContext } from '@modern-js/runtime-utils/remix-router';
import { time } from '@modern-js/runtime-utils/time';
import { parseHeaders } from '@modern-js/runtime-utils/universal/request';
Expand Down Expand Up @@ -31,83 +31,88 @@ export const renderString: RenderString = async (
) => {
const headersData = parseHeaders(request);

return run(headersData, async () => {
const { resource, runtimeContext, config, onError, onTiming } = options;
return storage.run(
{ headers: headersData, monitors: options.monitors },
async () => {
const { resource, runtimeContext, config, onError, onTiming } = options;

const tracer: Tracer = { onError, onTiming };
const tracer: Tracer = { onError, onTiming };

const routerContext = runtimeContext.routerContext as StaticHandlerContext;
const routerContext =
runtimeContext.routerContext as StaticHandlerContext;

const { htmlTemplate, entryName, loadableStats, routeManifest } = resource;
const { htmlTemplate, entryName, loadableStats, routeManifest } =
resource;

const ssrConfig = getSSRConfigByEntry(
entryName,
config.ssr,
config.ssrByEntries,
);

const chunkSet: ChunkSet = {
renderLevel: RenderLevel.CLIENT_RENDER,
ssrScripts: '',
jsChunk: '',
cssChunk: '',
};

let prefetchData = {};
const ssrConfig = getSSRConfigByEntry(
entryName,
config.ssr,
config.ssrByEntries,
);

try {
prefetchData = await prefetch(
const chunkSet: ChunkSet = {
renderLevel: RenderLevel.CLIENT_RENDER,
ssrScripts: '',
jsChunk: '',
cssChunk: '',
};

let prefetchData = {};

try {
prefetchData = await prefetch(
serverRoot,
request,
options,
ssrConfig,
tracer,
);
chunkSet.renderLevel = RenderLevel.SERVER_PREFETCH;
} catch (e) {
chunkSet.renderLevel = RenderLevel.CLIENT_RENDER;
tracer.onError(e, SSRErrors.PRERENDER);
}

const collectors = [
new StyledCollector(chunkSet),
new LoadableCollector({
stats: loadableStats,
nonce: config.nonce,
routeManifest,
template: htmlTemplate,
entryName,
chunkSet,
config,
}),
new SSRDataCollector({
request,
prefetchData,
ssrConfig,
ssrContext: runtimeContext.ssrContext!,
chunkSet,
routerContext,
nonce: config.nonce,
useJsonScript: config.useJsonScript,
}),
];

const rootElement = wrapRuntimeContextProvider(
serverRoot,
request,
options,
ssrConfig,
tracer,
Object.assign(runtimeContext, { ssr: true }),
);
chunkSet.renderLevel = RenderLevel.SERVER_PREFETCH;
} catch (e) {
chunkSet.renderLevel = RenderLevel.CLIENT_RENDER;
tracer.onError(e, SSRErrors.PRERENDER);
}

const collectors = [
new StyledCollector(chunkSet),
new LoadableCollector({
stats: loadableStats,
nonce: config.nonce,
routeManifest,
template: htmlTemplate,
entryName,
chunkSet,
config,
}),
new SSRDataCollector({
request,
prefetchData,
ssrConfig,
ssrContext: runtimeContext.ssrContext!,
const html = await generateHtml(
rootElement,
htmlTemplate,
chunkSet,
routerContext,
nonce: config.nonce,
useJsonScript: config.useJsonScript,
}),
];

const rootElement = wrapRuntimeContextProvider(
serverRoot,
Object.assign(runtimeContext, { ssr: true }),
);

const html = await generateHtml(
rootElement,
htmlTemplate,
chunkSet,
collectors,
runtimeContext.ssrContext?.htmlModifiers || [],
tracer,
);

return html;
});
collectors,
runtimeContext.ssrContext?.htmlModifiers || [],
tracer,
);

return html;
},
);
};

async function generateHtml(
Expand Down
Loading

0 comments on commit 753e63c

Please sign in to comment.