Skip to content

Commit c229c05

Browse files
committed
feat: support useRequest
1 parent 4d12432 commit c229c05

File tree

7 files changed

+92
-55
lines changed

7 files changed

+92
-55
lines changed

Diff for: packages/cli/uni-builder/src/shared/rsc/plugins/rsbuild-rsc-plugin.ts

+6
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,12 @@ export const rsbuildRscPlugin = ({
8686
.layer(webpackRscLayerName)
8787
.end();
8888

89+
// chain.module
90+
// .rule('default-module')
91+
// .resource([/node\/storage/])
92+
// .layer('default')
93+
// .end();
94+
8995
chain.module
9096
.rule(webpackRscLayerName)
9197
.issuerLayer(webpackRscLayerName)

Diff for: packages/runtime/plugin-runtime/src/core/server/stream/shared.tsx

+58-52
Original file line numberDiff line numberDiff line change
@@ -76,63 +76,69 @@ export function createRenderStreaming(
7676
const createReadableStreamFromElement = await createReadableStreamPromise;
7777
const headersData = parseHeaders(request);
7878

79-
return run(headersData, async () => {
80-
const end = time();
81-
const { runtimeContext, config, resource } = options;
82-
83-
const onError = createOnError(options.onError);
84-
const onTiming = createOnTiming(options.onTiming);
85-
86-
const { htmlTemplate, entryName } = resource;
87-
88-
const ssrConfig = getSSRConfigByEntry(
89-
entryName,
90-
config.ssr,
91-
config.ssrByEntries,
92-
);
93-
94-
const RSCServerRoot = ({ children }: { children: React.ReactNode }) => {
95-
return <>{children}</>;
96-
};
79+
return run(
80+
{
81+
request,
82+
headers: headersData,
83+
},
84+
async () => {
85+
const end = time();
86+
const { runtimeContext, config, resource } = options;
9787

98-
let rootElement = wrapRuntimeContextProvider(
99-
serverRoot,
100-
Object.assign(runtimeContext, { ssr: true }),
101-
);
88+
const onError = createOnError(options.onError);
89+
const onTiming = createOnTiming(options.onTiming);
10290

103-
rootElement = <RSCServerRoot>{rootElement}</RSCServerRoot>;
91+
const { htmlTemplate, entryName } = resource;
10492

105-
const stream = await createReadableStreamFromElement(
106-
request,
107-
rootElement,
108-
{
109-
config,
110-
htmlTemplate,
111-
runtimeContext,
112-
ssrConfig,
93+
const ssrConfig = getSSRConfigByEntry(
11394
entryName,
114-
rscClientManifest: options.rscClientManifest,
115-
rscSSRManifest: options.rscSSRManifest,
116-
rscServerManifest: options.rscServerManifest,
117-
rscRoot: options.rscRoot,
118-
onShellReady() {
119-
const cost = end();
120-
onTiming(SSRTimings.RENDER_SHELL, cost);
121-
},
122-
onAllReady() {
123-
const cost = end();
124-
onTiming(SSRTimings.RENDER_HTML, cost);
125-
},
126-
onShellError(error) {
127-
onError(SSRErrors.RENDER_SHELL, error);
128-
},
129-
onError(error) {
130-
onError(SSRErrors.RENDER_STREAM, error);
95+
config.ssr,
96+
config.ssrByEntries,
97+
);
98+
99+
const RSCServerRoot = ({ children }: { children: React.ReactNode }) => {
100+
return <>{children}</>;
101+
};
102+
103+
let rootElement = wrapRuntimeContextProvider(
104+
serverRoot,
105+
Object.assign(runtimeContext, { ssr: true }),
106+
);
107+
108+
rootElement = <RSCServerRoot>{rootElement}</RSCServerRoot>;
109+
110+
const stream = await createReadableStreamFromElement(
111+
request,
112+
rootElement,
113+
{
114+
config,
115+
htmlTemplate,
116+
runtimeContext,
117+
ssrConfig,
118+
entryName,
119+
rscClientManifest: options.rscClientManifest,
120+
rscSSRManifest: options.rscSSRManifest,
121+
rscServerManifest: options.rscServerManifest,
122+
rscRoot: options.rscRoot,
123+
onShellReady() {
124+
const cost = end();
125+
onTiming(SSRTimings.RENDER_SHELL, cost);
126+
},
127+
onAllReady() {
128+
const cost = end();
129+
onTiming(SSRTimings.RENDER_HTML, cost);
130+
},
131+
onShellError(error) {
132+
onError(SSRErrors.RENDER_SHELL, error);
133+
},
134+
onError(error) {
135+
onError(SSRErrors.RENDER_STREAM, error);
136+
},
131137
},
132-
},
133-
);
138+
);
134139

135-
return stream;
136-
});
140+
return stream;
141+
},
142+
);
137143
};
138144
}

Diff for: packages/runtime/plugin-runtime/src/rsc/server.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export * from '@modern-js/render/rsc';
2+
export { useRequest } from '@modern-js/runtime-utils/node';

Diff for: packages/toolkit/runtime-utils/src/node/index.ts

+15-2
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,22 @@
44
import type { IncomingHttpHeaders } from 'http';
55
import { createStorage } from './storage';
66

7-
const { run, useContext: useHeaders } = createStorage<IncomingHttpHeaders>();
7+
const { run, useContext } = createStorage<{
8+
request?: Request;
9+
headers?: IncomingHttpHeaders;
10+
}>();
811

9-
export { run, useHeaders };
12+
const useRequest = () => {
13+
const context = useContext();
14+
return context?.request;
15+
};
16+
17+
const useHeaders = () => {
18+
const context = useContext();
19+
return context?.headers;
20+
};
21+
22+
export { run, useHeaders, useRequest };
1023

1124
export { serializeJson } from './serialize';
1225
export * from './loaderContext';

Diff for: packages/toolkit/runtime-utils/src/node/storage.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,11 @@ const createStorage = <T>() => {
2929
throw new Error(`Unable to use async_hook, please confirm the node version >= 12.17
3030
`);
3131
}
32+
3233
const context = storage.getStore();
3334
if (!context) {
3435
throw new Error(
35-
`Can't call useContext out of scope, make sure @modern-js/utils is a single version in node_modules`,
36+
`Can't call useContext out of scope, make sure @modern-js/runtime-utils is a single version in node_modules`,
3637
);
3738
}
3839

Diff for: tests/integration/rsc-ssr-app/modern.config.ts

+7
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ export default applyBaseConfig({
2222
.add(path.resolve(__dirname, 'node_modules'))
2323
.add('node_modules');
2424
},
25+
webpack(config) {
26+
config.externals = {
27+
'@modern-js/runtime-utils/node': '@modern-js/runtime-utils/node',
28+
};
29+
30+
return config;
31+
},
2532
rspack(config, { isServer }) {
2633
if (!config.output) {
2734
config.output = {};

Diff for: tests/integration/rsc-ssr-app/src/server-component-root/App.tsx

+3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import 'server-only';
2+
import { useRequest } from '@modern-js/runtime/rsc/server';
23
import { Suspense } from 'react';
34
import styles from './App.module.less';
45
import Suspended from './Suspended';
56
import { Counter } from './components/Counter';
67
import { getCountState } from './components/ServerState';
78

89
const App = ({ name }: { name: string }) => {
10+
const request = useRequest();
11+
console.log('mmmmmmmmmm', request?.method);
912
const countStateFromServer = getCountState();
1013
return (
1114
<div className={styles.root}>

0 commit comments

Comments
 (0)