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

Edit Site: Remove templateIds state #22893

Merged
merged 17 commits into from
Jul 6, 2020
Merged

Conversation

ockham
Copy link
Contributor

@ockham ockham commented Jun 4, 2020

Description

Remove some now-obsolete state.

How has this been tested?

Verify that site editing still works 🙂

Types of changes

Janitorial.

Checklist:

  • My code is tested.
  • My code follows the WordPress code style.
  • My code follows the accessibility standards.
  • My code has proper inline documentation.
  • I've included developer documentation if appropriate.
  • I've updated all React Native files affected by any refactorings/renamings in this PR.

@github-actions
Copy link

github-actions bot commented Jun 4, 2020

Size Change: -112 B (0%)

Total Size: 1.13 MB

Filename Size Change
build/edit-site/index.js 16.6 kB -112 B (0%)
ℹ️ View Unchanged
Filename Size Change
build/a11y/index.js 1.14 kB 0 B
build/annotations/index.js 3.68 kB 0 B
build/api-fetch/index.js 3.4 kB 0 B
build/autop/index.js 2.82 kB 0 B
build/blob/index.js 620 B 0 B
build/block-directory/index.js 7.67 kB 0 B
build/block-directory/style-rtl.css 944 B 0 B
build/block-directory/style.css 945 B 0 B
build/block-editor/index.js 110 kB 0 B
build/block-editor/style-rtl.css 10.7 kB 0 B
build/block-editor/style.css 10.7 kB 0 B
build/block-library/editor-rtl.css 7.57 kB 0 B
build/block-library/editor.css 7.57 kB 0 B
build/block-library/index.js 130 kB 0 B
build/block-library/style-rtl.css 7.78 kB 0 B
build/block-library/style.css 7.79 kB 0 B
build/block-library/theme-rtl.css 728 B 0 B
build/block-library/theme.css 729 B 0 B
build/block-serialization-default-parser/index.js 1.88 kB 0 B
build/block-serialization-spec-parser/index.js 3.1 kB 0 B
build/blocks/index.js 48.2 kB 0 B
build/components/index.js 199 kB 0 B
build/components/style-rtl.css 15.8 kB 0 B
build/components/style.css 15.8 kB 0 B
build/compose/index.js 9.56 kB 0 B
build/core-data/index.js 11.4 kB 0 B
build/data-controls/index.js 1.29 kB 0 B
build/data/index.js 8.44 kB 0 B
build/date/index.js 5.38 kB 0 B
build/deprecated/index.js 772 B 0 B
build/dom-ready/index.js 569 B 0 B
build/dom/index.js 3.19 kB 0 B
build/edit-navigation/index.js 9.92 kB 0 B
build/edit-navigation/style-rtl.css 1.02 kB 0 B
build/edit-navigation/style.css 1.02 kB 0 B
build/edit-post/index.js 304 kB 0 B
build/edit-post/style-rtl.css 5.57 kB 0 B
build/edit-post/style.css 5.57 kB 0 B
build/edit-site/style-rtl.css 3.03 kB 0 B
build/edit-site/style.css 3.03 kB 0 B
build/edit-widgets/index.js 9.35 kB 0 B
build/edit-widgets/style-rtl.css 2.45 kB 0 B
build/edit-widgets/style.css 2.45 kB 0 B
build/editor/editor-styles-rtl.css 537 B 0 B
build/editor/editor-styles.css 539 B 0 B
build/editor/index.js 45 kB 0 B
build/editor/style-rtl.css 3.77 kB 0 B
build/editor/style.css 3.77 kB 0 B
build/element/index.js 4.65 kB 0 B
build/escape-html/index.js 733 B 0 B
build/format-library/index.js 7.73 kB 0 B
build/format-library/style-rtl.css 547 B 0 B
build/format-library/style.css 548 B 0 B
build/hooks/index.js 2.13 kB 0 B
build/html-entities/index.js 622 B 0 B
build/i18n/index.js 3.56 kB 0 B
build/is-shallow-equal/index.js 710 B 0 B
build/keyboard-shortcuts/index.js 2.52 kB 0 B
build/keycodes/index.js 1.94 kB 0 B
build/list-reusable-blocks/index.js 3.12 kB 0 B
build/list-reusable-blocks/style-rtl.css 476 B 0 B
build/list-reusable-blocks/style.css 476 B 0 B
build/media-utils/index.js 5.32 kB 0 B
build/notices/index.js 1.79 kB 0 B
build/nux/index.js 3.41 kB 0 B
build/nux/style-rtl.css 671 B 0 B
build/nux/style.css 668 B 0 B
build/plugins/index.js 2.56 kB 0 B
build/primitives/index.js 1.41 kB 0 B
build/priority-queue/index.js 788 B 0 B
build/redux-routine/index.js 2.85 kB 0 B
build/rich-text/index.js 13.9 kB 0 B
build/server-side-render/index.js 2.71 kB 0 B
build/shortcode/index.js 1.69 kB 0 B
build/token-list/index.js 1.28 kB 0 B
build/url/index.js 4.06 kB 0 B
build/viewport/index.js 1.85 kB 0 B
build/warning/index.js 1.14 kB 0 B
build/wordcount/index.js 1.17 kB 0 B

compressed-size-action

@ockham ockham marked this pull request as ready for review June 4, 2020 13:56
@ockham ockham changed the title Remove/edit site template ids state Edit Site: Remove templateIds state Jun 4, 2020
Comment on lines 161 to 164
$home_template = gutenberg_find_template_post_and_parts( 'front-page' );
if ( isset( $home_template ) ) {
$home_template_id = $home_template['template_post']->ID;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

What happens if there is no template here? That means that not even an index.html is provided. Should we create an auto-draft as a fallback?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, maybe. An empty one I guess? I'll implement a fix tomorrow.

Copy link
Contributor

Choose a reason for hiding this comment

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

An empty auto-draft.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Upon closer look, I'm no longer totally sure that this is the right solution 🤔

When rendering to the frontend, we fall back to an error message. Creating an emtpy auto-draft here would thus change the frontend output -- I don't think we want that.

$home_template_id is really just used in the editor to highlight which template is the home template -- so if there are simply no templates at all, it's probably fine to set it to null.

(The issue is also present in current master, where it affects both homeTemplateId and templateId, so I should fix it with a separate PR. What I said above applies to that scenario too, though.)

Copy link
Contributor

Choose a reason for hiding this comment

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

This is needed so people can access the site editor without these templates. Otherwise, they just get errors.

We can just make that error show on the front-end if the template is an empty auto-draft.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Makes sense. It's basically the 'user opens word processor and needs to see empty document' case. I think I have a fix. PR coming up...

Copy link
Contributor

Choose a reason for hiding this comment

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

Exactly

ockham added a commit that referenced this pull request Jun 9, 2020
## Description
This PR fixes two issues with template lookup:

### 1. Templates with `index.html` block template fallback

When using a theme that came with an `index.html` but not with a `front-page.html` block template, the site editor would show the following `Notice: Undefined index: front-page in /var/www/html/wp-content/plugins/gutenberg/lib/edit-site-page.php on line 162`.
 
The reason is that I was setting the wrong key here: https://github.com/WordPress/gutenberg/blob/710373b254fbcd15d524afdeb31da0d93c4defd4/lib/edit-site-page.php#L158

This would use the name of the template that was _found_, not the name of the key that was looked for. So if we're looking up `front-page`, but don't find any template, and [thus fall](https://github.com/WordPress/gutenberg/blob/710373b254fbcd15d524afdeb31da0d93c4defd4/lib/template-loader.php#L226) back to `index`, the template will be stored under the `index` key, rather than the front-end one. 

The fix for this is to use the template we were looking for as key (rather than the name of the template we actually found).

### 2. Templates without `index.html` block template fallback

When using a theme that doesn't come with any block templates at all, we need to provide a blank `index` template when the user first opens the site editor (see [discussion](#22893 (comment))).

Co-authored-by: Enrique Piqueras <epiqueras@users.noreply.github.com>
oandregal pushed a commit that referenced this pull request Jun 9, 2020
## Description
This PR fixes two issues with template lookup:

### 1. Templates with `index.html` block template fallback

When using a theme that came with an `index.html` but not with a `front-page.html` block template, the site editor would show the following `Notice: Undefined index: front-page in /var/www/html/wp-content/plugins/gutenberg/lib/edit-site-page.php on line 162`.
 
The reason is that I was setting the wrong key here: https://github.com/WordPress/gutenberg/blob/710373b254fbcd15d524afdeb31da0d93c4defd4/lib/edit-site-page.php#L158

This would use the name of the template that was _found_, not the name of the key that was looked for. So if we're looking up `front-page`, but don't find any template, and [thus fall](https://github.com/WordPress/gutenberg/blob/710373b254fbcd15d524afdeb31da0d93c4defd4/lib/template-loader.php#L226) back to `index`, the template will be stored under the `index` key, rather than the front-end one. 

The fix for this is to use the template we were looking for as key (rather than the name of the template we actually found).

### 2. Templates without `index.html` block template fallback

When using a theme that doesn't come with any block templates at all, we need to provide a blank `index` template when the user first opens the site editor (see [discussion](#22893 (comment))).

Co-authored-by: Enrique Piqueras <epiqueras@users.noreply.github.com>
@ockham ockham force-pushed the remove/edit-site-template-ids-state branch from f463ea2 to 1a3ab30 Compare June 22, 2020 10:46
@ockham
Copy link
Contributor Author

ockham commented Jun 22, 2020

TODO: Check what to do about this:

// If this is the Site Editor, auto-drafts for template parts have already been generated
// through `gutenberg_find_template_post_and_parts` in `gutenberg_edit_site_init`.

@epiqueras
Copy link
Contributor

TODO: Check what to do about this:

What needs to be done about it?

@ockham
Copy link
Contributor Author

ockham commented Jun 23, 2020

TODO: Check what to do about this:

What needs to be done about it?

I think that we can change the comment to

diff --git a/lib/template-loader.php b/lib/template-loader.php
index 9718b22313..d537178b46 100644
--- a/lib/template-loader.php
+++ b/lib/template-loader.php
@@ -422,7 +422,7 @@ function gutenberg_template_loader_filter_block_editor_settings( $settings ) {
        }
 
        // If this is the Site Editor, auto-drafts for template parts have already been generated
-       // through `gutenberg_find_template_post_and_parts` in `gutenberg_edit_site_init`.
+       // through `gutenberg_find_template_post_and_parts`, when called via the REST API.
        if ( isset( $settings['editSiteInitialState'] ) ) {
                return $settings;
        }

@ockham
Copy link
Contributor Author

ockham commented Jun 23, 2020

TODO: Check what to do about this:

What needs to be done about it?

I think that we can change the comment to

diff --git a/lib/template-loader.php b/lib/template-loader.php
index 9718b22313..d537178b46 100644
--- a/lib/template-loader.php
+++ b/lib/template-loader.php
@@ -422,7 +422,7 @@ function gutenberg_template_loader_filter_block_editor_settings( $settings ) {
        }
 
        // If this is the Site Editor, auto-drafts for template parts have already been generated
-       // through `gutenberg_find_template_post_and_parts` in `gutenberg_edit_site_init`.
+       // through `gutenberg_find_template_post_and_parts`, when called via the REST API.
        if ( isset( $settings['editSiteInitialState'] ) ) {
                return $settings;
        }

Done in f96d403.

Copy link
Member

@vindl vindl left a comment

Choose a reason for hiding this comment

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

Gave this a spin and it works fine for me. 🚀

Nice LOC diff. :)

lib/template-loader.php Outdated Show resolved Hide resolved
@@ -78,13 +78,6 @@ describe( 'actions', () => {
actionName: 'setPage',
Copy link
Contributor

Choose a reason for hiding this comment

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

This needs to check for done: true.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I actually had it check for done: true but removed it after unit tests were failing, and I couldn't quite work out why 😬

I'll try reverting that commit 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Failing again. I don't quite see why this isn't working, do you have any ideas?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I un-reverted that commit. AFAICS, this is failing since we're returning a dispatch action here, which is a generator itself.

Copy link
Contributor

Choose a reason for hiding this comment

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

dispatch just returns an object. Something else is wrong here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

master: 274 queries, 148,8 ms
This branch: 28 queries, 5.4 ms

🎤 💧

Copy link
Contributor

Choose a reason for hiding this comment

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

Sounds good.

Copy link
Member

Choose a reason for hiding this comment

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

That's a pretty nice performance improvement <3

Copy link
Contributor

@jeyip jeyip Jul 6, 2020

Choose a reason for hiding this comment

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

I might be missing something, but could you elaborate on how you were able to access those perf numbers, Bernie?

I'm trying to access the debug bar locally but can't seem to get the debug dashboard to display. (I'm using query params like so http://localhost:8888/wp-admin/admin.php?page=gutenberg-edit-site&debug-bar=1)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You need the debug bar plugin installed and a few config constants set. You can use a .wp-env.override.json in your local Gutenberg source checkout dir to achieve both. This is mine (also links theme-experiments themes).

{
        "core": "../wordpress-develop/build",
        "plugins": [ ".", "https://downloads.wordpress.org/plugin/debug-bar.zip" ],
        "themes": [ "WordPress/theme-experiments" ],
        "config": { "WP_DEBUG": true, "SCRIPT_DEBUG": true, "SAVEQUERIES": true }
}

lib/edit-site-page.php Show resolved Hide resolved
lib/edit-site-page.php Show resolved Hide resolved
lib/edit-site-page.php Show resolved Hide resolved
@ockham ockham force-pushed the remove/edit-site-template-ids-state branch from e3b7e08 to 82bea1b Compare June 28, 2020 10:29
@ockham
Copy link
Contributor Author

ockham commented Jun 29, 2020

This should be ready for another look.

Copy link
Contributor

@jeyip jeyip left a comment

Choose a reason for hiding this comment

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

  • Confirmed that I can create, update, and remove template parts in FSE.
  • Confirmed that I can create, update and remove content in template parts.

🚀

Copy link
Contributor

@jeyip jeyip left a comment

Choose a reason for hiding this comment

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

As someone without a lot of context, it looks like we're able to remove some state management of templates parts because we no longer inject template part data on initialization. Information is now hydrated from an API (I'm guessing the WordPress REST API?).

Does that sound right?

Copy link
Contributor

@jeyip jeyip left a comment

Choose a reason for hiding this comment

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

It seems like Enrique might want to chime in too, but this looks good to me. 👍

@ockham ockham force-pushed the remove/edit-site-template-ids-state branch from 8c3e40f to 738cd2d Compare July 6, 2020 16:25
@vindl vindl added this to the Gutenberg 8.5 milestone Jul 6, 2020
@ockham ockham force-pushed the remove/edit-site-template-ids-state branch from e8bfbaa to 7642660 Compare July 6, 2020 18:27
Copy link
Member

@noahtallen noahtallen left a comment

Choose a reason for hiding this comment

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

This works correctly for me. I tested:

  1. Loading the homepage tempalte
  2. Loading other templates
  3. Overriding the template for a specific post
  4. Reloading and trying to access the overridden template

It all works as I expected. 👍

packages/edit-site/src/store/reducer.js Show resolved Hide resolved
lib/edit-site-page.php Show resolved Hide resolved
@ockham ockham merged commit 6156812 into master Jul 6, 2020
@ockham ockham deleted the remove/edit-site-template-ids-state branch July 6, 2020 20:18
youknowriad pushed a commit that referenced this pull request Jul 9, 2020
Remove some now-obsolete state, and corresponding initial state passed from the server.

We've found that the now-obsolete `gutenberg_find_template_post_and_parts()` calls in `lib/edit-site-page.php` were causing a considerable slowdown of the initial page render (see #23662).
@ockham ockham mentioned this pull request Jul 16, 2020
6 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants