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

Fix overly aggressive view caching #5826

Merged
merged 2 commits into from
Aug 5, 2024
Merged
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
55 changes: 41 additions & 14 deletions src/components/viewManager/ViewManagerPage.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Action } from 'history';
import { FunctionComponent, useEffect } from 'react';
import { useLocation } from 'react-router-dom';
import { useLocation, useNavigationType } from 'react-router-dom';

import globalize from '../../scripts/globalize';
import type { RestoreViewFailResponse } from '../../types/viewManager';
Expand All @@ -15,6 +16,34 @@ export interface ViewManagerPageProps {
transition?: string
}

interface ViewOptions {
url: string
type?: string
// eslint-disable-next-line @typescript-eslint/no-explicit-any
state: any
autoFocus: boolean
fullscreen?: boolean
transition?: string
options: {
supportsThemeMedia?: boolean
enableMediaControl?: boolean
}
}

const loadView = async (controller: string, view: string, viewOptions: ViewOptions) => {
const [ controllerFactory, viewHtml ] = await Promise.all([
import(/* webpackChunkName: "[request]" */ `../../controllers/${controller}`),
import(/* webpackChunkName: "[request]" */ `../../controllers/${view}`)
.then(html => globalize.translateHtml(html))
]);

viewManager.loadView({
...viewOptions,
controllerFactory,
view: viewHtml
});
};

/**
* Page component that renders legacy views via the ViewManager.
* NOTE: Any new pages should use the generic Page component instead.
Expand All @@ -29,6 +58,7 @@ const ViewManagerPage: FunctionComponent<ViewManagerPageProps> = ({
transition
}) => {
const location = useLocation();
const navigationType = useNavigationType();

useEffect(() => {
const loadPage = () => {
Expand All @@ -45,27 +75,24 @@ const ViewManagerPage: FunctionComponent<ViewManagerPageProps> = ({
}
};

viewManager.tryRestoreView(viewOptions)
if (navigationType !== Action.Pop) {
console.debug('[ViewManagerPage] loading view [%s]', view);
return loadView(controller, view, viewOptions);
}

console.debug('[ViewManagerPage] restoring view [%s]', view);
return viewManager.tryRestoreView(viewOptions)
.catch(async (result?: RestoreViewFailResponse) => {
if (!result?.cancelled) {
const [ controllerFactory, viewHtml ] = await Promise.all([
import(/* webpackChunkName: "[request]" */ `../../controllers/${controller}`),
import(/* webpackChunkName: "[request]" */ `../../controllers/${view}`)
.then(html => globalize.translateHtml(html))
]);

viewManager.loadView({
...viewOptions,
controllerFactory,
view: viewHtml
});
console.debug('[ViewManagerPage] restore failed; loading view [%s]', view);
return loadView(controller, view, viewOptions);
}
});
};

loadPage();
},
// location.state is NOT included as a dependency here since dialogs will update state while the current view stays the same
// location.state and navigationType are NOT included as dependencies here since dialogs will update state while the current view stays the same
// eslint-disable-next-line react-hooks/exhaustive-deps
[
controller,
Expand Down
Loading