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

(5P) Starter Page Templates: Fetch assets from template before insertion #34839

Merged
merged 2 commits into from
Aug 29, 2019

Conversation

marekhrabe
Copy link
Contributor

@marekhrabe marekhrabe commented Jul 23, 2019

Changes proposed in this Pull Request

New flow after selecting a template:

  • analyze blocks used and look for images in them:
  • (optional) loading state
  • insert template into the editor and close modal

TODO:

  • add support for core/cover
  • core/gallery blocks
  • core/media-text blocks
  • document functions, params, return values…
  • gracefully handle URLs with resizing arguments (like ?w=640)
  • replace mocked API with real one
  • introduce a "loading state" between template selection and showing it inside editor

Testing instructions

Install plugin on your WP, following https://github.com/Automattic/wp-calypso/blob/master/apps/full-site-editing/README.md

  • Create a new page and use the tempalte selector
  • About or Contact templates should be inserted immediately (but the modal itself has a 300ms delay before it closes)
  • Other templates using images should fire a request to import images (follow your devtools and look for POST /wp-json/fse/v1/batch)
  • After the request succeeds, template is inserted into the editor and modal closes 300ms after
  • Inspect images inside the block editor - they should all be loaded from the same domain.
  • Check media library for new images.

Fixes #34603
Fixes #34570

@marekhrabe
Copy link
Contributor Author

The animated WP logo outline which might be a possible loading state comes from here: https://github.com/WordPress/gutenberg/blob/bb8debab915e9fab373e24f9e9dd9469ac27779a/packages/editor/src/components/post-preview-button/index.js#L19

It's currently embedded with the preview button itself (not sure if good idea) so we can either make some sort of abstraction or copy/paste the svg asset + animation 🤷‍♂️

@marekhrabe marekhrabe changed the base branch from master to add/fse-image-endpoint July 24, 2019 10:21
@marekhrabe marekhrabe force-pushed the add/fse-spt-asset-fetching branch from 49b53a4 to 9600707 Compare July 24, 2019 10:22
@marekhrabe marekhrabe changed the base branch from add/fse-image-endpoint to master July 24, 2019 10:22
@marekhrabe marekhrabe changed the base branch from master to add/fse-image-endpoint July 24, 2019 10:23
@marekhrabe marekhrabe force-pushed the add/fse-spt-asset-fetching branch from 9600707 to 9461d8a Compare July 24, 2019 10:25
@marekhrabe marekhrabe force-pushed the add/fse-image-endpoint branch from 3adb4e9 to b4647ac Compare July 24, 2019 10:27
@marekhrabe marekhrabe force-pushed the add/fse-spt-asset-fetching branch from 9461d8a to be8440d Compare July 24, 2019 10:28
@matticbot
Copy link
Contributor

matticbot commented Jul 24, 2019

This PR does not affect the size of JS and CSS bundles shipped to the user's browser.

Generated by performance advisor bot at iscalypsofastyet.com.

@obenland
Copy link
Member

9ae3312 makes use of the API endpoint. There are a few things worth discussing and adding to our todo list:

  • Currently the API doesn't care if images already exist and sideloads everything. It might be worth adding a mechanism to not do that. Maybe by saving the original URL in post meta?
  • The loading delay is significant. We should see if we can run that request async, after the content has been inserted, if possible
  • The batch API Endpoint is very generic atm. Maybe that's not needed?

@marekhrabe
Copy link
Contributor Author

marekhrabe commented Jul 25, 2019

The loading delay is significant. We should see if we can run that request async, after the content has been inserted, if possible

I'm affright this might not be possible - that's why I've started this by delaying the insertion. Once the blocks are inserted, any other change will create an entry in the todo undo stack. Allowing users to undo image replacements and also if they are quick enough to click edit on image/gallery they will end up in a broken state :(

Currently the API doesn't care if images already exist and sideloads everything. It might be worth adding a mechanism to not do that. Maybe by saving the original URL in post meta?

I have mentioned using postmeta for this in the other PR. Sounds like a good optimisation to do. You should see my media library on the site I test this PR with :D

The batch API Endpoint is very generic atm. Maybe that's not needed?

Maybe we can simplify it to just accept image URLs? It could route requests on its own to the right place.

@marekhrabe
Copy link
Contributor Author

I've build a support for a loading state but I haven't done anything in the UI. Let's do it as a separate task. State already has all the info required so minimal code to do it would be something like this:

<div>
  {
    this.state.isLoading
    ? <p>We are loading template { this.state.selectedTemplateTitle }.</p>
    : // …render TemplateSelectorControl here…
  }
</div>

(Written to be somewhere in the render() of starter-page-templates/page-template-modal/index.js)

@marekhrabe
Copy link
Contributor Author

Regarding the time it takes to import images - let's make sure we optimize images in our templates - jpg, appropriate resolution and compression. The timing might be a pain point at this moment as inserting the template is the only way to preview it but if we make better previews in-modal, I think it wouldn't be that big of an issue.

@obenland
Copy link
Member

obenland commented Jul 25, 2019

The loading delay is significant. We should see if we can run that request async, after the content has been inserted, if possible

I'm affright this might not be possible - that's why I've started this by delaying the insertion. Once the blocks are inserted, any other change will create an entry in the todo undo stack. Allowing users to undo image replacements and also if they are quick enough to click edit on image/gallery they will end up in a broken state :(

@youknowriad @mtias Is there any way you can think of where we could replace images after the template has been inserted, without adding to the todo undo stack?

@mtias
Copy link
Member

mtias commented Jul 31, 2019

Is there any way you can think of where we could replace images after the template has been inserted, without adding to the todo undo stack?

See WordPress/gutenberg#8119

@obenland obenland added this to the Ajax: Iteration 7 milestone Aug 8, 2019
@obenland obenland changed the title Starter Page Templates: Fetch assets from template before insertion (5P) Starter Page Templates: Fetch assets from template before insertion Aug 12, 2019
@marekhrabe marekhrabe force-pushed the add/fse-spt-asset-fetching branch 2 times, most recently from 6f3bcf9 to cd51599 Compare August 14, 2019 23:29
@marekhrabe marekhrabe changed the base branch from add/fse-image-endpoint to revert-35328-revert-34823-add/fse-image-endpoint August 14, 2019 23:30
@marekhrabe marekhrabe marked this pull request as ready for review August 14, 2019 23:39
@marekhrabe marekhrabe requested review from a team as code owners August 14, 2019 23:39
@obenland
Copy link
Member

I just tested it with D31446-code to use the optimized and smaller images, and it's still an excruciatingly long wait time after selecting a template. For the portfolio template the sideloading request alone took 10.46s.

I don't know if the work on large previews could help us soften that blow somehow, but for now it just simply takes too long :(

@marekhrabe
Copy link
Contributor Author

Have you tested on the sandbox or locally? I just did some more testing on my sandbox and noticed bugs that I'll need to address.

The main one is that on wpcom all requests go through the common API proxy and somehow we have double slash there which prevents it from working. Example URL: https://public-api.wordpress.com/wp/v2/sites/112010505//fse/v1/sideload/image/batch?_envelope=1&_locale=user

@obenland
Copy link
Member

I created D31603 as a fix for the API endpoint on dotcom

@marekhrabe marekhrabe changed the base branch from revert-35328-revert-34823-add/fse-image-endpoint to master August 16, 2019 22:01
@retrofox
Copy link
Contributor

I've tested the current approach and it seems to work properly according to the description.

It hits the /batch endpoint:

image

image

The template images are loaded from the local server:
http://localhost/wp-content/uploads/2019/08/36ba8-adventure-calm-clouds-414171.jpg

And the image is in the media gallery.
image

The problem here now is we've changed how the template selector works. Although it doesn't seem to be a big deal, we will need to rebase with master and adapt the behavior of this PR. The main difference is when the user selects the template. Now, instead of injecting it into the Editor we are showing the preview.

image

@marekhrabe
Copy link
Contributor Author

Although it doesn't seem to be a big deal, we will need to rebase with master and adapt the behavior of this PR

I started on Friday and have a WIP locally. Still a ton to update, the changes are significant…

@retrofox
Copy link
Contributor

I started on Friday and have a WIP locally. Still a ton to update, the changes are significant…

Yeah, that's true.
Also, maybe we could take advantage of these changes and consider a similar approach keeping in mind that now we are showing all templates through of the thumbnails.
Thinking this as an opportunity to move the images to the local server before to insert the template into the Editor.

@marekhrabe marekhrabe self-assigned this Aug 19, 2019
@marekhrabe marekhrabe force-pushed the add/fse-spt-asset-fetching branch from cd51599 to fdf6504 Compare August 21, 2019 22:55
<Spinner />
{ __( 'Inserting template…', 'full-site-editing' ) }
</div>
) : (
Copy link
Member

Choose a reason for hiding this comment

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

Anytime I come across this, I wonder if it may be time to separate it out into a function.

@marekhrabe marekhrabe force-pushed the add/fse-spt-asset-fetching branch from fdf6504 to 27fdab5 Compare August 28, 2019 23:16
@marekhrabe
Copy link
Contributor Author

This feature is now controlled through this prop: https://github.com/Automattic/wp-calypso/pull/34839/files#diff-a664d83ace51cb87304bf656c9e9f303R244

disabled by default so we can merge the code and evolve it while keeping the old behavior

@obenland obenland added the [Status] Needs Review The PR is ready for review. This also triggers e2e canary tests and wp-desktop tests automatically. label Aug 28, 2019
@obenland obenland merged commit 03e9665 into master Aug 29, 2019
@obenland obenland deleted the add/fse-spt-asset-fetching branch August 29, 2019 18:12
@matticbot matticbot removed the [Status] Needs Review The PR is ready for review. This also triggers e2e canary tests and wp-desktop tests automatically. label Aug 29, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
5 participants