Skip to content

Commit

Permalink
Cache
Browse files Browse the repository at this point in the history
  • Loading branch information
sebmarkbage committed Oct 23, 2022
1 parent caa84c8 commit db586ec
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 6 deletions.
15 changes: 12 additions & 3 deletions packages/react-server/src/ReactFlightServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ import {
resolveModuleMetaData,
getModuleKey,
isModuleReference,
supportsRequestStorage,
requestStorage,
} from './ReactFlightServerConfig';

import {
Expand Down Expand Up @@ -157,6 +159,16 @@ export function createRequest(
context?: Array<[string, ServerContextJSONValue]>,
identifierPrefix?: string,
): Request {
if (
ReactCurrentCache.current !== null &&
ReactCurrentCache.current !== DefaultCacheDispatcher
) {
throw new Error(
'Currently React only supports one RSC renderer at a time.',
);
}
ReactCurrentCache.current = DefaultCacheDispatcher;

const abortSet: Set<Task> = new Set();
const pingedTasks = [];
const request = {
Expand Down Expand Up @@ -1155,10 +1167,8 @@ function retryTask(request: Request, task: Task): void {

function performWork(request: Request): void {
const prevDispatcher = ReactCurrentDispatcher.current;
const prevCacheDispatcher = ReactCurrentCache.current;
const prevCache = getCurrentCache();
ReactCurrentDispatcher.current = HooksDispatcher;
ReactCurrentCache.current = DefaultCacheDispatcher;
setCurrentCache(request.cache);
prepareToUseHooksForRequest(request);

Expand All @@ -1177,7 +1187,6 @@ function performWork(request: Request): void {
fatalError(request, error);
} finally {
ReactCurrentDispatcher.current = prevDispatcher;
ReactCurrentCache.current = prevCacheDispatcher;
setCurrentCache(prevCache);
resetHooksForRequest();
}
Expand Down
5 changes: 5 additions & 0 deletions packages/react-server/src/ReactFlightServerConfigStream.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ import type {Chunk} from './ReactServerStreamConfig';

export type {Destination, Chunk} from './ReactServerStreamConfig';

export {
supportsRequestStorage,
requestStorage,
} from './ReactServerStreamConfig';

const stringify = JSON.stringify;

function serializeRowHeader(tag: string, id: number) {
Expand Down
5 changes: 5 additions & 0 deletions packages/react-server/src/ReactServerStreamConfigBrowser.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ export function flushBuffered(destination: Destination) {
// transform streams. https://github.com/whatwg/streams/issues/960
}

// For now we support AsyncLocalStorage as a global for the "browser" builds
// TODO: Move this to some special WinterCG build.
export const supportsRequestStorage = typeof AsyncLocalStorage === 'function';
export const requestStorage = new AsyncLocalStorage();

const VIEW_SIZE = 512;
let currentView = null;
let writtenBytes = 0;
Expand Down
4 changes: 4 additions & 0 deletions packages/react-server/src/ReactServerStreamConfigNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import type {Writable} from 'stream';
import {TextEncoder} from 'util';
import {AsyncLocalStorage} from 'async_hooks';

interface MightBeFlushable {
flush?: () => void;
Expand All @@ -33,6 +34,9 @@ export function flushBuffered(destination: Destination) {
}
}

export const supportsRequestStorage = true;
export const requestStorage = new AsyncLocalStorage();

const VIEW_SIZE = 2048;
let currentView = null;
let writtenBytes = 0;
Expand Down
16 changes: 16 additions & 0 deletions packages/react/src/__tests__/ReactFetch-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,22 @@ describe('ReactFetch', () => {
expect(fetchCount).toBe(1);
});

// @gate enableFetchInstrumentation && enableCache
it('can dedupe fetches in micro tasks', async () => {
async function getData() {
const r1 = await fetch('hello');
const t1 = await r1.text();
const r2 = await fetch('hello');
const t2 = await r2.text();
return t1 + ' ' + t2;
}
function Component() {
return use(getData());
}
expect(await render(Component)).toMatchInlineSnapshot(`"GET world []"`);
expect(fetchCount).toBe(2);
});

// @gate enableFetchInstrumentation && enableCache
it('can dedupe fetches using Request and not', async () => {
function Component() {
Expand Down
3 changes: 2 additions & 1 deletion scripts/error-codes/codes.json
Original file line number Diff line number Diff line change
Expand Up @@ -442,5 +442,6 @@
"454": "React expected a <body> element (document.body) to exist in the Document but one was not found. React never removes the body for any Document it renders into so the cause is likely in some other script running on this page.",
"455": "This CacheSignal was requested outside React which means that it is immediately aborted.",
"456": "Calling Offscreen.detach before instance handle has been set.",
"457": "acquireHeadResource encountered a resource type it did not expect: \"%s\". This is a bug in React."
"457": "acquireHeadResource encountered a resource type it did not expect: \"%s\". This is a bug in React.",
"458": "Currently React only supports one RSC renderer at a time"
}
4 changes: 2 additions & 2 deletions scripts/rollup/bundles.js
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ const bundles = [
global: 'ReactDOMServer',
minifyWithProdErrorCodes: false,
wrapWithModuleBoundaries: false,
externals: ['react', 'util', 'react-dom'],
externals: ['react', 'util', 'async-hooks', 'react-dom'],
},
{
bundleTypes: __EXPERIMENTAL__ ? [FB_WWW_DEV, FB_WWW_PROD] : [],
Expand Down Expand Up @@ -394,7 +394,7 @@ const bundles = [
global: 'ReactServerDOMServer',
minifyWithProdErrorCodes: false,
wrapWithModuleBoundaries: false,
externals: ['react', 'util', 'react-dom'],
externals: ['react', 'util', 'async-hooks', 'react-dom'],
},

/******* React Server DOM Webpack Client *******/
Expand Down

0 comments on commit db586ec

Please sign in to comment.