Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions src/routes/EntryPointRoute.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ import { InternalPreload } from "./internal-preload-symbol";

const preloadsToDispose = new Set();

type InternalPreloadProperties = {
entryPoint: () => Promise<SimpleEntryPoint<BaseEntryPointComponent, any>>;
resource: () => Promise<unknown>;
};

export type PreloadableComponent = ComponentType & {
[InternalPreload]?: {
entryPoint: () => Promise<SimpleEntryPoint<BaseEntryPointComponent, any>>;
resource: () => Promise<unknown>;
};
[InternalPreload]?: InternalPreloadProperties;
};

export default function EntryPointRoute(
Expand Down Expand Up @@ -84,7 +86,7 @@ export default function EntryPointRoute(
// This would be much better if it injected a modulepreload link. Unfortunately
// we don't have a mechanism for getting the right bundle file name to put into
// the href. We might be able to do it by building a rollup plugin.
Hoc[InternalPreload] = {
const internalPreloadProperties: InternalPreloadProperties = {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't get this to compile. It would fail saying that it has the type any because it referred to itself.

async entryPoint() {
return "load" in entryPoint ? entryPoint.load() : entryPoint;
},
Expand All @@ -93,6 +95,7 @@ export default function EntryPointRoute(
return entryPoint.root.load();
},
};
Hoc[InternalPreload] = internalPreloadProperties;

return Hoc;
}
31 changes: 26 additions & 5 deletions src/routes/create-entry-point-route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ import {
type IEnvironmentProvider,
loadQuery,
JSResourceReference,
PreloadOptions,
PreloadedQuery,
GraphQLTaggedNode,
PreloadableConcreteRequest,
PreloadOptions,
EnvironmentProviderOptions,
} from "react-relay";
import type { LoaderFunction, LoaderFunctionArgs } from "react-router-dom";
} from 'react-relay';
import type { LoaderFunction, LoaderFunctionArgs, ShouldRevalidateFunction, ShouldRevalidateFunctionArgs } from "react-router-dom";
import type { ComponentType } from "react";

import type {
Expand All @@ -16,9 +17,12 @@ import type {
SimpleEntryPoint,
} from "./entry-point.types";
import EntryPointRoute from "./EntryPointRoute";
import type {EntryPointRouteObject} from './entry-point-route-object.types';
import {OperationType} from 'relay-runtime';

type EntryPointRouteProperties = {
loader: LoaderFunction;
shouldRevalidate: ShouldRevalidateFunction;
Component: ComponentType<Record<string, never>>;
};

Expand All @@ -29,17 +33,19 @@ export function createEntryPointRoute<
entryPoint:
| SimpleEntryPoint<Component, PreloaderContext>
| JSResourceReference<SimpleEntryPoint<Component, PreloaderContext>>,
rest: Omit<EntryPointRouteObject, 'entryPoint'>,
environmentProvider: IEnvironmentProvider<never>,
contextProvider?: PreloaderContextProvider<PreloaderContext>
contextProvider?: PreloaderContextProvider<PreloaderContext>,
): EntryPointRouteProperties {
let queries: {[p: string]: PreloadedQuery<OperationType, any>} | undefined = undefined;

async function loader(args: LoaderFunctionArgs): Promise<any> {
const loadedEntryPoint =
"load" in entryPoint ? await entryPoint.load() : entryPoint;
const { queries: queryArgs, ...props } = loadedEntryPoint.getPreloadProps({
...args,
preloaderContext: contextProvider?.getPreloaderContext() as any,
});
let queries = undefined;
if (queryArgs) {
queries = Object.fromEntries(
Object.entries(queryArgs).map(
Expand Down Expand Up @@ -80,8 +86,23 @@ export function createEntryPointRoute<
};
}

// This is needed to avoid cases where the query has been disposed of but the
// router would not normally revalidate and rerun the loader which is needed
// to reload the query.
// See https://github.com/loop-payments/react-router-relay/issues/15.
Comment on lines +89 to +92
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I follow this part. My (definitely limited) understanding of shouldRevalidate is that it can be used to prevent calling the loader when it would've normally been called, but it can't trigger a load that wouldn't have otherwise happened?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The docs certainly make it sound that way but it gets called as part of getMatchesToLoad via shouldRevalidateLoader. shouldRevalidateLoader is used in two places. The first is to filter the matches to load. The second usage is when determining whether the fetcher needs to be revalidated. So it actually can be used to return a match that would have otherwise been filtered out or where the fetcher would not be revalidated.

function shouldRevalidate(args: ShouldRevalidateFunctionArgs): boolean {
for (let key in queries) {
const query = queries[key]
if (query.isDisposed) {
return true;
}
}
return rest.shouldRevalidate?.(args) ?? args.defaultShouldRevalidate;
}

return {
loader,
shouldRevalidate,
Component: EntryPointRoute(entryPoint),
};
}
1 change: 1 addition & 0 deletions src/routes/prepare-preloadable-routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export function preparePreloadableRoutes<PreloaderContext>(
...rest,
...createEntryPointRoute(
entryPoint,
rest,
environmentProvider,
preloaderContextProvider,
),
Expand Down