Skip to content

Commit b175988

Browse files
authored
[Flight] Bypass caches in Flight fixture if requested (#33445)
1 parent dddcae7 commit b175988

File tree

3 files changed

+21
-15
lines changed

3 files changed

+21
-15
lines changed

fixtures/flight/server/global.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@ async function renderApp(req, res, next) {
101101
} else if (req.get('Content-type')) {
102102
proxiedHeaders['Content-type'] = req.get('Content-type');
103103
}
104+
if (req.headers['cache-control']) {
105+
proxiedHeaders['Cache-Control'] = req.get('cache-control');
106+
}
104107

105108
const requestsPrerender = req.path === '/prerender';
106109

fixtures/flight/server/region.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ const {readFile} = require('fs').promises;
5050

5151
const React = require('react');
5252

53-
async function renderApp(res, returnValue, formState) {
53+
async function renderApp(res, returnValue, formState, noCache) {
5454
const {renderToPipeableStream} = await import(
5555
'react-server-dom-webpack/server'
5656
);
@@ -97,15 +97,15 @@ async function renderApp(res, returnValue, formState) {
9797
key: filename,
9898
})
9999
),
100-
React.createElement(App)
100+
React.createElement(App, {noCache})
101101
);
102102
// For client-invoked server actions we refresh the tree and return a return value.
103103
const payload = {root, returnValue, formState};
104104
const {pipe} = renderToPipeableStream(payload, moduleMap);
105105
pipe(res);
106106
}
107107

108-
async function prerenderApp(res, returnValue, formState) {
108+
async function prerenderApp(res, returnValue, formState, noCache) {
109109
const {unstable_prerenderToNodeStream: prerenderToNodeStream} = await import(
110110
'react-server-dom-webpack/static'
111111
);
@@ -152,7 +152,7 @@ async function prerenderApp(res, returnValue, formState) {
152152
key: filename,
153153
})
154154
),
155-
React.createElement(App, {prerender: true})
155+
React.createElement(App, {prerender: true, noCache})
156156
);
157157
// For client-invoked server actions we refresh the tree and return a return value.
158158
const payload = {root, returnValue, formState};
@@ -161,14 +161,17 @@ async function prerenderApp(res, returnValue, formState) {
161161
}
162162

163163
app.get('/', async function (req, res) {
164+
const noCache = req.get('cache-control') === 'no-cache';
165+
164166
if ('prerender' in req.query) {
165-
await prerenderApp(res, null, null);
167+
await prerenderApp(res, null, null, noCache);
166168
} else {
167-
await renderApp(res, null, null);
169+
await renderApp(res, null, null, noCache);
168170
}
169171
});
170172

171173
app.post('/', bodyParser.text(), async function (req, res) {
174+
const noCache = req.headers['cache-control'] === 'no-cache';
172175
const {decodeReply, decodeReplyFromBusboy, decodeAction, decodeFormState} =
173176
await import('react-server-dom-webpack/server');
174177
const serverReference = req.get('rsc-action');
@@ -201,7 +204,7 @@ app.post('/', bodyParser.text(), async function (req, res) {
201204
// We handle the error on the client
202205
}
203206
// Refresh the client and return the value
204-
renderApp(res, result, null);
207+
renderApp(res, result, null, noCache);
205208
} else {
206209
// This is the progressive enhancement case
207210
const UndiciRequest = require('undici').Request;
@@ -217,11 +220,11 @@ app.post('/', bodyParser.text(), async function (req, res) {
217220
// Wait for any mutations
218221
const result = await action();
219222
const formState = decodeFormState(result, formData);
220-
renderApp(res, null, formState);
223+
renderApp(res, null, formState, noCache);
221224
} catch (x) {
222225
const {setServerState} = await import('../src/ServerState.js');
223226
setServerState('Error: ' + x.message);
224-
renderApp(res, null, null);
227+
renderApp(res, null, null, noCache);
225228
}
226229
}
227230
});

fixtures/flight/src/App.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ async function ThirdPartyComponent() {
4747
// Using Web streams for tee'ing convenience here.
4848
let cachedThirdPartyReadableWeb;
4949

50-
function fetchThirdParty(Component) {
51-
if (cachedThirdPartyReadableWeb) {
50+
function fetchThirdParty(noCache) {
51+
if (cachedThirdPartyReadableWeb && !noCache) {
5252
const [readableWeb1, readableWeb2] = cachedThirdPartyReadableWeb.tee();
5353
cachedThirdPartyReadableWeb = readableWeb1;
5454

@@ -79,16 +79,16 @@ function fetchThirdParty(Component) {
7979
return result;
8080
}
8181

82-
async function ServerComponent() {
82+
async function ServerComponent({noCache}) {
8383
await new Promise(resolve => setTimeout(() => resolve('deferred text'), 50));
84-
return await fetchThirdParty();
84+
return fetchThirdParty(noCache);
8585
}
8686

87-
export default async function App({prerender}) {
87+
export default async function App({prerender, noCache}) {
8888
const res = await fetch('http://localhost:3001/todos');
8989
const todos = await res.json();
9090

91-
const dedupedChild = <ServerComponent />;
91+
const dedupedChild = <ServerComponent noCache={noCache} />;
9292
const message = getServerState();
9393
return (
9494
<html lang="en">

0 commit comments

Comments
 (0)