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

Kick off speculative preload/prerender when searching #1291

Open
westonruter opened this issue Jun 10, 2024 · 5 comments · May be fixed by #1297
Open

Kick off speculative preload/prerender when searching #1291

westonruter opened this issue Jun 10, 2024 · 5 comments · May be fixed by #1297
Labels
[Plugin] Speculative Loading Issues for the Speculative Loading plugin (formerly Speculation Rules) [Type] Feature A new feature within an existing module

Comments

@westonruter
Copy link
Member

Feature Description

See https://x.com/tunetheweb/status/1800104005873655924 from @tunetheweb

@westonruter westonruter added [Type] Enhancement A suggestion for improvement of an existing feature [Plugin] Speculative Loading Issues for the Speculative Loading plugin (formerly Speculation Rules) labels Jun 10, 2024
@tunetheweb
Copy link
Contributor

So the problem is that Speculation Rules is really only for safe, idempotent, requests (i.e. simple links that make GET requests).

Forms, by their very nature usually aren't guaranteed to be see (even if they result in GET requests that should in theory be idempotent). So currently the speculation rules API doesn't activate a prerender page even if it matches a <form>'s action URL. Which is a shame since search results, despite often being from a form, are usually safe to prerender—they are an exception to the usual rule that says forms aren't usually safe.

Now the original poster from the above tweet (Tim) gets around this with a little bit of cleverness by utilising prerender, even if he doesn't directly use that:

  1. Prerendering the search result page with JavaScript
  2. It stores both the search results page AND all the thumbnail images in the HTTP Cache.
  3. When the form is submitted, the prerendered page cannot be used because (as per above) forms are not supported by the Speculation Rules API. The prerender is wasted.😔
  4. The page is then loaded. However it first checks the HTTP Cache and finds everything it needs there because of the previous "wasted" prerendered attempt. 😀

So it's a fast load, even if not an instant one.

However, this depends on the Search Results page being cacheable (at least for a short period) so the earlier prerender isn't completely wasted and is resuable from the HTTP Cache. And in my experience a lot of WordPress sites don't make the HTML document cacheable 😔 For a prefetch or prerender that's used directly that's OK as the Speculation Rules API specifically allows non-cacheable resources to be speculation. But for an indirect usage, which is basically just prefilling the HTTP cache for you, this is not OK.

WDYT? Am I being overly pessimistic or does this mean we potentially can't enhance the plugin to do this?

@westonruter westonruter changed the title Kick off speculative preload/pretender when searching Kick off speculative preload/prerender when searching Jun 11, 2024
@westonruter westonruter added [Type] Feature A new feature within an existing module and removed [Type] Enhancement A suggestion for improvement of an existing feature labels Jun 11, 2024
@westonruter westonruter linked a pull request Jun 11, 2024 that will close this issue
@westonruter
Copy link
Member Author

westonruter commented Jun 11, 2024

@tunetheweb I got instant loading working: #1297

As noted there:

It seems the key is how the form submit event is handled. If the default form submission handling is done, then the speculative prerender is not reused. However, if the submit event handler does preventDefault and then manually navigates to the URL that the search submission was going to go to anyway, then it works!

handleFormSubmit: ( event ) => {
event.preventDefault();
const form = event.target;
const formData = new FormData( form );
const url = new URL( form.action );
url.search = new URLSearchParams( formData ).toString();
location.href = url.href;
},

This seems like a hacky workaround for something the browser should be doing automatically for GET form submissions.

@tunetheweb
Copy link
Contributor

This seems like a hacky workaround for something the browser should be doing automatically for GET form submissions.

Agreed. Raised this: WICG/nav-speculation#322

Also as noted in the PR I think you'll need to use history.pushState to add a history entry to navigate back. But not sure I like this overall approach anyway to be honest...

@westonruter
Copy link
Member Author

Also as noted in the PR I think you'll need to use history.pushState to add a history entry to navigate back.

@tunetheweb Why would that be needed? It's not navigating via location.replace(). If you try that test page you can see the back button works just fine.

@tunetheweb
Copy link
Contributor

Ah ignore me then. Been a while since I use this to update a URL! :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Plugin] Speculative Loading Issues for the Speculative Loading plugin (formerly Speculation Rules) [Type] Feature A new feature within an existing module
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants