Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: use proxy for response stub cache to simplify call site use #9145

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 12 additions & 15 deletions packages/remix-server-runtime/server.ts
Expand Up @@ -889,16 +889,8 @@ function getSingleFetchDataStrategy(
let responseStub: ResponseStub | undefined;
if (request.method !== "GET") {
responseStub = responseStubs[ResponseStubActionSymbol];
if (!responseStub) {
responseStub = getResponseStub();
responseStubs[ResponseStubActionSymbol] = responseStub;
}
} else {
responseStub = responseStubs[match.route.id];
if (!responseStub) {
responseStub = getResponseStub();
responseStubs[match.route.id] = responseStub;
}
}

let result = await match.resolve(async (handler) => {
Expand Down Expand Up @@ -943,7 +935,7 @@ function proxyResponseToResponseStub(
}

// Unsure why this is complaining? It's fine in VSCode but fails with tsc...
// @ts-expect-error
// @ts-ignore - ignoring instead of expecting because otherwise build fails locally
for (let v of headers.getSetCookie()) {
responseStub.headers.append("Set-Cookie", v);
}
Expand All @@ -955,11 +947,16 @@ function isResponseStub(value: any): value is ResponseStub {
);
}

function getResponseStubs(): {
[k: string]: ResponseStub;
[ResponseStubActionSymbol]?: ResponseStub;
} {
return {};
function getResponseStubs() {
return new Proxy({} as Record<string | symbol, ResponseStub>, {
get(responseStubCache, prop) {
let cached = responseStubCache[prop];
if (!cached) {
responseStubCache[prop] = cached = getResponseStub();
}
return cached;
},
});
}

function mergeResponseStubs(
Expand All @@ -974,7 +971,7 @@ function mergeResponseStubs(
let stubs = [
actionStub,
...context.matches.map((m) => responseStubs[m.route.id]),
].filter((stub) => stub) as ResponseStub[];
];

for (let stub of stubs) {
// Take the highest error/redirect, or the lowest success value - preferring
Expand Down