Skip to content

Instantly share code, notes, and snippets.

@0916dhkim
Created December 29, 2021 02:23
Show Gist options
  • Save 0916dhkim/2817d3e387cde5f09976653f2a0d1499 to your computer and use it in GitHub Desktop.
Save 0916dhkim/2817d3e387cde5f09976653f2a0d1499 to your computer and use it in GitHub Desktop.
// Original
export function useServerResponse(location) {
const key = JSON.stringify(location);
const cache = unstable_getCacheForType(createResponseCache);
let response = cache.get(key);
if (response) {
return response;
}
response = createFromFetch(
fetch('/react?location=' + encodeURIComponent(key))
);
cache.set(key, response);
return response;
}
// Request different route depending on window URL.
export function useServerResponse() {
const {pathname} = useLocation();
const cache = unstable_getCacheForType(createResponseCache);
let response = cache.get(pathname);
if (response) {
return response;
}
response = createFromFetch(fetch(`/react${pathname}`));
cache.set(pathname, response);
return response;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment