-
Notifications
You must be signed in to change notification settings - Fork 1
Log out warning when page navigation is not restored from bfcache when WP_DEBUG is enabled
#3
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
Conversation
…P_DEBUG is enabled
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Summary of Changes
Hello @westonruter, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!
This pull request introduces a debugging mechanism to help developers identify why a page might not be restored from the browser's back/forward cache (bfcache). When WP_DEBUG is enabled, the browser console will now display warnings detailing the reasons for bfcache invalidation, aiding in troubleshooting and optimization.
Highlights
- Debugging: Implemented a new feature that logs reasons for bfcache invalidation to the browser console when
WP_DEBUGis enabled, providing valuable insights for developers. - Data Export: The
WP_DEBUGstatus is now exported from PHP to the client-side JavaScript, enabling conditional debugging behavior.
Using Gemini Code Assist
The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.
Invoking Gemini
You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.
| Feature | Command | Description |
|---|---|---|
| Code Review | /gemini review |
Performs a code review for the current pull request in its current state. |
| Pull Request Summary | /gemini summary |
Provides a summary of the current pull request in its current state. |
| Comment | @gemini-code-assist | Responds in comments when explicitly tagged, both in issue comments and review comments. |
| Help | /gemini help |
Displays a list of available commands. |
Customization
To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.
Limitations & Feedback
Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.
You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.
Footnotes
-
Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request introduces a debugging feature for back/forward cache (bfcache) issues by logging reasons for navigation failures when WP_DEBUG is enabled. The changes involve modifications to both PHP and JavaScript files to facilitate this functionality. A suggestion has been provided to enhance the JavaScript code's readability and conciseness.
| if ( data.debug ) { | ||
| for ( const entry of performance.getEntriesByType( 'navigation' ) ) { | ||
| if ( | ||
| 'notRestoredReasons' in entry && | ||
| null !== entry.notRestoredReasons | ||
| ) { | ||
| window.console.warn( | ||
| '[No-cache BFCache] Reasons page navigation not restored from bfcache:', | ||
| entry.notRestoredReasons | ||
| ); | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This for...of loop can be simplified by directly accessing the first element of the performance.getEntriesByType('navigation') array, as it typically contains only one entry for the main document navigation. Using optional chaining (?.) can further streamline the null check for notRestoredReasons.
const [ navigationEntry ] = performance.getEntriesByType( 'navigation' );
if ( navigationEntry?.notRestoredReasons ) {
window.console.warn(
'[No-cache BFCache] Reasons page navigation not restored from bfcache:',
navigationEntry.notRestoredReasons
);
}There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gemini-code-assist Good suggestion, but navigationEntry?.notRestoredReasons is problematic because TypeScript then has an error:
TS2339: Property
notRestoredReasonsdoes not exist on typePerformanceEntry
So this seems to be the better improvement:
const [ navigationEntry ] = performance.getEntriesByType( 'navigation' );
if (
'notRestoredReasons' in navigationEntry &&
null !== navigationEntry.notRestoredReasons
) {
window.console.warn(
'[No-cache BFCache] Reasons page navigation not restored from bfcache:',
navigationEntry.notRestoredReasons
);
}There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are absolutely correct, @westonruter. My apologies for overlooking the TypeScript implications. The PerformanceEntry interface indeed does not directly expose notRestoredReasons, which is specific to PerformanceNavigationTiming.
Your proposed solution using 'notRestoredReasons' in navigationEntry && null !== navigationEntry.notRestoredReasons is an excellent and robust way to handle this. It correctly performs a runtime check for the property's existence and its non-null value, which is a common and idiomatic JavaScript pattern for dealing with potentially dynamic or less-specifically typed objects. This approach ensures type safety without requiring explicit type assertions that might obscure the underlying type hierarchy.
No description provided.