Skip to content

Commit

Permalink
Fix requestStaticProps cache key
Browse files Browse the repository at this point in the history
  • Loading branch information
willybrauner committed Oct 5, 2023
1 parent 5df6a1f commit 0fded56
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
26 changes: 14 additions & 12 deletions src/components/Router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -270,17 +270,19 @@ function Router(props: {

// check if new route data as been store in cache
// the matcher will match even if the URL ends with a slash,
// so we need to remove it to get the right cache key
const cacheKey = newRoute._fullUrl?.endsWith("/")
? removeLastCharFromString(newRoute._fullUrl, "/")
: newRoute._fullUrl
// so we need to remove it to get the right cache key and initialStaticPropsUrl
const initialStaticPropsUrl = removeLastCharFromString(props.initialStaticProps?.url, "/")
log(props.id,'initialStaticPropsUrl', initialStaticPropsUrl)

log('cache key', cacheKey)
const dataFromCache = cache.get(cacheKey)
const fullUrl = removeLastCharFromString(newRoute._fullUrl, "/")
log(props.id,'fullUrl (cache key)', fullUrl)

// first route visited (server & client)
const isFirstRouteVisited = cacheKey === props.initialStaticProps?.url;
log(props.id, "is first route visited?", isFirstRouteVisited);
const isFirstRouteVisited = initialStaticPropsUrl === fullUrl;
log(props.id, "initialStaticPropsUrl === fullUrl?", isFirstRouteVisited);

const dataFromCache = cache.get(fullUrl)

// Server and client
// check if is first route and initial static props exist
// in this case, we assign this response to newPage props and cache it
Expand All @@ -293,28 +295,28 @@ function Router(props: {
if (newRoute.props) {
Object.assign(newRoute.props, props.initialStaticProps?.props ?? {});
}
cache.set(newRoute._fullUrl, newRoute.props ?? {});
cache.set(fullUrl, newRoute.props ?? {});
}
// Client only (not the first route, it only can be the client)
// Case, is not first route OR no initial static props:
// If cache exist for this route, assign it and continue.
// else, we request the static props and cache it
else {
if (dataFromCache) {
log(props.id, "Not first route & no initialStaticProps > assign dataFromCache");
log(props.id, "no initialStaticProps > assign dataFromCache");
Object.assign(newRoute.props, dataFromCache);
} else if (newRoute.getStaticProps) {
log(
props.id,
"Not first route & no initialStaticProps & no dataFromCache > request getStaticProps"
"no initialStaticProps & no dataFromCache > request getStaticProps"
);
try {
const requestStaticProps = await newRoute.getStaticProps(
newRoute.props,
langService?.currentLang
);
Object.assign(newRoute.props, requestStaticProps);
cache.set(newRoute._fullUrl, requestStaticProps);
cache.set(fullUrl, requestStaticProps);
} catch (e) {
console.error("requestStaticProps failed");
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/staticPropsCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export function staticPropsCache(cache = Routers.staticPropsCache) {
const dataAlreadyExist = Object.keys(cache).some((el) => el === key);

if (!dataAlreadyExist) {
log(`"${key}" key doesn't exist in cache, we need to request the API.`, cache);
log(`"${key}" key doesn't exist in cache, we need to request the API.`);
return null;
}
const dataCache = cache[key];
Expand Down

0 comments on commit 0fded56

Please sign in to comment.